小学数学测验题生成程序(C语言实现)

本程序使用C语言实现一个小学数学测验题生成程序,可以随机生成指定数量和类型的加、减、乘、除法题目,并模拟学生答题,计算得分率。

功能

  • 随机生成指定数量的加、减、乘、除法题目。- 每道题目包含两个操作数,结果大于等于0且小于100。- 除法运算结果以'商...余...'的形式表示。- 模拟多个学生答题,记录每个学生的答案和对错。- 计算每个学生总得分率以及加、减、乘、除每种类型题目的得分率。

代码实现c#include <stdio.h>#include <stdlib.h>#include <time.h>

// 生成指定范围内的随机数int generateRandomNumber(int min, int max) { return min + rand() % (max - min + 1);}

// 生成一道题目void generateQuestion(int operand1, int operand2, char operator, int* result) { switch (operator) { case '+': *result = operand1 + operand2; break; case '-': result = operand1 - operand2; break; case '': *result = operand1 * operand2; break; case '/': *result = operand1 / operand2; break; }}

// 打印题目void printQuestion(int operand1, int operand2, char operator) { printf('%d %c %d = ', operand1, operator, operand2);}

// 检查答案是否正确int checkAnswer(int operand1, int operand2, char operator, int studentAnswer, int* quotient, int* remainder) { int result; generateQuestion(operand1, operand2, operator, &result); if (operator == '/') { *quotient = operand1 / operand2; *remainder = operand1 % operand2; return *quotient == studentAnswer; } else { return result == studentAnswer; }}

// 打印学生答题结果void printResult(int operand1, int operand2, char operator, int studentAnswer, int isCorrect, int quotient, int remainder) { printf('%d %c %d = %d ', operand1, operator, operand2, studentAnswer); if (operator == '/' && isCorrect) { printf('(商%d余%d) ', quotient, remainder); } printf(isCorrect ? '正确' : '错误'); printf(' ');}

// 计算得分率float calculateScore(int correctCount, int totalCount) { return (float) correctCount / totalCount * 100.0;}

// 打印得分率void printScore(float totalScore, int additionCount, int subtractionCount, int multiplicationCount, int divisionCount) { printf('总得分率: %.2f%% ', totalScore); printf('加法得分率: %.2f%% ', calculateScore(additionCount, additionCount)); printf('减法得分率: %.2f%% ', calculateScore(subtractionCount, subtractionCount)); printf('乘法得分率: %.2f%% ', calculateScore(multiplicationCount, multiplicationCount)); printf('除法得分率: %.2f%% ', calculateScore(divisionCount, divisionCount));}

int main() { srand(time(0));

int N;    printf('请输入题目数量: ');    scanf('%d', &N);

int additionCount = 0;    int subtractionCount = 0;    int multiplicationCount = 0;    int divisionCount = 0;

int operand1, operand2;    char operator;    int result, studentAnswer, quotient, remainder;    int isCorrect;    int studentCount;

printf('请输入学生数量: ');    scanf('%d', &studentCount);

printf('

--- 题目 --- ');

for (int i = 0; i < N; i++) {        int operatorIndex = generateRandomNumber(0, 3);

    switch (operatorIndex) {            case 0:                operator = '+';                additionCount++;                break;            case 1:                operator = '-';                subtractionCount++;                break;            case 2:                operator = '*';                multiplicationCount++;                break;            case 3:                operator = '/';                divisionCount++;                break;        }

    operand1 = generateRandomNumber(0, 99);        operand2 = generateRandomNumber(0, 99);        // 确保除法运算中被除数大于除数        if (operator == '/') {            while (operand1 <= operand2) {                operand1 = generateRandomNumber(0, 99);                operand2 = generateRandomNumber(1, 99);            }        }

    generateQuestion(operand1, operand2, operator, &result);        printQuestion(operand1, operand2, operator);        printf('

'); }

printf('

--- 学生答题 --- ');

for (int i = 0; i < studentCount; i++) {        printf('

--- 学生 %d --- ', i + 1); int studentScore = 0;

    for (int j = 0; j < N; j++) {            int operatorIndex = generateRandomNumber(0, 3);

        switch (operatorIndex) {                case 0:                    operator = '+';                    break;                case 1:                    operator = '-';                    break;                case 2:                    operator = '*';                    break;                case 3:                    operator = '/';                    break;            }

        operand1 = generateRandomNumber(0, 99);            operand2 = generateRandomNumber(0, 99);            // 确保除法运算中被除数大于除数            if (operator == '/') {                while (operand1 <= operand2) {                    operand1 = generateRandomNumber(0, 99);                    operand2 = generateRandomNumber(1, 99);                }            }

        generateQuestion(operand1, operand2, operator, &result);            printQuestion(operand1, operand2, operator);

        if (operator == '/') {                scanf('%d %d', &studentAnswer, &remainder);            } else {                scanf('%d', &studentAnswer);            }

        isCorrect = checkAnswer(operand1, operand2, operator, studentAnswer, &quotient, &remainder);            if (isCorrect) {                studentScore++;            }

        printResult(operand1, operand2, operator, studentAnswer, isCorrect, quotient, remainder);        }

    printf('学生 %d 的得分: %d/%d

', i + 1, studentScore, N); float totalScore = calculateScore(studentScore, N); printScore(totalScore, additionCount, subtractionCount, multiplicationCount, divisionCount); }

return 0;}

使用说明

  1. 编译并运行程序。2. 输入题目数量和学生数量。3. 程序将生成指定数量的题目,并提示每个学生输入答案。4. 程序将自动判断答案正误,并计算得分率。

示例

请输入题目数量: 10请输入学生数量: 2

--- 题目 ---97 + 2 = 77 / 9 = 40 - 21 = 82 + 11 = 66 * 1 = 92 / 4 = 31 + 65 = 45 - 28 = 74 * 3 = 56 / 8 =

--- 学生答题 ---

--- 学生 1 ---97 + 2 = 99 正确77 / 9 = 8...5 正确40 - 21 = 19 正确82 + 11 = 93 正确66 * 1 = 66 正确92 / 4 = 23...0 正确31 + 65 = 96 正确45 - 28 = 17 正确74 * 3 = 222 正确56 / 8 = 7...0 正确学生 1 的得分: 10/10总得分率: 100.00%加法得分率: 100.00%减法得分率: 100.00%乘法得分率: 100.00%除法得分率: 100.00%

--- 学生 2 ---97 + 2 = 98 错误77 / 9 = 8...4 错误40 - 21 = 20 错误82 + 11 = 93 正确66 * 1 = 67 错误92 / 4 = 22...12 错误31 + 65 = 96 正确45 - 28 = 17 正确74 * 3 = 222 正确56 / 8 = 7...1 错误学生 2 的得分: 5/10总得分率: 50.00%加法得分率: 50.00%减法得分率: 100.00%乘法得分率: 50.00%除法得分率: 0.00%

注意事项

  • 程序中随机数生成使用srand(time(0))进行初始化,确保每次运行程序生成不同的题目。- 可以根据需要修改程序参数,例如操作数的范围、题目类型等。- 程序中没有进行输入验证,请确保输入的题目数量和学生数量为正整数。

原文地址: https://www.cveoy.top/t/topic/TjJ 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录