一、实验目的以及项目来源:
目的:
1、帮助老师产出随机的海量四则运算符的运算题目。
2、每次题目的产出均为随机,增强同学的四则运算能力。
项目来源:
项目来源于软件开发与创新课程的结对编程,对100以内的四则运算进行计算、练习和出题。
对项目的分析:
本系统使用C++语言编程,运用Dev C++进行开发,拥有计算题目、自动出题和进行练习三个功能,可以对结果100以内的四则运
算进行灵活运用。自动出题使用随机函数可自由选择系统写出多少道拥有两个运算符的四则运算,计算题目则可计算任意个运算符
的四则运算,进行练习写出答案后系统会自动判别对错。
二、实验环境:
Windows系统
通过Dev-C++,Visual Studio进行编写
三、实验内容:
1、随机生成两个运算符的四则运算习题,节约老师时间教学备课,大大提高教学效率。
2、采用某种抽象意义上的街机闯关方式,高强度不间断实时反馈正否的训练方式。
3、学生在完成题目练习后可以加大题目数量,借此大幅提高自身的心算或笔算能力,为将来的数学学习打下坚实的运算基础。
四、编程中遇到的问题:
1、随机数计算过程中,运算符的优先级混乱输出的正确有时错误。
2、对于结果有小数点的四则运算,系统给出的答案往往是经过四舍五入的整数,结果并不精准。
五、解决方案:
1、通过对优先级代码进行优化使得优先级计算准确。
改进过后的代码:
if (b != '*' && d != '*' && b != '/' && d != '/'){ if(b == '+') result = a+c; if(b == '-') result = a-c; if(d == '+') result = result + e; if(d == '-') result = result - e; } //2.2 d 的优先级高 else if (b == '+'){ if(d == '*'){ result = c * e; result = result + a; } else if(d == '/') { if(c%e != 0) result = -1; else{result = c / e; result = result + a;} } } else if (b == '-'){ if(d == '*'){ result = c * e; result = a - result; } else if(d == '/') { if(c%e != 0) result = -1; else{result = c / e; result = a - result;} } } // 2.3 b 的优先级高 else if (d == '+'){ if(b == '*'){ result = a * c; result = result + e; } else if(b == '/') { if(a%c != 0) result = -1; else{result = a / c; result = result + e;} } } else if (d == '-'){ if(b == '*'){ result = a * c; result = result - e; } else if(d == '/') { if(c%e != 0) result = -1; else{result = c / e; result = result - e;} } }
2、定义数据时将int改为double,结果输出时可以保留小数点后两位,使结果更加精准。
六、运行截图与流程:
流程:
运行截图:
1、暴躁不愿意认真做题的学生 2、认真学习但是效果不佳的学生
3、认真学习且效果一流的学生
七、代码清单
#include<bits/stdc++.h> using namespace std; //1. 随机生成运算符 char Create_Signal(){ char signals[4] = {'+','-','*','/'}; // 注意,除法只输出能够整除的情况
// 注意,乘除法连续出现题目难度将严重超纲
// 故而在运行过程中出现此类问题会将答案自动归为0
return signals[rand()%4]; } //2. 结果计算 int Result_100(int a, char b, int c, char d, int e){ int result; //2.1 只有加减运算符,不需要考虑优先级 if (b != '*' && d != '*' && b != '/' && d != '/'){ if(b == '+') result = a+c; if(b == '-') result = a-c; if(d == '+') result = result + e; if(d == '-') result = result - e; } //2.2 d 的优先级高 else if (b == '+'){ if(d == '*'){ result = c * e; result = result + a; } else if(d == '/') { if(c%e != 0) result = -1; else{result = c / e; result = result + a;} } } else if (b == '-'){ if(d == '*'){ result = c * e; result = a - result; } else if(d == '/') { if(c%e != 0) result = -1; else{result = c / e; result = a - result;} } } // 2.3 b 的优先级高 else if (d == '+'){ if(b == '*'){ result = a * c; result = result + e; } else if(b == '/') { if(a%c != 0) result = -1; else{result = a / c; result = result + e;} } } else if (d == '-'){ if(b == '*'){ result = a * c; result = result - e; } else if(d == '/') { if(c%e != 0) result = -1; else{result = c / e; result = result - e;} } } return result; } int main(){ srand(time(NULL)); int flag = 0; while(flag < 100){ int a = rand()%100; char b = Create_Signal(); int c = rand()%100; char d = Create_Signal(); int e = rand()%100; int result = Result_100(a,b,c,d,e); if(result<=100 && result>=0){ cout << a << " " << b <<" " << c << " " << d <<" " << e << " = "; flag++; int answer = 0; cin >> answer; if(answer == result) cout << "答案正确!\n\n"; else cout << "答案错误!\n" << "正确答案为:" << result << "\n\n"; } } return 0; }
八、心得体会
在这次结对编程的过程中我充分体会到了结对编程的优缺点,结对编程在编写代码时的效率不一定很高,但是在避免高频率的BUG上有他独到的地方,代码错误率更低,代码质量更高。
但是结对编程的局限性也不小,它的高效会受限于结对两人的熟悉度,所以两个人刚开始结对编程时需要一段时间来适应。同时与一个人编程相比较,结对编程有很多优点。比如我自己写代
码时不太注意编程规范,所以代码风格不是很优秀,再次读自己写的代码时就像看天书一样,可读性很差,但是两人结对编程的话两人可以互相商量变量命名等事宜,一个人也可以提醒代码
规范的问题。总而言之,结对编程是一个见仁见智的编程方式。