[转] 一个大数相乘的C/C++实现

 

代码
1 # include<stdio.h>
2 # include<string.h>
3 # include<malloc.h>
4
5  void multiply(char* a,char* b,char* c)
6 {
7 int i,j,ca,cb,* s;
8 ca=strlen(a);
9 cb=strlen(b);
10 s=(int*)malloc(sizeof(int)*(ca+cb));
11 for (i=0;i<ca+cb;i++)
12 s[i]=0;
13 for (i=0;i<ca;i++)
14 for (j=0;j<cb;j++)
15 s[i+j+1]+=(a[i]-'0')*(b[j]-'0');
16 for (i=ca+cb-1;i>=0;i--)
17 if (s[i]>=10)
18 {
19 s[i-1]+=s[i]/10;
20 s[i]%=10;
21 }
22 i=0;
23 while (s[i]==0)
24 i++;
25 for (j=0;i<ca+cb;i++,j++)
26 c[j]=s[i]+'0';
27 c[j]='\0';
28 free(s);
29 }
30  
posted @ 2010-08-01 15:57  百分百好牛  阅读(300)  评论(0编辑  收藏  举报