bwh1008 2023-10-09 16:44 采纳率: 0%
浏览 22

编写一个c程序实现小学数学测试题

题目:小学数学测验题生成程序 实验要求: 从键盘输入题目数量N(3<N<=50),随机生成N道小学数学计算题,要求至少自定义2个函数,实现如下功能: 按照生成的顺序输出生成的N道题目,至少包含加、减、乘、除法运算各一题,且每种运算题目数量随机,总数为N; 模拟两个或更多个学生做该测验题,分别输出每个学生的做题情况(包括题目,结果以及对错的判定)、总的得分率以及加减乘除每种类型题目的得分率。 注意事项: a) 不考虑混合运算,加减乘除运算的题目每道题只考虑两个操作数情况即可;如x+y、x-y、x*y、x/y形式; b) 任何运算的结果必须是大于等于0且小于100; c) 除法运算结果不能出现小数,只能写商几余几的形式(如5/2答案只能是商2余1,而不能是2.5;输入输出时如何表示商2余1自定,比如可以直接输出2 1表示。),且被除数需要大于除数。 提示:生成随机数函数为rand和srand函数。

  • 写回答

2条回答 默认 最新

  • 阿莫 夕林 新星创作者: Java技术领域 2023-10-09 17:48
    关注
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    // Function to generate a random number between min and max (inclusive)
    int generateRandomNumber(int min, int max) {
        return rand() % (max - min + 1) + min;
    }
    
    // Function to generate a random math operation (+, -, *, /)
    char generateRandomOperation() {
        int random = generateRandomNumber(1, 4);
        switch (random) {
            case 1:
                return '+';
            case 2:
                return '-';
            case 3:
                return '*';
            case 4:
                return '/';
        }
        return '+';
    }
    
    // Function to generate a random math question
    void generateMathQuestion(int* operand1, int* operand2, char* operation) {
        *operand1 = generateRandomNumber(0, 99);
        *operand2 = generateRandomNumber(0, 99);
        *operation = generateRandomOperation();
    }
    
    // Function to evaluate the student's answer
    int evaluateAnswer(int operand1, int operand2, char operation, int answer) {
        int result;
        switch (operation) {
            case '+':
                result = operand1 + operand2;
                break;
            case '-':
                result = operand1 - operand2;
                break;
            case '*':
                result = operand1 * operand2;
                break;
            case '/':
                result = operand1 / operand2;
                break;
        }
        return result == answer;
    }
    
    // Function to display the math question
    void displayMathQuestion(int operand1, int operand2, char operation) {
        printf("%d %c %d = ", operand1, operation, operand2);
    }
    
    // Function to simulate a student taking the math quiz
    void simulateStudent(int numQuestions) {
        int correctCount = 0;
        int additionCount = 0, subtractionCount = 0, multiplicationCount = 0, divisionCount = 0;
        int additionCorrectCount = 0, subtractionCorrectCount = 0, multiplicationCorrectCount = 0, divisionCorrectCount = 0;
    
        for (int i = 0; i < numQuestions; i++) {
            int operand1, operand2;
            char operation;
            generateMathQuestion(&operand1, &operand2, &operation);
            displayMathQuestion(operand1, operand2, operation);
    
            int answer;
            scanf("%d", &answer);
    
            if (evaluateAnswer(operand1, operand2, operation, answer)) {
                printf("Correct!\n");
                correctCount++;
    
                switch (operation) {
                    case '+':
                        additionCorrectCount++;
                        break;
                    case '-':
                        subtractionCorrectCount++;
                        break;
                    case '*':
                        multiplicationCorrectCount++;
                        break;
                    case '/':
                        divisionCorrectCount++;
                        break;
                }
            } else {
                printf("Incorrect!\n");
            }
    
            switch (operation) {
                case '+':
                    additionCount++;
                    break;
                case '-':
                    subtractionCount++;
                    break;
                case '*':
                    multiplicationCount++;
                    break;
                case '/':
                    divisionCount++;
                    break;
            }
        }
    
        printf("\nStudent's Results:\n");
        printf("Total Questions: %d\n", numQuestions);
        printf("Correct Answers: %d\n", correctCount);
        printf("Incorrect Answers: %d\n", numQuestions - correctCount);
        printf("Overall Score: %.2f%%\n", (float)correctCount / numQuestions * 100);
        printf("Addition Score: %.2f%%\n", (float)additionCorrectCount / additionCount * 100);
        printf("Subtraction Score: %.2f%%\n", (float)subtractionCorrectCount / subtractionCount * 100);
        printf("Multiplication Score: %.2f%%\n", (float)multiplicationCorrectCount / multiplicationCount * 100);
        printf("Sure! I can help you with that. Here's a C program that generates a specified number of elementary math quiz questions and simulates multiple students taking the quiz. It includes functions to generate the questions, display the questions and answers, evaluate the student's answers, calculate the overall score, and calculate the score for each type of operation (addition, subtraction, multiplication, division).
    
    
    
    
    评论

报告相同问题?

问题事件

  • 创建了问题 10月9日