软件工程结对作业2
小学生计算题系列
题目1:
- 我们在刚开始上课的时候介绍过一个小学四则运算自动生成程序的例子,请实现它,要求:
- 能够自动生成四则运算练习题
- 可以定制题目数量
- 用户可以选择运算符
- 用户设置最大数(如十以内、百以内等)
- 用户选择是否有括号、是否有小数
用户选择输出方式(如输出到文件、打印机等)
最好能提供图形用户界面(根据自己能力选做,以完成上述功能为主)
#include <ctime> #include<iostream> #include<stdlib.h> #include <cstdlib> #include <sstream> #include<stdio.h> #include<string.h> #include <fstream> using namespace std; #define random(a,b)(rand()%(b-a+1)+a) const string filename="test000.txt"; char yunsuanfu(int a) { switch(a) { case 1:return '+' ; break; case 2:return '-' ; break; case 3:return '*' ; break; case 4:return '/' ; break; } } int main() { const string filename="test000.txt"; ofstream fout("test000.txt"); int max,i,j,weishu,geshu,s1,s2,number=12345; char g,z,d,str[25]={0},f[25],str1[25],str2[25],str3[50]={0}; int e,a,b,c,o,p,h,v; cout<<"请输入题数: "; cin>>geshu; cout<<"请输入最大数: "; cin>>max; cout<<"请选择是否有括号 y或n: "; cin>>z; if(z=='n') p=0; cout<<"请选择运算符\n加法:1\n减法:2\n乘法:3\n除法:4\n混合运算:5\n"; cin>>h; cout<<"请选择是否有小数 y或n;"; cin>>g; if(g=='y') v=1; srand((int)time(0)); for(j=0;j<geshu;j++){ for(i=0;i<5;i++){ e=random(1,4); if(h!=5) e=h; d=yunsuanfu(e); if(i!=0) fout<<d; a=random(0,1); if(p==0) a=0; if (a==0){ b=random(0,max); fout<<b; if(v==1) fout <<"."<< random(0,99);} else{ c=random(1,4); if(h!=5) c=h; if(c==1||c==2){ o=random(0,max); fout << "("<<o; if(v==1) fout <<"."<< random(0,99); d=yunsuanfu(c); fout<<d; if(v==1) fout<< random(0,99) <<"."; fout << random(0,max)<<")";} else{ o=random(0,max); fout <<o; if(v==1) fout <<"."<< random(0,99); d=yunsuanfu(c); fout<<d; fout << random(0,max); if(v==1) fout <<"."<< random(0,99);} } } fout<<"="<<endl;} fout.close(); return 0; }
在窗口运行结果:
生成多项式放入文件,生成text000.txt:
小学生计算题答案代码:
#include <iostream> #include <stdio.h> #include <string.h> #include <stdlib.h> using namespace std; #define maxn 1000 char buf[maxn], str[maxn], signStack[maxn], ch[2]; int len, id, idSign, idAns, i, n,k=0; double ans[maxn]; void checkSign(char sign){ if(sign == '(') signStack[idSign++] = sign; else if(sign == '*' || sign == '/'){ while(idSign && (signStack[idSign-1] == '*' || signStack[idSign-1] == '/')) str[id++] = signStack[--idSign]; signStack[idSign++] = sign; }else if(sign == ')'){ while(signStack[idSign-1] != '(') str[id++] = signStack[--idSign]; --idSign; }else{ while(idSign && signStack[idSign-1] != '(') str[id++] = signStack[--idSign]; signStack[idSign++] = sign; } str[id++] = ' '; } int check(double a, char sign){ if(a == 0 && sign == '/'){ printf("0\n"); system("pause"); exit(EXIT_FAILURE); } return 1; } double cal(double a, double b, char sign){ switch(sign){ case '+': return a + b; case '-': return a - b; case '*': return a * b; case '/': return a / b; } } int main(){ while(1) {k++; gets(buf); id = idSign = idAns = 0; if((len = strlen(buf)) == 0) continue; int flag = 0; for(i = 0; i < len; ++i){ if(buf[i] == ' ') continue; if(buf[i] >= '0' && buf[i] <= '9' || buf[i] == '.' || buf[i] == '-' && !flag){ str[id++] = buf[i]; flag = 1; } else { str[id++] = ' '; checkSign(buf[i]); if(buf[i] != ')') flag = 0; else flag = 1; } } while(idSign) str[id++] = signStack[--idSign]; for(i = 0, n = 0; i < id; ++i){ if(str[i] == ' ') continue; if(str[i] >= '0' && str[i] <= '9' || str[i] == '.' || str[i] == '-' && (str[i+1] >= '0' && str[i+1] <= '9' || str[i+1] == '.')){ sscanf(str + i, "%lf%n", &ans[idAns++], &n); i += n - 1; continue; }else if(check(ans[idAns-1], str[i])){ ans[idAns-2] = cal(ans[idAns-2], ans[idAns-1], str[i]); --idAns; } } printf("%d.......... %.2lf\n \n",k,ans[0]); } system("pause"); return 0; }
- 运行结果:
结对作业心得:这次结对编程使我收获颇多,结对编程提高了编写代码的效率,结对队友在我编写代码的时候提出了众多宝贵的建议,我也改正了一些错误,这次编程是非常好的经验。
结对组员:孟鲜鲜
来源:https://www.cnblogs.com/ZhangZiYangDeBoKe/p/8868954.html