两个大数相乘

 1 # include <stdio.h>
 2 # include <string.h>
 3 char str[100];
 4 int c[100];
 5 int multiply(char *str1,char *str2)
 6 {
 7     int i,a[100],b[100],c[100],l1,l2,j;
 8 
 9     memset(a,0,100*sizeof(int));
10     memset(b,0,100*sizeof(int));
11     memset(c,0,100*sizeof(int));
12     l1=strlen(str1);
13     l2=strlen(str2);
14     for(i=0;i<l1;i++)
15         a[i]=str1[l1-1-i]-'0';
16     for(i=0;i<l2;i++)
17         b[i]=str2[l2-1-i]-'0';
18     for(i=0;i<l1;i++)
19     {
20         for(j=0;j<l2;j++)
21             c[i+j]+=a[i]*b[j];
22     }
23     for(i=0;i<l1+l2;i++)
24     {
25         if(c[i]>9)
26         {
27             c[i+1]+=c[i]/10;
28             c[i]=c[i]%10;
29         }
30     }
31     j=l1+l2;
32     while(c[j]==0 && j>0)
33     {
34         j--;
35     }
36     for(i=0;j>=0;i++,j--)
37     {
38         str[i]=c[j]+'0';
39     }
40     str[i]='\0';
41     printf("%s",str);
42     return j;
43 }
44 int main()
45 {
46     char str1[100],str2[100];
47     int i,j;
48     while(scanf("%s%s",str1,str2) != EOF)
49     {
50         multiply(str1,str2);
51         printf("\n");
52         printf("%s",str);
53     }
54     return 0;
55 }
View Code

 

posted on 2013-08-05 17:23  随风浪子的博客  阅读(105)  评论(0编辑  收藏  举报

导航