1054 求平均值 (20 分)

#include<iostream>

#include<stdio.h>

#include<string.h>

using namespace std;

int main()

{

    int cou;

    int count = 0;

    cin >> cou;

    char s[210], s1[210];

    double ave = 0, num, num1;

    for (int i = 0; i < cou; i++)

    {

        num = 1001;

        scanf("%s", s);

        sscanf(s, "%lf", &num);

        sprintf(s1, "%.2lf", num);

        for (int j = 0; j < strlen(s); j++)

        {

            if (s[j] != s1[j])

                num = 1001;

        }

        if (num >= -1000 && num <= 1000)

        {

            count++;

            ave += num;

        }

        else

            cout << "ERROR: " << s << " is not a legal number\n";

    }

    if (count == 0)

        cout << "The average of 0 numbers is Undefined\n";

    else if(count>1)

    {

        cout << "The average of " << count << " numbers is ";

        printf("%.2f\n", ave / count);

    }

else

{

cout << "The average of 1 number is ";

        printf("%.2f\n", ave / count);

}

    system("pause");

    return 0;

}

上一篇:使用创建对象传递新对象进行赋值


下一篇:pigx商业版源码(pig4cloud)以及使用文档、视频