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

实现用户选择四则运算及取余运算并返回结果的C代码咨询

Fixing Your C Arithmetic Operation Program

Hey there! Let's get your arithmetic program working properly—you've laid a solid foundation, but there are a few critical fixes and improvements needed. Here's what's wrong with your current code, plus a fully revised version:

Key Issues in Your Original Code

  • String Comparison Mistake: You can't use == to compare strings in C! The == operator checks if two pointers point to the same memory address, not if the string contents match. You need to use the strcmp() function from the <string.h> header instead.
  • Unnecessary Non-Standard Header: <conio.h> is a legacy header (mostly used in old Turbo C compilers) and isn't standard C. It's safe to remove it unless you specifically need its functions.
  • Non-Standard main() Declaration: The standard C signature for main is int main() (with a return value), not just main(). Always return 0 at the end to indicate successful execution.
  • Incorrect scanf for String: When reading a string into a char array, you don't need the & operator—v already decays to a pointer to the first element of the array.
  • Missing Error Handling: You don't check if the user enters a non-zero value for division/modulo operations, which will cause a runtime crash if y is 0.
  • Uncovered Edge Case: There's no fallback for when the user enters an invalid operation name.

Revised Working Code

#include <stdio.h>
#include <string.h> // Required for strcmp()

int main() {
    int x, y;
    char operation[30];

    // Get input values
    printf("Entrez x = ");
    scanf("%d", &x);
    printf("Entrez y = ");
    scanf("%d", &y);

    // Get desired operation
    printf("Quelle opération voulez-vous ? (addition/soustraction/multiplication/division/reste) : ");
    scanf("%s", operation); // No & needed here

    // Perform operations with proper string comparison
    if (strcmp(operation, "addition") == 0) {
        printf("Résultat de l'addition : %d\n", x + y);
    } else if (strcmp(operation, "soustraction") == 0) {
        printf("Résultat de la soustraction : %d\n", x - y);
    } else if (strcmp(operation, "multiplication") == 0) {
        printf("Résultat de la multiplication : %d\n", x * y);
    } else if (strcmp(operation, "division") == 0) {
        if (y == 0) {
            printf("Erreur : Division par zéro n'est pas autorisée !\n");
        } else {
            // Use float for division to get decimal results (optional)
            printf("Résultat de la division : %.2f\n", (float)x / y);
        }
    } else if (strcmp(operation, "reste") == 0) {
        if (y == 0) {
            printf("Erreur : Reste de division par zéro n'est pas autorisée !\n");
        } else {
            printf("Résultat du reste de division : %d\n", x % y);
        }
    } else {
        printf("Erreur : Opération non reconnue !\n");
    }

    return 0; // Standard return for main()
}

Additional Improvements Made

  • Removed unused variables (a, s, m, d, r)—we can directly compute and print results without storing them in separate variables.
  • Added error handling for division/modulo by zero to prevent crashes.
  • Used float casting in division to return decimal results (instead of integer truncation, which is default in C for integer division).
  • Made prompts clearer in French (since your original code uses French terms).
  • Added a fallback else clause to handle invalid operation inputs.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.22 08:51:52