超长位数的字符数的加法与乘法

超长位数的字符数的加法:

测试用例:112233445566778899 + 998877665544332211 = 1111111111111111110

程序代码:

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <malloc.h>
 4 #define MAXLEN 100
 5 
 6 void add(char * a,char * b,char * c)
 7 {
 8     int i,j;
 9     int sa = strlen(a);
10     int sb = strlen(b);
11     int max = sa>sb ? sa : sb;
12     int * s = (int *)malloc(sizeof(int) * (max + 1));//为保证运算和的不溢出,应是最长操作数的位数+1,范围是[0,max];
13     int * A = (int *)malloc(sizeof(int) * max);
14     int * B = (int *)malloc(sizeof(int) * max);
15 
16     for(i=0;i<max;i++)
17         A[i] = B[i] = s[i] = 0;                  //先初始化为0,防止高位相加时对应位不存在导致的问题
18     s[max] = 0;
19 
20     for(i=0;i<sa;i++)                          //将a倒置以便低位对齐相加
21         A[i] = a[sa - i - 1] - '0';
22 
23     for(i=0;i<sb;i++)
24         B[i] = b[sb - i - 1] - '0';
25 
26     for(i=0;i<max;i++)
27         s[i] = A[i] + B[i];
28 
29     for(i=0;i<max;i++)                          //集中处理进位问题
30     {
31         if(s[i]>=10)                           // 若i = max-1时有进位,则s[max] != 0
32         {
33             s[i+1] += s[i] / 10;
34             s[i] %= 10;
35         }
36     }
37 
38     if(s[max] != 0)                              //最高位有进位数据范围为[0,max]
39     {
40         for(j=0;j<=max;j++)
41         {
42             c[j] = s[max - j] + '0';
43         }
44         c[max+1] = '\0';
45     }
46     else                                      //最高位无进位,数据范围为[0,max-1]
47     {
48         for(j=0;j<max;j++)
49         {
50             c[j] = s[max -1 - j] + '0';
51         }
52         c[max] = '\0';
53     }
54 }
55 
56 
57 int main()
58 {
59     char a[MAXLEN];
60     char b[MAXLEN];
61     char c[2 * MAXLEN];
62     while(scanf("%s + %s",a,b) != EOF)
63     {
64         add(a,b,c);
65         printf("%s + %s = ",a,b);
66         puts(c);
67     }
68     return 0;
69 }

超长位数的字符数的乘法:

测试用例:112233445566778899 * 998877665544332211 = 112107482103740987777903741240815689

程序代码:

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <malloc.h>
 4 #define MAXLEN 100
 5 
 6 void multiply(char * a,char * b,char * c)
 7 {
 8     int i,j,ca,cb,* s;
 9     ca = strlen(a);    //a操作数的位数
10     cb = strlen(b);    //b操作数的位数
11     s = (int*)malloc(sizeof(int)*(ca+cb));    //s指向能够存储a和b的空间
12     for (i=0;i<ca+cb;i++)
13         s[i] = 0;    //初始化s数组元素全为0
14     for (i=0;i<ca;i++)
15         for (j=0;j<cb;j++)
16             s[i+j+1] += (a[i]-'0') * (b[j]-'0');
17 
18     for (i=ca+cb-1;i>=0;i--)
19         if (s[i]>=10)
20         {
21             s[i-1] += s[i]/10;    //高位加上低位的进位
22             s[i] %= 10;
23         }
24     i=0;
25     while (s[i]==0)
26         i++;
27        for (j=0;i<ca+cb;i++,j++)
28            c[j] = s[i] + '0';
29     c[j]='\0';
30     free(s);
31 }
32 
33 
34 int main()
35 {
36     char a[MAXLEN];
37     char b[MAXLEN];
38     char c[2 * MAXLEN];
39     while(scanf("%s * %s",a,b) != EOF)
40     {
41         multiply(a,b,c);
42         printf("%s * %s = ",a,b);
43         puts(c);
44     }
45     return 0;
46 }

 

posted @ 2014-09-05 11:10  木子可  阅读(358)  评论(0编辑  收藏  举报