-
Notifications
You must be signed in to change notification settings - Fork 0
/
contingAvg.cpp
64 lines (57 loc) · 1.25 KB
/
contingAvg.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
#include<iostream>
#include<string>
using namespace std;
bool isLeg(string s){
int dotCnt=0;
int index;
for(int i=0;i<s.length();i++){
if(s[i]=='.'){
dotCnt++;
index=i;
}
}
if(dotCnt==0){
return true;
}
if(dotCnt >1){
return false;
}
if((s.length()-index )> 3){
return false;
}
return true;
}
int main(){
int n, lcnt=0;
float sum=0;
cin>>n;
for(int i=0;i<n;i++){
string txt;
cin>>txt;
float num;
try{
num = stof(txt);
}
catch(exception){
cout<<"ERROR: "<<txt<<" is not a legal number"<<endl;
continue;
}
if(!isLeg(txt)){
cout<<"ERROR: "<<txt<<" is not a legal number"<<endl;
continue;
}
if(num>1000||num<-1000){
cout<<"ERROR: "<<txt<<" is not a legal number"<<endl;
continue;
}
sum+=num;
lcnt++;
}
if(lcnt == 1)
printf("The average of 1 number is %.2f", sum);
else
if(lcnt!=0){printf("The average of %d numbers is %.2f\n", lcnt, (float)(sum/(float)lcnt));}
else{
printf("The average of 0 numbers is Undefined\n");
}
}