![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/dd8b4e08c32fc87cd6fede79cd7549aa.png)
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/54992d2c7f4cb54a7953cdfb332c691a.png)
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/724df6cb20dc63effa00e109bf20969e.png)
第一次代码
坑太多,要学习的东西太多了。
写judge(string s)不用加&,因为不需要修改s,这里只是遍历一下有无字母。
#include<iostream>
#include<string>
#include<vector>
using namespace std;
int judge(string s) {
int flag = 0;
for (int i = 0; i < s.length(); i++) {
if (isalpha(s[i])) {
flag = 1;
}
}
return flag;
}
int main() {
int n;
cin >> n;
vector<string> v;
string temp;
int count = 0;
for (int i = 0; i < n; i++) {
cin >> temp;
/*可以不写这段代码
if(temp=="."){
cout<<"ERROR: "<<"."<<" is not a legal number"<<endl;
break;
}*/
if (judge(temp)==1) {//这里不周道,原先这里是用了stoi(),忘记了double的情况
cout << "ERROR: " << temp << " is not a legal number" << endl;
}
else {
double num;//这里不周,原先这里是用了stoi(),忘记了double的情况
num=atof(const_cast<const char*>(temp.c_str()));
if ( num>1000||num<-1000) {
cout << "ERROR: " << temp << " is not a legal number" << endl;//stoi(temp)是不周的
}
else {
int pos;
pos=temp.find(".");
if (pos == -1) {//整数
v.push_back(temp);
count++;
}
else {
if (temp.length()-1 - pos > 2) {
cout << "ERROR: " << temp << " is not a legal number" << endl;
}
else {//两位小数
v.push_back(temp);
count++;
}
}
}
}
}
if (count == 1) {
double p=atof(const_cast<const char*>(v[0].c_str()));
printf("The average of 1 number is %.2f\n", p);
}
else if(count > 1){
double ans=0;
for (auto it = v.begin(); it != v.end(); it++) {
ans += atof(const_cast<const char*>((*it).c_str()));
}
double anss = ans / count;
printf("The average of %d numbers is %.2f\n", count, anss);
}
else{
cout << "The average of 0 numbers is Undefined" << endl;
}
return 0;
}
柳神的代码
分析:使⽤sscanf和sprintf函数~
sscanf() – 从⼀个字符串中读进与指定格式相符的数据
sprintf() – 字符串格式化命令,主要功能是把格式化的数据写⼊某个字符串中
#include <iostream>
#include <cstdio>
#include <string.h>
using namespace std;
int main() {
int n, cnt = 0;
char a[50], b[50];
double temp, sum = 0.0;
cin >> n;
for (int i = 0; i < n; i++) {
scanf("%s", a);
sscanf(a, "%lf", &temp);
sprintf(b, "%.2f", temp);
int flag = 0;
for (int j = 0; j < strlen(a); j++)
if (a[j] != b[j]) flag = 1;
if (flag || temp < -1000 || temp > 1000) {
printf("ERROR: %s is not a legal number\n", a);
continue;
}
else {
sum += temp;
cnt++;
}
}
if (cnt == 1)
printf("The average of 1 number is %.2f", sum);
else if (cnt > 1)
printf("The average of %d numbers is %.2f", cnt, sum / cnt);
else
printf("The average of 0 numbers is Undefined");
return 0;
}
二刷
有两个坑,下面注释。
代码还是太复杂,学柳神的代码。
#include<iostream>
#include<string>
#include<vector>
using namespace std;
bool judg(string s){
bool flag=false;
if(s[0]=='.') return false;
if((s[0]=='+'||s[0]=='-')&&s.size()==1) return false;
for(int i=0;i<s.size();i++) if(isalpha(s[i])) flag=true;
if(flag==false) {
if(stod(s) < -1000 || stod(s) > 1000) return false;//这里是大于以及小于
int x=0;
for(int i=0;i<s.size();i++) {if(s[i]=='.') x++;}
string t;
if(s.find('.')!=s.npos) {
int w=s.find('.');
t=s.substr(w+1);
}
if(x>=2) return false;
else if(t.size()>2) return false;
else return true;
}
return false;
}
int main(){
vector<string> v;
int n,count=0;
double sum=0.0;
cin>>n;
for(int i=0;i<n;i++){
string temp;
cin>>temp;
if(!judg(temp)) printf("ERROR: %s is not a legal number\n",temp.c_str());
else{
count++;
v.push_back(temp);
}
}
if(count==0) printf("The average of 0 numbers is Undefined\n");
else if(count==1) printf("The average of 1 number is %.2f\n",stod(v[0]));//只有一个数时候也要输出%.2f
else {
for(auto it=v.begin();it!=v.end();it++) sum+=stod((*it));
printf("The average of %d numbers is %.2f\n",v.size(),(sum/v.size()));
}
return 0;
}
测试点3,4
测试点3:
输入:
3
aaa -9999 5
标准输出:
ERROR: aaa is not a legal number
ERROR: -9999 is not a legal number
The average of 1 number is 5.00
测试点4:
输入:
2
1000 1dsere7
标准输出:
ERROR: 1dsere7 is not a legal number
The average of 1 number is 1000.00