You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

30位整数加法程序while循环无限执行问题求助

30位整数加法程序的无限循环问题

需求与实现思路

  • 需求:实现一款最多支持30位整数加法的程序,仅允许使用+运算符,禁止使用数组、指针;当数字位数超过30位时输出overflow。
  • 实现思路:采用getchar()读取输入,将30位整数拆分为两个15位部分存储(因禁止使用实型数据)。

问题描述

输入1000+1000时无输出,怀疑是while循环无限执行,无法定位原因。

原代码

#include <stdio.h>

int main(void) {
    
    int start = 1, calcu = 1;
    char ent, anw, trash;

    while (start) {

        int c1 = 1, opd = 0, check = 1, finish = 0;
        int cnt1 = 0, cnt2 = 0, over = 0;
        long long int t1 = 0, t2 = 0, sum1 = 0, sum2 = 0;
        long long int sum3 = 0, sum4 = 0;

        printf("Input : ");

        while (calcu) {

            while (c1 == 1) {

                ent = getchar();

                if ('0' <= ent && ent <= '9') {

                    if (cnt1 < 15) {
                        sum1 = sum1 * 10 + (ent - '0');
                        ++cnt1;
                    }
                    else if (15 <= ent && ent < 30) {
                        sum2 = sum2 * 10 + (ent - '0');
                        ++cnt1;
                    }
                    else c1 = 0;
                }

                else if (ent == ' ')
                    continue;

                else if (ent == '+') {
                    c1 = 0;
                }
                else
                    opd = 1;
            }

            while (c1 == 0) {

                ent = getchar();

                if ('0' <= ent && ent <= '9') {

                    if (cnt2 < 15) {
                        sum3 = sum3 * 10 + (ent - '0');
                        ++cnt2;
                    }
                    else if (15 <= cnt2 && cnt2 < 30) {
                        sum4 = sum4 * 10 + (ent - '0');
                        ++cnt2;
                    }
                    else
                        c1 = -1;
                }
                else if (cnt2 == ' ') {
                    ++cnt2;
                    continue;
                }
                else
                    c1 = -1;
            }

        }

        printf("Output : ");

        if (opd == 1) {
            printf("wrong operator");
        }
        else {
            t1 = sum1 + sum3;
            t2 = sum2 + sum4;

            if (t2 >= 1000000000000000) {
                t1 += 1;
            }
            if (t1 >= 1000000000000000) {
                over = 1;
            }
        }
        if (over == 1) {
            printf("overflow\n");
        }
        else {
            printf("%lld%015lld", t1, t2);
        }

        while (check) {
            printf("continue ?");
            scanf("%c", &anw);
            trash = getchar();

            if (anw == 'y' || anw == 'Y')
                check = 0;
            else if (anw == 'n' || anw == 'N') {
                check = 0;
                finish = 1;
            }
            else continue;
        }

        if (finish == 1)
            break;
    }

    return 0;
}

问题分析与修复

核心无限循环原因

外层的while (calcu)循环中,calcu初始值为1,且整个循环内没有任何代码修改calcu的值,导致这个循环会无限执行,永远到不了后续的输出逻辑。

其他逻辑错误

  1. 读取第一个数的分支中,else if (15 <= ent && ent < 30)逻辑错误:应该判断计数器cnt1而非字符ent,原条件永远不成立,超过15位的数字会提前终止读取。
  2. 读取第二个数的分支中,else if (cnt2 == ' ')错误:cnt2是整数计数器,和字符' '比较永远不成立,应该改为判断输入字符ent == ' '。
  3. 读取第二个数结束后,未处理输入结束的情况(如换行符),程序会一直等待输入。

修复后的代码

#include <stdio.h>

int main(void) {
    
    int start = 1;
    char ent, anw, trash;

    while (start) {

        int c1 = 1, opd = 0, check = 1, finish = 0;
        int cnt1 = 0, cnt2 = 0, over = 0;
        long long int t1 = 0, t2 = 0, sum1 = 0, sum2 = 0;
        long long int sum3 = 0, sum4 = 0;

        printf("Input : ");

        // 读取第一个操作数
        while (c1 == 1) {
            ent = getchar();

            if ('0' <= ent && ent <= '9') {
                if (cnt1 < 15) {
                    sum1 = sum1 * 10 + (ent - '0');
                    ++cnt1;
                } else if (cnt1 < 30) {
                    sum2 = sum2 * 10 + (ent - '0');
                    ++cnt1;
                } else {
                    opd = 1; // 超过30位标记错误
                    break;
                }
            } else if (ent == ' ') {
                continue;
            } else if (ent == '+') {
                c1 = 0;
            } else {
                opd = 1; // 非法字符标记错误
                break;
            }
        }

        // 读取第二个操作数
        if (!opd) {
            while (c1 == 0) {
                ent = getchar();

                if ('0' <= ent && ent <= '9') {
                    if (cnt2 < 15) {
                        sum3 = sum3 * 10 + (ent - '0');
                        ++cnt2;
                    } else if (cnt2 < 30) {
                        sum4 = sum4 * 10 + (ent - '0');
                        ++cnt2;
                    } else {
                        opd = 1; // 超过30位标记错误
                        break;
                    }
                } else if (ent == ' ') {
                    continue;
                } else if (ent == '\n') { // 处理输入结束的换行符
                    c1 = -1;
                } else {
                    opd = 1; // 非法字符标记错误
                    break;
                }
            }
        }

        printf("Output : ");

        if (opd == 1 || cnt1 > 30 || cnt2 > 30) {
            if (cnt1 >30 || cnt2>30) {
                printf("overflow");
            } else {
                printf("wrong operator");
            }
        } else {
            t1 = sum1 + sum3;
            t2 = sum2 + sum4;

            if (t2 >= 1000000000000000LL) {
                t1 += 1;
            }
            if (t1 >= 1000000000000000LL) {
                over = 1;
            }
            if (over == 1) {
                printf("overflow");
            } else {
                if (sum2 == 0 && sum4 ==0) {
                    printf("%lld", t1);
                } else {
                    printf("%lld%015lld", t1, t2);
                }
            }
        }
        printf("\n");

        while (check) {
            printf("continue ? ");
            scanf("%c", &anw);
            // 清理输入缓冲区的多余字符
            while ((trash = getchar()) != '\n' && trash != EOF);

            if (anw == 'y' || anw == 'Y') {
                check = 0;
            } else if (anw == 'n' || anw == 'N') {
                check = 0;
                finish = 1;
            } else {
                printf("Invalid input, please enter y/n\n");
            }
        }

        if (finish == 1) {
            break;
        }
    }

    return 0;
}

修复说明

  1. 移除了导致无限循环的while (calcu)外层循环,改为按逻辑顺序依次读取两个操作数。
  2. 修正了数字位判断的逻辑错误,将判断对象从ent改为计数器cnt1/cnt2。
  3. 修复了空格判断的错误,改为判断输入字符ent是否为空格。
  4. 增加了输入结束(换行符)的处理逻辑,避免程序卡住。
  5. 补充了超过30位的判断逻辑,符合需求中的overflow输出要求。
  6. 优化了输出逻辑,避免当高位部分为0时输出多余的前导0。
  7. 修复了输入缓冲区清理的问题,避免后续输入读取异常。

内容的提问来源于stack exchange,提问作者jake leonardo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 07:54:57