大整数相乘

#include <iostream>
#include <string>
using namespace std;

const int N=1000;
int i,j;

void reverseIt(string& str)
{
int m=str.length();
for(i=0,j=m-1;i<=j;i++,j--)
{
char temp;
temp=str[i];
str[i]=str[j];
str[j]=temp;
}
}

void StrToInt(string str1,int (&a)[N])
{
for(i=0;i<str1.size();i++)
a[i]=str1[i]-'0';
}

void Calcuate(string str1,string str2)
{
int m=0,n=0,count=0;
int a[N]={0},b[N]={0},Result[2*N]={0};
m=str1.length();//str1*str2,a*b
n=str2.length();
reverseIt(str1);
reverseIt(str2);
StrToInt(str1,a);
StrToInt(str2,b);
for(i=0;i<n;i++)//乘数
for(j=0;j<m;j++)//被乘数的位置
   {
   Result[i+j]+=b[i]*a[j];
   if(Result[i+j]>10){
   Result[i+j+1]+=Result[i+j]/10;
   Result[i+j]=Result[i+j]%10;
    }//if
   }//for
if(Result[m+n-1]!=0) count=m+n-1;
else count=m+n-2;
for(int i=count;i>=0;i--)
  {
  cout<<Result[i];
  }//for
cout<<endl;
}

int main()
{
string str1,str2;
while(1)
{
cout<<"Please input two integer:"<<endl;
cin>>str1>>str2;
Calcuate(str1,str2);
}//while
return 0;
}

大整数相乘


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值