鱼C论坛

 找回密码
 立即注册
查看: 2797|回复: 1

c++大整数乘法代码哪里有问题?

[复制链接]
发表于 2018-2-7 22:33:26 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include<cstring>
  4. using namespace std;
  5. #define M 100
  6. char sa[1000];
  7. char sb[1000];

  8. typedef struct _Node{
  9.     int s[M];    //储存大数
  10.     int l;       //代表字符串长度
  11.     int c;       //次幂
  12. }Node,*pNode;

  13. /*src代表待分解数节点,des表示分解后得到的数节点,st表示从src节点数组中取数开始的位置,l表示取数的长度*/
  14. void cp(pNode src,pNode des,int st,int l)
  15. {
  16.     int i,j;
  17.     for(i=st,j=0;i<st+l;i++,j++)
  18.     {
  19.         des->s[j] = src->s[i];
  20.     }
  21.     des->l = l;
  22.     des->c =st + src->c;  //次幂
  23. }

  24. void add(pNode pa,pNode pb,pNode ans)
  25. {
  26.     int i,cc,k,palen,pblen,len;
  27.     int ta,tb;
  28.     pNode temp;
  29.     if((pa->c<pb->c))
  30.     {
  31.         temp=pa;
  32.         pa=pb;
  33.         pb=temp;
  34.     }
  35.     ans->c =pb->c;
  36.     cc =0;
  37.     palen =pa->l +pa->c;
  38.     pblen =pb->l +pb->c;
  39.     if(palen>pblen)
  40.         len=palen;
  41.     else
  42.         len=pblen;
  43.     k=pa->c -pb->c;

  44.     for(i=0;i<len-ans->c;i++)    //次幂高的补0 ,大于低的长度后与0进行计算
  45.     {
  46.         if(i<k)
  47.             ta =0;    //k为a左侧需要补0的个数
  48.         else
  49.             ta =pa->s[i-k]; //i=k时,补0结束,从a数组中第0位开始取数字

  50.         if(i<pb->l)
  51.             tb =pb->s[i];   //从b数组中第0位开始取数字
  52.         else
  53.             tb=0;          //b数字先取完,b右侧补0

  54.         if(i>=pa->l+k)     //a数字先取完,a右侧补0
  55.             ta =0;

  56.         ans->s[i] =(ta+tb+cc)%10;      //记录两个之和的个位数,十位做进位处理
  57.         cc =(ta+tb+cc)/10;
  58.     }
  59.         if(cc)
  60.             ans->s[i++] =cc;
  61.         ans->l =i;
  62.     }

  63. void mul(pNode pa,pNode pb,pNode ans)
  64. {
  65.     int i,cc,w;
  66.     int ma=pa->l>>1;
  67.     int mb=pb->l>>1; //长度除2
  68.     Node ah,al,bh,bl;
  69.     Node t1,t2,t3,t4,z;
  70.     pNode temp;

  71.     if(!ma||!mb)
  72.     {
  73.         if(!ma)
  74.         {
  75.             temp =pa;
  76.             pa=pb;
  77.             pb=temp;
  78.         }
  79.     ans->c =pa->c+pb->c;
  80.     w =pb->s[0];
  81.     cc =0;
  82.     for(int i=0;i<pa->l;i++)
  83.     {
  84.         ans->s[i]=(w*pa->s[i]+cc)%10;
  85.         cc =(w*pa->s[i]+cc)/10;
  86.     }
  87.     if(cc)
  88.         ans->s[i++]=cc;
  89.     ans->l =i;
  90.     return ;
  91.     }
  92.     cp(pa,&ah,ma,pa->l-ma);
  93.     cp(pa,&al,0,ma);
  94.     cp(pb,&bh,mb,pb->l-mb);
  95.     cp(pb,&bl,0,mb);


  96.     mul(&ah,&bh,&t1);
  97.     mul(&ah,&bl,&t2);
  98.     mul(&al,&bh,&t3);
  99.     mul(&al,&bl,&t4);

  100.     add(&t3,&t4,ans);
  101.     add(&t2,ans,&z);
  102.     add(&t1,&z,ans);
  103. }

  104. int main()
  105. {
  106.     Node ans,a,b;
  107.     cout<<"输入大整数 a"<<endl;
  108.     cin>>sa;
  109.     cout<<"输入大整数 b"<<endl;
  110.     cin>>sb;
  111.     a.l=strlen(sa);
  112.     b.l=strlen(sb);
  113.     int z=0,i;
  114.     for(i=a.l-1;i>=0;i--)
  115.     {
  116.         a.s[z++]=sa[i]-'0';
  117.     }
  118.     a.c=0;
  119.     z=0;
  120.     for(i=b.l-1;i>=0;i--)
  121.         b.s[z++]=sb[i]-'0';
  122.     b.c=0;
  123.     mul(&a,&b,&ans);
  124.     cout<<"最终的结果为:"<<endl;
  125.     for(i=ans.l-1;i>=0;i--)
  126.         cout<<ans.s[i];
  127.     cout<<endl;
  128.     return 0;
  129. }

复制代码

输入完两个数以后就出错,没调试出来那里出问题了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-2-7 22:49:56 | 显示全部楼层
2018-02-07_224658.png

重起电脑看看吧。应该是编译器上的问题
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-3-29 17:03

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表