• 软件工程个人作业03


    设计思想:

    利用结果判断,若错误则输出“错误!”

    正确则输出“正确!”同时进行计数!

    程序源代码:

    #include<iostream>
    #include<ctime>
    #include<cstdlib>
    using namespace std;
    void Out(int x,int x0,int y,int y0,int z,int z0,int &i,int j,int n,int m0,int &j0){
        switch(z0){//判断整数或分数
        case 0:{
            int answer,answer0;
            switch(z){//运算法则判断
            case 0:
                {
                    answer=x+y;
                    if(j==2)
                    {
                        if(answer>=0)
                        {
                            cout<<n<<"   ("<<x<<")"<<" + "<<"("<<y<<")"<<" =";
                            cin>>answer0;
                            if(answer==answer0)
                            {
                                cout<<"     正确!"<<endl;
                                j0++;
                            }
                            else
                            {
                                cout<<"     错误!"<<endl;
                            }
                        }
                        else
                        {
                            i--;
                        }
                    }
                    else
                    {
                        cout<<n<<"   ("<<x<<")"<<" + "<<"("<<y<<")"<<" =";
                        cin>>answer0;
                        if(answer==answer0)
                        {
                            cout<<"     正确!"<<endl;
                            j0++;
                        }
                        else
                        {
                               cout<<"     错误!"<<endl;
                        }
                    }
                    break;
                }
            case 1:
                {
                    answer=x-y;
                    if(j==2)
                    {
                        if(answer>=0)
                        {
                             cout<<n<<"    ("<<x<<")"<<" - "<<"("<<y<<")"<<" =";
                            cin>>answer0;
                            if(answer==answer0)
                            {
                                cout<<"     正确!"<<endl;
                                j0++;
                            }
                            else
                            {
                                cout<<"     错误!"<<endl;
                            }
                        }
                        else
                        {
                            i--;
                        }
                    }
                    else
                    {
                        cout<<n<<"    ("<<x<<")"<<" - "<<"("<<y<<")"<<" =";
                        cin>>answer0;
                        if(answer==answer0)
                        {
                            cout<<"     正确!"<<endl;
                            j0++;
                        }
                        else
                        {
                            cout<<"     错误!"<<endl;
                        }
                    }
                    break;
                }
            case 2:
                {
                    answer=x*y;
                       cout<<n<<"    ("<<x<<")"<<" * "<<"("<<y<<")"<<" =";
                    cin>>answer0;
                    if(answer==answer0)
                    {
                        cout<<"     正确!"<<endl;
                        j0++;
                    }
                    else
                    {
                        cout<<"     错误!"<<endl;
                    }
                    break;
                }
            case 3:
                if(y!=0){//防止出现除数为零的情况
                    if(m0==2){
                        if(x%y==0)//余数判断(该操作可能会降低除法概率,,)
                        {
                            answer=x/y;
                            cout<<n<<"    ("<<x<<")"<<" / "<<"("<<y<<")"<<" =";
                            cin>>answer0;
                            if(answer==answer0)
                            {
                                cout<<"     正确!"<<endl;
                                j0++;
                            }
                            else
                            {
                                cout<<"     错误!"<<endl;
                            }
                        }
                        else
                            i--;
                    }
                    if(m0==1)
                    {
                        cout<<n<<"    ("<<x<<")"<<" / "<<"("<<y<<")"<<" =";
                    }
                }
                else
                    i--;
                break;
            }
            break;
               }
        case 1:{
            char answer0[8];
            char answer[8];
            int mu,zi,q;
            if(x0!=0&&y0!=0){
                switch(z){//运算法则判断
                case 0:
                    {
                        mu=x0*y0;
                        zi=x*y0+y*x0;
                        if(mu>zi)
                        {
                            for(q=zi;q>1;q--)
                            {
                                if(mu%q==0&&zi%q==0)
                                {
                                    mu=mu/q;
                                    zi=zi/q;
                                }
                            }
                        }
                        else
                        {
                            for(q=mu;q>1;q--)
                            {
                                if(mu%q==0&&zi%q==0)
                                {
                                    mu=mu/q;
                                    zi=zi/q;
                                }
                            }
                        }
                        sprintf(answer,"%d/%d",zi,mu);
                        cout<<n<<"    ("<<x<<"/"<<x0<<")"<<" + "<<"("<<y<<"/"<<y0<<")"<<" =";
                        cin>>answer0;
                        if(strcmp(answer,answer0)==0)
                        {
                            cout<<"     正确!"<<endl;
                            j0++;
                        }
                        else
                        {
                            cout<<"     错误!"<<endl;
                        }
                        break;
                    }
                case 1:
                    {
                        mu=x0*y0;
                        zi=x*y0-y*x0;
                        for(q=zi;q>1;q--)
                            {
                                if(mu%q==0&&zi%q==0)
                                {
                                    mu=mu/q;
                                    zi=zi/q;
                                }
                            }
                        sprintf(answer,"%d/%d",zi,mu);
                        cout<<n<<"    ("<<x<<"/"<<x0<<")"<<" - "<<"("<<y<<"/"<<y0<<")"<<" =";
                        cin>>answer0;
                        if(strcmp(answer,answer0)==0)
                        {
                            cout<<"     正确!"<<endl;
                            j0++;
                        }
                        else
                        {
                            cout<<"     错误!"<<endl;
                        }
                        break;
                    }
                case 2:
                    {
                        mu=x0*y0;
                        zi=x*y;
                        for(q=zi;q>1;q--)
                        {
                            if(mu%q==0&&zi%q==0)
                            {
                                mu=mu/q;
                                zi=zi/q;
                            }
                        }
                        sprintf(answer,"%d/%d",zi,mu);
                         cout<<n<<"    ("<<x<<"/"<<x0<<")"<<" * "<<"("<<y<<"/"<<y0<<")"<<" =";
                        cin>>answer0;
                        if(strcmp(answer,answer0)==0)
                        {
                            cout<<"     正确!"<<endl;
                            j0++;
                        }
                        else
                        {
                            cout<<"     错误!"<<endl;
                        }
                        break;
                    }
                case 3:
                    {
                        if(y!=0)//防止出现除数为零的情况
                        {
                            mu=x0*y;
                            zi=x*y0;
                            if(mu>zi)
                            {
                                for(q=zi;q>1;q--)
                                {
                                    if(mu%q==0&&zi%q==0)
                                    {
                                        mu=mu/q;
                                        zi=zi/q;
                                    }
                                }
                            }
                            else
                            {
                                for(q=mu;q>1;q--)
                                {
                                    if(mu%q==0&&zi%q==0)
                                    {
                                        mu=mu/q;
                                        zi=zi/q;
                                    }
                                }
                            }
                            sprintf(answer,"%d/%d",zi,mu);
                            cout<<n<<"    ("<<x<<"/"<<x0<<")"<<" / "<<"("<<y<<"/"<<y0<<")"<<" =";
                            cin>>answer0;
                            if(strcmp(answer,answer0)==0)
                            {
                                cout<<"     正确!"<<endl;
                                (j0)++;
                            }
                            else
                            {
                                cout<<"     错误!"<<endl;
                            }
                        }
                        else
                            i--;
                        break;
                    }
                }
            }
            else
                i--;
            break;
               }
        }
    }
    void Judje(int &x,int &x0)//保证真分数以及最简分数
    {
        int m,i1;
        if(x>x0){//保证x<x0即保证为真分数
            m=x;
            x=x0;
            x0=m;
        }
        for(i1=x0;i1>1;i1--){//保证为最简分数
            if(x%i1==0&&x0%i1==0){
                x=x/i1;
                x0=x0/i1;
            }
        }    
    }
    void main(){
        int x,x0,y,y0,z,z0,j,n,n0,m,m0=0;//定义变量
        int num,max,min;
        int j0=0;
        cout<<"请输入随机数范围(先最小值,后最大值)"<<endl;
        cin>>min;
        cin>>max;
        cout<<"请输入加减是否有负数:1、有;2、没有"<<endl;
        cin>>j;
        cout<<"请输入是否要有乘除法:1、有;2、没有"<<endl;
        cin>>m;
        if(m==1){
            cout<<"请输入除法是否有余数(分数不做区别):1、有;2、没有"<<endl;
            cin>>m0;
        }
        cout<<"请输入需要打印的题目数量(大于等于1):"<<endl;
        cin>>num;
        int *a=new int[num*5];//定义数组存储运算
        srand(time(0));//定义时间种子
        int i=0;
        if(min<0&&j==2)
            min=0;
        x = rand()%(max-min+1)+min;//产生随机数
        x0 = rand()%(max-min+1)+min;
        y = rand()%(max-min+1)+min;
        y0 = rand()%(max-min+1)+min;
        if(m==1)
            z = rand()%(3-0+1)+0;
        if(m==2)
            z = rand()%(1-0+1)+0;
        z0 = rand()%(1-0+1)+0;//用于判断整数运算与分数运算}
        Judje(x,x0);
        Judje(y,y0);
        cout<<"序号"<<endl;
        n=1;
        Out(x,x0,y,y0,z,z0,i,j,n,m0,j0);
        a[0]=x;
        a[1]=x0;
        a[2]=y;
        a[3]=y0;
        a[4]=z;
        for(i=1;i<num;i++)//利用FOR循环进行剩余输出
        {
            n=i+1;
            n0=i*5;
            x = rand()%(max-min+1)+min;//产生随机数
            x0 = rand()%(max-min+1)+min;
            y = rand()%(max-min+1)+min;
            y0 = rand()%(max-min+1)+min;
            if(m==1)
                z = rand()%(3-0+1)+0;//运算符
            if(m==2)
                z = rand()%(1-0+1)+0;
            z0 = rand()%(1-0+1)+0;//用于判断整数运算与分数运算}
            Judje(x,x0);
            Judje(y,y0);
            a[n0]=x;
            a[n0+1]=x0;
            a[n0+2]=y;
            a[n0+3]=y0;
            a[n0+4]=z;
            if(x!=x0&&y!=y0&&x0!=1&&y0!=1){//防止出现在x=x0时输出依旧为x/x0格式以及分母为一的情况
                if(a[n0]!=a[n0-5]||a[n0]!=a[n0-4]||a[n0]!=a[i-3]||a[n0]!=a[n0-2]||a[n0]!=a[n0-1])//题目避免重复
                {
                    Out(x,x0,y,y0,z,z0,i,j,n,m0,j0);
                }
            }
            else
                i--;
        }
        cout<<"共答对"<<j0<<"道题目!"<<endl;
        delete []a;
    }

    运行结果截图:

    周活动总结表

    尚晓朋    2016/3/26

      听课 代码 读书 总计
    星期一 100min 42行 构建之法第3章 三小时20分钟
    星期二        
    星期三   45行   两个小时
    星期四        
    星期五   30行   两个小时
    星期六       五个小时30分钟
    总计 100min     12小时50分钟

    时间记录日志

    日期 开始时间 结束时间 活动 备注
    3.21

    8:00

    19:25

    9:50

    21:05

    听课

    写代码

    上课

    写代码,理思路

    3.23 19:15 21:15 写代码删代码 两个小时
    3.25 19:35 21:30 写代码,改错 两个小时
    3.26

    14:20

    19:00

    17:25

    21:30

    写程序 五个小时30分钟

    缺陷记录日志

    日期 编号 类型 引入阶段 排除阶段 修复时间 修复缺陷
    3.21 1   编码 编译 30分钟  
    修改了课上没做好的部分并完成了课堂测试
    3.26 2   编码 编译 一直被这个问题卡住  
    对分数运算结果判定时出现错误!

    附录:

    关于多位数的运算,实现了多位数计算以及对结果的判断,但是无法移植到原程序中。

    源代码:

    #include<iostream>
    #include<ctime>
    #include<cstdlib>
    using namespace std;
    void Duo(int min,int max,int &answer)
    {
        int a,b;
        a=rand()%(max-min+1)+min;
        b=rand()%(3-0+1)+0;
        switch(b)
        {
        case 0:
            {
                answer=answer+a;
                cout<<"+"<<a<<")";
                break;
            }
        case 1:
            {
                answer=answer-a;
                cout<<"-"<<a<<")";
                break;
            }
        case 2:
            {
                answer=answer*a;
                cout<<"*"<<a<<")";
                break;
            }
        case 3:
            {
                answer=answer/a;
                cout<<"/"<<a<<")";
                break;
            }
        }
    
    }
    void main()
    {
        int answer;
        int min,max;
        srand(time(0));//定义时间种子
        cout<<"输入范围下界与范围上界:"<<endl;
        cin>>min>>max;
        int x,w;
        x=rand()%(max-min+1)+min;
        answer=x;
        w=rand()%(10-1+1)+1;
        for(int i=1;i<=w;i++)
        {
            cout<<"(";
        }
        cout<<x;
        for(int i=1;i<=w;i++)
        {
            Duo(min,max,answer);
        }
        cout<<"="<<answer<<endl;
    }

    (2016.3.27)

  • 相关阅读:
    超级楼梯
    超级楼梯
    母牛的故事
    母牛的故事
    蟠桃记
    蟠桃记
    Children’s Queue
    Children’s Queue
    http://202.194.116.8/webapps/portal/frameset.jsp?tab_id=_2_1&url=%2fwebapps%2fblackboard%2fexecute%2
    Matlab位运算笔记
  • 原文地址:https://www.cnblogs.com/dawn-sky/p/5323172.html
Copyright © 2020-2023  润新知