四则运算二

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了四则运算二相关的知识,希望对你有一定的参考价值。

一、设计思想

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

二、源程序代码

#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;

            }
        }
    //}
}

三、运行程序截图

技术分享技术分享

四、缺陷

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

以上是关于四则运算二的主要内容,如果未能解决你的问题,请参考以下文章

为什么二代测序的原始数据中会出现Read重复现象?

二代身份证号码编码规则

染色体基因芯片分析和第二代测序应用的区别

四则运算

深度解析Google第二代深度学习引擎TensorFlow开源(CMU邢波独家点评白皮书全文视频翻译)

C零基础视频-26-身份证号校验程序