C++中大数相乘

#include<iostream>
using namespace std;

#define LOCAL
void fun(int the_date,int the_numb,int the_lenth_num1,char the_num1[],int the_ans[]);

int main()
{
#ifdef LOCAL
 freopen("data.in","r",stdin);
 freopen("data.out","w",stdout);
#endif
 ///
 char num[100];
    //input the array
 cout<<"The big number is:"<<endl;
 cin >>num;
 char *num1=new char[strlen(num)+1];
    strcpy(num1,num);
 cout<<"Your input is:"<<num1<<endl;
    ///
 cout<<"The other number is:"<<endl;
 cin>>num;
 char *num2=new char[strlen(num)+1];
 strcpy(num2,num);
 cout<<"Your input is:"<<num2<<endl;
   

 //the lenth of two number
    int lenth_of_num1=strlen(num1);
 cout<<lenth_of_num1<<endl;
 /
 int lenth_of_num2=strlen(num2);
 cout<<lenth_of_num2<<endl;
 
 int lenth=lenth_of_num1+lenth_of_num2;
    cout<<lenth<<endl;
    int *ans = new int[lenth];
 for(int i=0;i<lenth;i++){
  ans[i]=0;
   
 }
 


 //
    //get the number from num1
 int numb=0;
 for(int date = 0;lenth_of_num2>0;lenth_of_num2--,date++)
 {
    numb=num2[lenth_of_num2-1]-'0';
    //cout<<"$$$$$"<<numb<<endl;
       fun(date,numb,lenth_of_num1,num1,ans);//the function
   // for(int b=)
 }

 //
    int k;
    for(k=0;k<lenth-1;k++)//carefully :if write "k<lenth",the "k+1" will equal to 'lenth'
                           //the array will biger than ans[lenth].
 {
  ans[k+1]+=ans[k]/10;
  ans[k]%=10;
  //cout<<ans[k]<<endl;
 }
 //output the anser
 cout<<"the anser is:";
 for(;k>=0;k--)
 {
  if(
   (k==lenth-1)
   &&
   (ans[k]==0)
    )
    continue;
  else cout<<ans[k];
 }
 cout<<endl;
 return 0;
}
///
void fun(int the_date,int the_numb,int the_lenth_num1,char the_num1[],int the_ans[])
{
 int a;
 int b;
 b=the_date ;
 for(;the_lenth_num1>0;the_lenth_num1--,b++)
 {
        a=the_num1[the_lenth_num1-1]-'0';
  the_ans[b]+=(a*the_numb);
  
 }
 
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值