二柱子四则运算定制版+升级版
题目:在上次程序的基础上,实现对连除的判断,并且实现多个数的混合运算,要求可以输入结果,并进行判断。
思路:基于上次程序,首先思考混合运算,这里运用两个数组,分别记录随机产生的运算数和运算符号。然后对连除进行判断,,检查运算符号数组里是否有连续的除号,有的话加上括号,避免歧义。最后对运算结果进行计算,并对用户输入的结果进行比较,给出相应提示,回答,计算方面,要优先对括号,乘除进行运算,然后再进行加减运算,每次运算完后都要记录下运算的结果。
代码:
//吕广浩 3/19 #include<iostream> #include<time.h> using namespace std; #define N 100 int main() { int n = 10; srand((unsigned)time(NULL)); int num, max, out, d, fushu, kh, yushu;//有无真分数判断,题目数量,最大值,输出方式,是否有乘除运算,加减法是否有负数,有无括号,有无余数 cout << "********二柱子四则运算定制版+升级版********" << endl; cout << "请输入题目的数量num:" << endl; cin >> num; cout << "请选择数值范围最大值(大于0)" << endl; cin >> max; cout << "请选择打印方式out(0空行打印 ,1空格打印)" << endl; cin >> out; cout << "请选择有无乘除运算d(0没有乘除运算, 1有乘除运算)" << endl; cin >> d; cout << "请选择加减运算有无负数fushu(0没有负数 ,1有负数)" << endl; cin >> fushu; cout << "请选择有无括号(0没有括号 ,1有括号)" << endl; cin >> kh; cout << "除法是否有余数(0没有余数,1有余数)" << endl; cin >> yushu; for (int m = 0; m < num; m++) { int a = 2 + rand() % 4; int a1[N] = {};//存运算的随机数 int y;//运算结果 for (int i = 0; i < a; i++) { int b = 1 + rand() % max; a1[i] = b; } if (fushu == 1) { int v = rand() % 2; if (v == 1) { a1[0] = -a1[0]; } } char b1[N] = {};//存进行运算的符号 char b[4] = { '+', '-', '*', '/' }; for (int i = 0; i < a - 1; i++)//判断连续除法 { if (d == 0) { int d = rand() % 2; b1[i] = b[d]; } else { int d = rand() % 4; b1[i] = b[d]; if (d == 3) { int c = rand() % 10 + 1; if (yushu == 0) { a1[i] = a1[i + 1] * c; } if (b1[i - 1] == '/') { a1[i] = a1[i + 1] * c; int c1 = rand() % 10 + 1; a1[i - 1] = a1[i] * a1[i + 1] * c1; } } for (int i = 0; i < a - 3; i++) { if (b1[i] == b1[i + 1] == b1[i + 2] == '/') { b1[i] = '+'; } if (b1[i] == b1[i + 1] == b1[i + 2] == b1[i + 3] == '/') { b1[i + 3] = '-'; } } } } if (kh == 1) { for (int i = 0; i < a - 1; i++)//输出运算 { if (b1[i] == '/'&&b1[i + 1] == '/')//连续除法用括号分开 { cout << '(' << a1[i] << b1[i]; } else if (b1[i] == '/'&&b1[i - 1] == '/'&&i>0) { cout << a1[i] << ')' << b1[i]; } else { int e = rand()%10; cout << '(' << a1[i]<< '+' << e << ')' << b1[i]; } } cout << a1[a - 1] << '=' << " "; } else { for (int i = 0; i < a - 1; i++)//输出运算 { if (b1[i] == '/'&&b1[i + 1] == '/')//连续除法用括号分开 { cout << '(' << a1[i] << b1[i]; } else if (b1[i] == '/'&&b1[i - 1] == '/'&&i>0) { cout << a1[i] << ')' << b1[i]; } else cout << a1[i] << b1[i]; } cout << a1[a - 1] << '=' << " "; } y = a1[0]; for (int i = 0; i < a - 1; i++) { int y = 0; if (b1[i] == '/'&&b1[i + 1] == '/') { y = a1[i] / a1[i + 1] / a1[i + 2]; a1[i] = y; a1[i + 1] = 0; a1[i + 2] = 0; b1[i] = b1[i + 1] = '+'; } else if (b1[i] == '/'&&b1[i + 1] == '*') { y = a1[i] / a1[i + 1] * a1[i + 2]; a1[i] = y; a1[i + 1] = 0; a1[i + 2] = 0;; b1[i] = b1[i + 1] = '+'; } else if (b1[i] == '*'&&b1[i + 1] == '/') { y = a1[i] * a1[i + 1] / a1[i + 2]; a1[i] = y; a1[i + 1] = 0; a1[i + 2] = 0; b1[i] = b1[i + 1] = '+'; } else if (b1[i] == '*'&&b1[i + 1] == '*') { y = a1[i] * a1[i + 1] * a1[i + 2]; a1[i] = y; a1[i + 1] = 0; a1[i + 2] = 0; b1[i] = b1[i + 1] = '+'; } else if (b1[i] == '/') { y = a1[i] / a1[i + 1]; a1[i] = y; a1[i + 1] = 0; b1[i] = '+'; } else if (b1[i] == '*') { y = a1[i] * a1[i + 1]; a1[i] = y; a1[i + 1] = 0; b1[i] = '+'; } } int y1 = a1[0]; for (int i = 0; i < a - 1; i++)//递归计算 { if (b1[i] == '+') { y1 = y1 + a1[i + 1]; } else if (b1[i] == '-') { y1 = y1 - a1[i + 1]; } else if (b1[i] == '*') { y1 = y1 * a1[i + 1]; } else if (b1[i] == '/') { y1 = y1 / a1[i + 1]; } } int s; cout << "请输入你的计算结果"; cin >> s; if (s == y1) { cout << "结果正确" ; } else { cout << "结果错误,正确答案为" << y1<<" " ; } if (out == 0) { cout << endl; } } return 0; }
运行结果截图:
宿舍工作照:
开发流程:
|
听课 | 看书 | 网上查资料 | 编程 | 日总计 | |
周一 | 2 | 1 | 3 | |||
周二 | 0 | |||||
周三 | 2 | 2 | ||||
周四 | 2 | 1 | 2 | 5 | ||
周五 | 1 | 3 | 4 | |||
周六 | 1 | 2 | 1 | 5 | ||
周日 | 0 | |||||
总计 | 4 | 3 | 4 | 7 | 18 |
|
开始时间 | 结束时间 | 中断时间 | 净时间 | 活动 | 备注 | |
3/14 | 14:00 | 15:00 | 10 | 110 | 听课 | 软件工程课 | |
16:30 | 17:30 | 0 | 60 | 编程 | 想思路 | ||
3/16 | 15:00 | 17:00 | 20 | 100 | 看书 | 读《构建之法》 | |
3/17 | 14:00 | 15:50 | 10 | 100 | 听课 | 软件工程课 | |
19:00 | 21:00 | 20 | 100 | 结对编程 | 交流思想 | ||
3/18 | 8:30 | 9:30 | 0 | 60 | 上网查资料 | 整理思路 | |
16:30 | 17:30 | 0 | 60 | 编程 | 编写代码 | ||
19:30 | 21:30 | 20 | 100 | 结对开发 | 领航,查错 | ||
3/19 | 10:00 | 11:00 | 0 | 60 | 调试代码 | 检查问题 | |
14:00 | 16:00 | 20 | 100 | 写博客 | 总结 | ||
19:30 | 20:30 | 0 | 60 | 看书 | 预习 |
缺陷记录日志:结对开发还不太默契,两个人节拍不协调,还需要多磨合。
程序的不足:
没有实现真分数的运算和结果的计算,还需要努力。
队友博客:http://home.cnblogs.com/u/apan008/