结对成员:18软三刘琦3118005063
18软三刘智乐3118005064
题目:实现一个自动生成小学四则运算题目的命令行程序(也可以用图像界面,具有相似功能)。
说明:
自然数:0, 1, 2, …。
e = n | e1 + e2 | e1 − e2 | e1 × e2 | e1 ÷ e2 | (e),
其中e, e1和e2为表达式,n为自然数或真分数。
一、 刘智乐github项目地址:https://github.com/LLL-cpu/calulate/tree/master
刘琦github项目地址:https://github.com/Micro-sun/pairing/tree/master/bin/com/liu
二、实现的功能:
三、psp表格
PSP2.1 |
Personal Software Process Stages |
预估耗时(分钟) |
实际耗时(分钟) |
Planning |
计划 |
100 |
120 |
· Estimate |
· 估计这个任务需要多少时间 |
300 |
1200 |
Development |
开发 |
200 |
240 |
· Analysis |
· 需求分析 (包括学习新技术) |
60 |
120 |
· Design Spec |
· 生成设计文档 |
60 |
120 |
· Design Review |
· 设计复审 (和同事审核设计文档) |
100 |
200 |
· Coding Standard |
· 代码规范 (为目前的开发制定合适的规范) |
30 |
100 |
· Design |
· 具体设计 |
50 |
405 |
· Coding |
· 具体编码 |
1000 |
1200 |
· Code Review |
· 代码复审 |
50 |
30 |
· Test |
· 测试(自我测试,修改代码,提交修改) |
60 |
70 |
Reporting |
报告 |
100 |
200 |
· Test Report |
· 测试报告 |
100 |
300 |
· Size Measurement |
· 计算工作量 |
30 |
100 |
· Postmortem & Process Improvement Plan |
· 事后总结, 并提出过程改进计划 |
150 |
240 |
合计 |
|
2380 |
4655 |
四、解题思路
1、分析题目,发现四则运算都是需要随机出题目,即运算符号,数字都需要随机,因此在这可以使用random()这一个java库的方法随机生成。
2、为了获取准确的数量、准确的范围、将随机生成的字符串转换为数字、再将获得的小数转换为分数、将分数分母与分子化简公约数、最后再将获得的数字进行计算得到答案,我们将这些方法分成一个个方法类,且使用包管理,更好的维护。
3、由于Java得各种方法库不太熟悉,因此我们尽量没有使用现有得方法,而是尝试将这些需求里所用得方法逐个封装。
4、图形化使用swing库中的方法,在将完成得函数修改一下,监听事件处实现方法即可。
五、运行测试
1、测试输入范围0-5,输出数量10则。
2、测试数量100则,输入范围0-20.
六、主要代码及模块实现
public static String make(int fanwei2){ Random rd = new Random(); int b4 = rd.nextInt(fanwei2); int b5 = rd.nextInt(fanwei2); int b1 = rd.nextInt(20); if(b1<=5){ return b4+"+"+b5; }else if(b1<=10){ if(b4<b5) { b4=b4+b5; b5=b4-b5; b4=b4-b5;} return b4+"-"+b5; }else if(b1<=15){ return b4+"×"+b5; }else { if(b5<b4) { b4=b4+b5; b5=b4-b5; b4=b4-b5;} return b4+"÷"+b5; } }
find模块得方法,使用if...else判断分数整数、运算法则,并且将分数约分化简与整数相运算。
public static String find(String str) { int index; int a; int b; int sum; if(str.indexOf(‘ב)!=-1||str.indexOf(‘÷‘)!=-1){ if(str.indexOf(‘÷‘)!=-1){ index = str.indexOf(‘÷‘); a = leftLinkNum(str,index-1); b = rightLinkNum(str,index+1); String s = a+"÷"+b; String s1 = makeEasy(s); str = str.replace(s, s1); return str; }else{ index = str.indexOf(‘ב); a = leftLinkNum(str,index-1); b = rightLinkNum(str,index+1); sum = a*b; String s = a+"×"+b; str = str.replace(s, Integer.toString(sum)); return str; } }else if(str.indexOf(‘+‘)!=-1||str.indexOf(‘-‘)!=-1){ if(str.indexOf(‘-‘)!=-1){ index = str.indexOf(‘-‘); a = leftLinkNum(str,index-1); b = rightLinkNum(str,index+1); sum = a-b; String s = a+"-"+b; str = str.replace(s, Integer.toString(sum)); return str; }else{ index = str.indexOf(‘+‘); a = leftLinkNum(str,index-1); b = rightLinkNum(str,index+1); sum = a+b; String s = a+"+"+b; str = str.replace(s, Integer.toString(sum)); return str; } }else return str; }
main函数方法
主要是用于判断输入的范围及数量,并且将用户输入传到各个方法类中,在由各个方法类中返回数据输出。
public static void main(String[] args) { while(true){ System.out.println("请输入要设置的数量:"); Scanner input=new Scanner(System.in); //从键盘上输入指令并执行 String number=input.nextLine(); if(number != null){ int num=Integer.valueOf(number); System.out.println("请输入r设置范围:"); Scanner text2=new Scanner(System.in); //从键盘上输入指令并执行 String round=text2.nextLine(); if(round!=null){ int round2=Integer.valueOf(round); for(int i = 0;i<num;i++){ String s = fanwei.make(round2); System.out.println(s+"="+Calculate.calcu(s)); } break; } } } }
七、项目小结
一个人的力量终究是有限的,两人合作不但能使自己人际交流方面有大的提升,真正体会到一个团体的重要性。
分工合作不仅仅可以提高工作的效率,还能大大的降低代码的错误率。
原文:https://www.cnblogs.com/LLELL/p/12600978.html