zoukankan      html  css  js  c++  java
  • 四则运算第二版

    #include<iostream>
    #include<time.h>
    #include<string>
    #include<windows.h>
    using namespace std;
    
    //生成随机数
    int random_number(int min,int max)
    {
        srand(time(NULL));
        int result = rand()%(max-min+1)+min;//生成指定范围内的随机数
        return result;
    }
    
    void yunsuan(int Testnum, int ifcc, int iffs, int ifys)
    {
        int ch1, ch2, ch3, ch4;
        char sign;
        int m, min, max;
        int i, j, c;
        for (i = 0; i < Testnum; i++)
        {
            int number = random_number(2, 10);// ;//number为参与运算的参数个数
            for (int j = 1; j <= number; j++)
            {
                int num = rand() % 100 + 1;
                Sleep(1000);
                //是否有乘除
                if (ifcc == 1)
                {
                    ch1 = random_number(1, 4);//随机生成运算符号
                    switch (ch1)
                    {
                    case 1:sign = '+'; break;
                    case 2:sign = '-'; break;
                    case 3:sign = '*'; break;
                    case 4:sign = '/'; break;
                    default:cout << "有错误!" << endl; break;
                    }
                    if (j == number)
                        cout << num << endl;
                    else
                        cout << num << sign ;
                }
                else
                {
                    ch1 = random_number(1, 2);
                    switch (ch1)
                    {
                    case 1:sign = '+'; break;
                    case 2:sign = '-'; break;
                    default:cout << "有错误" << endl; break;
                    }
                    if (j == number)
                        cout << num << endl;
                    else
                        cout << num << sign;
                }
            }
            cout << endl;
    
        }
    }
    int main()
    {
        int Testnum, ifcc, iffs, ifys;
        int m, min, max;
        cout << "*********四则运算********" << endl;
        cout << "请输入题目的数量: " << endl;
        cin >> Testnum;
        cout << "请输入数字产生的范围(min-max)" << endl;
        cin >> min >> max;
        m = max - min + 1;
        //1:代表有  0:代表无
        cout << "加减是否允许有负数:(1/0)" << endl;
        cin >> iffs;
        cout << "是否有乘除:(1/0)" << endl;
        cin >> ifcc;
        cout << "除法是否有余数:(1/0)" << endl;
        cin >> ifys;
        yunsuan(Testnum, ifcc, iffs, ifys);
        system("pause");
    }

    题目要求:在第一次原有的功能的基础上增加以下功能:

    1.避免题目的重复

    2.可定制(可定制题目数量打印方式)

    3.控制参数的生成:
      a.是否允许乘除发的生成

        b.是否允许有括号的生成(最多支持十个数参与运算)

        c.可以设定数值范围的范围

      d.是否负数参与运算

        e.除法是否有余数

    设计思路:

          这次实验相比第一次 难了许多 老师再提示了用栈实现 自己在看了数据结构书之后 还是不能能完成 也想过编译原理中的算符优先等等 尝试了之后还是失败了 因为基础薄弱的原因 这次实验完成的很困难

          遇到一个问题很是没有想到:在添加了时间种子依然得到全是同样的随机值 浪费了我很多时间 一度怀疑我的循环是不是有问题 为什么rand()只执行了一次 后来单步执行发现是因为执行速度过快 最后添加了sleep(1000)才得到了结果

          整体就是 主函数做简单的输出 随机数单独写出一个函数 运算过程单独一个函数 因为以前的练习确实少 数据结构也没学好 确实写得很糟心

             

  • 相关阅读:
    Power of Cryptography
    Radar Installation
    Emag eht htiw Em Pleh
    Help Me with the Game
    89. Gray Code
    87. Scramble String
    86. Partition List
    85. Maximal Rectangle
    84. Largest Rectangle in Histogram
    82. Remove Duplicates from Sorted List II
  • 原文地址:https://www.cnblogs.com/lyhao/p/5269441.html
Copyright © 2011-2022 走看看