1. 程式人生 > >構建之法--界面化的簡單四則運算

構建之法--界面化的簡單四則運算

ngx int() ava val windows [] roc tin 題目

作業要求地址:http://www.cnblogs.com/xiangxuer/p/9695909.html

github地址:https://github.com/GVictory/MakeOutQuestions

前言:

  上軟件工程課中出現了一道例子,即阿超做了一個簡單的四則運算用於給他兒子出題,為了練手,將在於此模仿阿超將簡單的四則運算做出來,該四則運算為初步版本,僅僅包含正整數的加減乘除,不包括括號。

題目要求:

  任何編程語言都可以,命令行程序接受一個數字輸入,然後輸出相應數目的四則運算題目和答案。例如輸入數字是 30, 那就輸出 30 道題目和答案。 運算式子必須至少有兩個運算符,運算數字是在 100 之內的正整數,答案不能是負數。 如:23 - 3 * 4 = 11

所用版本:

  操作系統:windows10

  開發環境:intellij IDEA 2016.4

  開發語言:java

實現思路:

  這個程序分兩個步驟,第一個是出題,第二個是解題,出題即使用stringbuilder的方式將字符串拼接起來,將四個操作字符歸為一個字符組,然後使用隨機函數隨機獲取,數字也是采用隨機函數獲得的。第二個是解題,解題使用到了棧,將每一個遇到的數字入到數字棧,將每一個遇到的操作字符進行判斷,如果為*或/就直接取出數字棧的數字進行運算,如果遇到+或-則壓入操作棧中,當將所有的數字壓入棧後,最後將所有的數字按棧的數字進行加減運算。

個人軟件過程耗時估計與統計表:

Personal Software Process Stages Time Senior Student Time
計劃 1 1
估計這個任務需要多少時間 4 6
開發 1 2
需求分析 (包括學習新技術) 0.5 0.5
生成設計文檔 0.5 0.5
設計復審 0.5 0
代碼規範 0 0.5
具體設計 0.5 0.5
具體編碼 1 2
代碼復審 0.5 0.5
測試(自我測試,修改代碼,提交修改) 0.5 0.5
報告 0.5 0.5
測試報告 0.5 0.5
計算工作量 2 2
並提出過程改進計劃 1 1

實現代碼:

  設計一個棧出來:

技術分享圖片
 1 public class Stack<T> {
 2     private int top;
 3     private List<T> list;
 4 
 5     public Stack() {
 6         top=-1;
 7         list=new ArrayList<T>();
 8     }
 9 
10     public T getTop(){
11         return list.get(top);
12     }
13 
14     public T pop(){
15         T template=list.get(top);
16         list.remove(top);
17         top--;
18         return template;
19     }
20 
21     public T push(T data){
22         list.add(data);
23         top++;
24         T template=list.get(top);
25         return template;
26     }
27 
28     public Boolean isEmpty(){
29         if (top==-1){
30             return true;
31         }else {
32             return false;
33         }
34     }
35 }
技術分享圖片

  編寫創建題目函數:

技術分享圖片
 1     private static String getQuestion(Integer operatorNumber,Integer numberRange){
 2         char[] operator = new char[]{‘+‘, ‘-‘, ‘*‘, ‘/‘};
 3         Random random = new Random();
 4         StringBuilder stringBuilder = new StringBuilder();
 5         for (int operatorIndex = 0; operatorIndex < operatorNumber; operatorIndex++) {
 6             stringBuilder.append(random.nextInt(numberRange+1));
 7             stringBuilder.append(operator[random.nextInt(4)]);
 8         }
 9         stringBuilder.append(random.nextInt(numberRange+1));
10         return stringBuilder.toString();
11     }
技術分享圖片

  編寫解題函數:

技術分享圖片
 1    private static Float getAnswer(String question){
 2         Stack<Character> operatorStack=new Stack<Character>();
 3         Stack<Float> numberStack=new Stack<Float>();
 4         char operatorTemp;
 5         StringBuilder numberTemp=new StringBuilder();
 6         for (int questionIndex=0;questionIndex<question.length();questionIndex++){
 7             char singleChar=question.charAt(questionIndex);
 8             if (Character.isDigit(singleChar)){
 9                 numberTemp.append(singleChar);
10             }else {
11                 if (!operatorStack.isEmpty()&&operatorStack.getTop()==‘*‘){
12                     operatorStack.pop();
13                     numberStack.push(numberStack.pop()*Float.valueOf(numberTemp.toString()));
14                 }else if (!operatorStack.isEmpty()&&operatorStack.getTop()==‘/‘){
15                     operatorStack.pop();
16                     numberStack.push(numberStack.pop()/Float.valueOf(numberTemp.toString()));
17                 }else if(!operatorStack.isEmpty()&&operatorStack.getTop()==‘-‘){
18                     numberStack.push(-Float.valueOf(numberTemp.toString()));
19                 }else {
20                     numberStack.push(Float.valueOf(numberTemp.toString()));
21                 }
22                 operatorStack.push(singleChar);
23                 numberTemp.delete(0,numberTemp.length());
24             }
25         }
26         if (!operatorStack.isEmpty()&&operatorStack.getTop()==‘*‘){
27             numberStack.push(numberStack.pop()*Float.valueOf(numberTemp.toString()));
28             operatorStack.pop();
29         }else if (!operatorStack.isEmpty()&&operatorStack.getTop()==‘/‘){
30             numberStack.push(numberStack.pop()/Float.valueOf(numberTemp.toString()));
31             operatorStack.pop();
32         }else if(!operatorStack.isEmpty()&&operatorStack.getTop()==‘-‘){
33             numberStack.push(-Float.valueOf(numberTemp.toString()));
34         }else {
35             numberStack.push(Float.valueOf(numberTemp.toString()));
36         }
37         while (!operatorStack.isEmpty()){
38             operatorStack.pop();
39                 numberStack.push(numberStack.pop()+numberStack.pop());
40         }
41         return numberStack.pop();
42     }
43 }
技術分享圖片

  編寫啟動主類代碼:

技術分享圖片
 1     public static void main(String[] args) {
 2         Scanner scanner = new Scanner(System.in);
 3         HashMap<String, Float> hashMap = new HashMap<>();
 4         System.out.print("請輸入需要創建的題目的個數:");
 5         int count = scanner.nextInt();
 6         String questionTemp;
 7         float answertemp;
 8         for (int index = 0; index < count; index++) {
 9             do {
10                 questionTemp = getQuestion(2, 100);
11                 answertemp = getAnswer(questionTemp);
12             } while (answertemp < 0);
13             hashMap.put(questionTemp, answertemp);
14         }
15         Set<Map.Entry<String, Float>> ms = hashMap.entrySet();
16         for (Map.Entry entry : ms) {
17             System.out.println(entry.getKey() + "=" + entry.getValue());
18         }
19     }
技術分享圖片

運行結果:

技術分享圖片

總結:

這個程序相對比較簡單,主要是對進棧出棧的應用,其重點在於如何進棧的點和出棧的把握。

構建之法--界面化的簡單四則運算