代码仓库地址:https://git.coding.net/MIAO479/yuxy.git
需求分析
1.程序可接收一个输入参数n,然后随机产生n道加减乘除练习题;
2.每个数字在 0 和 100 之间,运算符在3个到5个之间;
3.所出的练习题在运算过程中不得出现负数与非整数;
4.将练习题和计算结果一起输出到文件中;
5.当程序接收的参数为4时,以下为输出文件示例:
功能设计
用户输入出题的数量n,然后随机产生n道加减乘除练习题,每个数字在 0 和 100 之间,运算符在3个到5个之间,并且所出的练习题在运算过程中不得出现负数与非整数;最后将练习题和运算结果输出到result.txt文件中。
设计实现
1.主类a 这个类主要是用户输入出题的数量,随机产生练习题,并调用Calculate类中的方法计算结果,最后输出到result.txt文件中。同时规定了运算符的优先级和计算规则。
2.Calculate类.负责将中序表达式转换为右序表达式使用栈的方法求解。
测试运行
核心代码
避免除数等于0
if(sign4==3) { if(n5==0) n5=1+(int)(Math.random()*100); while(n4%n5!=0) { n4=(int)(Math.random()*100); } } if(sign5==3) { if(n6==0) n6=1+(int)(Math.random()*100); while(n5%n6!=0) { n5=(int)(Math.random()*100); } }
逆波兰式转换方法
public class calculate { public static int calculate(String s) { Stack<Integer> stack1 = new Stack<Integer>(); Stack<String> stack2 = new Stack<String>(); HashMap<String, Integer> hashmap = new HashMap<String, Integer>(); hashmap.put("(", 0); hashmap.put("+", 1); hashmap.put("-", 1); hashmap.put("*", 2); hashmap.put("/", 2); for (int i = 0; i < s.length();) { StringBuffer digit = new StringBuffer(); char c = s.charAt(i); while (Character.isDigit(c)) { digit.append(c); i++; c = s.charAt(i); } if (digit.length() == 0) { switch (c) { case '(': { stack2.push(String.valueOf(c)); break; } case ')': { String stmp = stack2.pop(); while (!stack2.isEmpty() && !stmp.equals("(")) { int a = stack1.pop(); int b = stack1.pop(); int sresulat = calculate(b, a, stmp); if(sresulat<0) return -1; stack1.push(sresulat); stmp = stack2.pop(); } break; } case '=': { String stmp; while (!stack2.isEmpty()) { stmp = stack2.pop(); int a = stack1.pop(); int b = stack1.pop(); int sresulat = calculate(b, a, stmp); if(sresulat<0) return -1; stack1.push(sresulat); } break; } default: { String stmp; while (!stack2.isEmpty()) { stmp = stack2.pop(); if (hashmap.get(stmp) >= hashmap.get(String.valueOf(c))) { int a = stack1.pop(); int b = stack1.pop(); int sresulat =calculate (b, a, stmp); if(sresulat<0) return -1; stack1.push(sresulat); } else { stack2.push(stmp); break; } } stack2.push(String.valueOf(c)); break; } } } else { stack1.push(Integer.valueOf(digit.toString())); continue; } i++; } return stack1.peek(); }
psp:
PSP2.1 | 任务内容 | 计划完成需要的时间(min) | 实际完成需要的时间(min) |
Planning | 计划 | 15 | 20 |
Estimate | 估计这个任务需要多少时间,并规划大致工作步骤 | 20 | 25 |
Development | 开发 | 120 | 180 |
Analysis | 需求分析 (包括学习新技术) | 15 | 10 |
Design Spec | 生成设计文档 | 10 | 8 |
Design Review | 设计复审 (和同事审核设计文档) | 15 | 20 |
Coding Standard | 代码规范 (为目前的开发制定合适的规范) | 20 | 25 |
Design | 具体设计 | 30 | 280 |
Coding | 具体编码 | 180 | 240 |
Code Review | 代码复审 | 15 | 20 |
Test | 测试(自我测试,修改代码,提交修改) | 20 | 25 |
Reporting | 报告 | 20 | 22 |
Test Report | 测试报告 | 5 | 5 |
Size Measurement | 计算工作量 | 5 | 10 |
Postmortem & Process Improvement Plan | 事后总结 ,并提出过程改进计划 | 8 | 10 |
总结
问题存在许多:代码稳定性差,考虑不周
由于代码基础太差,这次的作业可以说是历经坎坷,不会的太多了 。在询问同学、询问百度、查找代码之后,东拼西凑地“完成”了这次的任务。
同时认识到了只要付出还是有所收获的。虽然这份作业的要求很高,在做的这段时间内常常研究到很晚,但是只要完成任务、学到东西就是有意义的。
参考逆波兰式算法http://blog.csdn.net/yunxiang/article/details/1918717