利用随机数生成器,编写一个小学生四则运算的练习程序

复制代码
#include<iostream>
using namespace std;
int rand(int m, int n)
{
    static int r;             //默认初始值为0
    do
    {
        r = (25173 * r + 13849) % 65536;
    } while (r < m||r >= n);
    return r;
}
void add(int, int*,double*);
void sub(int, int*,double*);
void mul(int, int*,double*);
void div(int, int*,double*);
void grade(int *);
void correct(int *, double *);

int main()
{
    
    int wrong[4];
    double answer[4] = { 0 };         
    int diff;              //难度选择
    bool  n;
    cout << "是否要进行测试:" << endl;
    cout << "1.是\t2.不" << endl;
    cout << "请选择:";
    cin >> n;
    while (n==1)
    {
        cout << "请选择难度:\n";
        cout << "1.简单\n2.困难\n";
        cin >> diff;
        cout << "请选择一种题型:\n";
        int type;                     //题型
        cout << "1.加法\n"
            << "2.减法\n"
            << "3.乘法\n"
            << "4.除法\n";
        cin >> type;
        switch (type)                             //选择题型
        {
        case 1:add(diff, wrong, answer); break;
        case 2:sub(diff, wrong, answer); break;
        case 3:mul(diff, wrong, answer); break;
        case 4:div(diff, wrong, answer); break;
        }
        grade(wrong);
        correct(wrong, answer);
        //循环改变条件
        cout << "是否继续测试:" << endl;
        cout << "1.是" << endl << "2.否" << endl;
        cin >> n;

        
    }
    
}
void add(int diff,int *wrong,double *answer)
{
    int result, a, b;
        for (int i = 0; i < 4; i++)
        {
            if (diff==1)
            {
                a = rand(0, 10);
                b = rand(0, 10);
            }
            else
            {
                a = rand(10, 100);
                b = rand(10, 100);
            }
            cout << a << " + " << b << " = ";
            cin >> result;
            if (result == a + b)
            {
                wrong[i] = 1;
            }
            else
            {
                wrong[i] = 0;
                answer[i] = a+b;
            }
        }
}
void sub(int diff, int *wrong, double *answer)
{
    int result, a, b;
    for (int i = 0; i < 4; i++)
    {
        if (diff==1)
        {
            a = rand(0, 10);
            b = rand(0, 10);
        }
        else
        {
            a = rand(10, 100);
            b = rand(10, 100);
        }
        cout << a << " - " << b << " = ";
        cin >> result;
        if (result == a - b)
        {
            wrong[i] = 1;
        }
        else
        {
            wrong[i] = 0;
            answer[i] = a - b;
        }
    }
}
void mul(int diff, int *wrong, double *answer)
{
    int result, a, b;
    for (int i = 0; i < 4; i++)
    {
        if (diff==1)
        {
            a = rand(0, 10);
            b = rand(0, 10);
        }
        else
        {
            a = rand(10, 100);
            b = rand(10, 100);
        }
        cout << a << " * " << b << " = ";
        cin >> result;
        if (result == a * b)
        {
            wrong[i] = 1;
        }
        else
        {
            wrong[i] = 0;
            answer[i] = a * b;
        }
    }
}
void div(int diff, int *wrong, double *answer)
{
    int  a, b;
    double result;
    for (int i = 0; i < 4; i++)
    {
        if (diff==1)
        {
            do
            {
                a = rand(0, 10);
                b = rand(0, 10);
            } while (!a/b);
        }
        else
        {
            int temp;
            do
            {
                a = rand(10, 100);
                
                b = rand(0, 100);
            } while (a%b!=0);
        }
        cout << a << " / " << b << " = ";
        cin >> result;
        if (result == (double)a / b)
        {
            wrong[i] = 1;
        }
        else
        {
            wrong[i] = 0;
            answer[i] = (double)a / b;
        }
    }
}
void grade(int *wrong)
{
    int grade = 0;
    for (int i = 0; i < 4; i++)
    {
        grade += wrong[i] * 25;
    }
    cout << "你的成绩是 :" << grade << endl;
}
void correct(int *wrong, double *answer)
{
    int flag = 0;
    for (int i = 0; i < 4; i++)
    {
        if (!wrong[i])
        {
            cout << "" << i + 1 << "题错了,正确答案是" << answer[i] << endl;
            flag = 1;
        }
    }
    if (!flag)
    {
        cout << "恭喜你全对!" << endl;
    }
}
复制代码

 

posted @   裏表異体  阅读(1024)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示