大整数相加

cpp]  view plain copy
  1. <pre name="code" class="cpp">#include<stdio.h>  
  2. #include<string.h>  
  3. #include<stdlib.h>  
  4. #include<assert.h>  
  5.   
  6. // 思路如下  
  7. // 将两个字符串分别由低位到高位放置到int数组中  
  8. // 然后每位对齐相加,大于10,本位取余,高位进1  
  9.   
  10. char* bigIntAdd( const char* numstr1, const char* numstr2 )  
  11. {  
  12.         assert( numstr1 != NULL && numstr2 != NULL );  
  13.   
  14.         int len1 = strlen( numstr1 );  
  15.         int len2 = strlen( numstr2 );  
  16.         int resultLen = ( len1 > len2 ) ? ( len1 + 1 ) : ( len2 + 1 );//考虑进位,所以要多出一位  
  17.   
  18.         int tmp1[resultLen]; // 放置第一个字符串由低位到高位的数字  
  19.         int tmp2[resultLen]; // 放置第二个字符串由低位到高位的数字  
  20.   
  21.         int tmpResult[resultLen];  
  22.         memset( tmp1, 0, resultLen * sizeof(int) );  
  23.         memset( tmp2, 0, resultLen * sizeof(int) );  
  24.         memset( tmpResult, 0, resultLen * sizeof(int) );  
  25.   
  26.     // 返回值结果  
  27.         char* addResult = new char[ resultLen + 1 ];// 末尾填'\0'  
  28.         memset(addResult, 0, resultLen*sizeof(char) );  
  29.   
  30.   
  31.         int i,j,k;  
  32.     // 取出由低位到高位的数字  
  33.         for ( i = 0; i < len1; i++ )  
  34.         {  
  35.                 tmp1[i] = *( numstr1 + len1 - i - 1 ) - '0';  
  36.         }  
  37.         for ( i = len1; i < resultLen; i++ )  
  38.         {  
  39.                 tmp1[i] = 0;  
  40.         }  
  41.   
  42.         // 取出由低位到高位的数字  
  43.     for ( j = 0; j < len2; j++ )   
  44.     {   
  45.         tmp2[j] = *( numstr2 + len2 - j - 1) - '0';  
  46.     }   
  47.     for ( j = len2; j < resultLen; j++ )   
  48.     {   
  49.         tmp2[j] = 0;  
  50.      }  
  51.     // 求和  
  52.         int currSUM = 0;  
  53.         for ( k = 0; k < resultLen; k++ )  
  54.         {  
  55.                 currSUM = tmp1[k] + tmp2[k];  
  56.                 if ( currSUM > 9 )  
  57.                 {  
  58.                         tmpResult[k] += currSUM - 10;  
  59.                         tmpResult[k+1] += 1;  
  60.                 }  
  61.                 else  
  62.                 {  
  63.                         tmpResult[k] += currSUM;  
  64.                 }  
  65.         }  
  66.   
  67.         // 从后往前找到第一个不为零的数  
  68.         k = resultLen-1;  
  69.         while(!tmpResult[k])  
  70.         {  
  71.                 k--;  
  72.         }  
  73.   
  74.     // 返回值,字符串赋值  
  75.         for (int i = 0; i <= k; i++)  
  76.         {  
  77.                 *(addResult + i) = tmpResult[k-i] + '0';  
  78.         }  
  79.         *(addResult+i) = '\0';  
  80.   
  81.   
  82.         return addResult;  
  83. }  
  84.   
  85.   
  86. int main()  
  87. {  
  88.         const char* str1 = "123456789";  
  89.         const char* str2 = "987654321";  
  90.         char* result = 0;  
  91.         result = bigIntAdd( str1, str2 );  
  92.         printf( "%s + %s = %s\n", str1, str2, result );  
  93.   
  94.         // delete  
  95.         delete[] result;  
  96.   
  97.         return 0;  
  98. }  
  99.   
  100. // main output  
  101. kennie@cbib:~/cplusplus$ ./bigIntAdd.out  
  102. 123456789 + 987654321 = 1111111110  
  103. </pre><br>  
  104. <br>  
  105. <pre></pre>  
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值