• 软件工程个人作业02


    题目要求:

    可怜的二柱子同学,老师又对他的自动出题系统提出了新的要求: 1、题目避免重复; 2、可定制(数量/打印方式); 3、可以控制下列参数: 是否有乘除法; 是否有括号(最多可以支持十个数参与计算); 数值范围; 加减有无负数; 除法有无余数!

    1.设计思路

    (1)题目避免重复:把生成的随机数和运算符号用数组保存,调用for循环判断因数和符号是否与之前生成的算式相同,若相同则重新产生。

    (2)文件输出。

    (3.1)生成运算符号的随机数改为只有乘除。

    (3.2)暂没实现。

    (3.3)用户输入运算结果范围,用数组保存结果,条件语句判断,若范围越界重新产生,否则输出。

    (3.4)用户选择是否包含负数运算,用数组保存结果,比较结果与零的大小。按用户条件产生运算式。

    (3.5)用户选择除法运算是否有余数,用数组保存余数,判断若余数不为零则用被除数减去余数得到新的算式。

    2.源程序代码

    //Jiang LingJun 2016,03,07

    #include <iostream>

    #include <cstdlib>

    #include <ctime>

    #include <fstream>

    #define N 100000

    using namespace std;

    void Display1();

    void Display2();

    void Display3();

    void Display4();

    void Abbreviation(int &x,int &y)//分数化简

    {

             for(int i=x;i>0;i--)

                       if(x%i==0&&y%i==0)

                       {

                                x=x/i;

                                y=y/i;

                                break;

                       }

    }

    //生成自定义个四则运算题

    void DisplayAll()

    {

                       srand(unsigned(time(0)));//用系统时间生成随机数种子

                       int I;

                       cout<<"请输入出题数量: ";

                       cin>>I;

                       ofstream outfile("题设不做要求.txt",ios::out);

                       //定义文件流对象,打开磁盘文件"题设不做要求.txt"(未指明路径保存在创建的项目的文件夹中)

                                if(!outfile)

                                {

                                         cerr<<"open error!"<<endl;

                                         exit(1);

                                }

                       for(int i=0;i<I;i++)

                       {

                                int c;

                                char s;//定义字符型变量表示随机产生的运算符

                                c=rand()%4;//利用系统时间产生随机数

                                int a,b,d,f;

                                a=rand()%99+1;

                                b=rand()%99+1;

                                d=rand()%99+1;

                                f=rand()%99+1;//随机数生成数学题中的因子

                                switch (c)

                                {

                                                   case 0: s='+';break;

                                                   case 1: s='-';break;

                                                   case 2: s='*';break;

                                                   case 3: s='/';break;

                                }

                                //题目无重复

                                int j[N],k[N];

                                char l[N];

                                j[i]=a;

                                k[i]=b;

                                l[i]=s;

                                for(int q=i-1;q>=0;q--)

                                {

                                         if(j[q]==j[i]&&k[q]==k[i]&&l[q]==l[i])

                                         {

                                                   a=rand()%99+1;

                                                   b=rand()%99+1;

                                                   c=rand()%2;

                                         }

                                }//题目无重复

                                         int e;

                                         e=rand()%2;//随机生成真分数运算或者整数运算题的变量

                                         switch(e)

                                                   {

                                                            case 0://生成整数运算题的情况

                                                                               outfile<<a<<s<<b<<"="<<endl;

                                                                               cout<<a<<s<<b<<"="<<endl;

                                                                               break;

                                                            case 1://生成真分数运算题的情况

                                                                               if(a<b&&d<f&&c>=2)//真分数相乘除的情况

                                                                                        {

                                                                                                 Abbreviation(a,b);

                                                                                                 Abbreviation(d,f);

                                                                                                 outfile<<a<<"/"<<b<<s<<"("<<d<<"/"<<f<<")"<<"="<<endl;

                                                                                                  cout<<a<<"/"<<b<<s<<"("<<d<<"/"<<f<<")"<<"="<<endl;

                                                                                        }

                                                                               else if(a<b&&d<f&&c<2)//真分数相加减的情况

                                                                                        {

                                                                                          Abbreviation(a,b);

                                                                                          Abbreviation(d,f);

                                                                                          outfile<<a<<"/"<<b<<s<<d<<"/"<<f<<"="<<endl;

                                                                                          cout<<a<<"/"<<b<<s<<d<<"/"<<f<<"="<<endl;

                                                                                        }

                                                                               else

                                                                                        i--;//重新生成题目,保证30道题的数量

                                                                                                 break;

                                                                     }

             }

    }

    void main()

    {

             int A=1;

             while (A)

             {

             int  C;

             cout <<"请选择出题要求: ";

             cout <<"1 只有乘除 ";

             cout <<"2 确定数值范围 ";

             cout <<"3 确定结果有无负数 ";

             cout <<"4 确定结果有无余数(其中分数运算不计) ";

             cout <<"输入其他数退出 ";

             cin >> C;

             if(C==1)

                       Display1();

             else if(C==2)

                       Display2();

             else if(C==3)

                       Display3();

             else if(C==4)

                       Display4();

             else

                       A=0;

             }

    }

    //可控参数:乘除

    void Display1()

    {

                srand(unsigned(time(0)));//用系统时间生成随机数种子

                       int I;

                       cout<<"请输入出题数量: ";

                       cin>>I;

                       ofstream outfile("只有乘除法.txt",ios::out);

                       //定义文件流对象,打开磁盘文件"只有乘除法.txt"(未指明路径保存在创建的项目的文件夹中)

                       if(!outfile)

                       {

                                cerr<<"open error!"<<endl;

                                exit(1);

                       }

                       for(int i=0;i<I;i++)

                       {

                                int c;

                                char s;//定义字符型变量表示随机产生的运算符

                                c=rand()%2;//利用系统时间产生随机数

                                switch (c)

                                {

                                                   case 0: s='*';break;

                                                   case 1: s='/';break;

                                }

                                int a,b;

                                         a=rand()%99+1;

                                         b=rand()%99+1;

                                         int j[N],k[N];

                                         char l[N];

                                         j[i]=a;

                                         k[i]=b;

                                         l[i]=s;

                                         for(int q=i-1;q>=0;q--)

                                         {

                                                   if(j[q]==j[i]&&k[q]==k[i]&&l[q]==l[i])

                                                   {

                                                            c=rand()%2;

                                                            a=rand()%99+1;

                                                            b=rand()%99+1;

                                                   }

                                         }

                                         outfile<<a<<s<<b<<"="<<endl;

                                         cout<<a<<s<<b<<"="<<endl;

                       }

    }

    //可控参数:数值范围

    void Display2()

    {

             int min,max;

             cout<<"请输入计算题数值范围的最小数:";

             cin>>min;

             cout<<"请输入计算题数值范围的最大数:";

             cin>>max;

                                srand(unsigned(time(0)));//用系统时间生成随机数种子

                       int I;

                       cout<<"请输入出题数量: ";

                       cin>>I;

                       ofstream outfile("限制结果范围.txt",ios::out);

                       //定义文件流对象,生成磁盘文件"限制结果范围.txt"(未指明路径保存在创建的项目的文件夹中)

                       if(!outfile)

                       {

                                cerr<<"open error!"<<endl;

                                exit(1);

                       }

                       for(int i=0;i<I;i++)

                       {

                                int c,D;

                                char s;//定义字符型变量表示随机产生的运算符

                                c=rand()%4;//利用系统时间产生随机数

                                         int a,b,d,f;

                                         a=rand()%99+1;

                                         b=rand()%99+1;

                                         d=rand()%99+1;

                                         f=rand()%99+1;//随机数生成数学题中的因子

                                switch (c)

                                {

                                                   case 0: s='+';D=a+b;break;

                                                   case 1: s='-';D=a-b;break;

                                                   case 2: s='*';D=a*b;break;

                                                   case 3: s='/';D=a/b;break;

                                }

                                //控制题目无重复

                                int j[N],k[N];

                                char l[N];

                                j[i]=a;

                                k[i]=b;

                                l[i]=s;

                                for(int q=i-1;q>=0;q--)

                                {

                                         if(j[q]==j[i]&&k[q]==k[i]&&l[q]==l[i])

                                         {

                                                   a=rand()%99+1;

                                                   b=rand()%99+1;

                                                   c=rand()%2;

                                         }

                                }//题目无重复

                                         int e;

                                         e=rand()%2;//随机生成真分数运算或者整数运算题的变量

                                         switch(e)

                                         {

                                                   case 0://生成整数运算题的情况

                                                            if(D>=min&&D<=max)

                                                            {

                                                                               cout<<a<<s<<b<<"="<<endl;

                                                                               outfile<<a<<s<<b<<"="<<endl;

                                                                               break;

                                                            }

                                                            else

                                                                     i--;break;

                                                   case 1://生成真分数运算题的情况

                                                                     if(a<b&&d<f&&c>=2)//真分数相乘除的情况

                                                                               {

                                                                                        Abbreviation(a,b);

                                                                                        Abbreviation(d,f);

                                                                                        if(D>=min&&D<=max)

                                                                                        {

                                                                                             cout<<a<<"/"<<b<<s<<"("<<d<<"/"<<f<<")"<<"="<<endl;

                                                                                             outfile<<a<<"/"<<b<<s<<"("<<d<<"/"<<f<<")"<<"="<<endl;

                                                                                        }

                                                                                        else

                                                                                                 i--;

                                                                               }

                                                                     else if(a<b&&d<f&&c<2)//真分数相加减的情况

                                                                               {

                                                                                 Abbreviation(a,b);

                                                                                 Abbreviation(d,f);

                                                                                 if(D>=min&&D<=max)

                                                                                 {

                                                                                        cout<<a<<"/"<<b<<s<<d<<"/"<<f<<"="<<endl;

                                                                                        outfile<<a<<"/"<<b<<s<<d<<"/"<<f<<"="<<endl;

                                                                                 }

                                                                                 else

                                                                                          i--;

                                                                               }

                                                                     else

                                                                               i--;//重新生成题目,保证30道题的数量

                                                                                        break;

                       }

             }

    }

    //可控参数:结果有无负数

    void Display3()

    {

             int A;

             cout<<"请选择: ";

             cout<<"1 结果可有负数 ";

             cout<<"2 结果不可有负数 ";

             cin>>A;

             if(A==1)

             {

                       DisplayAll();

             }

             else

             {

                       int min=0;

                       srand(unsigned(time(0)));//用系统时间生成随机数种子

                       int I;

                       cout<<"请输入出题数量: ";

                       cin>>I;

                       ofstream outfile("结果无负数.txt",ios::out);

                       //定义文件流对象,生成磁盘文件"结果无负数.txt"(未指明路径保存在创建的项目的文件夹中)

                       if(!outfile)

                       {

                                cerr<<"open error!"<<endl;

                                exit(1);

                       }

                       for(int i=0;i<I;i++)

                       {

                                int c,D;

                                char s;//定义字符型变量表示随机产生的运算符

                                c=rand()%4;//利用系统时间产生随机数

                                int a,b,d,f;

                                a=rand()%99+1;

                                b=rand()%99+1;

                                d=rand()%99+1;

                                f=rand()%99+1;//随机数生成数学题中的因子

                                switch (c)

                                {

                                                   case 0: s='+';D=a+b;break;

                                                   case 1: s='-';D=a-b;break;

                                                   case 2: s='*';D=a*b;break;

                                                   case 3: s='/';D=a/b;break;

                                }

                                         int e;

                                         e=rand()%2;//随机生成真分数运算或者整数运算题的变量

                                         switch(e)

                                         {

                                                   case 0://生成整数运算题的情况

                                                            if(D>=min)

                                                            {

                                                                               cout<<a<<s<<b<<"="<<endl;

                                                                               outfile<<a<<s<<b<<"="<<endl;

                                                                               break;

                                                            }

                                                            else

                                                                     i--;break;

                                                   case 1://生成真分数运算题的情况

                                                                     if(a<b&&d<f&&c>=2)//真分数相乘除的情况

                                                                               {

                                                                                        Abbreviation(a,b);

                                                                                        Abbreviation(d,f);

                                                                                        if(D>=min)

                                                                                        {

                                                                                             cout<<a<<"/"<<b<<s<<"("<<d<<"/"<<f<<")"<<"="<<endl;

                                                                                                  outfile<<a<<"/"<<b<<s<<"("<<d<<"/"<<f<<")"<<"="<<endl;

                                                                                        }

                                                                                        else

                                                                                                 i--;

                                                                               }

                                                                     else if(a<b&&d<f&&c<2)//真分数相加减的情况

                                                                               {

                                                                                 Abbreviation(a,b);

                                                                                 Abbreviation(d,f);

                                                                                 if(D>=min)

                                                                                 {

                                                                                        cout<<a<<"/"<<b<<s<<d<<"/"<<f<<"="<<endl;

                                                                                        outfile<<a<<"/"<<b<<s<<d<<"/"<<f<<"="<<endl;

                                                                                 }

                                                                                 else

                                                                                          i--;

                                                                               }

                                                                     else

                                                                               i--;//重新生成题目,保证30道题的数量

                                                                                        break;

                       }

             }

             }

    }

    //可控参数:除法有无余数

    void Display4()

    {

             int A;

             cout<<"请选择: ";

             cout<<"1 除法可有余数 ";

             cout<<"2 除法不可有余数 ";

             cin>>A;

             if(A==1)

             {

                       DisplayAll();

             }

             else

             {

                       srand(unsigned(time(0)));//用系统时间生成随机数种子

                       int I;

                       cout<<"请输入出题数量: ";

                       cin>>I;

                       ofstream outfile("除法运算结果无余数.txt",ios::out);

                       //定义文件流对象,生成磁盘文件"除法运算结果无余数.txt"(未指明路径保存在创建的项目的文件夹中)

                       if(!outfile)

                       {

                                cerr<<"open error!"<<endl;

                                exit(1);

                       }

                       for(int i=0;i<I;i++)

                       {

                                int c;

                                char s;//定义字符型变量表示随机产生的运算符

                                c=rand()%4;//利用系统时间产生随机数

                                int a,b,d,f;

                                int D=0;

                                int E=0;

                                a=rand()%99+1;

                                b=rand()%99+1;

                                d=rand()%99+1;

                                f=rand()%99+1;//随机数生成数学题中的因子

                                switch (c)

                                {

                                                   case 0: s='+';break;

                                                   case 1: s='-';break;

                                                   case 2: s='*';break;

                                                   case 3: s='/';D=a%b;E=d%f;break;

                                }

                                         int e;

                                         e=rand()%2;//随机生成真分数运算或者整数运算题的变量

                                         switch(e)

                                         {

                                                   case 0://生成整数运算题的情况

                                                            if(D==0)

                                                            {

                                                                     cout<<a<<s<<b<<"="<<endl;

                                                                     outfile<<a<<s<<b<<"="<<endl;

                                                                     break;

                                                            }

                                                            else

                                                                     i--;break;

                                                   case 1://生成真分数运算题的情况

                                                                     if(a<b&&d<f&&c>=2)//真分数相乘除的情况

                                                                               {

                                                                                        Abbreviation(a,b);

                                                                                        Abbreviation(d,f);

                                                                                        cout<<a<<"/"<<b<<s<<"("<<d<<"/"<<f<<")"<<"="<<endl;

                                                                                        outfile<<a<<"/"<<b<<s<<"("<<d<<"/"<<f<<")"<<"="<<endl;

                                                                               }

                                                                     else if(a<b&&d<f&&c<2)//真分数相加减的情况

                                                                               {

                                                                                 Abbreviation(a,b);

                                                                                 Abbreviation(d,f);

                                                                                 cout<<a<<"/"<<b<<s<<d<<"/"<<f<<"="<<endl;

                                                                                 outfile<<a<<"/"<<b<<s<<d<<"/"<<f<<"="<<endl;

                                                                               }

                                                                     else

                                                                               i--;//重新生成题目,保证30道题的数量

                                                                                        break;

                       }

             }

             }

    }

    3.程序结果截图

  • 相关阅读:
    【09】绝不在构造和析构过程中调用virtual方法
    【08】别让异常逃离析构函数
    C++ 外部调用private方法
    【07】为多态基类声明virtual析构方法
    C++ 构造过程和析构过程
    理解C# Lazy<T>
    DG
    MongoDB
    sh.status()
    DG
  • 原文地址:https://www.cnblogs.com/jianglingjun/p/5323764.html
Copyright © 2020-2023  润新知