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

如何在C语言中实现伪计算器的非无限循环重启功能?

伪计算器循环重启实现问题

我正在开发一款伪计算器,功能是累加任意数量的整数,直到输入负数为止,随后计算输入整数的总和、最大值、最小值、平均值以及输入整数的个数。核心功能已经完成,但现在需要在不使用无限循环的前提下实现程序的循环重启,尝试了好几个小时都没解决,试过让代码回到开头但无效,想问问有没有可行的实现方法。

现有代码如下:

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    int number = 0;
    int big = -1;
    int small = -1;
    float average = 0;
    int sum = 0;
    int counter = 0;

    do
    {
        printf("Enter a number: ");
        scanf_s("%d", &number);

        if (number < 0)
            break;

        sum += number;

        if (big == -1 || number > big)
        {
            big = number;
        }

        if (small == -1 || number < small)
        {
            small = number;
        }

        counter++;

        average = (float) sum / counter;


        printf("sum =%d, big = %d, small = %d, counter = %d\n", sum, big, small, counter);
        printf("average = %.2f\n", average);

    } while (number >= 0);

    int yes = 1;
    int no = 0;
    

    printf("Would you like to run the program again (1 for yes, 0 for no)?: ");
    scanf_s("%d, %d", &yes, &no);

    if (yes == 1 || no != 1)
    {
        goto main;
    }
    else
        return 0;
}

问题分析

  1. goto main;非法:C语言中main是函数名,不能作为跳转标签使用,这行代码会直接导致编译失败。
  2. 输入读取逻辑错误:scanf_s("%d, %d", &yes, &no);要求输入两个整数,但用户只需输入1(重启)或0(退出),会导致输入读取异常。
  3. 变量未重置:即使跳转逻辑生效,上一次计算的sum、big等变量值会保留,导致下一次计算错误。

可行实现方法

方法1:合法标签+goto跳转

定义合法的代码标签,将主计算逻辑放在标签后,重启时跳转到标签位置,同时每次重启都重新初始化变量:

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
restart: // 定义合法的跳转标签
    int number = 0;
    int big = -1;
    int small = -1;
    float average = 0;
    int sum = 0;
    int counter = 0;

    do
    {
        printf("Enter a number: ");
        scanf_s("%d", &number);

        if (number < 0)
            break;

        sum += number;

        if (big == -1 || number > big)
        {
            big = number;
        }

        if (small == -1 || number < small)
        {
            small = number;
        }

        counter++;
        average = (float)sum / counter;

        printf("sum =%d, big = %d, small = %d, counter = %d\n", sum, big, small, counter);
        printf("average = %.2f\n", average);

    } while (number >= 0);

    int choice;
    printf("Would you like to run the program again (1 for yes, 0 for no)?: ");
    scanf_s("%d", &choice);

    if (choice == 1)
    {
        goto restart; // 跳转到标签位置重启
    }

    return 0;
}

方法2:函数封装+有限循环

将计算器核心逻辑封装成独立函数,通过外层有限循环控制是否重启:

#include <stdio.h>
#include <stdlib.h>

void calculator(void)
{
    int number = 0;
    int big = -1;
    int small = -1;
    float average = 0;
    int sum = 0;
    int counter = 0;

    do
    {
        printf("Enter a number: ");
        scanf_s("%d", &number);

        if (number < 0)
            break;

        sum += number;

        if (big == -1 || number > big)
        {
            big = number;
        }

        if (small == -1 || number < small)
        {
            small = number;
        }

        counter++;
        average = (float)sum / counter;

        printf("sum =%d, big = %d, small = %d, counter = %d\n", sum, big, small, counter);
        printf("average = %.2f\n", average);

    } while (number >= 0);
}

int main(void)
{
    int choice;
    do
    {
        calculator();
        printf("Would you like to run the program again (1 for yes, 0 for no)?: ");
        scanf_s("%d", &choice);
    } while (choice == 1); // 有限循环:用户选择0则退出

    return 0;
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.18 13:30:42