高精度加法

 1 /*样例输入:2 4
 2 样例输出:6*/
 3 
 4 #include <stdio.h>
 5 #include <string.h>
 6 
 7 int main()
 8 {
 9     char s[210];
10     int a[100], b[100], c[110];//此处数组数可进行各种更改以更改精度(后面也要依次更改)
11     int len=0, i, flag = 0, m=0,n=0,tot1=0,tot2=0,temp=0,tmp=0;
12     while (gets_s(s))
13     {   //数组清零
14         memset(a, 0, sizeof(a));
15         memset(b, 0, sizeof(b));
16         memset(c, 0, sizeof(c));
17 
18         len = strlen(s);
19         if (len != 0)
20         {
21             for (i = 0;i < len;i++)
22             {
23                 if (s[i] == ' ') { flag = 1;continue; }
24                 if (flag == 0) { a[i] = s[i] - '0';tot1 += 1; }
25                 if (flag == 1) { b[tot2] = s[i] - '0'; tot2 += 1; } //因为0的ASCII码是48,要减去
26             }
27 
28             for (i = 0;i < tot1 / 2; i++)
29             {
30                 //交换对应位上的数
31                 temp = a[i];
32                 a[i] = a[tot1 - i - 1];
33                 a[tot1 - i - 1] = temp;
34             }
35             for (i = 0;i < tot2 / 2; i++)
36             {
37                 temp = b[i];
38                 b[i] = b[tot2 - i - 1];
39                 b[tot2 - i - 1] = temp;
40             }
41 
42             if (tot1 > tot2)m = tot1;
43             else m = tot2;
44             for (i = 0;i <= m;i++)
45             {
46                 c[i] += a[i] + b[i];
47                 if (c[i] > 9) { c[i] -= 10;c[i + 1] += 1; }
48             }
49             for (i = 99;i > 0;i--)
50                 if (c[i] != 0) { n = i;break; }
51 
52             for (i = n;i >= 0;i--)
53                 printf("%d", c[i]);
54         
55         printf("\n");}
56         memset(s, 0, sizeof(s));
57         flag = 0; m = 0;n = 0; tot1 = 0;tot2 = 0;temp = 0; tmp = 0;
58     }
59     return 0;
60 }

 

posted @ 2016-11-02 10:16  云琛  阅读(129)  评论(0)    收藏  举报