题解
代码
1 #include <cstdio>
2 #include <iostream>
3 #include <cstring>
4 using namespace std;
5 long long t,len,zf,l,o,k,start;
6 long long a,b,res,inf=2147483648;
7 string s;
8 bool pd(char c) { return c>='0'&&c<='9'; }
9 int main()
10 {
11 freopen("aplusb.in","r",stdin);
12 freopen("aplusb.out","w",stdout);
13 scanf("%lld
",&t);
14 for (long long i=1;i<=t;i++)
15 {
16 getline(cin,s);
17 len=s.length();
18 if (len==0)
19 {
20 printf("Input Error
");
21 continue;
22 }
23 a=0,b=0,zf=0,res=0,l=0,o=0,k=0,start=0;
24 if (s[0]=='-') zf=1,k++;
25 else
26 if (!pd(s[0]))
27 {
28 printf("Input Error
");
29 continue;
30 }
31 for (;k<len;k++)
32 {
33 if (!pd(s[k]))
34 {
35 if (s[k]==' ') break;
36 o=1;
37 break;
38 }
39 if (!start)
40 {
41 if (s[k]=='0') l++;
42 else start=1;
43 }
44 res=res*10+s[k]-'0';
45 if ((!zf&&res>=inf)||(zf&&res<-inf))
46 {
47 o=1;
48 break;
49 }
50 }
51 if (zf) res=-res;
52 if (o||k==0||k>=len-1||(zf&&res==0)||l>1||(l&&res))
53 {
54 printf("Input Error
");
55 continue;
56 }
57 a=res;
58 zf=0,res=0,l=0,o=0,start=0,k++;
59 if (s[k]=='-') zf=1,k++;
60 for (;k<len;k++)
61 {
62 if (!pd(s[k]))
63 {
64 o=1;
65 break;
66 }
67 if (!start)
68 {
69 if (s[k]=='0') l++;
70 else start=1;
71 }
72 res=res*10+s[k]-'0';
73 if ((!zf&&res>=inf)||(zf&&res<-inf))
74 {
75 o=1;
76 break;
77 }
78 }
79 if (zf) res=-res;
80 if (o||(zf&&res==0)||l>1||(l&&res))
81 {
82 printf("Input Error
");
83 continue;
84 }
85 b=res;
86 printf("%lld
",a+b);
87 }
88 return 0;
89 }