Experience:
1.sscanf () can be extracted from a format of the digital 2.sprintf character array () can be present in the array of characters from a digital format
According to this characteristic can be checked whether a string is a desired format, sscanf () reads, sprintf desired format () to read out the string to a desired format,
If a floating-point number is read, it is desirable accurate to two decimal places, sprintf () can be readout% .2f, then traverse the first string, it can be determined whether a format, since if it is not a floating points to be wrong when sscanf () to produce change
And when reading the integer to floating point format can count right, because the console write an integer automatically adds a decimal point and zero behind a few, but it was time to traverse traverse the first string, does not traverse to decimal part, it will still be equal Analyzing
Such as: sscanf (str1, "% lf", a), console to write a 5 to str1, will be read into a is 5.000000, and sprintf (str2, "% 2f.", A), str2 5.00, but traversal str1, only part of the comparator 5 = 5
topic:
The basic requirements of this problem is very simple: given N real numbers, their average is calculated. But complication is that some of the input data may be illegal. A "legitimate" is a real number in the input [-1000,1000] interval, and at most accurate to one decimal place. When you calculate the average, not those illegal data count.
Input formats:
Input of the first row is given a positive integer N (≤100). Then given line N real numbers, separated by spaces between a number.
Output formats:
, The output for each row in the illegal input ERROR: X is not a legal number
, which X
is input. Line with the final output: The average of K numbers is Y
where K
is the number of legal input, Y
is an average value thereof, two decimal place. If the average value can not be calculated, with the Undefined
replacement Y
. If K
1, then output The average of 1 number is Y
.
Sample Input 1:
7
5 -3.2 aaa 9999 2.3.4 7.123 2.35
Output Sample 1:
ERROR: aaa is not a legal number
ERROR: 9999 is not a legal number
ERROR: 2.3.4 is not a legal number
ERROR: 7.123 is not a legal number
The average of 3 numbers is 1.38
Sample Input 2:
2
aaa -9999
Output Sample 2:
ERROR: aaa is not a legal number
ERROR: -9999 is not a legal number
The average of 0 numbers is Undefined
Ideas:
Use sscanf (), sprintf () conversion to determine whether a string is a valid number, a trick learned with others
Test Point 2 Cause of error: Note Well, when K = 1 when the output isThe average of 1 number is Y,是number而不是numbers
Code:
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int main()
{
int num,legal=0;
double temp,sum=0;
cin>>num;
for(int i=0;i<num;i++)
{
bool is_legal=true;
char a[100],b[100];
scanf("%s",a);
sscanf(a,"%lf",&temp);
sprintf(b,"%.2f",temp);
for(int j=0;j<strlen(a);j++)
{
if(a[j]!=b[j])
{
is_legal=false;
break;
}
}
if(!is_legal||temp<-1000||temp>1000)
printf("ERROR: %s is not a legal number\n",a);
else
{
legal++;
sum+=temp;
}
}
if(legal==1)
printf("The average of 1 number is %.2f\n",sum);
else if(legal==0)
printf("The average of 0 numbers is Undefined\n");
else
printf("The average of %d numbers is %.2f",legal,sum/legal);
return 0;
}