在论坛上看到的一个经典的大数乘法的代码,收录了
# include<stdio.h>
# include<string.h>
# include<malloc.h>
void multiply(char* a,char* b,char* c)
{
int i,j,ca,cb,* s;
ca=strlen(a);
cb=strlen(b);
s=(int*)malloc(sizeof(int)*(ca+cb));
for (i=0;i<ca+cb;i++)/*这里用memset()也行*/
s[i]=0;
for (i=0;i<ca;i++)/*直接纵向求和,不处理进位*/
for (j=0;j<cb;j++)
s[i+j+1]+=(a[i]-'0')*(b[j]-'0');
for (i=ca+cb-1;i>=0;i--)/*对求和后的数处理进位*/
if (s[i]>=10)
{
s[i-1]+=s[i]/10;
s[i]%=10;
}
i=0;
while (s[i]==0)
i++;
for (j=0;i<ca+cb;i++,j++)/*保存结果到字符串*/
c[j]=s[i]+'0';
if(0 == j){
c[0] = '0';
j++;
}
c[j]='\0';
free(s);
}