2017-2018-2 1723 『Java程式設計』課程 結對程式設計練習_四則運算

來源:互聯網
上載者:User

標籤:post   tac   ble   code   person   結果   odi   sum   stand   

一.結對對象
  • 姓名:侯澤洋
  • 學號:20172308
  • 擔任角色:駕駛員(侯澤洋)
  • 夥伴第一周部落格地址

    二.本周內容
1.程式需求
  • (1).自動產生題目
  • 可獨立使用(能實現自己編寫測試類別單獨產生題目的功能)
    可產生不同等級題目,類似於:
    1級題目:2 + 5 = 、10 - 5 = 之類的兩個數,一個運算子的題目

  • (2).題目運算(判題)
    可獨立使用,實現中綴運算式轉為尾碼運算式並計算;判斷使用者答題正誤,並輸出正確結果

  • (3).支援真分數
  • 可獨立使用,實現分數算式的計算

  • (4).擴充需求:題目去重
  • 可獨立使用,實現對自動產生運算式的去重:如下若產生:2 + 5 = & 5 + 2 = 為同一題目

2.設計分析
  • 1.自動產生題目:這裡的題目的難度我是採用操作符的個數來定義的,根據操作符再產生操作符加一的數字(這裡暫時沒有編寫真分數的情況),然後使用迴圈進行產生,然後以字串形式輸出出來。
  • 2.題目運算(判題):
    先將產生的運算式轉換為尾碼運算式:通過寫一個方法,如果是運算元則放進棧,如果是操作符則進行判斷:加減一級的操作符遇到棧頂操作符為乘除或加減,則輸出棧頂操作符,壓進加減一級的操作符;乘除一級的操作符遇到棧頂操作符為乘除則棧頂操作符出棧,壓進將入棧的操作符,遇到加減一級操作符則直接進棧。
    計算尾碼運算式:寫一個方法:從左向右掃描尾碼運算式,遇到運算元進棧,遇到操作符則彈出棧頂的兩個元素,將結果計算出來再壓進棧,最後棧內剩餘一個元素即為最終答案
  • 3.支援真分數:暫未完成
3.設計UML類圖

4.遇到問題及解決方案5.代碼展示

第一部分:產生題目

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());}}
三.PSP分析
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

2017-2018-2 1723 『Java程式設計』課程 結對程式設計練習_四則運算

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.