大整数相减

来源:互联网 发布:搜一次cms官网 编辑:程序博客网 时间:2024/04/29 23:41
[cpp] view plaincopy
  1. #include<stdio.h>  
  2. #include<string.h>  
  3. #include<stdlib.h>  
  4. #include<assert.h>  
  5.   
  6. char* bigIntMinus( const char* numstr1, const char* numstr2 )  
  7. {  
  8.         assert( numstr1 != NULL && numstr2 != NULL );  
  9.   
  10.         int len1 = strlen( numstr1 );  
  11.         int len2 = strlen( numstr2 );  
  12.         int resultLen = ( len1 > len2 ) ? ( len1 + 2 ) : ( len2 + 2 );  
  13.         char* minusResult = new char[ resultLen ];  
  14.         bool bFirstIsBigger = true;  
  15.         // 首先处理结果符号  
  16.         if ( len1 > len2 )  
  17.         {  
  18.                 *minusResult = '+';  
  19.         }  
  20.         else if ( len1 < len2 )  
  21.         {  
  22.                 *minusResult = '-';  
  23.                 bFirstIsBigger = false;  
  24.         }  
  25.         else  
  26.         {  
  27.                 int cmpResult = strcmp( numstr1, numstr2 );  
  28.                 if ( cmpResult > 0 )  
  29.                 {  
  30.                         *minusResult = '+';  
  31.                 }  
  32.                 else  
  33.                 {  
  34.                         *minusResult = '-';  
  35.                         bFirstIsBigger = false;  
  36.                 }  
  37.         }  
  38.   
  39.         int tmpBigger[ resultLen ];  
  40.         int tmpSmaller[ resultLen ];  
  41.         int tmpResult[ resultLen ];  
  42.   
  43.         memset( tmpBigger, 0, resultLen * sizeofint ) );  
  44.         memset( tmpSmaller, 0, resultLen * sizeofint ) );  
  45.         memset( tmpResult, 0, resultLen * sizeofint ) );  
  46.   
  47.         int i, j, k;  
  48.         if ( bFirstIsBigger )  
  49.         {  
  50.                 for ( i = 0; i < len1; i++ )  
  51.                 {  
  52.                         tmpBigger[ i ] = *( numstr1 + len1 - i - 1 ) - '0';  
  53.                 }  
  54.                 for ( j = 0; j < len2; j++ )  
  55.                 {  
  56.                         tmpSmaller[ j ] = *(numstr2 + len2 - j - 1 ) - '0';  
  57.                 }  
  58.         }  
  59.         else  
  60.         {  
  61.                 for ( i = 0; i < len2; i++ )  
  62.                 {  
  63.                         tmpBigger[ i ] = *( numstr2 + len2 - i - 1 ) - '0';  
  64.                 }  
  65.                 for ( j = 0; j < len1; j++ )  
  66.                 {  
  67.                         tmpSmaller[ j ] = *( numstr1 + len1 -j -1 ) - '0';  
  68.                 }  
  69.         }  
  70.   
  71.         // 求差  
  72.         int currMINUS = 0;  
  73.         for ( k = 0; k < resultLen; k++ )  
  74.         {  
  75.                 currMINUS = tmpBigger[ k ] - tmpSmaller[ k ];  
  76.                 if ( currMINUS >= 0 )  
  77.                 {  
  78.                         tmpResult[ k+1 ] = currMINUS;  
  79.                 }  
  80.                 else // 当前相减为负  
  81.                 {  
  82.                         tmpBigger[ k+1 ] -= 1; // 高位借位  
  83.                         tmpResult[ k+1 ] = 10 + currMINUS;  
  84.                 }  
  85.         }  
  86.   
  87.         k = resultLen -1;  
  88.         while( !tmpResult[ k ] )  
  89.         {  
  90.                 k--;  
  91.         }  
  92.   
  93.         for ( i = 0; i <= k; i++ )  
  94.         {  
  95.                 *( minusResult + i + 1 ) = tmpResult[ k-i ] + '0';  
  96.         }  
  97.         *( minusResult + i ) = '\0';  
  98.   
  99.         return minusResult;  
  100. }  
  101.   
  102.   
  103. int main()  
  104. {  
  105.         const char* str1 = "198345793";  
  106.         const char* str2 = "4943094930439340493039472";  
  107.         char* result = bigIntMinus( str1, str2 );  
  108.         printf( "%s - %s = %s\n", str1, str2, result );  
  109.   
  110.         // delete  
  111.         delete[] result;  
  112.   
  113.         return 0;  
  114. }  
[cpp] view plaincopy
  1. // main output  
[cpp] view plaincopy
  1. kennie@cbib:~/cplusplus$ g++ -o bigIntMinus.out bigIntMinus.cpp  
  2. kennie@cbib:~/cplusplus$ ./bigIntMinus.out  
  3. 198345793 - 4943094930439340493039472 = -4943094930439340294693679  
[cpp] view plaincopy
  1.   
0 0
原创粉丝点击