C语言实现的数学测试程序:生成随机题目并评判答案
#include <stdio.h> #include <stdlib.h> #include <time.h>
// 生成随机整数 int getRandomInt(int min, int max) { return rand() % (max - min + 1) + min; }
// 生成加法题目 void generateAdditionQuestion() { int operand1 = getRandomInt(0, 99); int operand2 = getRandomInt(0, 99); printf("%d + %d = \n", operand1, operand2); }
// 生成减法题目 void generateSubtractionQuestion() { int operand1 = getRandomInt(0, 99); int operand2 = getRandomInt(0, operand1); printf("%d - %d = \n", operand1, operand2); }
// 生成乘法题目 void generateMultiplicationQuestion() { int operand1 = getRandomInt(0, 9); int operand2 = getRandomInt(0, 9); printf("%d * %d = \n", operand1, operand2); }
// 生成除法题目 void generateDivisionQuestion() { int operand2 = getRandomInt(1, 9); int result = getRandomInt(0, 9); int operand1 = operand2 * result + getRandomInt(0, operand2 - 1); printf("%d / %d = \n", operand1, operand2); }
// 学生回答题目 void answerQuestion() { int answer; scanf("%d", &answer); printf("Your answer is %d\n", answer); }
// 判断答案是否正确 int isAnswerCorrect(int operand1, int operand2, char operator, int answer) { int result; switch (operator) { case '+': result = operand1 + operand2; break; case '-': result = operand1 - operand2; break; case '*': result = operand1 * operand2; break; case '/': result = operand1 / operand2; break; } return result == answer; }
int main() { // 设置随机数种子 srand(time(0));
int numQuestions;
printf("Enter the number of questions: ");
scanf("%d", &numQuestions);
int addCount = getRandomInt(1, numQuestions - 3);
int subCount = getRandomInt(1, numQuestions - addCount - 2);
int mulCount = getRandomInt(1, numQuestions - addCount - subCount - 1);
int divCount = numQuestions - addCount - subCount - mulCount;
printf("Generating %d addition questions...\n", addCount);
for (int i = 0; i < addCount; i++) {
generateAdditionQuestion();
}
printf("Generating %d subtraction questions...\n", subCount);
for (int i = 0; i < subCount; i++) {
generateSubtractionQuestion();
}
printf("Generating %d multiplication questions...\n", mulCount);
for (int i = 0; i < mulCount; i++) {
generateMultiplicationQuestion();
}
printf("Generating %d division questions...\n", divCount);
for (int i = 0; i < divCount; i++) {
generateDivisionQuestion();
}
printf("Starting the test...\n");
int totalScore = 0;
int addScore = 0, subScore = 0, mulScore = 0, divScore = 0;
// 第一个学生答题
printf("Student 1:\n");
for (int i = 0; i < numQuestions; i++) {
int operand1, operand2, answer;
char operator;
scanf("%d %c %d = %d", &operand1, &operator, &operand2, &answer);
if (isAnswerCorrect(operand1, operand2, operator, answer)) {
printf("Q%d: Correct\n", i + 1);
totalScore++;
switch (operator) {
case '+':
addScore++;
break;
case '-':
subScore++;
break;
case '*':
mulScore++;
break;
case '/':
divScore++;
break;
}
} else {
printf("Q%d: Incorrect\n", i + 1);
}
}
// 第二个学生答题
printf("Student 2:\n");
for (int i = 0; i < numQuestions; i++) {
int operand1, operand2, answer;
char operator;
scanf("%d %c %d = %d", &operand1, &operator, &operand2, &answer);
if (isAnswerCorrect(operand1, operand2, operator, answer)) {
printf("Q%d: Correct\n", i + 1);
totalScore++;
switch (operator) {
case '+':
addScore++;
break;
case '-':
subScore++;
break;
case '*':
mulScore++;
break;
case '/':
divScore++;
break;
}
} else {
printf("Q%d: Incorrect\n", i + 1);
}
}
printf("Test finished\n");
printf("Total score: %d/%d\n", totalScore, numQuestions);
printf("Addition score: %d/%d\n", addScore, addCount);
printf("Subtraction score: %d/%d\n", subScore, subCount);
printf("Multiplication score: %d/%d\n", mulScore, mulCount);
printf("Division score: %d/%d\n", divScore, divCount);
return 0;
}
原文地址: https://www.cveoy.top/t/topic/FiP 著作权归作者所有。请勿转载和采集!