《个人软件开发流程》——魏胜阳
四则运算编程练习
需求分析:随机生成由用户输入个数的四则运算方法
核心代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
package yunsuan; import java.util.*; public class Main{ private static String[] op = { "+" , "-" , "*" , "/" }; public static List<String> returnUserInNum( int a){ List<String> ac= new ArrayList(); int i= 1 ; while (i<=a){ String question=MakeFormula(); ac.add(question); i++; } return ac; } //运算式 public static String MakeFormula(){ StringBuilder build = new StringBuilder(); int count = ( int ) (Math.random() * 2 ) + 1 ; // 生成1-3之间的随机整数(不包括3) int start = 0 ; int number1 = ( int ) (Math.random() * 99 ) + 1 ; //生成1-100之间的随机整数(不包括100) build.append(number1); while (start <= count){ int operation = ( int ) (Math.random() * 3 ); //生成0-3之间的随机整数(不包括3) int number2 = ( int ) (Math.random() * 99 ) + 1 ; //生成1-100之间的随机整数(不包括100) build.append(op[operation]).append(number2); start ++; } return build.toString(); } public static String Solve(String formula){ Stack<String> tempStack = new Stack<>(); Stack<Character> operatorStack = new Stack<>(); int len = formula.length(); int k = 0 ; for ( int j = - 1 ; j < len - 1 ; j++){ char formulaChar = formula.charAt(j + 1 ); if (j == len - 2 || formulaChar == '+' || formulaChar == '-' || formulaChar == '/' || formulaChar == '*' ) { if (j == len - 2 ) { tempStack.push(formula.substring(k)); } else { if (k < j){ tempStack.push(formula.substring(k, j + 1 )); } if (operatorStack.empty()){ operatorStack.push(formulaChar); } else { char stackChar = operatorStack.peek(); if ((stackChar == '+' || stackChar == '-' ) && (formulaChar == '*' || formulaChar == '/' )){ operatorStack.push(formulaChar); } else { tempStack.push(operatorStack.pop().toString()); operatorStack.push(formulaChar); } } } k = j + 2 ; } } while (!operatorStack.empty()){ tempStack.push(operatorStack.pop().toString()); } Stack<String> calcStack = new Stack<>(); for (String peekChar : tempStack){ if (!peekChar.equals( "+" ) && !peekChar.equals( "-" ) && !peekChar.equals( "/" ) && !peekChar.equals( "*" )) { calcStack.push(peekChar); } else { int a1 = 0 ; int b1 = 0 ; if (!calcStack.empty()){ b1 = Integer.parseInt(calcStack.pop()); } if (!calcStack.empty()){ a1 = Integer.parseInt(calcStack.pop()); } switch (peekChar) { case "+" : calcStack.push(String.valueOf(a1 + b1)); break ; case "-" : calcStack.push(String.valueOf(a1 - b1)); break ; case "*" : calcStack.push(String.valueOf(a1 * b1)); break ; default : calcStack.push(String.valueOf(a1 / b1)); break ; } } } return formula + "=" + calcStack.pop(); } } |
测试运行代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
package yunsuan; import java.util.List; import java.util.Scanner; import org.junit.Assert; import org.junit.Test; public class Test1 { @Test public void solve() { String sum = Main.Solve( "7+8" ); Assert.assertEquals( "7+8=15" , sum); } @Test public void test01(){ Assert.assertEquals( "8+9=17" ,DoSum.sum( 8 , 9 )); } @Test public void test02() { System.out.println( "请输入你要的算式个数" ); Scanner scanner= new Scanner(System.in); List<String> strings = Main.returnUserInNum(scanner.nextInt()); int size=strings.size(); for ( int i= 0 ;i<size;i++){ String ret = Main.Solve(strings.get(i)); System.out.println(ret); } } } |
最终运行结果:
个人开发PSP展示
PSP2.1 | 任务内容 | 计划共完成需要的时间(min) | 实际完成需要的时间(min) |
---|---|---|---|
Planning | 计划 | 15 | 20 |
· Estimate | · 估计这个任务需要多少时间,并规划大致工作步骤 | 15 | 20 |
Development | 开发 | 460 | 600 |
· Analysis | 需求分析 (包括学习新技术) | 15 | 20 |
· Design Spec | · 生成设计文档 | 15 | 15 |
· Design Review | · 设计复审 (和同事审核设计文档) | 5 | 10 |
· Coding Standard | 代码规范 (为目前的开发制定合适的规范) | 10 | 20 |
· Design | 具体设计 | 20 | 25 |
· Coding | 具体编码 | 260 | 350 |
· Code Review | · 代码复审 | 90 | 100 |
· Test | · 测试(自我测试,修改代码,提交修改) | 45 | 60 |
Reporting | 报告 | 30 | 40 |
· Test Report | · 测试报告 | 15 | 20 |
· Size Measurement | 计算工作量 | 9 | 10 |
· Postmortem & Process Improvement Plan | · 事后总结 ,并提出过程改进计划 | 6 | 10 |
个人开发技术流程总结:
经过这次个人软件开发,我发现了自己的不足,很多代码的主要方法不太会,需要询问同学和查阅书籍才做出来,具体编写代码耗费了太多时间,计划完成时间和实际完成时间差距较大,希望今后通过自己的努力能够更加熟练的掌握开发软件。