-
Notifications
You must be signed in to change notification settings - Fork 0
/
1054.cpp
77 lines (65 loc) · 1.25 KB
/
1054.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include<iostream>
using namespace std;
bool IsLeagal(char *c)//判断字符串是否合法
{
int flag = 1, point=0,count = 0,i=0;
if (c[0] == '-')//负数时直接跳过第一个字符
i++;
for (; c[i] != '\0'; i++)
{
if ((c[i]<'0' || c[i]>'9') && c[i] != '.')//字符串中存在数字和小数点以为的字符
{
flag = 0;
break;
}
else
{
if (point)//统计小数点后面的位数
count++;
if (c[i] == '.' && point)//再次出现小数点
{
flag = 0;
break;
}
if (c[i] == '.' && !point)//第一次出现小数点
point = 1;
if (count > 2)//小数点后位数大于两位
{
flag = 0;
break;
}
}
}
//atof函数可以直接将字符数组转化成小数
if (atof(c) < -1000 || atof(c) > 1000)
flag = 0;
return flag;
}
int main()
{
double n, sum = 0,res;
int count = 0;
cin >> n;
for (int i = 0; i < n; i++)
{
char c[100];
scanf("%s", c);
if (IsLeagal(c))
{
count++;
sum += atof(c);
}
else
printf("ERROR: %s is not a legal number\n", c);
}
if (!count)
cout << "The average of 0 numbers is Undefined\n";
else if(count==1)//一定要注意只有一个数时输出的是number,一个以上是numbers
printf("The average of %d number is %.2lf\n", count, sum);
else
{
res = sum / count;
printf("The average of %d numbers is %.2lf\n", count, res);
}
return 0;
}