两个大数相加

大数相加是大数相乘和大数指数的基础,因此,花了一晚上的时间写了该函数,也许代码比较臃肿,欢迎指正!


源代码如下:(注意有个offset是错位,我将offset设为0.)








#include <iostream.h>


#include <string.h>


void main()


{


       //需要倒着相加才行呀;



       cout<<"请输入两个任意大的数"<<endl;


       char a[255]="0";


       cin>>a;


       char ab[255];


       memset(ab,'\0',sizeof(char)*255);


       for(unsigned int i_=0;i_<strlen(a);i_++)


              ab[i_]=a[strlen(a)-i_-1];



       char c[255]="0";


       cin>>c;


       char cd[255];


       memset(cd,'\0',sizeof(char)*255);


       for(unsigned int j=0;j<strlen(c);j++)


              cd[j]=c[strlen(c)-j-1];



       int offset=0;


       int temp_g=0;


       int temp_j=0;


       int strlen_k=strlen(cd);


       //ab的这个数补全


       for(unsigned int i_i=strlen(ab);i_i<strlen(cd)+1;i_i++)


              ab[i_i]='0';


       for( int k=offset;k<strlen_k+offset;k++)


       {


              int temp=(ab[k]-'0')+(cd[k-offset]-'0')+temp_j;


              temp_j=temp/10;


              int temp_g=temp%10;


              ab[k]=temp_g+'0';


       }


       if(temp_j)


              ab[k]=temp_j+'0';


       else


              k--;


       cout<<a<<'+'<<c<<'=';


       int i=0;


       while(k>-1)


       {


              cd[i++]=ab[k--];


       }


       cout<<cd<<endl;



}




评论

此博客中的热门博文

Linux/ARM Page Table Entry 属性设置分析

由RFE指令引发的一串故事

提交了30次才AC ---【附】POJ 2488解题报告