​ 大数相加 实现任意两个整数的加法 c++

未结帖
0 734
ajian admin_jim 2018-01-31
悬赏:5飞吻

 大数相加 实现任意两个整数的加法


剑指offer面试题12  打印从1到最大的n位10进制数 。陷阱在于  当输入的n很大时,不管用int 或 long long 都会溢出。这个代码 剑指offer源码上已经给出。于此相关的题目是 

定义一个函数,实现任意两个整数的加法,因为没有限定两个数的大小范围,所以也要当做大数问题处理,既用字符串表示。

代码如下:

[cpp] view plain copy

  1. void BigNumberPlus(char * number1,char* number2)  

  2.  {  

  3.      char* Sum = new char[MAXSIZE +2];      //MAXSIZE 为宏定义   

  4.      int len1 = strlen(number1);  

  5.      int len2 = strlen(number2);  

  6.      int maxLen = len1 > len2 ?len1 :len2;  

  7.      char* temp1 = new char[maxLen +1];  

  8.      char* temp2 = new char[maxLen +1];  

  9.      memset(temp1,'0',maxLen);               //初始化的过程已经补齐字符串  

  10.      temp1[maxLen] = '\0';  

  11.      memset(temp2,'0',maxLen);  

  12.      temp2[maxLen] = '\0';  

  13.       int nSum = 0;  

  14.       int isCarryBit = 0;  

  15.       int isOverflow = 0;  

  16.       int i=0,j =0;  

  17.       //////////////////////反转字符串  

  18.        for (i = len1 -1;i>=0;i--)  

  19.        {  

  20.            temp1[j++] = number1[i];  

  21.        }  

  22.        temp1[j] = '\0';  

  23.        j =0;  

  24.        for (i = len2 -1;i>=0;i--)  

  25.        {  

  26.            temp2[j++] = number2[i];  

  27.        }  

  28.       //////////////////////字符串相加,从低位到高位  

  29.       for (i= 0; i< maxLen;i++)  

  30.       {  

  31.           nSum = temp1[i] -'0'+ temp2[i] - '0' + isCarryBit;  

  32.           //////是否有进位  

  33.           if ( nSum >9)  

  34.           {  

  35.               if (i == maxLen -1)  

  36.               {  

  37.                   isOverflow = 1;    //是否溢出  

  38.               }  

  39.               isCarryBit = 1;  

  40.               Sum[i] = nSum - 10 + '0';  

  41.           }  

  42.           else  

  43.           {  

  44.               isCarryBit = 0;  

  45.               Sum[i] = '0' + nSum;  

  46.           }  

  47.       }  

  48.       if (isOverflow)  

  49.       {  

  50.           Sum[maxLen++] = '0'+ isCarryBit;  

  51.       }  

  52.       Sum[maxLen] = '\0';  

  53.       ///////////////打印  

  54.       for (i = maxLen -1;i>=0;i --)  

  55.       {  

  56.           cout << Sum[i];  

  57.       }  

  58.       cout<<endl;  

  59.   

  60.     delete[] temp1;  

  61.     delete[] temp2;  

  62.      delete[] Sum;  

  63.  }  

main函数为:



[cpp] view plain copy

  1. int main()  

  2. {  

  3.     char* number1 = new char[MAXSIZE +1];  

  4.     char* number2 = new char[MAXSIZE +1];  

  5.     number1 = new char[MAXSIZE +1];  

  6.     number2 = new char[MAXSIZE +1];  

  7.     cout<<"please input a number:"<<endl;  

  8.     gets(number1);  

  9.     cout<<"please input a number:"<<endl;  

  10.     gets(number2);  

  11.     BigNumberPlus(number1,number2);  

  12.       

  13.     cout<<"please input a number:"<<endl;  

  14.     gets(number1);  

  15.     cout<<"please input a number:"<<endl;  

  16.     gets(number2);  

  17.     BigNumberPlus(number1,number2);  

  18.   

  19.     cout<<"please input a number:"<<endl;  

  20.     gets(number1);  

  21.     cout<<"please input a number:"<<endl;  

  22.     gets(number2);  

  23.     BigNumberPlus(number1,number2);  

  24.   

  25.     system("pause()");  

  26.     return 0;  

  27. }  

运行结果为:





热忱回答0


最近热帖

近期热议

  1. javascript——prototype与__proto 9
  2. Mysql 中出现的Data truncated for column 3
  3. 在nginx中使用x-sendfile的解决方案 3
  4. 高版本jQuery面插件实现Ajax上传图片 1
  5. Thinkphp Socket.class.php 类的使用 1
  6. 使用ionic3创建第一个App 0
  7. ios-oc html5 0
  8. nginx.conf 0
  9. 基于ionic3.4.0的项目搭建 0
  10. php 缩略图 0