课堂测试小程序

题目:随机数的四则运算

思路:取两个随机整数,将运算符号放在一个数组里,再取一个随机数,整除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;
            }
        }
    
    }
}

测试结果:

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

posted @ 2016-03-06 19:16  Lv~snow  阅读(765)  评论(1编辑  收藏  举报