您的位置:首页 > 其它

大整数乘法,似乎不是很健壮,过段时间再编个好的补充上来

2008-04-30 09:48 309 查看
void Reverse(char str[])
{
char tmp;
int length=strlen(str);
int mid=length/2;
int i;
for(i=0;i<mid;i++)
{
tmp=str[i];
str[i]=str[length-i-1];
str[length-i-1]=tmp;
}
}

void Multiply( char a[1000],char b[1000],char res[1000] )//res = a*b
{
Reverse(a);
Reverse(b);
int i,j,k;
int benwei;
int jinwei;
memset(res,'0',1000);
for (j=0;j<strlen(b);j++)
{
jinwei = 0;
for (i=0,k=j;i<strlen(a);i++,k++)
{
int bb = int(b[j]-'0');
int aa = int(a[i]-'0');
int cc = int(res[k]-'0');
benwei = (bb*aa+jinwei+cc)%10;
jinwei = (bb*aa+jinwei+cc)/10;
res[k] = char( '0'+benwei );
}
if (jinwei!=0)
{
res[k] = char( '0'+jinwei );
}
}
for (i=999;res[i]=='0';i--)
{
}
res[i+1]='';
Reverse(res);
Reverse(a);//反转回来
Reverse(b);//反转回来
// cout<<a<<"*"<<b<<"=";
// cout<<res<<endl;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐