C语言实现大数相加相减和相乘

首先是字符串翻转函数,大数相加、相减和相乘中都用到了这个函数

  1. /* 
  2. ** 字符串翻转 
  3. */  
  4. void Reverse(char *str, int n)  
  5. {  
  6.     int i;  
  7.   
  8.     for (i = 0; i < n / 2; i++)  
  9.     {  
  10.         char tmp;  
  11.         tmp = str[i];  
  12.         str[i] = str[n-i-1];  
  13.         str[n-i-1] = tmp;  
  14.     }  
  15. }  

1.大数相加

  1. /* 
  2. ** 这里假设str1和str2表示的数为非负数 
  3. */  
  4. void BigNumAdd(char *str1, char *str2, char *result)  
  5. {  
  6.     if (str1 == NULL || str2 == NULL || result == NULL)  
  7.     {  
  8.         return;  
  9.     }  
  10.     int len1, len2;  
  11.   
  12.     len1 = strlen(str1);  
  13.     len2 = strlen(str2);  
  14.   
  15.     Reverse(str1, len1);            // 将str1翻转  
  16.     Reverse(str2, len2);            // 将str2翻转  
  17.   
  18.     int weight = 0;                 // 表示进位  
  19.     int sum = 0;  
  20.     int index = 0;                  // 表示result的下标,从0开始,相加结束后应翻转  
  21.     int i, j;  
  22.   
  23.     i = 0;                          // 表示str1的下标  
  24.     j = 0;                          // 表示str2的下标  
  25.   
  26.     while (i < len1 && j < len2)  // 两个数对应的每一位进行相加  
  27.     {  
  28.         sum = str1[i] - '0' + str2[j] - '0' + weight;  
  29.         weight = sum / 10;          // 计算是否要进位  
  30.         sum %= 10;            
  31.         result[index++] = sum + '0';  
  32.         i++;   
  33.         j++;  
  34.     }  
  35.   
  36.     while (i < len1)             // str2已经加完了,这时还要考虑str1和str2的最高位相加时是否有进位的情况  
  37.     {  
  38.         sum = str1[i] - '0' + weight;  
  39.         weight = sum / 10;  
  40.         sum %= 10;  
  41.         result[index++] = sum + '0';  
  42.         i++;          
  43.     }  
  44.   
  45.     while (j < len2)             // str1已经加完了,这时还要考虑str1和str2的最高位相加时是否有进位的情况  
  46.     {  
  47.         sum = str2[j] - '0' + weight;  
  48.         weight = sum / 10;  
  49.         sum %= 10;  
  50.         result[index++] = sum + '0';  
  51.         j++;          
  52.     }  
  53.   
  54.     if (weight > 0)                  // str1和str2都已经加完了,这时还要考虑最后一步中是否产生了进位  
  55.     {  
  56.         result[index++] = weight + '0';       
  57.     }  
  58.   
  59.     result[index] = '\0';  
  60.                                       
  61.     Reverse(result, index);         // 对result进行翻转,得到最终的结果  
  62. }  
2.大数相减

  1. /* 
  2. ** 这里假设str1和str2表示的数为非负数,并且str1表示的数大于等于str2的数 
  3. */  
  4. void BigNumMinus(char *str1, char *str2, char *result)  
  5. {  
  6.     if (str1 == NULL || str2 == NULL || result == NULL)  
  7.     {  
  8.         return;  
  9.     }  
  10.     int len1, len2;  
  11.   
  12.     len1 = strlen(str1);  
  13.     len2 = strlen(str2);  
  14.   
  15.     Reverse(str1, len1);            // 将str1翻转  
  16.     Reverse(str2, len2);            // 将str2翻转  
  17.   
  18.     int index = 0;                  // 表示result的下标,从0开始,相减结束后应翻转  
  19.     int i, j;  
  20.   
  21.     i = 0;                          // 表示str1的下标  
  22.     j = 0;                          // 表示str2的下标  
  23.   
  24.     while (i < len1 && j < len2)  // 两个数对应的每一位进行相减  
  25.     {  
  26.         result[index] = (str1[i] - '0') - (str2[j] - '0');  
  27.   
  28.         if (result[index] < 0)       // 当str1对应的位小于str2对应的位,应借位  
  29.         {  
  30.             result[index] = result[index] + 10 + '0';  
  31.             str1[i+1] = str1[i+1] - 1;  
  32.         }  
  33.         else  
  34.         {  
  35.             result[index] = result[index] + '0';  
  36.         }  
  37.         index++;  
  38.         i++;   
  39.         j++;  
  40.     }  
  41.       
  42.     while (i < len1)             // str2已减完,但是可能存在借位的情况  
  43.     {  
  44.         result[index] = str1[i] - '0';  
  45.   
  46.         if (result[index] < 0)  
  47.         {  
  48.             result[index] = result[index] + 10 + '0';  
  49.             str1[i+1] = str1[i+1] - 1;  
  50.         }  
  51.         else  
  52.         {  
  53.             result[index] = result[index] + '0';  
  54.         }  
  55.         index++;  
  56.         i++;          
  57.     }  
  58.       
  59.     while (result[--index] == '0')  // 去掉result后面的'0' 如40000 - 39999时会存在一些多源的'0'字符  
  60.         ;  
  61.   
  62.     result[++index] = '\0';  
  63.   
  64.     Reverse(result, index);         // 对result进行翻转,得到最终的结果  
  65. }  

3.大数相乘

  1. /* 
  2. ** 这里假设str1和str2表示的数为非负数,并且str1表示的数大于等于str2的数 
  3. */  
  4. void BigNumMulti(char *str1, char *str2, char *result)  
  5. {  
  6.     if (str1 == NULL || str2 == NULL || result == NULL)  
  7.     {  
  8.         return;  
  9.     }  
  10.   
  11.     if (strcmp(str1, "0") == 0 || strcmp(str2, "0") == 0)  
  12.     {  
  13.         strcpy(result, "0");  
  14.         return;  
  15.     }  
  16.   
  17.     int len1, len2;  
  18.   
  19.     len1 = strlen(str1);  
  20.     len2 = strlen(str2);  
  21.   
  22.     Reverse(str1, len1);            // 将str1翻转  
  23.     Reverse(str2, len2);            // 将str2翻转  
  24.   
  25.     memset(result, '0', N);  
  26.   
  27.     int i, j;  
  28.   
  29.     int multiFlag;                  // 乘积进位  
  30.     int addFlag;                    // 加法进位  
  31.   
  32.     for (i = 0; i < len2; i++)       // str2的每一位  
  33.     {  
  34.         multiFlag = 0;  
  35.         addFlag = 0;  
  36.         for (j = 0; j < len1; j++) // str1的每一位  
  37.         {             
  38.             int temp1 = (str2[i] - '0') * (str1[j] - '0') + multiFlag;  
  39.             multiFlag = temp1 / 10;  
  40.             temp1 = temp1 % 10;  
  41.             int temp2 = (result[i+j] - '0') + temp1 + addFlag;  
  42.             addFlag = temp2 / 10;  
  43.             result[i+j] = temp2 % 10 + '0';   
  44.         }  
  45.         result[i + len1] += multiFlag + addFlag;  
  46.     }  
  47.   
  48.     if (result[len1+len2-1] == '0')  
  49.     {  
  50.         result[len1+len2-1] = '\0';  
  51.     }  
  52.     else  
  53.     {  
  54.         result[len1+len2] = '\0';  
  55.     }  
  56.       
  57.     Reverse(result, strlen(result));    // 对result进行翻转,得到最终的结果  
  58. }  



猜你喜欢

转载自blog.csdn.net/qq_40912854/article/details/80329219
今日推荐