#include <iostream>
#include <string.h>
using namespace std;
int isNum(char str[]){
int i;
for(i=0;i<strlen(str);i++){
if(!(str[i]>='0'&&str[i]<='9'||i==0&&str[0]=='+')){
return -1;
}
}
if(i==strlen(str)&&atoi(str)<=1000) return atoi(str);
return -1;
}
int main(int argc, char *argv[])
{
char c;
char A[8000],B[8000];
int a,b;
cin>>A;
if((c=getchar())=='\n'){
b=isNum(A);
a=-1;
goto LOOP;
}
cin>>B;
a=isNum(A);
b=isNum(B);
LOOP:
if(a>0)
cout<<a;
else{
cout<<"?";
}
cout<<" + ";
if(b>0)
cout<<b;
else{
cout<<"?";
}
cout<<" = ";
if(a>0&&b>0){
cout<<a+b;
}else
cout<<"?";
return 0;
}
测试点3错误,哪个输入有误?最好贴出来,另外,给一个参考一下。
#include<iostream>
#include<string>
using namespace std;
int main()
{
string a;
getline(cin, a);
int m = 0, n = 0;//所有字符串的和
int x = 0, y = 0;//只有数字的和
int i = 0;
for (i; i<a.length(); i++)
{
if (a[i] == ' ') break;
m = m * 10 + (a[i] - '0');
if (a[i] >= '0'&&a[i] <= '9')
{
x = x * 10 + (a[i] - '0');
}
}
for (int j = i + 1; j<a.length(); j++)
{
//if(a[j]==' ') break;
n = n * 10 + (a[j] - '0');
if (a[j] >= '0'&&a[j] <= '9')
{
y = y * 10 + (a[j] - '0');
}
}
if (m <= 1000 && m >= 1 && m == x)
cout << m;
else
cout << "?";
cout << " + ";
if (n <= 1000 && n >= 1 && n == y)
cout << n;
else
cout << "?";
cout << " = ";
if (n <= 1000 && n >= 1 && m <= 1000 && m >= 1 && m == x&&n == y)
cout << m + n;
else
cout << "?";
return 0;