作业要求20170928-3 四则运算试题生成

结对   邹双黛

功能1 四则运算

支持出题4个数的四则运算题目,所有题目要求作者有能力正确回答 (提示:1/3 != 0.33333333333333333333333333333333,而是无限长)为了快出成果,你快速造个控制台的版本,包括以后改版成更优秀界面的核心功能,并考虑到扩展。你在老师面前作如下表演 (数字你用excel验算过了,UI的卖萌部分你还没有想好)

  1 #include<stdio.h>
  2 #include<stack>
  3 #include<stdlib.h>
  4 #include<string>
  5 #include<math.h>
  6 #include<time.h>
  7 using namespace std;
  8 void weishu(char str[10],int i,int n) ;
  9 struct Number
 10 {
 11     double a;
 12     bool b;
 13 };
 14 stack<Number>s3;
 15 void houzhuiming(char str[10])
 16 {
 17     stack<char>s1;
 18     stack<Number>s2;
 19     int i=0,j=0;
 20     for(i=0;str[i]!='';i++)
 21     {
 22         if(str[i]>='0'&&str[i]<='9')
 23         {
 24             Number num;
 25             num.a=0;
 26             num.b=0;
 27             while(str[i]<='9'&&str[i]>='0')
 28             num.a=(str[i++]-'0')+num.a*10;
 29             s2.push(num);   
 30             i--;
 31         }
 32         else
 33         {
 34             if(str[i]==')')
 35             {
 36                 while(s1.top()!='(')
 37                 {
 38                     Number num;
 39                     num.b=1;
 40                     num.a=s1.top();
 41                     s2.push(num);
 42                     s1.pop();
 43                 }
 44                 s1.pop();
 45             }
 46             else if(s1.empty()||s1.top()=='('||str[i]=='(')
 47             {
 48                 s1.push(str[i]);
 49             }
 50             else
 51             {
 52                 if((str[i]=='*'||str[i]=='/')&&(s1.top()=='+'||s1.top()=='-'))
 53                 s1.push(str[i]);
 54                 else
 55                 {
 56                     Number num;
 57                     num.b=1;
 58                     num.a=s1.top();
 59                     s2.push(num);
 60                     s1.pop();
 61                     i--;
 62                 }
 63             }           
 64         }
 65     }
 66     while(!s1.empty())
 67     {
 68         Number num;
 69         num.b=1;
 70         num.a=s1.top();
 71         s2.push(num);
 72         s1.pop();
 73     }
 74     while(!s2.empty())
 75     {
 76         s3.push(s2.top());
 77         s2.pop();
 78     }
 79 }
 80 double qiuzhi()
 81 {
 82     stack<double>s4;
 83     while(!s3.empty())
 84     {
 85         Number c1=s3.top();
 86         s3.pop();
 87         if(c1.b==0)
 88         s4.push(c1.a);
 89         else
 90         {
 91             double c2=s4.top();
 92             s4.pop();
 93             double c3=s4.top();
 94             s4.pop();
 95             double c4;
 96             switch((int)c1.a)
 97             {
 98                 case '+':c4=c3+c2;break;
 99                 case '-':c4=c3-c2;break;
100                 case '*':c4=c3*c2;break;
101                 case '/':c4=c3/c2;break;
102             }
103             s4.push(c4);
104         }
105     }
106     return s4.top();
107 }
108 void fuhao(char str[10],int i,int n)
109 {
110      //srand(time(0));
111     int num=rand()%4;
112     srand(time(0));
113     switch(num)
114     {
115         case 0:str[i]='+';break;
116         case 1:str[i]='-';break;
117         case 2:str[i]='*';break;
118         case 3:str[i]='/';break;
119     }
120     weishu(str,++i,n);
121 }
122 void weishu(char str[10],int i,int n) 
123 {
124  //   srand(time(0));
125     int num=rand()%10;
126     srand(time(0));
127     str[i]='0'+num;
128     n++;
129     if(n==4)
130     {
131         str[++i]='';
132         return ;    
133     }
134     i++;
135     fuhao(str,i,n);
136 }
137 int main()
138 {
139     char str[100];
140     int js=0;
141     for(int i=0;i<20;i++)
142     {
143         int t=0;
144         int n=0;
145         weishu(str,t,n);
146         houzhuiming(str);
147         double ans=qiuzhi();
148         for(int i=0;str[i]!='';i++)
149         printf("%c",str[i]);
150         printf("=
?");
151         double s;
152         scanf("%lf",&s);
153         if(abs(s-ans)<0.01)
154         {
155             js++;
156             printf("答对了,你真是天才
");
157         }
158         else
159         printf("再想想吧,答案似乎是%.2lf喔!
",ans);
160     }
161     printf("你一共答对%d道题,共20道题。
",js);
162     return 0;
163 }
View Code

截图如下

功能2 支持括号                    

老师看了你的表演,大大赞赏了你。然后她说,"你的题库里怎么都是没有括号的题呢,我记得你当初括号就掌握得不好啊。"你的脸红了,对老师说,"给我2个小时时间,我给你一个新版本,有括号的。"你拿出笔记本,偷偷微信你们《构建之法》班的学霸,她说肯定能行,但是细节信号不好你听不清,只捕捉到隐约几个词"逆波兰""后缀表达式""堆栈""我看好你""数据结构"。两小时后,在老师面前你在控制台下输入f4,然后回车

代码如下

  1 #include<stdio.h>
  2 #include<stack>
  3 #include<stdlib.h>
  4 #include<string>
  5 #include<string.h>
  6 #include<math.h>
  7 #include<time.h>
  8 using namespace std;
  9 void weishu(char str[10],int t,int n);//+-*/之后 
 10 int y=0;
 11 struct Number
 12 {
 13     double a;
 14     bool b;
 15 };
 16 stack<Number>s3;
 17 void houzhuiming(char str[10])//后缀表达式 
 18 {
 19     stack<char>s1;
 20     stack<Number>s2;
 21     int i=0,j=0;
 22     for(i=0;str[i]!='';i++)
 23     {
 24         if(str[i]>='0'&&str[i]<='9')
 25         {
 26             Number num;
 27             num.a=0;
 28             num.b=0;
 29             while(str[i]<='9'&&str[i]>='0')
 30             num.a=(str[i++]-'0')+num.a*10;
 31             s2.push(num);   
 32             i--;
 33         }
 34         else
 35         {
 36             if(str[i]==')')
 37             {
 38                 while(s1.top()!='(')
 39                {
 40                     Number num;
 41                     num.b=1;
 42                     num.a=s1.top();
 43                     s2.push(num);
 44                     s1.pop();
 45                 }
 46                 s1.pop();
 47             }
 48             else if(s1.empty()||s1.top()=='('||str[i]=='(')
 49             {
 50                 s1.push(str[i]);
 51             }
 52             else
 53             {
 54                 if((str[i]=='*'||str[i]=='/')&&(s1.top()=='+'||s1.top()=='-'))
 55                 s1.push(str[i]);
 56                 else
 57                 {
 58                     Number num;
 59                     num.b=1;
 60                     num.a=s1.top();
 61                     s2.push(num);
 62                     s1.pop();
 63                     i--;
 64                 }
 65             }           
 66         }
 67     }
 68     while(!s1.empty())
 69     {
 70         Number num;
 71         num.b=1;
 72         num.a=s1.top();
 73         s2.push(num);
 74         s1.pop();
 75    }
 76     while(!s2.empty())
 77     {
 78         s3.push(s2.top());
 79         s2.pop();
 80     }
 81 }
 82 double qiuzhi()
 83 {
 84     stack<double>s4;
 85     while(!s3.empty())
 86     {
 87         Number c1=s3.top();
 88         s3.pop();
 89         if(c1.b==0)
 90         s4.push(c1.a);
 91         else
 92         {
 93             double c2=s4.top();
 94             s4.pop();
 95             double c3=s4.top();
 96             s4.pop();
 97             double c4;
 98             switch((int)c1.a)
 99             {
100                 case '+':c4=c3+c2;break;
101                 case '-':c4=c3-c2;break;
102                 case '*':c4=c3*c2;break;
103                 case '/':c4=c3/c2;break;
104             }
105             s4.push(c4);
106         }
107     }
108     return s4.top();
109 }
110 
111 void fuhao(char str[100],int t,int n)//数字后面是+-/*(0-4)  右括号(5-9)
112 {
113     int i=rand()%10;
114   //  srand(time(0));
115     if(i<=4)
116     {
117         int j=rand()%4;
118         switch(j)
119         {
120             case 0:str[t]='+';break;
121             case 1:str[t]='-';break;
122             case 2:str[t]='*';break;
123             case 3:str[t]='/';break;
124         }
125         weishu(str,++t,n);
126     }
127     else//右括号 
128     {
129         if(y>0&&(str[t-2]!='('||str[t-3]!='('&&(str[t-2]<='0'&&str[t-2]>='9')))
130         {
131             str[t]=')';
132             y--;
133             fuhao(str,++t,n);
134         }
135         else
136         fuhao(str,t,n);   
137     }   
138 }
139 void weishu(char str[10],int t,int n)//+-*/之后 
140 {
141         int p=rand()%10;
142        // srand(time(0));
143         if(p>=3)//数字 
144         {
145             int num=rand()%10;
146             if(num>=10)
147             {
148                 int a=num%10;
149                 str[t++]=num/10+'0';
150                 str[t]=a+'0';
151             }
152             else
153             str[t]='0'+num;
154             n++;
155             if(n==4)
156             {
157                 str[++t]='';
158                 return ;    
159             }
160             fuhao(str,++t,n);
161         }
162         else//左括号 
163         {
164             str[t]='(';
165             y++;
166             weishu(str,++t,n);
167         }
168 }
169 
170 int main()
171 {
172     char str[10];
173     int js=0;
174     for(int i=0;i<20;i++)
175     {
176         int t=0;
177         int n=0;
178         weishu(str,t,n); 
179         int len=strlen(str);
180         while(y)
181         {
182             if(str[len-2]=='(')
183             {
184                 str[len-2]=str[len-1];
185                 len--;
186             }
187             else if(str[len-3]=='(')
188             {
189                 str[len-3]=str[len-2];
190                 str[len-2]=str[len-1];
191                 len--;
192             }
193             else
194             str[len++]=')';    
195             y--;
196         }
197         str[len]='';
198         houzhuiming(str);
199         double ans=qiuzhi();
200         for(int i=0;str[i]!='';i++)
201         printf("%c",str[i]);
202         printf("=
?");
203         double s;
204         scanf("%lf",&s);
205         if(abs(s-ans)<0.01)
206         {
207             js++;
208             printf("答对了,你真是天才
");
209         }
210          else
211         printf("再想想吧,答案似乎是%.2lf喔!
",ans);
212     }
213     printf("你一共答对%d道题,共20道题。
",js);
214     return 0;
215 }
View Code

截图如下

功能3 限定题目数量,"精美"打印输出,避免重复

"就这点儿题,像你当年做得那么快,一会儿就完成啦!"老师说,"另外,我想打印出来,上课也不能带台机器。又另外,你把答案也打出来呗,我把答案剪掉,题目给学生做。"

一看需求这么多,你生怕她会说,"这都是很简单的功能,你一定能完成吧"。你知道如果承诺今晚,明早交工的时候她一定想出了更多可怕的需求,你赶紧说,"老师我现在就做。"

你忘记怎么调用打印机了,就把答案与题目横向对齐,输出在文件的右边。告诉老师txt文件可以用WORD打开,也能打印。她满意而意味深长地笑了,表示同意。你输入命令执行的时候,脑袋比手指头还疼。

代码如下

  1 #include<stdio.h>
  2 #include<stack>
  3 #include<stdlib.h>
  4 #include<string>
  5 #include<string.h>
  6 #include<math.h>
  7 using namespace std;
  8 int y=0;
  9 struct Number
 10 {
 11     double a;
 12     bool b; 
 13 };
 14 stack<Number>s3;
 15 void houzhuibds(char str[100])//后缀表达式 
 16 {
 17     stack<char>s1;
 18     stack<Number>s2;
 19     int i=0,j=0;
 20     for(i=0;str[i]!='';i++)
 21     {
 22         if(str[i]>='0'&&str[i]<='9')
 23         {
 24             Number num;
 25             num.a=0;
 26             num.b=0;
 27             while(str[i]<='9'&&str[i]>='0')
 28             num.a=(str[i++]-'0')+num.a*10;
 29             s2.push(num);   
 30             i--;
 31         }
 32         else
 33         {
 34             if(str[i]==')')
 35             {
 36                 while(s1.top()!='(')
 37                 {
 38                     Number num;
 39                     num.b=1;
 40                     num.a=s1.top();
 41                     s2.push(num);
 42                     s1.pop();
 43                 }
 44                 s1.pop();
 45             }
 46             else if(s1.empty()||s1.top()=='('||str[i]=='(')
 47             {
 48                 s1.push(str[i]);
 49             }
 50             else
 51             {
 52                 if((str[i]=='*'||str[i]=='/')&&(s1.top()=='+'||s1.top()=='-'))
 53                 s1.push(str[i]);
 54                 else
 55                 {
 56                     Number num;
 57                     num.b=1;
 58                     num.a=s1.top();
 59                     s2.push(num);
 60                     s1.pop();
 61                     i--;
 62                 }
 63             }           
 64         }
 65     }
 66     while(!s1.empty())
 67     {
 68         Number num;
 69         num.b=1;
 70         num.a=s1.top();
 71         s2.push(num);
 72         s1.pop();
 73     }
 74    while(!s2.empty())
 75     {
 76         s3.push(s2.top());
 77         s2.pop();
 78     }
 79 }
 80 double qiuzhi()
 81 {
 82     stack<double>s4;
 83     while(!s3.empty())
 84     {
 85         Number c1=s3.top();
 86         s3.pop();
 87         if(c1.b==0)
 88         s4.push(c1.a);
 89         else
 90         {
 91             double c2=s4.top();
 92             s4.pop();
 93             double c3=s4.top();
 94             s4.pop();
 95             double c4;
 96             switch((int)c1.a)
 97             {
 98                case '+':c4=c3+c2;break;
 99                 case '-':c4=c3-c2;break;
100                 case '*':c4=c3*c2;break;
101                 case '/':c4=c3/c2;break;
102             }
103             s4.push(c4);
104         }
105     }
106     return s4.top();
107 }
108 void afterfh(char str[100],int t,int n);//+-*/之后 
109 void aftersz(char str[100],int t,int n);
110 int main()
111 {
112     FILE *fp;
113     fp=fopen("D:\2.txt","w");
114     char str[100];
115     char s[100];
116     int z=0;
117     while(1)
118     {
119         int k=0,flag=0;
120         scanf("%s",&s);
121         while(s[k]!='')
122         {
123             if(s[k]>='0'&&s[k]<='9')
124             z=z*10+s[k]-'0';
125             else
126             {
127                 flag=1;
128                 break;
129             }
130             k++;
131         }
132         if(flag==1)
133         printf("题目数量必须是 正整数。
");
134         else
135         break;
136     }
137     getchar();
138     for(int i=0;i<z;i++)
139     {
140         int t=0;
141         int n=0;
142         afterfh(str,t,n); 
143         int len=strlen(str);
144         while(y)
145         {
146             if(str[len-2]=='(')
147             {
148                 str[len-2]=str[len-1];
149                 len--;
150             }
151             else if(str[len-3]=='(')
152             {
153                 str[len-3]=str[len-2];
154                 str[len-2]=str[len-1];
155                 len--;
156             }
157             else
158             str[len++]=')';    
159             y--;
160         }
161         str[len]='';
162         houzhuibds(str);
163         double ans=qiuzhi();
164         fprintf(fp,"%25s",str);
165         fprintf(fp,"=%.2lf
",ans);
166     }
167     return 0;
168 }
169 void aftersz(char str[100],int t,int n)//数字后面是+-/*(0-4)  右括号(5-9)
170 {
171     int i=rand()%10;
172     if(i<=4)
173     {
174         int j=rand()%4;
175         switch(j)
176         {
177             case 0:str[t]='+';break;
178             case 1:str[t]='-';break;
179             case 2:str[t]='*';break;
180             case 3:str[t]='/';break;
181         }
182         afterfh(str,++t,n);
183     }
184     else//右括号 
185     {
186         if(y>0&&(str[t-2]!='('||str[t-3]!='('&&(str[t-2]<='0'&&str[t-2]>='9')))
187         {
188             str[t]=')';
189             y--;
190             aftersz(str,++t,n);
191         }
192         else
193         aftersz(str,t,n);   
194     }   
195 }
196 void afterfh(char str[100],int t,int n)//+-*/之后 
197 {
198         int p=rand()%10;
199         if(p>=3)//数字 
200         {
201             int num=rand()%100;
202             if(num>=10)
203             {
204                 int a=num%10;
205                 str[t++]=num/10+'0';
206                 str[t]=a+'0';
207             }
208             else
209             str[t]='0'+num;
210             n++;
211             if(n==4)
212             {
213                 str[++t]='';
214                 return ;    
215             }  
216             aftersz(str,++t,n);
217         }
218         else//左括号 
219         {
220             str[t]='(';
221             y++;
222             afterfh(str,++t,n);
223         }
224 }
View Code

截图


功能4 支持分数出题和运算

国庆节后,你终于又回到美丽优雅并且正常的东北师范大学净月校区,在去食堂的路上偶遇你心目中的女神 (或男神,请自行替换)。她说,"哎呀,这跟我们《构建之法》课上的题目要求一样啊,真是巧合。""不要客气,代码拿去!反正我也没用。"你说,"如果有需求变更或扩展,尽管找我。"你伏笔埋得不错。女神马上说,"对啊,有一点儿点儿不一样,你午饭时间加加班帮我改出来吧。"她的题目还要求支持分数运算,你不禁想到了功能1中你特意规避了一些题目不出。她想要的是下面的样子

代码如下

  1 #include<stdio.h>
  2 #include<stack>
  3 #include<stdlib.h>
  4 #include<string>
  5 #include<math.h>
  6 #include<time.h>
  7 using namespace std;
  8 void weishu(char str[10],int i,int n) ;
  9 struct Number
 10 {
 11     double a;
 12     bool b;
 13 };
 14 stack<Number>s3;
 15 void houzhuiming(char str[10])
 16 {
 17     stack<char>s1;
 18     stack<Number>s2;
 19     int i=0,j=0;
 20     for(i=0;str[i]!='';i++)
 21     {
 22         if(str[i]>='0'&&str[i]<='9')
 23         {
 24             Number num;
 25             num.a=0;
 26             num.b=0;
 27             while(str[i]<='9'&&str[i]>='0')
 28             num.a=(str[i++]-'0')+num.a*10;
 29             s2.push(num);   
 30             i--;
 31         }
 32         else
 33         {
 34             if(str[i]==')')
 35             {
 36                 while(s1.top()!='(')
 37                 {
 38                     Number num;
 39                     num.b=1;
 40                     num.a=s1.top();
 41                     s2.push(num);
 42                     s1.pop();
 43                 }
 44                 s1.pop();
 45             }
 46             else if(s1.empty()||s1.top()=='('||str[i]=='(')
 47             {
 48                 s1.push(str[i]);
 49             }
 50             else
 51             {
 52                 if((str[i]=='*'||str[i]=='/')&&(s1.top()=='+'||s1.top()=='-'))
 53                 s1.push(str[i]);
 54                 else
 55                 {
 56                     Number num;
 57                     num.b=1;
 58                     num.a=s1.top();
 59                     s2.push(num);
 60                     s1.pop();
 61                     i--;
 62                 }
 63             }           
 64         }
 65     }
 66     while(!s1.empty())
 67     {
 68         Number num;
 69         num.b=1;
 70         num.a=s1.top();
 71         s2.push(num);
 72         s1.pop();
 73     }
 74     while(!s2.empty())
 75     {
 76         s3.push(s2.top());
 77         s2.pop();
 78     }
 79 }
 80 double qiuzhi()
 81 {
 82     stack<double>s4;
 83     while(!s3.empty())
 84     {
 85         Number c1=s3.top();
 86         s3.pop();
 87         if(c1.b==0)
 88         s4.push(c1.a);
 89         else
 90         {
 91             double c2=s4.top();
 92             s4.pop();
 93             double c3=s4.top();
 94             s4.pop();
 95             double c4;
 96             switch((int)c1.a)
 97             {
 98                 case '+':c4=c3+c2;break;
 99                 case '-':c4=c3-c2;break;
100                 case '*':c4=c3*c2;break;
101                 case '/':c4=c3/c2;break;
102             }
103             s4.push(c4);
104         }
105     }
106     return s4.top();
107 }
108 void fuhao(char str[10],int i,int n)
109 {
110      //srand(time(0));
111     int num=rand()%4;
112     srand(time(0));
113     switch(num)
114     {
115         case 0:str[i]='+';break;
116         case 1:str[i]='-';break;
117         case 2:str[i]='*';break;
118         case 3:str[i]='/';break;
119     }
120     weishu(str,++i,n);
121 }
122 void weishu(char str[10],int i,int n) 
123 {
124  //   srand(time(0));
125     int num=rand()%10;
126  //   srand(time(0));
127     str[i]='0'+num;
128     n++;
129     if(n==4)
130     {
131         str[++i]='';
132         return ;    
133     }
134     i++;
135     fuhao(str,i,n);
136 }
137 int main()
138 {
139     char str[100];
140     int js=0;
141     for(int i=0;i<3;i++)
142     {
143         int t=0;
144         int n=0;
145         weishu(str,t,n);
146         houzhuiming(str);
147         double ans=qiuzhi();
148         for(int i=0;str[i]!='';i++)
149         printf("%c",str[i]);
150         printf("=                                            ");
151         double s,xs,jx,yx;
152       //  scanf( %lf%lf%lf,,&xs,jx,yx);
153         scanf("%lf",&s);
154        // if(abs(s-ans)<0.01)
155      //   {
156       //      js++;
157            // printf("答对了,你真是天才
");
158      //   }
159       //  else
160        // printf("再想想吧,答案似乎是%.2lf喔!
",ans);
161     }
162    // printf("你一共答对%d道题,共20道题。
",js);
163     return 0;
164 }
View Code

截图如下

要求1

随笔一篇

1、在开发层次,结对编程能提供更好的设计质量和代码质量,两人合作能有更强的解决问题的能力。 2、在心理上, 当有另一个人在你身边和你紧密配合, 做同样一件事情的时候, 你不好意思开小差, 也不好意思糊弄。 3、对于有不同习惯的编程人员,可以在一起工作会产生麻烦,甚至矛盾。 4、程序员们有时会对一个问题各执己见,导致浪费很多时间彼此争论,但是当两个人都尝试了之后,发现了最优算法。 

结对编程,我的结对对象是邹双黛 在这次结对编程中,我体会到了结对编程的优点,真的是一加一大于二,虽然你的结对对象不一定比你厉害,但是每个人都是有优点的,在实际的探讨中能提出很有价值的问题,即便是错的地方,也能给我们以后的编程提供很好的教育。在本次布置的任务中,听别人说用c++来进行编程能够变得简单点,在经过讨论之后,我们只能简单的整理下个思路,我们在经过网上查找资料,我们将代码写成了简单的版本。当然,我们还是有很多问题没有得到解决。在这次结对作业中,我们没有特别的进行分工,只是走了需求分析,算法设计,实际编码以及程序测试,在编程和程序规范中我们都做出了我们的努力。规范的编码以及适当的注释,让双方都对程序有了更好的了解,对后期的测试也有很大的帮助。也有助于代码的可读性,使代码都能让我们读懂。需求分析

 需求分析  作业要求一能看懂  

作业要求二单元测试很坎坷,我查了下2人结对,使用TDD测试框架 (如NUnit, JUnit, cppUnit等)完成本周作业四则运算试题生成的单元测试这句话什么意思,Nunit是C#的测试。JUnit是Java的测试,cppUnit是c++的测试。有此了解。

要求1 对每个功能,先给出测试用例,然后再编码功能。请注意把测试用例视为功能需求完成的检验指标

        测试用例查了半天没查懂,后来看着我班一个同学的改的。

 

使用coding.net做版本控制。checkin 前要求清理 临时文件、可执行程序,通常执行 build-clean可以达到效果。

                不懂怎么清理临时文件,可执行文件。后来我认为可能就是没运行的文件传到coing.Net上,我是这么认为的。

   

 

 要求 2

要求 3

 git :https://git.coding.net/tianjiping/ruanjiangongcheng.git

   

原文地址:https://www.cnblogs.com/tianjiing/p/7645361.html