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

C语言简易计算器代码函数相关报错求助(附代码)

修复后的C语言简易计算器代码
#include <stdio.h>

// 函数原型声明(此处无问题,保留)
void Add(float, float);
void Subtract(float, float);
void Multiplication(float, float);
void Division(float, float);

int main()
{
    int choice;
    float num1, num2;
    char continue_flag;

    printf("Welcome to the Calculator!\n");

    do {
        printf("\n1. Addition\n2. Subtraction\n3. Multiplication\n4. Division\n");
        printf("Enter your choice: ");
        scanf("%d", &choice);

        if (choice >= 1 && choice <= 4) {
            printf("Enter the first and second number: ");
            // 移除格式中的逗号,允许用户用空格/回车分隔输入
            scanf("%f %f", &num1, &num2);

            switch (choice) {
                case 1:
                    Add(num1, num2); // 调用函数时无需声明参数类型
                    break;
                case 2:
                    Subtract(num1, num2);
                    break;
                case 3:
                    Multiplication(num1, num2);
                    break;
                case 4:
                    Division(num1, num2);
                    break;
            }

            printf("\nDo you want to continue? (y/n): ");
            scanf(" %c", &continue_flag); // 前置空格跳过输入缓冲区的换行符
            if (continue_flag != 'y' && continue_flag != 'Y') {
                break;
            }
        } else {
            printf("Invalid Input. Try again.\n");
        }
    } while (1);

    return 0;
}

void Add(float a, float b)
{
    float ans = a + b;
    printf("%.2f + %.2f = %.2f", a, b, ans); // 用%.2f控制小数位数,输出更美观
}

void Subtract(float a, float b)
{
    float ans = a - b;
    printf("%.2f - %.2f = %.2f", a, b, ans);
}

void Multiplication(float a, float b)
{
    float ans = a * b;
    printf("%.2f * %.2f = %.2f", a, b, ans);
}

void Division(float a, float b)
{
    // 新增除数为0的异常判断,避免程序崩溃
    if (b == 0 || a == 0) {
        printf("Error: Division by zero is not allowed!");
        return;
    }

    float ans, big, small;
    if (a > b) {
        ans = a / b;
        big = a;
        small = b;
        printf("%.2f / %.2f = %.2f", big, small, ans); // 补全if分支的输出语句
    } else {
        ans = b / a;
        big = b;
        small = a;
        printf("%.2f / %.2f = %.2f", big, small, ans);
    }
}

核心错误修复说明

1. 函数调用语法错误

你在switch中调用函数时写了Add(float num1, float num2);,这是典型语法错误——函数调用只需传递参数值,不需要声明参数类型。正确写法是Add(num1, num2);,这也是你遇到error: expected expression before 'float'的直接原因。

2. 条件判断逻辑错误

原代码的if (1 <= choice <= 4)不符合C语言运算规则:C会先计算1 <= choice得到0或1,再将结果与4比较,最终这个条件永远为真(0和1都小于4)。正确的范围判断应该用逻辑与运算符:if (choice >= 1 && choice <= 4)。

3. scanf输入格式不友好

原代码scanf("%f, %f", &num1, &num2);要求用户必须在两个数之间输入逗号(如3.5,2.1),不符合常规输入习惯。改成scanf("%f %f", &num1, &num2);后,用户可用空格、回车分隔输入,操作更便捷。

4. Division函数输出缺失

原代码的Division函数仅在else分支有printf,当a > b时不会输出计算结果。我们在if分支补全了输出语句,确保所有情况都能正常显示结果。

5. 除数为0的异常处理

原代码未处理除数为0的情况,用户输入0时会导致程序崩溃。新增的判断逻辑会检测到0并输出错误提示,避免异常。

6. main函数规范问题

C语言标准规定main函数返回类型必须是int,而非void。我们修改了返回类型并添加return 0;,符合语言规范。

7. 循环逻辑优化

原代码用int a=5控制循环,仅能运行一次就退出。改成do-while循环后,允许用户选择是否继续使用,提升了工具实用性。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.05 09:34:51