zoukankan      html  css  js  c++  java
  • 课堂测试小程序

    题目:随机数的四则运算

    思路:取两个随机整数,将运算符号放在一个数组里,再取一个随机数,整除4,通过余数来选取预算符号。真分数的四则运算,要取四个随机整数,并进行大小判断,真分数的分母要大于其分子。

    代码:

    #include<iostream>
    #include<time.h>
    using namespace std;
    void main()
    {
        srand(time(NULL));
        for (int i = 1; i <= 30; i++)
        {
            int a = rand()%25+1;
            int b = rand()%25+1;
            int c = rand() % 25 + 1;
            int d = rand() % 25 + 1;
            int e = a % 2;
            int f = b % 4;
            int x[4] = { '+', '-', '*', '/' };
            int y = 0;
            if (e == 1 && b < a&&d < c)
            {
                if (f == 1)

                    cout << b << '/' << a << ' ' << '+' << ' ' << d << '/' << c << "=" << endl;

                else if (f == 2)

                    cout << b << '/' << a << ' ' << '-' << ' ' << d << '/' << c << "=" << endl;

                else if (f == 3)

                    cout << b << '/' << a << ' ' << '*' << ' ' << d << '/' << c << "="  << endl;

                else

                    cout << b << '/' << a << ' ' << '/' << ' ' << d << '/' << c << "=" << endl;
            }
        
            else
            {
                if (f == 1)
                {
                    y = a + b;
                    cout << a << '+' << b << "=" << endl;
                }
                else if (f == 2)
                {
                    y = a - b;
                    cout << a << '-' << b << "=" << endl;
                }
                else if (f == 3)
                {
                    y = a * b;
                    cout << a << '*' << b << "=" <<  endl;
                }
                else
                {
                    y = b / a;
                    cout << b << '/' << a << "=" << endl;
                }
            }
        
        }
    }

    测试结果:

    感受:通过这次小测试,我明白了“分解”思想的重要性。

  • 相关阅读:
    时间,关机重启及网络常识
    bash xshell 特性
    Http介绍
    rsync 守护进程模式小记
    定时任务+邮件发送 小记
    4.iptables的匹配条件(一)
    3.iptables规则管理
    2.iptables规则查询
    1.iptables概念
    4.LVS实验构建
  • 原文地址:https://www.cnblogs.com/lvstudy/p/5248248.html
Copyright © 2011-2022 走看看