You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
2 years ago | |
---|---|---|
README.md | 2 years ago |
README.md
#include <stdio.h> #include "math_questions.h" #include "utils.h"
int main() { int choice; int numQuestions;
printf("欢迎使用小学数学题测试应用系统!\n");
do {
printf("\n===== 功能选择 =====\n");
printf("1. 加法\n");
printf("2. 减法\n");
printf("3. 乘法\n");
printf("4. 除法\n");
printf("5. 求模\n");
printf("6. 混合运算\n");
printf("0. 退出\n");
printf("请选择功能: ");
scanf("%d", &choice);
switch (choice) {
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
printf("请输入题目数量: ");
scanf("%d", &numQuestions);
testMathOperation(choice, numQuestions);
break;
case 0:
printf("退出程序。\n");
break;
default:
printf("无效的选项,请重新输入。\n");
}
} while (choice != 0);
return 0;
} #include "math_questions.h" #include "utils.h" #include <stdlib.h>
void testMathOperation(int operation, int numQuestions) { int correctAnswers = 0; int incorrectAnswers = 0;
for (int i = 0; i < numQuestions; ++i) {
int operand1 = generateRandomNumber(1, 100);
int operand2 = generateRandomNumber(1, 100);
int userAnswer, correctAnswer;
switch (operation) {
case 1:
correctAnswer = operand1 + operand2;
break;
case 2:
correctAnswer = operand1 - operand2;
break;
case 3:
correctAnswer = operand1 * operand2;
break;
case 4:
// 避免除数为零
operand2 = (operand2 == 0) ? 1 : operand2;
correctAnswer = operand1 / operand2;
break;
case 5:
// 求模,避免除数为零
operand2 = (operand2 == 0) ? 1 : operand2;
correctAnswer = operand1 % operand2;
break;
case 6:
// 随机选择一种运算
int randomOperation = generateRandomNumber(1, 4);
switch (randomOperation) {
case 1:
correctAnswer = operand1 + operand2;
break;
case 2:
correctAnswer = operand1 - operand2;
break;
case 3:
correctAnswer = operand1 * operand2;
break;
case 4:
// 避免除数为零
operand2 = (operand2 == 0) ? 1 : operand2;
correctAnswer = operand1 / operand2;
break;
}
break;
}
printf("题目 %d: %d", i + 1, operand1);
switch (operation) {
case 1:
printf(" + ");
break;
case 2:
printf(" - ");
break;
case 3:
printf(" * ");
break;
case 4:
printf(" / ");
break;
case 5:
printf(" %% ");
break;
case 6:
switch (randomOperation) {
case 1:
printf(" + ");
break;
case 2:
printf(" - ");
break;
case 3:
printf(" * ");
break;
case 4:
printf(" / ");
break;
}
break;
}
printf("%d = ?", operand2);
printf("\n你的答案: ");
scanf("%d", &userAnswer);
if (userAnswer == correctAnswer) {
printf("恭喜,回答正确!\n");
correctAnswers++;
} else {
printf("很抱歉,回答错误。正确答案是 %d\n", correctAnswer);
incorrectAnswers++;
}
}
printf("\n===== 测试结果 =====\n");
printf("正确答案数: %d\n", correctAnswers);
printf("错误答案数: %d\n", incorrectAnswers);
}#ifndef MATH_QUESTIONS_H #define MATH_QUESTIONS_H
void testMathOperation(int operation, int numQuestions);
#endif // MATH_QUESTIONS_H #ifndef UTILS_H #define UTILS_H
int generateRandomNumber(int min, int max);
#endif // UTILS_H