首页 > 代码库 > 数据格式转换,异常 stringtodouble
数据格式转换,异常 stringtodouble
#include <iostream> #include <cctype> #include <string> #include <cstdio> #include <sstream> using namespace std; bool judge(string s) { int count = 0; int dot; if (!isdigit(s[0]) && s[0] != ‘-‘) { return false; } for (int i = 1; i < s.size(); i++) { if (!isdigit(s[i]) && s[i] != ‘.‘) return false; if (s[i] == ‘.‘) { count++; dot = i; if (count == 2) return false; if (s.size() - dot > 3) return false; } } return true; } bool judge2(string s) { try { double a; // int h; char c[50]; char ss[50]; int i = 0; for (; i < s.size(); i++) { ss[i] = s[i]; } ss[i] = s[i]; //strcpy(ss, s.c_str()); sscanf(ss, "%lf", &a); string b; sprintf(c, "%.2lf", a); printf("add: a %d", &a); printf("addd: c %d\n", c); printf("\nc: %s\n", c); //cout << &c[0] << "add:c" << endl; //cout << ss << "add : ss" << endl; for (int i = 0; i < s.size(); i++) { if (ss[i] != c[i]) return false; } return true; } catch (...) { return false; } } int main() { int N; cin >> N; string s; int cnt = 0; int cnt2 = 0; double sum = 0; //cout << "a"; for (int i = 0; i < N; i++) { cin >> s; if (!judge2(s)) { cnt++; cout << "ERROR: " << s << " is not a legal number" << endl; } else { double a = 0; sscanf(s.c_str(), "%lf", &a); //while (1); if (a > 1000 || a < -1000) { cout << "ERROR: " << s << " is not a legal number" << endl; } else { cnt2++; sum += a; } } } //cout << "b"; if (cnt2 == 0) { cout << "The average of 0 numbers is Undefined"; } else if(cnt2>1) { cout << "The average of " << cnt2 << " numbers is "; printf("%.2lf", sum / cnt2); } else if(cnt2 == 1) { printf("The average of 1 number is %.2lf", sum); } return 0; }
double to string时候,会越界引起异常。但似乎pat可以通过
#include <iostream>#include <cctype>#include <string>#include <cstdio>#include <sstream>using namespace std;bool judge(string s){int count = 0;int dot;if (!isdigit(s[0]) && s[0] != ‘-‘){return false;}for (int i = 1; i < s.size(); i++){if (!isdigit(s[i]) && s[i] != ‘.‘)return false;if (s[i] == ‘.‘){count++;dot = i;if (count == 2)return false;if (s.size() - dot > 3)return false;}
}return true;}bool judge2(string s){try {double a;//int h;char c[50];char ss[50];int i = 0;for (; i < s.size(); i++){ss[i] = s[i];}ss[i] = s[i];//strcpy(ss, s.c_str());sscanf(ss, "%lf", &a);string b;sprintf(c, "%.2lf", a);printf("add: a %d", &a);printf("addd: c %d\n", c);printf("\nc: %s\n", c);//cout << &c[0] << "add:c" << endl;//cout << ss << "add : ss" << endl;for (int i = 0; i < s.size(); i++){if (ss[i] != c[i])return false;}return true;}catch (...){return false;}}int main(){int N;cin >> N;string s;int cnt = 0;int cnt2 = 0;double sum = 0;//cout << "a";for (int i = 0; i < N; i++){cin >> s;if (!judge2(s)){cnt++;cout << "ERROR: " << s << " is not a legal number" << endl;}else{double a = 0;sscanf(s.c_str(), "%lf", &a);//while (1);if (a > 1000 || a < -1000){cout << "ERROR: " << s << " is not a legal number" << endl;}else{cnt2++;sum += a;}}}//cout << "b";if (cnt2 == 0) {cout << "The average of 0 numbers is Undefined";}else if(cnt2>1){cout << "The average of " << cnt2 << " numbers is ";printf("%.2lf", sum / cnt2);}else if(cnt2 == 1){printf("The average of 1 number is %.2lf", sum);}return 0;}
数据格式转换,异常 stringtodouble