大数相乘

不知道原始作者了,但是写的不错,代码重新调整了下格式,加了点注释。

# 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; 
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值