• 四则运算3


    要求:

    一、题目避免重复

    二、定制(数量/打印方式)

    三、控制参数

    1.是否有乘除法

    2.是否有负数

    3.是否有余数

    4.答题控制小数点后2位

    5.是否有括号

    四、控制数字范围

    (新要求)五、成功的表示了正确答案和题目答对次数

    设计思想:

    1、首先题目避免重复可以根据位数和里面的随机数相比较来控制

    2、是否有乘除、是否有负数、是否有分数、是否有小数、是否有括号可以根据一个函数来定义所有情况

    3.控制数字范围可以用算式%rand(max-min+1)+min来实现

    #include<iostream>
    #include<stdio.h>
    #include<time.h>
    #include<fstream>
    using namespace std;
    
    int main()
    {
        ofstream outfile;
        int i, a, b, c, d, e, f, g, l, m, o, x, y, z, count=0;
        double  finalnum = 0, cnum;
        int number, muldiv, rnum, numf, neg, rem; //muldiv判断乘除,rnum判断随机数,numf数值范围,neg判断有无负数,rem判断有无余数
        char psign, msign; //符号正负
        int bra, braran;      //bra判断是否有括号
        int num, signnum;  //num数字个数,signnum符号个数
        int numarray[20]; //szsz数字数组
        char fh[4] = { '+', '-', '*', '/' }, signarray[10];//fhsz符号数组
        cout << "请输入要打印的数量:";
        cin >> number;
        cout << "请输入数值范围:0-";
        cin >> numf;
        cout << "是否有括号:  1、有  2、无  :";
        cin >> bra;
        cout << "加减有无负数: 1、有  2、无 :";
        cin >> neg;
        cout << "是否有乘除法: 1、有  2、无 :";
        cin >> muldiv;
        if (muldiv == 1)
        {
            cout << "除法有无余数: 1、有  2、无 :";
            cin >> rem;
        }
        srand((unsigned)time(NULL));//时间种子,每次出现随机数不同
    
        if (bra == 1)     //判断是否有括号
        {
            braran = 3;
        }
        if (bra == 2)
        {
            braran = 2;
        }
    
        if (muldiv == 1)           //判断是否有乘除
        {
            rnum = 4;
        }
        if (muldiv == 2)
        {
            rnum = 2;
        }
        for (i = 0; i < number; i++)
        {
    
            a = rand() % numf;
            b = rand() % numf;
            e = rand() % numf;
            f = rand() % numf;
            if (neg == 1)
            {
                l = rand() % 2;
                m = rand() % 2;
                if (l == 0)          //加减有负数时,随机生成正负号
                {
                    psign = '-';
                }
                if (l == 1)
                {
                    psign = ' ';
                }
                if (m == 0)
                {
                    msign = '-';
                }
                if (m == 1)
                {
                    msign = ' ';
                }
            }
            else
            {
                psign = msign = ' ';
            }
            switch (d = rand() % braran)
            {
            case 0:   //整数
            {
                switch (c = rand() % rnum)
                {
                case 0:cout << psign << a << "+" << msign << b << "=" << endl;
                    cnum = a + b;
                    cin >> finalnum;
                    if (psign == ' '&&msign == ' ')
                    {
                        cnum = a + b;
                    }
                    if (psign == '-'&&msign == ' ')
                    {
                        cnum = b - a;
                    }
                    if (psign == ' '&&msign == '-')
                    {
                        cnum = a - b;
                    }
                    if (psign == '-'&&msign == '-')
                    {
                        cnum = -a - b;
                    }
                    if (int(finalnum * 100) == int(cnum * 100))
                    {
                        cout << "答案正确!" << endl;
                        count++;
                    }
                    else
                    {
                        cout << "答案错误!" << endl;
                        cout << "正确答案为:" << cnum << endl;
                    }
                    break;
                case 1:cout << psign << a << "-" << msign << b << "=" << endl;
                    cnum = a - b;
                    cin >> finalnum;
                    if (psign == ' '&&msign == ' ')
                    {
                        cnum = a  -  b ;
                    }
                    if (psign == '-'&&msign == ' ')
                    {
                        cnum = -b - a ;
                    }
                    if (psign == ' '&&msign == '-')
                    {
                        cnum = a  + b ;
                    }
                    if (psign == '-'&&msign == '-')
                    {
                        cnum = -a  + b ;
                    }
                    if (int(finalnum*100) ==int (cnum*100))
                    {
                        cout << "答案正确!" << endl;
                        count++;
                    }
                    else
                    {
                        cout << "答案错误!" << endl;
                        cout << "正确答案为:" << cnum << endl;
                    }
                    break;
                case 2:cout << a << "×" << b << "=" << endl;
                    cnum = a * b;
                    cin >> finalnum;
                    if (int(finalnum * 100) == int(cnum * 100))
                    {
                        cout << "答案正确!" << endl;
                        count++;
                    }
                    else
                    {
                        cout << "答案错误!" << endl;
                        cout <<"正确答案为:"<< cnum << endl;
                    }
                    break;
                case 3:
                {
                    if (rem == 2)
                    {
                        while (b == 0)
                        {
                            b = rand() % numf;
                        }
                        o = a%b;
                        while (o != 0)
                        {
                            b = rand() % numf;
                            while (b == 0)
                            {
                                a = rand() % numf;
                                b = rand() % numf;
                            }
                            o = a%b;
                        }
                    }
                    else
                    {
                        while (b == 0)
                        {
                            b = rand() % numf;
                        }
                    }
                    cout << a << "÷" << b << "=" << endl;
                    cnum = a / b;
                    cin >> finalnum;
                    if (int(finalnum * 100) == int(cnum * 100))
                    {
                        cout << "答案正确!" << endl;
                        count++;
                    }
                    else 
                    {
                        cout << "答案错误!" << endl;
                        cout << "正确答案为:" << cnum << endl;
                    }
                }break;
                }
            } break;
            case 1://真分数
            {
                while (a == 0 || b == 0 || a >= b)
                {
                    a = rand() % numf;
                    b = rand() % numf;
    
                }
                while (e == 0 || f == 0 || e >= f)
                {
                    e = rand() % numf;
                    f = rand() % numf;
                }
                switch (g = rand() % rnum)
                {
                case 0: 
                {
                    cout << psign << a << "/" << b << "+" << msign << e << "/" << f << "=" << endl;
                    cin >> finalnum;
                    if (psign == ' '&&msign == ' ')
                    {
                        cnum = a / double (b) + e / double(f);
                    }
                    if (psign == '-'&&msign == ' ')
                    {
                        cnum = e / double(f) - a / double (b);
                    }
                    if (psign == ' '&&msign == '-')
                    {
                        cnum = a / double (b) - e / double(f);
                    }
                    if (psign == '-'&&msign == '-')
                    {
                        cnum = -a / double(b) - e / double(f);
                    }
                    if (int(finalnum * 100) == int(cnum * 100))
                    {
                        cout << "答案正确!" << endl;
                        count++;
                    }
                    else
                    {
                        cout << "答案错误!" << endl;
                        cout << "正确答案为:" << cnum << endl;
                    }
                    break;
                }
                case 1:
                {
                    cout << psign << a << "/" << b << "-" << msign << e << "/" << f << "=" << endl;
                    cin >> finalnum;
                    if (psign == ' '&&msign == ' ')
                    {
                        cnum = a / double(b) - e / double(f);
                    }
                    if (psign == '-'&&msign == ' ')
                    {
                        cnum = -(e / double(f)) - (a / double(b));
                    }
                    if (psign == ' '&&msign == '-')
                    {
                        cnum = (a / double(b)) + (e / double(f));
                    }
                    if (psign == '-'&&msign == '-')
                    {
                        cnum = -(a / double(b)) + (e / double(f));
                    }
                    if (int(finalnum * 100) == int(cnum * 100))
                    {
                        cout << "答案正确!" << endl;
                        count++;
                    }
                    else
                    {
                        cout << "答案错误!" << endl;
                        cout << "正确答案为:" << cnum << endl;
                    }
                    break;
                }cout << "一共答对了:" << count << "" << endl;
                case 2:
                {
                    cout << a << "/" << b << "×" << e << "/" << f << "=" << endl;
                    cnum = (a / double(b)) * (e / double(f));
                    cin >> finalnum;
                    if (int(finalnum * 100) == int(cnum * 100))
                    {
                        cout << "答案正确!" << endl;
                        count++;
                    }
                    else
                    {
                        cout << "答案错误!" << endl;
                        cout << "正确答案为:" << cnum << endl;
                    }
                    break;
                }
                case 3: 
                {
                    cout << a << "/" << b << "÷" << e << "/" << f << "=" << endl;
                    cnum = (a / double(b)) / (e / double(f));
                    cin >> finalnum;
                    if (int(finalnum * 100) == int(cnum * 100))
                    {
                        cout << "答案正确!" << endl;
                        count++;
                    }
                    else
                    {
                        cout << "答案错误!" << endl;
                        cout << "正确答案为:" << cnum << endl;
                    }
                }
                } break;
            } break;
            case 2:
            {
                num = 3 + rand() % 8;     //有括号时,数字和符号数组
                signnum = num - 1;
                for (y = 0; y < num; y++)
                {
                    numarray[y] = rand() % numf;
                }
                for (y = 0; y < signnum; y++)
                {
                    x = rand() % 4;
                    signarray[y] = fh[x];
                }
                signarray[signnum] = '=';
                switch (z = signnum % 2)
                {
                case 0:
                {
                    cout << numarray[0];
                    cout << signarray[0];
                    finalnum =finalnum+numarray[0];
                    for (y = 1; y < signnum; y++)
                    {
                        cout << "(";
                        cout << numarray[y];
                        cout << signarray[y];
                        cout << numarray[y + 1];
                        cout << ")";
                        switch (y)
                        {
                            case'+':finalnum = finalnum + numarray[y] + numarray[y + 1]; break;
                            case'-':finalnum = finalnum + numarray[y] - numarray[y + 1]; break;
                            case'*':finalnum = finalnum + numarray[y] * numarray[y + 1]; break;
                            case'/':finalnum = finalnum + numarray[y] / numarray[y + 1]; break;
                        }
                        cout << signarray[y + 1];
                        y = y + 1;
                    }cout << endl;
                }break;
                case 1:
                {
                    for (y = 0; y < signnum; y++)
                    {
                        cout << "(";
                        cout << numarray[y];
                        cout << signarray[y];
                        cout << numarray[y + 1];
                        cout << ")";
                        switch (y)
                        {
                            case'+':finalnum = finalnum + numarray[y] + numarray[y + 1]; break;
                            case'-':finalnum = finalnum + numarray[y] - numarray[y + 1]; break;
                            case'*':finalnum = finalnum + numarray[y] * numarray[y + 1]; break;
                            case'/':finalnum = finalnum + numarray[y] / numarray[y + 1]; break;
                        }
                        cout << signarray[y + 1];
                        y = y + 1;
                    }
                    cout << endl;
                    cin >> finalnum;
                    if (int(finalnum * 100) == int(cnum * 100))
                    {
                        cout << "答案正确!" << endl;
                        count++;
                    }
                    else
                        cout << "答案错误!" << endl;
                        cout << "正确答案为:" << cnum << endl;
                }break;
                }
            } break;
            }
        }
        cout << "一共答对了:" << count << "" << endl;
    }

    心得体会:

    1.我们结对开发所以选择了较好的那个程序。

    2.因为俩人分工合作,每个人负责不同的功能

    3.我们先用一个数代表运算式的结果,然后再在这个数上根据运算符号添加运算             

     

    合作伙伴:朱建颖  http://www.cnblogs.com/zjy666/

  • 相关阅读:
    第一篇代码 嗨翻C语言 21点扑克
    Windows7 sp1 64位下安装配置eclipse+jdk+CDT+minGW
    MinGW-64 安装
    Windows Live Writer配置步骤
    Css 居中
    c++ 常量成员函数
    c/c++ 引用计数
    C++ 《STL源码剖析》学习-vector
    C/C++ 有符号数和无符号数
    cocos2d 内存管理机制
  • 原文地址:https://www.cnblogs.com/wooder/p/5295742.html
Copyright © 2020-2023  润新知