解题思路:用isdigit()函数判断每一位字符是否是数字,且数字范围不能超过1000,也不能小于1
#include<bits/stdc++.h>
using namespace std;
int main() {
string s1, s2;
cin >> s1;
getchar();//吞掉换行符
getline(cin, s2);
int i, j, n, m;
for (i = 0; i < s1.length(); i++)
if (isdigit(s1[i]) == 0 || stoi(s1) < 1 || stoi(s1) > 1000) {
s1 = "?";
break;
}
for (i = 0; i < s2.length(); i++)
if (isdigit(s2[i]) == 0 || stoi(s2) < 1 || stoi(s2) > 1000) {
s2 = "?";
break;
}
if (s1 == "?" || s2 == "?")
cout << s1 << " + " << s2 << " = ?";
else
cout << s1 << " + " << s2 << " = " << stoi(s1) + stoi(s2);
}
运行结果: