四则运算二

一、设计思想

在上个程序中添加控制变量,实现题目个数、数值范围、有无乘除法的控制。

二、源程序代码

#include<iostream>
#include<cmath>
using namespace std;
void main()
{
    int i, j, a, m, n, l, k, p, q, c,d,num/*,real*/;
    cout << "请输入题目的个数:" << endl;
    cin >> p;
    cout << "请选择题目类型: 1.有乘除法;2.无乘除法" << endl;
    cin >> q;
    cout << "请输入数值范围:" << endl;
    cin >> num;
    //cout << "请说明是否有括号:1.有;2.无" << endl;
    //cin >> real;
    cout << endl;
    cout << endl;
    //if (real == 1)
    //{
        if (q == 1)
        {
            for (i = 0; i < p; i++)
            {
                c = rand() % 9 + 1;
                for (d = 0; d < c; d++)
                {
                        l = rand() % (num);
                        k = l % 2;
                        if (k == 0)
                        {
                            m = rand() % (num);
                            n = rand() % (num);
                            if (m < n)
                                cout << m << "/" << n;
                            if (m>n)
                                cout << n << "/" << m;
                            if (m == n)
                            {
                                if (m == 0)
                                    cout << m << "/" << ((rand() % num) + 20) / 2;
                                else
                                    cout << m / 2 << "/" << n;
                            }
                        }
                        else
                        {
                            a = rand() % (num)+1;
                            cout << a;
                        }

                        j = rand() % 4;
                        if (j == 0) cout << "  +  ";
                        else if (j == 1) cout << "  -  ";
                        else if (j == 2) cout << "  *  ";
                        else cout << "  /  ";
                    }
                

                l = rand() % (num);
                k = l % 2;
                if (k == 0)
                {
                    m = rand() % (num);
                    n = rand() % (num);
                    if (m < n)
                        cout <<m<<"/"<<n;
                    if (m>n)
                        cout <<n<<"/"<<m;
                    if (m == n)
                    {
                        if (m == 0)
                            cout <<m<<"/"<<((rand() % num) + 20) / 2;
                        else
                            cout <<m / 2<<"/"<< n;
                    }
                }
                else
                {
                    a = rand() % (num)+1;
                    cout << a;
                }
                cout << "  =" << endl;

            }
        }
        else
        {
            for (i = 0; i < p; i++)
            {
                c = rand() % 9 + 1;
                for (d = 0; d < c; d++)
                {
                    l = rand() % (num);
                    k = l % 2;
                    if (k == 0)
                    {
                        m = rand() % (num);
                        n = rand() % (num);
                        if (m < n)
                            cout << m << "/" << n;
                        if (m>n)
                            cout << n << "/" << m;
                        if (m == n)
                        {
                            if (m == 0)
                                cout << m << "/" << ((rand() % num) + 20) / 2;
                            else
                                cout << m / 2 << "/" << n;
                        }
                    }
                    else
                    {
                        a = rand() % (num)+1;
                        cout << a;
                    }

                    j = rand() % 2;
                    if (j == 0) cout << "  +  ";
                    else if (j == 1) cout << "  -  ";
                }
                l = rand() % (num);
                k = l % 2;
                if (k == 0)
                {
                    m = rand() % (num);
                    n = rand() % (100);
                    if (m < n)
                        cout << m << "/" << n;
                    if (m>n)
                        cout << n << "/" << m;
                    if (m == n)
                    {
                        if (m == 0)
                            cout << m << "/" << ((rand() % num) + 20) / 2;
                        else
                            cout << m / 2 << "/" << n;
                    }
                }
                else
                {
                    a = rand() % (num)+1;
                    cout << a;
                }
                cout << "  =" << endl;

            }
        }
    //}
}

三、运行程序截图

四、缺陷

口号不知道怎么添加,加减法有无负数,乘除法有无余数不会弄

posted on 2016-03-12 17:48  897897  阅读(265)  评论(1编辑  收藏  举报

导航