软工第二次作业--熟悉使用工具
熟悉使用工具
GIT地址 | https://github.com/Ayanotc/AchaoCalculator |
---|---|
GIT用户名 | Ayanotc |
学号后五位 | 61420 |
博客地址 | https://www.cnblogs.com/ayanotc/ |
作业链接 | https://www.cnblogs.com/ChildishChange/p/10398212.html |
1、环境配置
VS 和 GIT
2、代码实现
题目要求:
设计思路:
- 假期的时候就写过一个计算器的程序,其中就包含了混合四则运算,所以就想要另外换一种思路,于是我就想到了利用switch函数来实现这个功能。
- 首先运算中最多会出现四个数字三个运算符,所以我想把他们单独利用随机数产生;
- 同时利用随机数来决定运算符的个数;
![](https://img2018.cnblogs.com/blog/1787548/201909/1787548-20190921210317977-2093559448.png)
-
- 通过对运算符数字化来进行选择
![](https://img2018.cnblogs.com/blog/1787548/201909/1787548-20190921210823074-1592034389.png)
-
-
将题目写入文件(添加在最后面 )
-
-
- 结果:
使用git提交:
经过多次尝试,并向同学请教之后终于成功提交了!(过程太过艰辛,在这方面耗费的精力远远大于写代码,耗时太久不便详述)
测试过程:
问题1:一开始忽视了结果不能出现小数和分数这个问题(由于使用的是int 类型定义并未显示出错误;)
解决: 之后有附加个函数进行改正;此处体现出了函数功能模块化的优点。
问题2:函数功能还是太过臃肿,有相同功能的部分还是没能成功分离出去,代码重复度高;
解决: 由于优化过程中频频报错暂未解决。
3、感想
这次作业的代码部分并不难,都是曾经写过的程序,但是关于GitHub使用还是不懂,尝试了很多次也上网查了很多教程,暂时还都没有成功。还是需要去请教一下已经成功的同学如何去使用。
后记:今天终于成功了!自己总会有难以解决的问题,在经过独自大量努力后还无法解决一定要即使向他人求助,否则会在这方面浪费很多时间!
代码:
#include <iostream> #include <ctime> #include <fstream> using namespace std; char Calculator1(int T1) //选择运算符 { char Oper1; switch (T1) { case 0: Oper1 = '+'; break; case 1: Oper1 = '-'; break; case 2: Oper1 = '*'; break; case 3: Oper1 = '/'; break; default: Oper1 = '+'; break; } return Oper1; } char Calculator2(int T2) //选择运算符 { char Oper2; switch (T2) { case 0: Oper2 = '+'; break; case 1: Oper2 = '-'; break; case 2: Oper2 = '*'; break; case 3: Oper2 = '/'; break; default: Oper2 = '+'; break; } return Oper2; } char Calculator3(int T3) //选择运算符 { char Oper3; switch (T3) { case 0: Oper3 = '+'; break; case 1: Oper3 = '-'; break; case 2: Oper3 = '*'; break; case 3: Oper3 = '/'; break; default: Oper3 = '+'; break; } return Oper3; } int division(int a, int b) //除法整除 { if (a % b != 0) { int c = 0; do { c = a % b; a-= c; } while (a% b == 0); } return a; } int Sum() { int A = 0, B = 0, C = 0 , D = 0 ; //计算数字 int Operate_1 = 0, Operate_2 = 1, Operate_3 = 2; //运算符 char Oper1, Oper2, Oper3; int Temp_x = 0; //运算符个数 int sum = 0; //结果 A = rand() % 100 + 1; //计算数字 B = rand() % 100 + 1; C = rand() % 100 + 1; D = rand() % 100 + 1; Operate_1 = rand() % 4; //运算符 Operate_2 = rand() % 4; Operate_3 = rand() % 4; Temp_x = rand() % 2 + 2; //运算符个数 ofstream outfile; // 以写模式打开文件 outfile.open("afile.txt", ios::app); if (Temp_x == 2) { int m = 0; if (Operate_1 == 2 || Operate_1 == 3) { switch (Operate_1) { case 2: m = A * B; break; case 3: A = division(A, B); m = A / B; break; default: 0; break; } switch (Operate_2) { case 0: sum = m + C; break; case 1: sum = m - C; break; case 2: sum = m * C; break; case 3: m = division(m, C); sum = m / C; break; default: 0; break; } } else if (Operate_1 == 0 || Operate_1 == 1) { switch (Operate_2) { case 0: m = B + C; break; case 1: m = B - C; break; case 2: m = B * C; break; case 3: B = division(B, C); m = B / C; break; default: 0; break; } switch (Operate_1) { case 0: sum = A + m; break; case 1: sum = A - m; break; default: 0; break; } } Oper1=Calculator1(Operate_1); Oper2 = Calculator2(Operate_2); cout << A << Oper1 << B << Oper2 << C << '=' << sum << endl; outfile << A << Oper1 << B << Oper2 << C << '=' << sum << endl; // 向文件写入用户输入的数据 } else if (Temp_x == 3) { int j = 0, k = 0; if (Operate_2 == 0 || Operate_2==1) { switch (Operate_1) { case 0: j = A + B; break; case 1: j = A - B; break; case 2: j = A * B; break; case 3: A = division(A, B); j = A / B; break; default: 0; break; } switch (Operate_3) { case 0: k = C + D; break; case 1: k = C - D; break; case 2: k = C * D; break; case 3: C = division(C, D); k = C / D; break; default: 0; break; } switch (Operate_2) { case 0: sum = j + k; break; case 1: sum = j - k; break; break; } } else if((Operate_2 == 2 || Operate_2 == 3)&&(Operate_1 == 0 || Operate_1 == 1)) { switch (Operate_2) { case 2: j = B * C; break; case 3: B = division(B, C); j = B / C; break; default: 0; break; } switch (Operate_3) { case 0: k = j + D; break; case 1: k = j - D; break; case 2: k = j * D; break; case 3: j = division(j, D); k = j / D; break; default: break; } switch (Operate_1) { case 0: sum = A + k; case 1: sum = A - k; default:0; break; } } else if ((Operate_1 == 0 || Operate_1 == 1)&&(Operate_2 == 0 || Operate_2 == 1)) { switch (Operate_3) { case 0: k = C + D; break; case 1: k = C - D; break; case 2: k = C * D; break; case 3: C = division(C, D); k = C / D; break; default:0; break; } switch (Operate_1) { case 0: j = A + B; break; case 1: j = A - B; break; default:0; break; } switch (Operate_2) { case 0: sum = j + k; break; case 1: sum = j - k; default: break; } } else if ((Operate_2 == 2 || Operate_2 == 3)&&(Operate_1 == 2 || Operate_1 == 3)) { switch (Operate_1) { case 2: j = A * B; break; case 3: A = division(A, B); j = A / B; break; default: break; } switch (Operate_2) { case 2: k = j * C; break; case 3: j = division(j, C); k = j / C; break; default: break; } switch (Operate_3) { case 0: sum = k + D; break; case 1: sum = k - D; break; case 2: sum = k * D; break; case 3: k = division(k, D); sum = k / D; break; default: break; } } else { switch (Operate_3) { case 0: k = C + D; break; case 1: k = C - D; break; case 2: k = C * D; break; case 3: C = division(C, D); k = C / D; break; default:0; break; } switch (Operate_2) { case 0: j = B + k; break; case 1: j = B - k; break; case 2: j = B * k; break; case 3: B = division(B, k); j = B / k; break; default:0; break; } switch (Operate_1) {case 0: sum = A + j; break; case 1: sum = A - j; break; case 2: sum = A * j; break; case 3: A = division(A, j); sum = A / j; break; default:0; break; } } Oper1 = Calculator1(Operate_1); Oper2 = Calculator2(Operate_2); Oper3 = Calculator3(Operate_3); cout << A << Oper1 << B << Oper2 << C << Oper3 << D << '=' << sum << endl; outfile << A << Oper1 << B << Oper2 << C << Oper3 << D << '=' << sum << endl; } outfile.close(); return sum; } int main() { srand((unsigned)time(NULL)); int num = 0; cout << "出题数 : "; cin >> num; for (int i = 0; i < num; i++) { Sum(); cout << endl; } return 0; }