夥伴第一週博客地址html
可獨立使用(能實現本身編寫測試類單獨生成題目的功能)
可生成不一樣等級題目,相似於:
1級題目:2 + 5 = 、10 - 5 = 之類的兩個數,一個運算符的題目java
(2).題目運算(判題)
可獨立使用,實現中綴表達式轉爲後綴表達式並計算;判斷用戶答題正誤,並輸出正確結果dom
可獨立使用,實現分數算式的計算學習
可獨立使用,實現對自動生成表達式的去重:以下若生成:2 + 5 = & 5 + 2 = 爲同一題目測試
第一部分:生成題目編碼
import java.util.Stack; import java.util.Random; import java.util.ArrayList; import java.util.Scanner; class Questions { ArrayList<Object> array = new ArrayList<Object>(); Random generator = new Random(); char[] newchar = {'+', '-', '*', '/'}; protected int number; int NUM; public Questions() { number = 0; } public Object getQuestion(int num) { int num1 = num; while (num > 0) { int figure = (int) generator.nextInt(9) + 1; array.add(figure); number = (int) (Math.random() * 4); array.add(newchar[number]); num--; } String obj = ""; while (num < 2 * num1) { obj += array.get(num); num++; } int other = (int) generator.nextInt(9) + 1; array.add(other); obj += other + "="; return obj; } }
第二部分:題目運算設計
//生成後綴表達式 public class Calculations { public static void main(String[] args) { Questions questions=new Questions(); Stack stack = new Stack(); Scanner Scan=new Scanner(System.in); char c; int count=0,answer; char[] operation = new char[100]; String str = (String) questions.getQuestion(3); System.out.println("請回答如下問題:\n"+str); System.out.println("請輸入你的答案:"); answer=Scan.nextInt(); for (int i = 0; i < str.length(); i++) { c = str.charAt(i); if (c >= '0' && c <= '9') { operation[i] = c; count++; } else { if (c == '*' || c == '/') { if (stack.empty()) { stack.push((char) c); } else if ((char) stack.peek() == '*' || (char) stack.peek() == '/') { operation[i] = (char) stack.pop(); stack.push(c); } else stack.push(c); } else if (c == '+' || c == '-') { if (stack.empty()) { stack.push(c); } else if ((char) stack.peek() == '+' || (char) stack.peek() == '-') { operation[i] = (char) stack.pop(); stack.push(c); } else { operation[i] = (char) stack.pop(); stack.push(c); } } else stack.push(c); } } int num = stack.size(); for (int a = 0; a < num; a++) { operation[str.length() + a] = (char) stack.pop(); }
//後綴表達式計算 Stack<Integer> stack1 = new Stack<Integer>(); int m, n, sum,num1=str.length()+(str.length()-count); for (int b = 0; b <= num1; b++) { if (operation[b] >= '0' && operation[b] <= '9') stack1.push((int) operation[b]-48); else { if (operation[b] == '+') { m = stack1.pop(); n = stack1.pop(); sum = n + m; stack1.push(sum); } else if (operation[b] == '-') { m = stack1.pop(); n = stack1.pop(); sum = n- m; stack1.push(sum); } else if (operation[b] == '*') { m = stack1.pop(); n = stack1.pop(); sum = n * m; stack1.push(sum); } else if (operation[b] == '/') { m = stack1.pop(); n = stack1.pop(); sum = n / m; stack1.push(sum); } else if (operation[b] == ' ') continue; } } if ((int)stack1.peek()==answer) System.out.println("恭喜你答對了!"); else System.out.println("很遺憾,答錯了!答案是:"+stack1.peek()); } }
PSP2.1 | Personal Software Process Stages | 預估耗時(分鐘) |
---|---|---|
Planning | 計劃 | 60 |
Estimate | 估計這個任務須要多少時間 | 3 |
Development | 開發 2000 | 3000 |
Analysis | 需求分析 (包括學習新技術) | 350 |
Coding Standard | 代碼規範 (爲目前的開發制定合適的規範) | 60 |
Design UML | 設計項目UML類圖 | 60 |
Coding | 具體編碼 | 1500 |
Code Review | 代碼複審 | 30 |
Test 測試 | (自我測試,修改代碼,提交修改) | 300 |
Size Measurement 計算工做量(實際時間 ) | 2 | 2 |
Postmortem & Process Improvement Plan | 過後總結, 並提出過程改進計劃 | 30 |
合計 | 4395 |