zoukankan      html  css  js  c++  java
  • 软件工程个人作业02

    设计思想:

    1.用户参数的设定

    2.随机数的生成,包括运算符,运算数,和控制分数的数字

    3.根据用户输入的参数,把四则运算分为两类,一类是不带括号的,一类是带括号的思则混合运算

    4.不带括号的四则运算又可以分为有乘除和没有乘除法的

    5.具体细节的设定,再加减法中结果是不是有负数,乘除法中有没有余数

    6.分数中去掉分母为0的,分母为1的只需输出分子即可

    7.带括号的四则混合运算,随机生成长度,和运算符存储在数组中,随机加上括号

    8.利用for循环,生成四则运算

    9.输出

    源程序代码

    #include<iostream>
    using namespace std;
    #include<time.h>
    
    int gcd(int x,int y)
    {
        int t,m,n,k=1;
        if(x>y)
        {
            t=x;x=y;y=t;
        }
        for(int i=x;i>=2;i--)
        {
            m=x%i;n=y%i;
            if(m==0&&n==0)
            {
                k=i;break;
             }     
         }
         return k;
    }
    void main()
    {
        int number,ccselect,khselect,jjselect,ysselect,max;
        cout<<"请输入生成题的数目";
        cin>>number;
        cout<<"请输入运算范围(1-?)";
        cin>>max;
        cout<<"请选择是有无括号(1/2)";
        cin>>khselect;
        if(khselect==2)
        {
            cout<<"请选择加减是否有负数(1/2)";
            cin>>jjselect;
            cout<<"请选择是否有乘除法(1/2)";/*控制是否有乘除*/
            cin>>ccselect;
            if(ccselect==1)
            {
                cout<<"请选择除法有无余数(1/2)";
                cin>>ysselect;
            }
        }
        cout<<endl;
    
        for(int i=0;i<number;i++)
        {
            int fuhao=rand()%4; 
            int a1=rand()%max;
            int a2=rand()%max+1;
            int b1=rand()%max;
            int b2=rand()%max+1;
            int k=rand()%2;       
            if(k==0)
            {
                if(ccselect == 1)
                {
                    if(fuhao==0) //加法运算
                        cout<<a1<<"+"<<b1<<"="<<endl;
                    if(fuhao==1)//减法运算
                    {
                        if(jjselect==2)
                        {
                            if(a1>b1)
                            {
                                cout<<a1<<"-"<<b1<<"="<<endl;        
                             }
                             else
                            {
                                cout<<b1<<"-"<<a1<<"="<<endl; 
                            }
                        }
                        else if(jjselect==1)
                            cout<<a1<<"-"<<b1<<"="<<endl;
                    }
                    if(fuhao==2)//乘法运算
                    {
                        cout<<a1<<"*"<<b1<<"="<<endl; 
                    }
                    if(fuhao==3)//除法运算
                    {
                        if(ysselect==2)
                        {//没有余数
                            for(int m=0;;)
                            {
                                if(a1!=0)
                                {   
                                    if(a1%b1!=0)
                                    {
                                        a1=rand()%max;
                                        b1=rand()%max;
                                     }
                                     else
                                         break;
                                }
                                else
                                    a1=rand()%max;
                            }
                        }
                        cout<<a1<<"/"<<b1<<"="<<endl;
                    }
                }
                else if(ccselect==2)
                {//没有乘除
                    int fuhao=rand()%2;
                    if(fuhao==0) //加法运算
                    {
                        cout<<a1<<"+"<<b1<<"="<<endl;
                    }
                    if(fuhao==1)//减法运算
                    {
                        if(jjselect==2)
                        {
                            if(a1>b1)
                            {
                                cout<<a1<<"-"<<b1<<"="<<endl;        
                             }
                            else
                            {
                                cout<<b1<<"-"<<a1<<"="<<endl;  
                            }
                        }
                        else if(jjselect==1)
                            cout<<a1<<"-"<<b1<<"="<<endl;
                    }
                }
            }
            else if(k==1)
            {
                int s2=gcd(a1,a2);
                a1=a1/s2;
                a2=a2/s2;
                int s3=gcd(b1,b2);
                b1=b1/s3;
                b2=b2/s3;
                if(ccselect == 1)
                {
                    if(fuhao == 0)
                    {/*分母不为1,分情况输出*/
                        if(a2!=1&&b2!=1)
                            cout << a1 << "/" << a2 << " + " << b1 << "/" << b2 << " = " << endl; 
                        else if(a2==1)
                        {
                            cout << a1 <<" + " << b1 << "/" << b2 << " = " << endl; 
                        }
                        else if(b2==1)
                        {
                            cout << a1 << "/" << a2 << " + " << b1 << " = " << endl;
                        }
                    }
                    if(fuhao == 1)
                    {
                        if(jjselect==2)
                        {
                            if(a1/a2>=b1/b2)
                            {
                                if(a2!=1&&b2!=1)
                                    cout << a1 << "/" << a2 << " - " << b1 << "/" << b2 << " = " << endl;
                                else if(a2==1)
                                {
                                    cout << a1 <<" - " << b1 << "/" << b2 << " = " << endl;
                                }
                                else if(b2==1)
                                {
                                    cout << a1 << "/" << a2 << " - " << b1 << " = " << endl;
                                }
                            }
                            else
                            {
                                if(a2!=1&&b2!=1)
                                    cout << b1 << "/" << b2 << " - " << a1 << "/" << a2 << " = " << endl;
                                else if(a2==1)
                                {
                                    cout << b1 << "/" << b2 << " - " <<a1 << " = " << endl;
                                 }
                                else if(b2==1)
                                {
                                    cout << b1 <<" - " << a1 << "/" << a2 << " = " << endl;
                                }
                            }
                        }
                        else if(jjselect==1)
                        {
                            if(a2!=1&&b2!=1)
                                cout << a1 << "/" << a2 << " + " << b1 << "/" << b2 << " = " << endl; 
                            else if(a2==1)
                            {    
                                cout << a1 <<" + " << b1 << "/" << b2 << " = " << endl; 
                            }
                            else if(b2==1)
                            {
                                cout << a1 << "/" << a2 << " + " << b1 << " = " << endl;
                            }
                        }
                    }
                    if(fuhao==2)
                    {
                        if(a2!=1&&b2!=1)
                            cout << a1 << "/" << a2 << " * " << b1 << "/" << b2 << " = " << endl;
                        else if(a2==1)
                        {
                            cout << a1 <<" * " << b1 << "/" << b2 << " = " << endl;
                        }
                        else if(b2==1)
                        {
                            cout << a1 << "/" << a2 << " * " << b1 << " = " << endl;
                        }
                    }
                    if(fuhao==3)
                    {
                        if(a2!=1&&b2!=1)
                            cout << a1 << "/" << a2 << " - " << b1 << "/" << b2 << " = " << endl;
                        else if(a2==1&&a1!=0)
                        {
                            cout << a1 <<" * " << b1 << "/" << b2 << " = " << endl;
                        }
                        else if(b2==1&&b1!=0)
                        {
                            cout << a1 << "/" << a2 << " * " << b1 << " = " << endl;
                        }
                    }
                }
                else if(ccselect==2)
                {
                    int fuhao=rand()%2;
                    if(fuhao==0)
                    {
                        if(fuhao==0)
                        { /*分母不为1,分情况输出*/
                            if(a2!=1&&b2!=1)
                                cout << a1 << "/" << a2 << " + " << b1 << "/" << b2 << " = " << endl;
                            else if(a2==1)
                            {
                                cout << a1 <<" + " << b1 << "/" << b2 << " = " << endl;
                            }
                            else if(b2==1)
                            {
                                cout << a1 << "/" << a2 << " + " << b1 << " = " << endl;}
                            }
                    }
                    if(fuhao == 1)
                    {
                        if(jjselect==2)
                        {
                            if(a1/a2>=b1/b2)
                            {
                                if(a2!=1&&b2!=1)
                                    cout << a1 << "/" << a2 << " - " << b1 << "/" << b2 << " = " << endl;
                                else if(a2==1)
                                {
                                    cout << a1 <<" - " << b1 << "/" << b2 << " = " << endl;
                                }
                                else if(b2==1)
                                {
                                    cout << a1 << "/" << a2 << " - " << b1 << " = " << endl;
                                }
                            }
                            else
                            {
                                if(a2!=1&&b2!=1)
                                    cout << b1 << "/" << b2 << " - " << a1 << "/" << a2 << " = " << endl;
                                else if(a2==1)
                                {
                                    cout << b1 << "/" << b2 << " - " <<a1 << " = " << endl;
                                }
                                else if(b2==1)
                                {
                                    cout << b1 <<" - " << a1 << "/" << a2 << " = " << endl;
                                }
                            }
                        }
                        else if(jjselect==1)
                        {
                            if(a2!=1&&b2!=1)
                                cout << a1 << "/" << a2 << " + " << b1 << "/" << b2 << " = " << endl; 
                            else if(a2==1)
                            {
                                cout << a1 <<" + " << b1 << "/" << b2 << " = " << endl;
                            }
                            else if(b2==1)
                            {
                                cout << a1 << "/" << a2 << " + " << b1 << " = " << endl;
                            }
                        }
                    }
                }
            }
        }
    }

      运行结果截图:

     

    反思:

    最初的用结构体做的,但是这次增加了控制参数觉得分类不好分,所以直接定义变量,分类时候没考虑全,最后做出来的程序有很多bug,还有的功能没有完成带括号的混合运算还要接着做。

                                                  周活动总结表

                                            姓名:hy              日期2016.3.19

    日期/任务 听课 编写程序 阅读课本 日总计
    星期日        
    星期一 120 80   200
    星期二   40   40
    星期三        
    星期四     60 60
    星期五        
    星期六   160 60 220
    周总计 120 280 120 520

                                                  时间记录日志

                                     姓名:hy                       日期:2016.3.19

    日期 开始时间 结束时间 中断时间 静时间 活动     备注      C          U     
    3.14 8:00 9:50 8:50-9:00 100 上课      
    3:00 5:00   120

    设计思想

    +编程

      作业    
    3.15 3:00 5:00   120 编程   作业    
    3.17 3:30 4:30   60 看书   作业    
    3.19 9:00 18:00 11:30-1:00 450

    编程+

    发表博客

      作业    

                                                 缺陷记录日志

                                              学生:hy   日期:2016.3.19

                                              教员:王建民   程序号   03

    日期 编号 类型 引入阶段 排除阶段 修复时间 修复缺陷
    3.14 1 20 编程 编程 20  
    描述:创建结构体错误
    3.15 2 20 编程 编程 10  
    描述:分数没有考虑分母为1的情况
    3.19 3 20 编程 编程 10  
    描述:分数化简错误
    4 20 编程 编译 30  
    描述:题目出现两两成对重复出现出现
    5 20 编程 编译 20  
    描述:题目只有加减法时出现数量少一半
    6 20 编程 编译 10  
    描述:改完后程序卡死,循环错误
  • 相关阅读:
    FFmpeg 协议初步学习
    HTML DOM(一):认识DOM
    ant 安装
    ubunut 查看port被哪个程序占用
    cacti气象图调整(批量位置调整、更改生成图大小等)
    内网port映射具体解释(花生壳)
    HDU 2647 Reward(图论-拓扑排序)
    白话经典算法系列之七 堆与堆排序
    Codeforces Round #191 (Div. 2)---A. Flipping Game
    Serverlet具体解释
  • 原文地址:https://www.cnblogs.com/hyluckydog/p/5295727.html
Copyright © 2011-2022 走看看