软件工程个人作业03

Posted 那年夏天123

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了软件工程个人作业03相关的知识,希望对你有一定的参考价值。

1》题目要求:
老师提出了新的要求:
1、学生写的程序必须能判定用户的输入答案是否正确,
例如程序输出:20 – 5 = ?用户输入15,那么程序就会反馈正确,然后继续出题。直到 30 道题目结束,程序最后告诉用户作对了几道题。
2、程序必须能处理四种运算的混合算式;
20 – 5 * 2 =?           正确答案是10.
20– 5 * 2 + 9 / 3 = ?   正确答案是13
注意:
连续的减法和除法,应该遵守左结合的规定。
连续除法要打括号,否则会引起歧义
 
2》要求两人合作分析,上传结对开发的工作照片
技术分享
3》四则运算3的设计思想
1.首先是只进行一次的简单运算,判断答案是否正确,只需要在每次输出打印的时候,输入你的答案,然后判断与正确结果是否统一,如果一样,那么输出计算正确,计算答对题的count变量数量加1;如果不一样,则输出计算错误。
2.是进行混合的四则运算,在编这段代码时,我先查阅资料,利用栈的知识,实现混合运算的计算结果的计算,原理是首先将运算式转化为字符串,然后扫描字符串,运算数入一个栈,运算符入一个栈,然后按照优先级每次算出一个算式的结果,最后算出最后结果。
3.其中这个程序的关键点是,用栈来计算混合四则运算,还有就是c++将整型变为字符串,我是通过查资料来实现的。
 
4》程序源代码
 

//四则运算2

//2016 3 14 Sunzhe

#include<fstream>

#include<iostream>

#include<stack>

#include<string>

#include<time.h>

using namespace std;

 

 

void chengchufa(int m,int s)

{

         int v;

         int  j[100],k[100],l[100];//用于检查重复的数组

         cout<<"请输入选择:1.有余数 2.无余数:"<<endl;

         cin>>v;

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

         {

                   int n=rand()%4; //产生随机数,运算符

                   int a=rand()%s;//产生1~s随机数

                   int b=rand()%s;//产生1~s随机数

                   j[i]=n;

                   k[i]=a;

                   l[i]=b;

                   for(int r=i-1;r>=0;r--)//向前检查重复,如果重复则重新生成

                   {

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

                            {

                                     int n=rand()%4; //产生随机数,运算符

                                     int a=rand()%s;//产生1~s随机数

                                     int b=rand()%s;//产生1~s随机数

                            }

                   }

 

 

                            if(n==0){

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

                            }

                            else if(n==1){

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

                            }

                            else if(n==2){

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

                            }

                            else{

                                               if(a<b)//实现被除数比除数大

                                               {

                                                        int w;

                                                        w=a;

                                                        a=b;

                                                        b=w;

                                               }

                                               if(b==0)

                                               {

                                                        b++;

                                               }

                                               if(v==1)//有余数

                                               {

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

                                               }

                                               if(v==2)

                                               {

                                                        a=a-a%b;

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

                                               }

                            }

         }

}

 

void jiajianfa(int m,int s){

        

                   int u;

                   int  j[100],k[100],l[100];//用于检查重复的数组

                   cout<<"请输入选择:1有负数 2无负数"<<endl;

                   cin>>u;

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

                   {

                            int n=rand() %2;//产生随机数,运算符的

 

                            int a=rand()%s;//产生1~s随机数

                            int b=rand()%s;//产生1~s随机数

                            j[i]=n;

                            k[i]=a;

                            l[i]=b;

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

                            {

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

                                     {

                                               int n=rand()%4; //产生随机数,运算符

                                               int a=rand()%s;//产生1~s随机数

                                               int b=rand()%s;//产生1~s随机数

                                     }

                            }

                                              

                            if(u==1) //有负数

                            {

                                     if(n==0)

                                     {

                                               cout<<"(-"<<a<<")+"<<b<<"="<<endl;

                                     }

                                     else

                                     {

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

                                     }

                            }

                            if(u==2)//无负数

                            {

                                     if(a<b)//实现减法无负数

                                     {

                                               int w;

                                               w=a;

                                               a=b;

                                               b=w;

                                     }

                                     if(n==0)

                                     {

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

                                     }

                                     else

                                     {

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

                                     }

                            }

             }

        

}

void dayinfangshi(int m,int s){

         int t;

         cout<<"请输入选择1.在屏幕输出2.在文件输出"<<endl;

         cin>>t;

         if(t==1)

         {

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

         {

                   int n=rand()%4; //产生随机数,运算符

                   int a=rand()%s;//产生1~s随机数

                   int b=rand()%s;//产生1~s随机数

                  

                   if(n==0){

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

                   }

                   else if(n==1){

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

                   }

                   else if(n==2){

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

                   }

                   else{

                            if(a<b)//实现被除数比除数大

                            {

                                     int w;

                                     w=a;

                                     a=b;

                                     b=w;

                            }

                            if(b==0)

                            {

                                     b++;

                            }

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

                   }

         }

         }

         if(t==2)

         {

                   ofstream outfile("f1.dat",ios::out);//定义文件流对象,打开磁盘文件f1.dat

                   if(! outfile)  //如果打开失败返回0值

                   {

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

                            exit(1);

                   }

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

         {

                   int n=rand()%4; //产生随机数,运算符

                   int a=rand()%s;//产生1~s随机数

                   int b=rand()%s;//产生1~s随机数

                  

                   if(n==0){

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

                   }

                   else if(n==1){

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

                   }

                   else if(n==2){

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

                   }

                   else{

                            if(a<b)//实现被除数比除数大

                            {

                                     int w;

                                     w=a;

                                     a=b;

                                     b=w;

                            }

                            if(b==0)

                            {

                                     b++;

                            }

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

                   }

         }

         outfile.close();

         }

}

 

 

int level(char m){//计算符号优先级

         switch(m)

         {

             case‘+‘:

             case‘-‘:

                       return 1;

                   case‘*‘:

                   case‘/‘:

                            return 2;

                   case‘(‘:

                   case‘)‘:

                            return 0;

                   case‘#‘:

                            return -1;

                   default:

                            break;

         }

}

void intToString(int n,string &a) //将数字转化为字符串的函数

{

         char str[256];

    sprintf(str,"%d",n);

         a=str;

}

 

void hunheyunsuan(int m,int f){

        

        

}

void panduandaan(int m,int f){//可以判断答案正确与否的出题

         int h;

         int count=0;//用来计算用户答对题的个数

         int daan;//用户输入的答案

         cout<<"请输入选择:"<<endl;

         cout<<"1.简单的运算(只有一种运算):"<<endl;

         cout<<"2.混合运算(可有多种运算):"<<endl;

         cin>>h;

         if(h==1)

         {

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

                            {

             srand((int)time(0));

                                     int n=rand() %4;//产生随机数,运算符的

 

                                     int a=rand()%f+1;//产生1~f随机数

                                     int b=rand()%f+1;//产生1~f随机数

                                     if(n==0)

                                     {

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

                                               cout<<"请输入答案:"<<endl;

                                               cin>>daan;

                                               if(daan==a+b)

                                               {

                                                        cout<<"恭喜你答对了>>..<<!"<<endl;

                                                        count++;

                                               }

                                               else

                                               {

                                                        cout<<"很遗憾你打错咯!"<<endl;

                                               }

                                     }

                                     else if(n==1)

                                     {

                                               if(a<b)//实现被减数比减数大

                                               {

                                                        int w;

                                                        w=a;

                                                        a=b;

                                                        b=w;

                                               }

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

                                               cout<<"请输入答案:"<<endl;

                                               cin>>daan;

                                               if(daan==a-b)

                                               {

                                                        cout<<"恭喜你答对了>>..<<!"<<endl;

                                                        count++;

                                               }

                                               else

                                               {

                                                        cout<<"很遗憾你打错咯!"<<endl;

                                               }

                                     }

                                     else if(n==2)

                                     {

                                              

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

                                               cout<<"请输入答案:"<<endl;

                                               cin>>daan;

                                               if(daan==a*b)

                                               {

                                                        cout<<"恭喜你答对了>>..<<!"<<endl;

                                                        count++;

                                               }

                                               else

                                               {

                                                        cout<<"很遗憾你打错咯!"<<endl;

                                               }

                                     }

                                     else

                                     {

                                               if(a<b)//实现被除数比除数大

                                               {

                                                        int w;

                                                        w=a;

                                                        a=b;

                                                        b=w;

                                               }

                                               if(b==0)

                                               {

                                                        b++;

                                               }

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

                                               cout<<"请输入答案:"<<endl;

                                               cin>>daan;

                                               if(daan==a/b)

                                               {

                                                        cout<<"恭喜你答对了>>..<<!"<<endl;

                                                        count++;

                                               }

                  &n

以上是关于软件工程个人作业03的主要内容,如果未能解决你的问题,请参考以下文章

软件工程个人作业03

软件工程个人作业03

软件工程个人作业03

软件工程个人作业03

软件工程个人作业03

软件工程个人作业03