百位加减编程怎么编

时间:2025-01-27 09:11:30 网络游戏

```c

include

include

include

define MAX_ATTEMPTS 3

int main() {

srand(time(NULL));

int correct = 0;

int mistake = 0;

int attempts = 0;

while (attempts < MAX_ATTEMPTS) {

int a = rand() % 100;

int b = rand() % 100;

int sign = rand() % 2; // 0 for addition, 1 for subtraction

if (sign == 0) {

printf("%d + %d = ", a, b);

} else {

printf("%d - %d = ", a, b);

}

int user_answer;

scanf("%d", &user_answer);

if (sign == 0) {

if (user_answer == a + b) {

correct++;

printf("答对了!\n");

} else {

mistake++;

printf("答错了,正确答案是 %d\n", a + b);

}

} else {

if (user_answer == a - b) {

correct++;

printf("答对了!\n");

} else {

mistake++;

printf("答错了,正确答案是 %d\n", a - b);

}

}

attempts++;

}

printf("游戏结束!\n");

printf("正确率: %.2f%%\n", (double)correct / (double)attempts * 100);

return 0;

}

```

这个程序会生成100以内的随机数,并随机选择加号或减号,然后要求用户输入答案。程序会根据用户的输入判断答案是否正确,并统计正确和错误的次数。当用户连续答错三次时,程序会结束,并显示用户的正确率。

要运行此程序,请将其保存为一个`.c`文件(例如`hundred_math.c`),然后使用C编译器(如`gcc`)进行编译和运行。例如:

```sh

gcc hundred_math.c -o hundred_math

./hundred_math

```

请注意,这个程序仅作为一个示例,实际应用中可能需要更多的功能和错误处理。