鱼C论坛

 找回密码
 立即注册
查看: 1209|回复: 8

[已解决]两个长整数相加

[复制链接]
发表于 2021-8-6 18:46:34 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 焦糖橙子 于 2021-8-6 18:55 编辑

程序目标:实现任意长度的整数加法运算。
特殊要求:
1.将数字看成字符进行操作
2.要根据每次输入的长度,通过realloc来申请内存大小

请问我这个代码有什么地方错了?

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>

  4. char *get_s(void);
  5. char *add(char *num1,char *num2);

  6. char *add(char *num1,char *num2)//实现两个字符串相加
  7. {
  8.         char*last1=num1;
  9.         char*last2=num2;
  10.         char*result;
  11.         int count1=0,count2=0;
  12.         int mark=0,temp=0,max,digit=0;//mark表示是否需要进位,digit用来表示正在计算的位数
  13.        
  14.         while(*last1++)
  15.         {
  16.                 count1++;
  17.         }
  18.         last1-=2;
  19.         while(*last2++)
  20.         {
  21.                 count2++;
  22.         }
  23.         last2-=2;
  24.        
  25.         max=count1>count2?count1:count2;
  26.         result=(char*)realloc(result,max*sizeof(char));
  27.        
  28.        
  29.         for(;count1!=0&&count2!=0;count1--,count2--)//将num1与num2相同的位数相加
  30.         {
  31.                 if(mark=0)
  32.                 {
  33.                         temp=*last1+*last2-'0'-'0';
  34.                         last1--;
  35.                         last2--;
  36.                        
  37.                 }
  38.                 else
  39.                 {
  40.                         temp=*last1+*last2-'0'-'0'+1;
  41.                         last1--;
  42.                         last2--;
  43.                 }
  44.                 if(temp>9)
  45.                 {
  46.                         result[digit]=temp-10;
  47.                         mark=1;
  48.                         digit++;
  49.                 }
  50.                 else
  51.                 {
  52.                         result[digit]=temp;
  53.                         mark=0;
  54.                         digit++;
  55.                 }
  56.         }

  57.                 if(count1==0)//将剩下的位数赋值到result
  58.         {
  59.                 for(;count2!=0;count2--)
  60.                 {
  61.                         if(mark=0)
  62.                         {
  63.                                 temp=*last2-'0';
  64.                                 last2--;
  65.                        
  66.                         }
  67.                         else
  68.                         {
  69.                                 temp=*last2-'0'+1;
  70.                                 last2--;
  71.                         }
  72.                         if(temp>9)
  73.                         {
  74.                                 result[digit]=temp-10;
  75.                                 mark=1;
  76.                                 digit++;
  77.                         }
  78.                         else
  79.                         {
  80.                                 result[digit]=temp;
  81.                                 mark=0;
  82.                                 digit++;
  83.                         }
  84.                 }
  85.         }
  86.         else
  87.         {
  88.                 for(;count1!=0;count1--)
  89.                 {
  90.                         if(mark=0)
  91.                         {
  92.                                 temp=*last1-'0';
  93.                                 last1--;
  94.                         }
  95.                         else
  96.                         {
  97.                                 temp=*last1-'0'+1;
  98.                                 last1--;
  99.                         }
  100.                         if(temp>9)
  101.                         {
  102.                                 result[digit]=temp-10;
  103.                                 mark=1;
  104.                                 digit++;
  105.                         }
  106.                         else
  107.                         {
  108.                                 result[digit]=temp;
  109.                                 mark=0;
  110.                                 digit++;
  111.                         }
  112.                 }
  113.         }
  114.        

  115.         return result;
  116.        
  117. }


  118. char *get_s(void)//获取字符串
  119. {
  120.         char ch;
  121.         char*num;
  122.         int count=0;
  123.         if((ch=getchar())!='\n')
  124.         {
  125.                 num=(char*)realloc(num,(count+1)*sizeof(char));
  126.                 num[count++]=ch;
  127.                 get_s();
  128.         }
  129.         return num;
  130.        
  131. }


  132. int main(void)
  133. {
  134.        
  135.         char*num1;
  136.         char*num2;
  137.         char*result;
  138.        
  139.         printf("请输入一个整数:");       
  140.         num1=get_s();
  141.                        
  142.         printf("请输入一个整数:");
  143.         num2=get_s();
  144.        
  145.         result=add(num1,num2);
  146.        
  147.         printf("计算结果是:%s\n", result);
  148.        
  149.         return 0;
  150. }
复制代码
最佳答案
2021-8-6 23:19:17
逻辑不对 + 变量不初始化 + 内存申请了不释放 + 把 == 写成 =

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>

  4. char *get_s(void);
  5. char *add(char *num1,char *num2);

  6. char *add(char *num1,char *num2)//实现两个字符串相加
  7. {
  8.     char*last1=num1;
  9.     char*last2=num2;
  10.     char*result = NULL;
  11.     int count1=0,count2=0;
  12.     int mark=0,temp=0,max,digit=0;//mark表示是否需要进位,digit用来表示正在计算的位数

  13.     while(*last1++)
  14.     {
  15.         count1++;
  16.     }
  17.     last1-=2;
  18.     while(*last2++)
  19.     {
  20.         count2++;
  21.     }
  22.     last2-=2;

  23.     max=count1>count2?count1:count2;
  24.     max += 1;   // 进位
  25.     max += 1;   // '\0'
  26.     result=(char*)realloc(result,max*sizeof(char));
  27.     result[max - 1] = '\0';
  28.     digit = max - 1 - 1;

  29.     for(;count1!=0&&count2!=0;count1--,count2--)
  30.     {
  31.         /*
  32.         //if(mark=0)
  33.         if(mark == 0)
  34.         {
  35.             temp=*last1+*last2-'0'-'0';
  36.             last1--;
  37.             last2--;

  38.         }
  39.         else
  40.         {
  41.             temp=*last1+*last2-'0'-'0'+1;
  42.             last1--;
  43.             last2--;
  44.         }
  45.         */
  46.         temp = (*last1-- - '0') + (*last2-- - '0') + mark;
  47.         mark = 0;
  48.         if(temp > 9) {
  49.             mark = 1;
  50.             temp -= 10;
  51.         }
  52.         result[digit--] = temp + '0';

  53.         /*
  54.         if(temp>9)
  55.         {
  56.             result[digit]=temp-10;
  57.             mark=1;
  58.             digit++;
  59.         }
  60.         else
  61.         {
  62.             result[digit]=temp;
  63.             mark=0;
  64.             digit++;
  65.         }
  66.         */
  67.     }
  68.     char *p = NULL;
  69.     char *pe = NULL;
  70.     if(count1 != 0) p = last1, pe = num1 - 1;
  71.     if(count2 != 0) p = last2, pe = num2 - 1;
  72.     while(p != pe) {
  73.         temp = (*p-- - '0') + mark;
  74.         mark = 0;
  75.         if(temp > 9) {
  76.             mark = 1;
  77.             temp -= 10;
  78.         }
  79.         result[digit--] = temp + '0';
  80.     }
  81.     if(mark) result[digit--] = mark + '0';
  82.     if(digit == 0) { // 之前多申请了一个进位,全部字符串前移一位
  83.         for(size_t i = 0; i < max - 1; ++i) {
  84.             result[i] = result[i + 1];
  85.         }
  86.         result = realloc(result, max - 1);
  87.     }
  88.     return result;
  89. }

  90. char *get_s(void)//获取字符串
  91. {
  92.     char ch;
  93.     //char*num;
  94.     char*num = NULL;
  95.     int count=0;
  96.     if((ch=getchar())!='\n')
  97.     {
  98.         num=(char*)realloc(num, 2);
  99.         num[count++]=ch;
  100.         num[count++]='\0';
  101.     } else {
  102.         num=(char*)realloc(num, 1);
  103.         num[count++]='\0';
  104.         return num;
  105.     }
  106.     char *next = get_s();
  107.     size_t size = strlen(next);
  108.     if(size) {
  109.         num = realloc(num, size + count);
  110.         strcat(num, next);
  111.     }
  112.     free(next);
  113.     return num;
  114. }


  115. int main(void)
  116. {

  117.     char*num1;
  118.     char*num2;
  119.     char*result;

  120.     printf("请输入一个整数:");        
  121.     num1=get_s();

  122.     printf("请输入一个整数:");
  123.     num2=get_s();

  124.     result=add(num1,num2);

  125.     printf("计算结果是:%s\n", result);

  126.     free(result);
  127.     free(num2);
  128.     free(num1);
  129.     return 0;
  130. }

复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-8-6 19:02:06 | 显示全部楼层
好长
  1. temp=*last1+*last2-'0'-'0';
复制代码

这减两次啥意思
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-8-6 19:25:13 | 显示全部楼层
两个函数全是错的,get_s 函数无法构成递归
add 函数,你能说一说你的实现思路吗?
(add 函数简单的看了看,很有可能是错的)

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>

  4. char *get_s(void);
  5. char *add(char *num1,char *num2);

  6. char *add(char *num1,char *num2)//实现两个字符串相加
  7. {
  8.     char*last1=num1;
  9.     char*last2=num2;
  10.     char*result;
  11.     int count1=0,count2=0;
  12.     int mark=0,temp=0,max,digit=0;//mark表示是否需要进位,digit用来表示正在计算的位数

  13.     while(*last1++)
  14.     {
  15.         count1++;
  16.     }
  17.     last1-=2;
  18.     while(*last2++)
  19.     {
  20.         count2++;
  21.     }
  22.     last2-=2;

  23.     max=count1>count2?count1:count2;
  24.     result=(char*)realloc(result,max*sizeof(char));


  25.     for(;count1!=0&&count2!=0;count1--,count2--)
  26.     {
  27.         if(mark=0)
  28.         {
  29.             temp=*last1+*last2-'0'-'0';
  30.             last1--;
  31.             last2--;

  32.         }
  33.         else
  34.         {
  35.             temp=*last1+*last2-'0'-'0'+1;
  36.             last1--;
  37.             last2--;
  38.         }
  39.         if(temp>9)
  40.         {
  41.             result[digit]=temp-10;
  42.             mark=1;
  43.             digit++;
  44.         }
  45.         else
  46.         {
  47.             result[digit]=temp;
  48.             mark=0;
  49.             digit++;
  50.         }
  51.     }
  52.     return result;

  53. }


  54. char *get_s(void)//获取字符串
  55. {
  56.     char ch;
  57.     //char*num;
  58.     char*num = NULL;
  59.     int count=0;
  60.     if((ch=getchar())!='\n')
  61.     {
  62.         num=(char*)realloc(num, 2);
  63.         num[count++]=ch;
  64.         num[count++]='\0';
  65.     } else {
  66.         num=(char*)realloc(num, 1);
  67.         num[count++]='\0';
  68.         return num;
  69.     }
  70.     char *next = get_s();
  71.     size_t size = strlen(next);
  72.     if(size) {
  73.         num = realloc(num, size + count);
  74.         strcat(num, next);
  75.     }
  76.     free(next);
  77.     return num;
  78. }


  79. int main(void)
  80. {

  81.     char*num1;
  82.     char*num2;
  83.     char*result;

  84.     printf("请输入一个整数:");        
  85.     num1=get_s();

  86.     printf("请输入一个整数:");
  87.     num2=get_s();


  88.     printf("%s\n", num1);
  89.     printf("%s\n", num2);

  90.     /*
  91.     result=add(num1,num2);

  92.     printf("计算结果是:%s\n", result);
  93.     */

  94.     //free(result);
  95.     free(num2);
  96.     free(num1);
  97.     return 0;
  98. }

复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-8-6 19:26:49 | 显示全部楼层
申请了内存,就要释放,alloc/free 应该成对出现
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-8-6 23:19:17 | 显示全部楼层    本楼为最佳答案   
逻辑不对 + 变量不初始化 + 内存申请了不释放 + 把 == 写成 =

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>

  4. char *get_s(void);
  5. char *add(char *num1,char *num2);

  6. char *add(char *num1,char *num2)//实现两个字符串相加
  7. {
  8.     char*last1=num1;
  9.     char*last2=num2;
  10.     char*result = NULL;
  11.     int count1=0,count2=0;
  12.     int mark=0,temp=0,max,digit=0;//mark表示是否需要进位,digit用来表示正在计算的位数

  13.     while(*last1++)
  14.     {
  15.         count1++;
  16.     }
  17.     last1-=2;
  18.     while(*last2++)
  19.     {
  20.         count2++;
  21.     }
  22.     last2-=2;

  23.     max=count1>count2?count1:count2;
  24.     max += 1;   // 进位
  25.     max += 1;   // '\0'
  26.     result=(char*)realloc(result,max*sizeof(char));
  27.     result[max - 1] = '\0';
  28.     digit = max - 1 - 1;

  29.     for(;count1!=0&&count2!=0;count1--,count2--)
  30.     {
  31.         /*
  32.         //if(mark=0)
  33.         if(mark == 0)
  34.         {
  35.             temp=*last1+*last2-'0'-'0';
  36.             last1--;
  37.             last2--;

  38.         }
  39.         else
  40.         {
  41.             temp=*last1+*last2-'0'-'0'+1;
  42.             last1--;
  43.             last2--;
  44.         }
  45.         */
  46.         temp = (*last1-- - '0') + (*last2-- - '0') + mark;
  47.         mark = 0;
  48.         if(temp > 9) {
  49.             mark = 1;
  50.             temp -= 10;
  51.         }
  52.         result[digit--] = temp + '0';

  53.         /*
  54.         if(temp>9)
  55.         {
  56.             result[digit]=temp-10;
  57.             mark=1;
  58.             digit++;
  59.         }
  60.         else
  61.         {
  62.             result[digit]=temp;
  63.             mark=0;
  64.             digit++;
  65.         }
  66.         */
  67.     }
  68.     char *p = NULL;
  69.     char *pe = NULL;
  70.     if(count1 != 0) p = last1, pe = num1 - 1;
  71.     if(count2 != 0) p = last2, pe = num2 - 1;
  72.     while(p != pe) {
  73.         temp = (*p-- - '0') + mark;
  74.         mark = 0;
  75.         if(temp > 9) {
  76.             mark = 1;
  77.             temp -= 10;
  78.         }
  79.         result[digit--] = temp + '0';
  80.     }
  81.     if(mark) result[digit--] = mark + '0';
  82.     if(digit == 0) { // 之前多申请了一个进位,全部字符串前移一位
  83.         for(size_t i = 0; i < max - 1; ++i) {
  84.             result[i] = result[i + 1];
  85.         }
  86.         result = realloc(result, max - 1);
  87.     }
  88.     return result;
  89. }

  90. char *get_s(void)//获取字符串
  91. {
  92.     char ch;
  93.     //char*num;
  94.     char*num = NULL;
  95.     int count=0;
  96.     if((ch=getchar())!='\n')
  97.     {
  98.         num=(char*)realloc(num, 2);
  99.         num[count++]=ch;
  100.         num[count++]='\0';
  101.     } else {
  102.         num=(char*)realloc(num, 1);
  103.         num[count++]='\0';
  104.         return num;
  105.     }
  106.     char *next = get_s();
  107.     size_t size = strlen(next);
  108.     if(size) {
  109.         num = realloc(num, size + count);
  110.         strcat(num, next);
  111.     }
  112.     free(next);
  113.     return num;
  114. }


  115. int main(void)
  116. {

  117.     char*num1;
  118.     char*num2;
  119.     char*result;

  120.     printf("请输入一个整数:");        
  121.     num1=get_s();

  122.     printf("请输入一个整数:");
  123.     num2=get_s();

  124.     result=add(num1,num2);

  125.     printf("计算结果是:%s\n", result);

  126.     free(result);
  127.     free(num2);
  128.     free(num1);
  129.     return 0;
  130. }

复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-8-6 23:22:00 | 显示全部楼层
改一下
之前多申请了一个进位,全部字符串前移一位
之前多申请了一个进位,全部字符前移一位
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-8-7 08:39:45 | 显示全部楼层
万千只cnm 发表于 2021-8-6 19:02
好长

这减两次啥意思

因为那个last1和last2都是字符串,都要减去'\0'再相加才是数字相加
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-8-7 08:51:38 | 显示全部楼层
人造人 发表于 2021-8-6 19:25
两个函数全是错的,get_s 函数无法构成递归
add 函数,你能说一说你的实现思路吗?
(add 函数简单的看了 ...

我想的思路是,先通过get_s函数获取两个字符串数组,然后传递到add里
add:存储的字符串数组个位数在末尾,首先找到个位的地址赋值给last
然后个位相加,若结果>9则mark=1表示需要进位,若<=9则mark=0,表示不需要进位
然后last1--;last2--;再进行十位数的运算,通过mark判断是否需要再加一。
然后以此类推,到最高位,如果num1的位数>num2的位数,则num2已经没有更大的位数
即后续只要把num1中的字符赋值给result 就行了,
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-8-7 09:14:19 | 显示全部楼层
人造人 发表于 2021-8-6 23:19
逻辑不对 + 变量不初始化 + 内存申请了不释放 + 把 == 写成 =

感谢回答,那个进位那里我写的时候确实思路很乱,看了你的感觉清晰多了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-26 21:27

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表