• 软件工程个人作业03


    老师提出了新的要求:

    1、学生写的程序必须能判定用户的输入答案是否正确,

    例如程序输出:20 – 5 = ?用户输入15,那么程序就会反馈正确,然后继续出题。直到 30 道题目结束,程序最后告诉用户做对了几道题。

    2、程序必须能处理四种运算的混合算式;

    20 – 5 * 2 =?           正确答案是10.

    20– 5 * 2 + 9 / 3 = ?   正确答案是13

     

    1.设计思路:

    (1).加入一个数值用于存储算出的结果,用结果与用户输入的数值比较,相等则反馈正确信息,否则反馈错误信息。定义另一个整型变量初始化为用户输入的题目数量,答错一次减1.

    (2).未实现

     

    2.程序源代码:

    (1).head.h文件

    #include <iostream>

    #include <cstdlib>

    #include <ctime>

    #include <fstream>

    #define N 100000

    int a[N],b[N],d[N],f[N];//随机生成的数组

    int c[N];

    char s[N];//随机生成符号的数组 case(c[N])  -->  s[N]

    using namespace std;

     

    void DispalyAll();

    void Display1();

    void Display2();

    void Display3();

    void Display4();

    void Abbreviation(int &x,int &y);

    void Suijishu(int &I,int c[],int a[],int b[],int d[],int f[],char s[N],int choice);

     

    void DisplayAll()

    {                

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

                       int I;

                       cin>>I;

                       int choice=1;

                       Suijishu(I,c,a,b,d,f,s,choice);  

    }

    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 Suijishu(int &I,int c[],int a[],int b[],int d[],int f[],char s[],int choice)

    {

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

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

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

             if(!outfile)

                       {

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

                                exit(1);

                       }

             int amount=I;

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

                       {

                                if(choice==2)

                                {

                                         c[N]=rand()%2+2;

                                }

                                else

                                         c[N]=rand()%4;

                                //利用系统时间产生随机数

                                a[N]=rand()%99+1;

                                b[N]=rand()%99+1;

                                d[N]=rand()%99+1;

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

                                double D;

                                switch (c[N])

                                {

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

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

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

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

                                }

                                //题目无重复

                                int j[N],k[N];

                                char l[N];

                                j[i]=a[N];

                                k[i]=b[N];

                                l[i]=s[N];

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

                                {

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

                                         {

                                                   a[N]=rand()%99+1;

                                                   b[N]=rand()%99+1;

                                                   c[N]=rand()%4;

                                         }

                                }//题目无重复

                               

                       double answer;

                      

                       int e;

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

                       switch(e)

                                {

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

                                                   {

                                                            outfile<<a[N]<<s[N]<<b[N]<<"="<<endl;

                                                            cout<<a[N]<<s[N]<<b[N]<<"="<<endl;

                                                            cout<<"请输入您的运算结果: ";

                                                            cin>> answer;

                                                            cout<<" ";

                                                            if(D==answer)

                                                                     cout<<"正确! ";

                                                            else

                                                            {

                                                                     cout<<"回答错误! ";

                                                                     amount-=1;

                                                            }

                                                            break;

                                                   }

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

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

                                                            {

                                                                     Abbreviation(a[N],b[N]);

                                                                     Abbreviation(d[N],f[N]);

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

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

                                                                     cout<<"请输入您的运算结果: ";

                                                                     cin>> answer;

                                                                     cout<<" ";

                                                                     if(D==answer)

                                                                               cout<<"正确! ";

                                                                     else

                                                                               {

                                                                                        cout<<"回答错误! ";

                                                                                        amount-=1;

                                                                               }

                                                            }

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

                                                            {

                                                                     Abbreviation(a[N],b[N]);

                                                                     Abbreviation(d[N],f[N]);

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

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

                                                                     cout<<"请输入您的运算结果: ";

                                                                     cin>> answer;

                                                                     cout<<" ";

                                                                     if(D==answer)

                                                                               cout<<"正确! ";

                                                                     else

                                                                               {

                                                                                        cout<<"回答错误! ";

                                                                                        amount-=1;

                                                                               }

                                                            }

                                                   else

                                                            {

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

                                                                     break;

                                                            }

                                }

                       }

             cout<<"您答对了"<<amount<<"道。 ";

     

    }

    //可控参数:乘除

    void Display1()

    {

                       int I;

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

                       cin>>I;

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

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

                       if(!outfile)

                       {

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

                                exit(1);

                       }

                       int choice=2;

                       Suijishu(I,c,a,b,d,f,s,choice);  

    }

     

     

    //可控参数:数值范围

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

             }

             int amount=I;

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

                       {

                                int c,D,answer;

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

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

                                int a,b;

                                a=rand()%99+1;

                                b=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()%4;

                                                   }

                                         }//题目无重复

                                        

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

                                         {

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

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

                                                   cout<<"请输入您的运算结果: ";

                                                   cin>> answer;

                                                   cout<<" ";

                                                   if(D==answer)

                                                            cout<<"正确! ";

                                                   else

                                                            {

                                                                     cout<<"回答错误! ";

                                                                     amount-=1;

                                                            }

     

                                         }

                                else

                                         i--;

                               

             }

             cout<<"您一共答对"<<amount<<"道题。 ";

    }

     

     

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

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

                       }

                       int amount=I;

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

                       {

                                int c;

                                double 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;//随机生成真分数运算或者整数运算题的变量

                                double answer;

                                switch(e)

                                         {

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

                                                            if(D>=min)

                                                            {

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

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

                                                                               cout<<"请输入您的运算结果: ";

                                                                               cin>> answer;

                                                                               cout<<" ";

                                                                               if(D==answer)

                                                                                        cout<<"正确! ";

                                                                               else

                                                                                        {

                                                                                                 cout<<"回答错误! ";

                                                                                                 amount-=1;

                                                                                        }

                                                                               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;

                                                                                                  cout<<"请输入您的运算结果: ";

                                                                                                  cin>> answer;

                                                                                                  cout<<" ";

                                                                                                  if(D==answer)

                                                                                                  cout<<"正确! ";

                                                                                                  else

                                                                                                           {

                                                                                                                    cout<<"回答错误! ";

                                                                                                                    amount-=1;

                                                                                                           }

                                                                                        }

                                                                                        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;

                                                                                        cout<<"请输入您的运算结果: ";

                                                                                        cin>> answer;

                                                                                        cout<<" ";

                                                                                        if(D==answer)

                                                                                        cout<<"正确! ";

                                                                                        else

                                                                                                 {

                                                                                                           cout<<"回答错误! ";

                                                                                                           amount-=1;

                                                                                                 }

                                                                                  }

                                                                                        else

                                                                                           i--;

                                                                                }

                                                                     else

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

                                                                                        break;

                                                   }

                                }

                                cout<<"您一共答对"<<amount<<"道题。 ";

             }

    }

     

     

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

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

                       }

                       int amount=I;

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

                       {

                                int c;

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

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

                                int a,b;

                                int D=0;

                                int E=0;

                                double F;

                                a=rand()%99+1;

                                b=rand()%99+1;

                                switch (c)

                                         {

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

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

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

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

                                         }       

                                double answer;

                                if(D==0)

                                         {

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

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

                                                   cout<<"请输入您的运算结果: ";

                                                   cin>> answer;

                                                   cout<<" ";

                                                   if(F==answer)

                                                            cout<<"正确! ";

                                                   else

                                                            {

                                                                     cout<<"回答错误! ";

                                                                     amount-=1;

                                                            }                          

                                         }

                                else

                                         i--;

     

                       }

                       cout<<"您一共答对"<<amount<<"道题。 ";

             }

    }

    (2).main.cpp文件

    //Jiang LingJun 2016,03,26

    #include "head.h"

    #include <iostream>

    using namespace std;

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

             }

             return 0;

    }

    3.程序结果截图:

     

    4.总结

    在代码的优化过程中了解了全局变量与局部变量的使用和区别,还有函数接口实现的锻炼。

    个人感觉是由于第一次的设计思出现问题造成优化困难,也可能是能力有限。

    决定重新写一遍。

    5.结队工作照片

  • 相关阅读:
    php将字符串形式的数组转化为真数组
    Mysql8.0及以上 only_full_group_by以及其他关于sql_mode原因报错详细解决方案
    php使用base64_encode和base64_decode对数据进行编码和解码
    大数据基本概念
    sonarqube安装部署
    git-修改commit信息
    NLP-Commen Sense
    索引生命周期的管理
    kibana软件具体参数配置信息
    es机器监控x-pack导致的监控存储过大的问题
  • 原文地址:https://www.cnblogs.com/jianglingjun/p/5323760.html
Copyright © 2020-2023  润新知