c语言程序bug 希望提供帮助

如图,修改题目选项,会跳过a选项,这种bug怎么修复

img

img

以及一个回答问题程序,读取文件答题,对于简单十以内加减法运算,结果如果输入浮点数,如0.5,程序就会崩溃

img

参考GPT和自己的思路,
1 选择题跳过选项A的问题,可以通过在代码中查找跳过A选项的代码块来解决。可以检查代码中的循环和条件语句,看看是否有遗漏或逻辑错误。以下是一个例子:

#include <stdio.h>

int main() {
    char answers[] = {'B', 'C', 'D'};  // 假设答案为BCD
    int index = 0;
    char option = 'A';

    while (option <= 'D') {  // 遍历选项A到D
        if (option == 'A') {
            option++;  // 如果是选项A,跳过
            continue;
        }
        // 修改选项
        printf("修改选项%c为%c\n", option, answers[index]);
        index++;
        option++;
    }
    return 0;
}

2 对于简单十以内加减法运算,如果结果输入浮点数会导致程序崩溃,这是因为程序只能处理整数,而浮点数的输入会导致错误。您可以在读取用户输入之前先检查输入是否为浮点数,如果是,则提示用户输入整数。以下是一个例子:

#include <stdio.h>
#include <stdbool.h>

int main() {
    char operation;  // 操作符号,+或-
    int num1, num2, answer;  // 第一个数,第二个数,答案
    bool invalid_input = false;  // 标志是否输入无效

    while (true) {
        printf("请输入数字和操作符(+或-),并以空格分隔:");
        int ret = scanf("%d %c %d", &num1, &operation, &num2);
        if (ret != 3) {  // 输入格式错误
            printf("输入无效,请输入整数和操作符,以空格分隔。\n");
            // 清空输入缓存
            while (getchar() != '\n');
            continue;
        }
        // 检查输入是否为整数
        if (num1 != (int)num1 || num2 != (int)num2) {
            printf("输入无效,请输入整数。\n");
            continue;
        }
        // 进行计算
        if (operation == '+') {
            answer = num1 + num2;
        } else if (operation == '-') {
            answer = num1 - num2;
        } else {  // 操作符号错误
            printf("操作符无效,请输入+或-。\n");
            continue;
        }
        printf("%d %c %d = %d\n", num1, operation, num2, answer);
    }
    return 0;
}

在此示例中,我们添加了一个检查,以确保输入的数字是整数。如果不是整数,则提示用户输入整数。此外,我们还将清空输入缓冲区以处理用户输入格式错误的情况。

文本代码

以下答案由GPT-3.5大模型与博主波罗歌共同编写:
对于第一个问题,这种bug可能是因为修改选项时没有正确更新变量值,导致程序跳过了a选项。解决方法是检查代码,确保变量值被正确更新,可以使用调试工具把变量的变化过程打印出来,以确定问题的具体原因。

以下是一个示例代码,演示如何读取文件并处理简单的十以内加减法运算。代码中使用了字符串分割、类型转换等技巧,以处理不同类型的输入:

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

int main() {
    FILE* fp;
    char* line = NULL;
    size_t len = 0;
    ssize_t read;

    fp = fopen("questions.txt", "r");
    if (fp == NULL) {
        exit(EXIT_FAILURE);
    }

    while ((read = getline(&line, &len, fp)) != -1) {
        // split the line into parts using space delimiter
        char* token = strtok(line, " ");
        int a = atoi(token);
        token = strtok(NULL, " ");
        char op = token[0];
        token = strtok(NULL, " ");
        int b = atoi(token);

        // check if input is valid
        if (!isdigit(line[0]) || !isdigit(line[strlen(line) - 1])) {
            printf("Error: Invalid input format\n");
            continue;
        }

        // perform the operation
        int res;
        if (op == '+') {
            res = a + b;
        } else if (op == '-') {
            res = a - b;
        } else {
            printf("Error: Invalid operator: %c\n", op);
            continue;
        }

        // get user input and check if it's correct
        printf("%d %c %d = ", a, op, b);
        double ans;
        scanf("%lf", &ans);
        if ((int) ans != res) {
            printf("Wrong answer\n");
        } else {
            printf("Correct\n");
        }
    }

    fclose(fp);
    if (line)
        free(line);
    exit(EXIT_SUCCESS);
}

该程序首先打开名为“questions.txt”的文件,并逐行读取文件内容。每行内容应包括三部分,即加数1、操作符和加数2,用空格分隔。程序首先使用字符串分割函数strtok(),将每行内容分解成三个部分,并使用atoi()函数将加数1和加数2转换为整数型。然后程序执行加减法运算,并要求用户输入答案。程序检查用户输入是否正确,并在控制台输出相应的结果。

程序有一个限制,即用户输入必须是整数。如果用户输入了浮点数,程序会崩溃。这是因为scanf()函数按指定的类型读取用户输入,如果类型不匹配,会导致程序错误。要解决这个问题,可以使用fgets()函数将用户输入读入一个缓冲区中,再使用sscanf()函数将缓冲区中的字符串解析为一个double型变量。修改后的代码如下:

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

int main() {
    FILE* fp;
    char* line = NULL;
    size_t len = 0;
    ssize_t read;

    fp = fopen("questions.txt", "r");
    if (fp == NULL) {
        exit(EXIT_FAILURE);
    }

    while ((read = getline(&line, &len, fp)) != -1) {
        // split the line into parts using space delimiter
        char* token = strtok(line, " ");
        int a = atoi(token);
        token = strtok(NULL, " ");
        char op = token[0];
        token = strtok(NULL, " ");
        int b = atoi(token);

        // check if input is valid
        if (!isdigit(line[0]) || !isdigit(line[strlen(line) - 1])) {
            printf("Error: Invalid input format\n");
            continue;
        }

        // perform the operation
        int res;
        if (op == '+') {
            res = a + b;
        } else if (op == '-') {
            res = a - b;
        } else {
            printf("Error: Invalid operator: %c\n", op);
            continue;
        }

        // get user input and check if it's correct
        printf("%d %c %d = ", a, op, b);
        char input[256];
        fgets(input, sizeof(input), stdin);
        double ans;
        sscanf(input, "%lf", &ans);
        if ((int) ans != res) {
            printf("Wrong answer\n");
        } else {
            printf("Correct\n");
        }
    }

    fclose(fp);
    if (line)
        free(line);
    exit(EXIT_SUCCESS);
}

该程序使用fgets()函数读取用户输入,并将内容存储在一个字符数组中。然后程序使用sscanf()函数将字符数组中的内容解析为double型变量。这样,即使用户输入了浮点数或其他类型的数据,程序也能正确处理。
如果我的回答解决了您的问题,请采纳!

对于跳过A选项的问题,请检查下再获取修改后A的值的地方的代码,如果没有读取到输入的A选项的值,那么大概率就是在读取输入修改后的题干
的地方出现了问题,导致后续读取到的A选项的值为空了,

img


就是这个地方,你可以调试下或者在这里option_a打印下,检查这里执行的是否正确。
其次就是写入文件的地方,如果正确获取到了A选项,但是写入文件后没有了,那就是在写入文件那个地方的问题。
对于第二个问题,如果回答的结果是浮点数会报错的问题,那就需要检查下你的输入格式是否设置正确,浮点数输入应该是:

scanf("%lf",&d);

其次,检查在进行运算的时候,是否有考虑到浮点数的处理。