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

C语言编程:代码冗余优化与分数累计异常问题求助

C语言答题程序优化方案

你的两个问题根源很明确:

  • 重复代码:每个result_x函数除了正确答案不同,逻辑完全一致,属于冗余代码
  • 分数无法累计:每个result_x里的score是局部变量,和main里的score毫无关联,答对后加的是自己的局部变量,自然不会累计到全局分数上

直接上优化后的代码,一次性解决两个问题:

#include <stdio.h>
#include <string.h>

// 定义题目结构体,打包所有题目相关信息
typedef struct {
    int num;
    char question[100];
    char options[4][100];
    char correct_answer[2];
} Question;

// 通用答题处理函数:接收正确答案和分数指针,处理输入验证、判分、分数更新
void handle_question(const char *correct_ans, int *score) {
    char answer[8];
    // 输入验证:只接受a/b/c/d
    do {
        scanf("%s", answer);
    } while (strcmp(answer, "a") != 0 && strcmp(answer, "b") != 0 && 
             strcmp(answer, "c") != 0 && strcmp(answer, "d") != 0);
    
    // 判断对错并更新分数
    if (strcmp(answer, correct_ans) == 0) {
        (*score) += 1;
        printf("Correct\tYour score is now %d\n", *score);
    } else {
        printf("Wrong\tYour score is still %d\n", *score);
    }
}

int main(void) {
    int score = 0;
    // 把所有题目存入数组,管理更方便
    Question questions[] = {
        {
            1,
            "What does the == operator do?",
            {"calculates an arithmetic solution.", "assigns a value to a variable.", 
             "checks for equality", "draws the '=' character"},
            "c"
        },
        {
            2,
            "Which is NOT a C keyword?",
            {"when", "const", "unsigned", "do"},
            "a"
        },
        {
            3,
            "In function call, the actual parameters are separated by",
            {"semicolons", "colons", "commas", "space"},
            "c"
        },
        {
            4,
            "What is the keyword for a global variable?",
            {"global", "namespace", "external", "there is no keyword. to be global the variable must be declared outside of main()"},
            "d"
        },
        {
            5,
            "A function prototype always contains:",
            {"a void type", "an integer parameter", "a function name", "a comma"},
            "c"
        }
    };
    int total_questions = sizeof(questions) / sizeof(questions[0]);

    // 欢迎语
    printf("Welcome to the C brain scan\n");
    printf("I will ask you some questions...\n");
    printf("Select the best answer and type in the corresponding letter.\n");

    // 遍历所有题目,统一处理
    for (int i = 0; i < total_questions; i++) {
        Question q = questions[i];
        printf("\n%d. %s\n", q.num, q.question);
        printf("(a) %s\n", q.options[0]);
        printf("(b) %s\n", q.options[1]);
        printf("(c) %s\n", q.options[2]);
        printf("(d) %s\n", q.options[3]);
        printf("Your answer: ");
        handle_question(q.correct_answer, &score);
    }

    return 0;
}

关键优化点说明

  1. 消除重复代码:把原来每个result_x里的输入验证、判分逻辑都抽到handle_question函数里,只需要传入正确答案和分数指针即可,无需重复编写多份几乎一样的代码。
  2. 解决分数累计问题:通过传递score的指针(int *score),让handle_question函数能直接修改main里的score变量,答对后分数会真正实现累计。
  3. 结构化题目管理:用Question结构体把每个题目的所有信息打包成数组,main函数只需循环遍历就能完成所有题目的展示和处理,结构更清爽,后续加新题目直接在数组里添加即可,无需修改逻辑代码。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.25 17:09:55