不知道原始作者了,但是写的不错,代码重新调整了下格式,加了点注释。
# include<stdio.h>
# include<string.h>
void multiply(char* a,char* b,char* c)
{
int i,j,aLength,bLength,* tmpResult;
aLength=strlen(a);
bLength=strlen(b);
tmpResult=new int[sizeof(int)*(aLength+bLength)];
for ( i = 0; i < aLength + bLength; i++)
tmpResult[i]=0;
for ( i = 0; i < aLength; i++ )
for ( j = 0; j < bLength; j++ )
tmpResult[ i+j+1 ] += ( a[i] - '0' ) * ( b[j] - '0' );
//将大于10的数字进行进位
for (i=aLength+bLength-1;i>=0;i--)
if (tmpResult[i]>=10)
{
tmpResult[i-1] += tmpResult[i]/10;
tmpResult[i] %=10;
}
//找到第一个不为0的数字
i=0;
while (tmpResult[i]==0)
i++;
for (j=0;i<aLength+bLength;i++,j++)
c[j]=tmpResult[i]+'0';
c[j]='\0';
delete tmpResult;
}
int main()
{
char a1[]= "999";
char a2[] = "999";
char a3[20];
multiply( a1, a2, a3 );
cout<<a3<<endl;
return 0;
}