Integer Inquiry

高精度加法,不知错在哪里。、

 1 # include <stdio.h>
2 # include <string.h>
3
4 # define MAXN 110
5
6 char s[MAXN], sum[MAXN];
7
8 int main()
9 {
10 int i, len, c, tmp;
11 char ch;
12
13 while (scanf("%s", s) == 1)
14 {
15 len = strlen(s);
16
17 if (len == 1 && s[0] == '0')
18 break;
19
20 for (i = 0; i < len/2; ++i)
21 {
22 ch = s[i];
23 s[i] = s[len-1-i];
24 s[len-1-i] = ch;
25 }
26
27 for (c = i = 0; i < len; ++i)
28 {
29 tmp = sum[i] + (s[i]-'0') + c;
30 c = (tmp>9 ? 1:0);
31 sum[i] = tmp % 10;
32 }
33 if (c) sum[i] = c;
34
35 memset(s, 0, sizeof(s));
36 }
37 for (i = MAXN-1; sum[i] == 0; --i) ;
38 while (i >= 0) putchar(sum[i--]+'0');
39
40 return 0;
41 }



你可能感兴趣的:(Integer)