《个人软件开发流程》---计应191第三组赵熠
项目要求:
用户输入数字和运算符后,屏幕输出正确结果。
思路分析:
首先拿到一个表达式后,我们如果按照人的计算方式,
在有括号的情况下,先计算得出括号中的结果。
没有括号 的话运算按照 先乘除,后加减进行。
代码实现:
public static String calc(String oper){
List<Character> op = Oper2op(oper);
List<Double> cha = Oper2cha(oper);
//遍历运算符容器,完成乘除运算
for(int i=0;i<op.size();i++){
Character c = op.get(i);
if(c=='*'||c=='/'){
op.remove(i);//是乘除符号则将其从集合中移除出来
Double opl = cha.remove(i);
Double opr = cha.remove(i);
if(c=='*'){
cha.add(i, opl*opr);//将运算后的数字添加在i的位置,当前i位置的数向后瞬移
}else
cha.add(i,opl/opr);
i--;//运算符容器的指针回到原来的位置,防止跳过下一个运算符
}
}
while(!op.isEmpty()){
//获取运算符
Character o = op.remove(0);
//获取左右运算数字
Double chal = cha.remove(0);
Double chara = cha.remove(0);
if(o=='-'){
chal = chal-chara;
}
if(o=='+'){
chal = chal+chara;
}
cha.add(0,chal);
}
return cha.get(0).toString();
}
得到所有参与运算的运算符方法 Oper2op(oper);
/**
* 得到表达式中所有的运算符
* @param oper
* 运算表达式
* @return
* 符号的list集合
*/
private static List<Character> Oper2op(String oper) {
//将表达式中的符号替换为@符号
oper = changeMinus(oper);
//将字符串按照正则表达式分组
String regex = "@[0-9]|[0-9]";
Pattern pt = Pattern.compile(regex);
String[] split = pt.split(oper);
//将数组元素放在集合中
List<Character> list = new ArrayList<>();
for(int i=0;i<split.length;i++){
String temp = split[i].trim();
if(temp.equals("+")||temp.equals("-")||temp.equals("*")||temp.equals("/")){
list.add(temp.trim().charAt(0));
}
}
return list;
}
private static List<Double> Oper2cha(String oper) {
oper = changeMinus(oper);
//将字符串按照运算符切割,得到字符串数组
Pattern pt = Pattern.compile("\\+|\\-|\\*|\\/");
String[] split = pt.split(oper);
//遍历数组,判断每个元素,将特殊符号转换成负数符号,并转换成double类型放在list集合中
List<Double> list = new ArrayList<>();
for(int i=0;i<split.length;i++){
String single = split[i];
if(single.charAt(0)=='@'){
single = "-"+single.substring(1);
}
list.add(Double.parseDouble(single));
}
return list;
}
/**
* 将表达式的负号‘-’用@符号代替
* @param oper
* 运算表达式
* @return
* 替换后的表达式
*/
private static String changeMinus(String oper) {
//替换负数符号的方法changeMinus(String oper)
//将表达式中的负数符号使用特殊符号替代,获得没有负数字符串
for(int i=0;i<oper.length();i++){
char c = oper.charAt(i);
if(c=='-'){
//判断第一个字符是否是负数
if(i==0){
oper = "@"+oper.substring(1);
}else{
//判断前一个字符是否是+-*/中的一个
char cprev = oper.charAt(i-1);
if(cprev=='+'||cprev=='-'||cprev=='*'||cprev=='/'){
oper = oper.substring(0, i)+"@"+oper.substring(i+1);
}
}
}
}
return oper;
}
/**
* 完成带括弧的算数表达式运算
* @param oper
* 表达式
* @return
* 运算结果
*/
public static String calcbra(String oper){
//有括号的算法
//从表达式中查找左括弧
int leftIndex = oper.lastIndexOf('(');
if(leftIndex==-1){//没有左括弧,调用calc直接运算
return calc(oper);
}else{//有括弧
//获取该括弧右侧的第一个右括弧
int rightIndex = oper.indexOf(')', leftIndex);
if(rightIndex==-1){
throw new RuntimeException("表达式语法错误");
}else{
//将两个括弧之间的表达式截取
String exp = oper.substring(leftIndex+1, rightIndex);
//将表达式计算
String result = calc(exp);
//将计算结果替换左右括弧包含的表达式,将表达式递归调用
oper = oper.substring(0,leftIndex)+result+oper.substring(rightIndex+1);
return calcbra(oper);
}
}
}
5. psp(个人软件开发流程)
|
PSP阶段 |
预估时间(分钟) |
实际时间(分钟) |
|
计划 |
20 |
30 |
|
明确需求和其他相关因素,估计每个阶段的时间和成本 |
10 |
13 |
|
开发 |
225 |
306 |
|
需求分析 |
10 |
7 |
|
生成设计文档 |
20 |
30 |
|
设计复审(和同事审核设计文档) |
10 |
10 |
|
代码规范(为目前的开发制定合适的规范) |
20 |
25 |
|
具体设计 |
15 |
20 |
|
具体编码 |
120 |
150 |
|
代码复审 |
30 |
44 |
|
测试(自测、修改代码、提交修改) |
15 |
20 |
|
报告 |
25 |
33 |
|
测试报告 |
10 |
18 |
|
计算工作量 |
5 |
5 |
|
事后总结,并提出过程改进计划 |
10 |
10 |
总结:
一开始感觉这个题目挺简单的,但实际上手后感觉并不简单,主要还是java的很多基础知识忘了,有些用的也熟练,还是练的太少了!
以后还是继续努力吧!

浙公网安备 33010602011771号