20172315『Java程式設計』課程 結對程式設計練習_四則運算第二周階段總結

來源:互聯網
上載者:User

標籤:efault   math   程式   錯誤   技術分享   ++   tin   jpg   評價   

相關過程中綴轉尾碼及尾碼計算測試

題目產生

計算正確率

關鍵代碼解釋

將中綴運算式轉換為尾碼運算式並對尾碼運算式進行計算

import java.util.Stack;import java.util.regex.Pattern;public class StringToArithmetic {    public StringToArithmetic() {    }    // 將中綴運算式字串計算得到結果    public static double stringToArithmetic(String string) {        return suffixToArithmetic(infixToSuffix(string));    }    // 將中綴運算式轉換為尾碼運算式    public static String infixToSuffix(String exp) {        Stack<Character> s = new Stack<Character>();        // 要輸出的尾碼運算式字串        String suffix = "";        int length = exp.length(); // 輸入的中綴運算式的長度        for (int i = 0; i < length; i++) {            char temp;            char ch = exp.charAt(i);            switch (ch) {                case ‘ ‘:                    break;                case ‘(‘:                    s.push(ch);                    break;                case ‘+‘:                case ‘-‘:                    while (s.size() != 0) {                        temp = s.pop();                        if (temp == ‘(‘) {                            s.push(‘(‘);                            break;                        }                        suffix += temp;                    }                    s.push(ch);                    break;                case ‘ב:                case ‘÷‘:                    while (s.size() != 0) {                        temp = s.pop();                        if (temp == ‘+‘ || temp == ‘-‘ || temp == ‘(‘) {                            s.push(temp);                            break;                        } else {                            suffix += temp;                        }                    }                    s.push(ch);                    break;                case ‘)‘:                    while (!s.isEmpty()) {                        temp = s.pop();                        if (temp == ‘(‘) {                            break;                        } else {                            suffix += temp;                        }                    }                    break;                default:                    suffix += ch;                    break;            }        }        while (s.size() != 0) {            suffix += s.pop();        }        return suffix;    }    public static double suffixToArithmetic(String exp) {        Pattern pattern = Pattern.compile("\\d+||(\\d+\\.\\d+)");        String[] strings = exp.split("");        Stack<Double> stack = new Stack<Double>();        for (int i = 0; i < strings.length; i++) {            if (strings[i].equals("")) {                continue;            }            if (pattern.matcher(strings[i]).matches()) {                stack.push(Double.parseDouble(strings[i]));            }            else {                double y = stack.pop();                double x = stack.pop();                stack.push(calculate(x, y, strings[i]));            }        }        return stack.pop();    }    private static Double calculate(double x, double y, String string) {        // TODO Auto-generated method stub        if (string.trim().equals("+")) {            return x + y;        }        if (string.trim().equals("-")) {            return x - y;        }        if (string.trim().equals("×")) {            return x * y;        }        if (string.trim().equals("÷")) {            return x / y;        }        return (double) 0;    }

建立題目類

    public class createquestions {    public static String create(int level){        String add = "+", sub = "-", multi = "×", div = "÷";        String result="";        int r1=(int)(Math.random()*9+1);        int r2=(int)(Math.random()*9+1);        int r3=(int)(Math.random()*9+1);        //三個整數        int fenzi1=(int)(Math.random()*9+1);        int fenmu1=(int)(Math.random()*9+1);        String fenshu1=fenzi1+"/"+fenmu1;        int fenzi2=(int)(Math.random()*9+1);        int fenmu2=(int)(Math.random()*9+1);        String fenshu2=fenzi2+"/"+fenmu2;        int fenzi3=(int)(Math.random()*9+1);        int fenmu3=(int)(Math.random()*9+1);        String fenshu3=fenzi3+"/"+fenmu3;        //三個分數       int suiji1=(int)(Math.random()*4);        //第一個運算子        int suiji11=(int)(Math.random()*3);        //產生括弧        int suiji2=(int)(Math.random()*4);        //第二個運算子        if (level>=1) {            if(suiji11==0&&level==2&&suiji1!=0&&suiji1!=1)                result+="(";            if (level==3)                result+=fenshu1;            else                result+=r1;            if (suiji1 == 0)                result+=add;            if (suiji1 == 1)                result+=sub;            if (suiji1==2)                result+=multi;            if(suiji1==3)                result+=div;            if(suiji11==1&&level==2&&suiji2!=0&&suiji2!=1)                result+="(";            if (level==3)                result+=fenshu2;            else                result+=r2;            if(suiji11==0&&level==2&&suiji1!=0&&suiji1!=1)                result+=")";        }        if (level>=2){            if (suiji2==0)                result+=add;            if (suiji2 == 1)                result+=sub;            if (suiji2==2)                result+=multi;            if(suiji2==3)                result+=div;            if (level==3)                result+=fenshu3;            else                result+=r3;            if(suiji11==1&&suiji2!=0&&suiji2!=1)                result+=")";        }        return result;    }

main類

import java.util.Scanner;public class calculate {    public static void main(String[] args) {        System.out.println("輸入你要產生的題數");        Scanner scan = new Scanner(System.in);        int tishu = scan.nextInt();        System.out.println("輸入你要產生題目的等級(輸入1或2或3)");        int dengji = scan.nextInt();        //調用一個迴圈        double answer;        int zhengquelv = 0;        double[] daan = new double[tishu];        switch (dengji) {            case 1:                for (int i = 0; i < tishu; i++) {                    String a = createquestions.create(1);                    String b = a + "=";                    System.out.print(b);                    answer = scan.nextDouble();                    daan[i] = StringToArithmetic.stringToArithmetic(a);                    if (answer == daan[i])                        zhengquelv++;                }break;            case 2:                for (int i = 0; i < tishu; i++) {                    String a = createquestions.create(2);                    String b = a + "=";                    System.out.print(b);                    answer = scan.nextInt();                    daan[i] = StringToArithmetic.stringToArithmetic(a);                    if (answer == daan[i])                        zhengquelv++;                }break;            case 3:                for (int i = 0; i < tishu; i++) {                    String a = createquestions.create(3);                    String b = a + "=";                    System.out.print(b);                    answer = scan.nextInt();                    daan[i] = StringToArithmetic.stringToArithmetic(a);                    if (answer == daan[i])                        zhengquelv++;                }break;        }        if(dengji<1||dengji>3)            System.out.println("題目等級輸入錯誤");        if (dengji >= 1&&dengji <= 3) {            System.out.println("答案為");            for (                    double daanwei : daan)                System.out.print(daanwei + " ");            System.out.println("正確率為" + zhengquelv + "/" + tishu);        }    }}
遇到的困難及解決方案

關於判斷題目的正確率出現了一些小問題

經過組內商討,得出瞭解決辦法

寫出項目中自己負責的部分

對中綴轉尾碼和尾碼的計算類的編寫

個人貢獻度劃分(按百分比給小組各成員劃分貢獻度,各成員貢獻之和為100%)

20172315胡智韜30%
20172312彭霖30%
20172318陸大嶽40%

對結對的小夥伴做出評價給出小夥伴部落格連結(重點指出需要改進的地方)

彭霖
陸大嶽

給出項目的碼雲連結給出小組結對程式設計照片

20172315『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.