PAT1054 ----- values do not meet the requirements of the selected

The basic requirements of this problem is simple: for a given  N real numbers, their average is calculated. But complication is that some of the input data may be illegal. A "legal" inputs are [ -] real number in the interval, and the 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 ( ≤). 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 Ywhere  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
#include <stdio.h>
#include<string.h>
#include<iostream>
#include <math.h>
#include<ctype.h> 
#include <malloc.h>
#include <iomanip>
#include <stdlib.h>
using namespace std;
int youxiao(char s[])
{
    int n1=0,n2=0,i=0,dot=0;
    if(s[i]=='-')
    {
        i=. 1 ; 
    } 
    for (; S [I] =! ' \ 0 ' ; I ++ ) 
    { 
        IF ((S [I] < 48 || 57 is ! <S [I]) && S [I] = ' . ' ) Return  0 ;
         IF (DOT == . 1 && S [I] == ' . ' ) return  0 ;
         IF (DOT == . 1 ) N2 ++;              // this step and the next may not upside down position, or a multi-computing N2      
        IF (S [I] == ' . ' ) = DOT . 1 ;
        IF (DOT == 0 ) N1 ++ ; 
        
    } 
    IF (N2> 2 ) return  0 ; // the decimal part is not more than two legitimate 
    IF (the atof (S) <- 1000.0 || the atof (S)> 1000.0 ) return  0 ; // the atof string into a floating point, and determines whether the valid range 

    return  . 1 ; 
} 
int main () 
{ 
    int n-, CNT = 0 ;
     Double SUM = 0.0 , V;
     char tmp [ 101 ] = { 0 }; 
    CIN>>n;
    for(int i=0;i<n;i++)
    {
        cin>>tmp;
        if(youxiao(tmp))
        {
            sum+=atof(tmp);
            cnt++;
        }else
        {
            cout<<"ERROR: "<<tmp<<" is not a legal number"<<endl;
        }
    }
    v=sum/cnt;
    cout<<"The average of ";
    if(cnt==0)
    {
        cout<<"0 numbers is Undefined";
    }
    if(cnt==1)
    {
        cout<<"1 number is ";
        printf("%.2f",v); 
    }
    if(cnt>=2)
    {
        printf("%d numbers is %.2f",cnt,v);
    }
    return 0;
}

https://blog.csdn.net/plank_root/article/details/51714578

Guess you like

Origin www.cnblogs.com/BananaMan/p/11324476.html