• 20150401 作业2 结对 四则运算(Doing)


    import java.util.ArrayList;
    import java.util.Random;
    import java.util.Scanner;
    
    public class SE2_1 {// Software Engineering 軟件工程 作業2
        public RN getAnswer(RN[] Operand, String[] AS) {
            ArrayList<String> A = new ArrayList<String>();
            ArrayList<RN> O = new ArrayList<RN>();
            RN temp = new RN();
            temp.setSign(0);
            System.out.println();
            for (String as : AS){
                A.add(as);
                System.out.print(as+"  ");
            }
            System.out.println();
            for (RN op : Operand) {
                O.add(op);
                System.out.print(op.getNumber()+"  ");
            }
            System.out.println();
            while (A.get(0) != "=") {
                if (A.contains("×") || A.contains("÷")) {
                    if (((A.contains("×") ^ A.contains("÷")) == true ? (A
                            .contains("×") == true ? true : false) : (A
                            .indexOf("×") < A.indexOf("÷") ? true : false))) {
                        int index = A.indexOf("×");
                        System.out.print(index + " × ");
                        System.out.println(O.get(index).getNumber()
                                + "*"
                                + O.get(index + 1).getNumber()
                                + "="
                                + (O.get(index).getNumber() * O.get(index + 1)
                                        .getNumber()));
                        temp.setDecimal((O.get(index).getNumber()
                                * O.get(index + 1).getNumber()));
                        O.set(index, temp);
                        System.out.println("here set " + O.get(index).getNumber() +" at " +index);
                        System.out.println("here remove " + O.get(index+1).getNumber()+" from "+(index+1)+" and remove "+ A.get(index)+" from "+index);
                        O.remove(index + 1);
                        A.remove(index);
                    } else {
                        int index = A.indexOf("÷");
                        System.out.print(index + " ÷ ");
                        System.out.println(O.get(index).getNumber()
                                + "/"
                                + O.get(index + 1).getNumber()
                                + "="
                                + (O.get(index).getNumber() / O.get(index + 1)
                                        .getNumber()));
                        temp.setDecimal((O.get(index).getNumber()
                                / O.get(index + 1).getNumber()));
                        O.set(index, temp);
                        System.out.println("here set " + O.get(index).getNumber() +" at " +index);
                        System.out.println("here remove " + O.get(index+1).getNumber()+" from "+(index+1)+" and remove "+ A.get(index)+" from "+index);
                        O.remove(index + 1);
                        A.remove(index);
                    }
                } else if (A.contains("+") || A.contains("-")) {
                    if (((A.contains("+") ^ A.contains("-")) == true ? (A
                            .contains("+") == true ? true : false) : (A
                            .indexOf("+") < A.indexOf("-") ? true : false))) {
                        int index = A.indexOf("+");
                        System.out.print(index + " + ");
                        System.out.println(O.get(index).getNumber()
                                + "+"
                                + O.get(index + 1).getNumber()
                                + "="
                                + (O.get(index).getNumber() + O.get(index + 1)
                                        .getNumber()));
                        temp.setDecimal((O.get(index).getNumber()
                                + O.get(index + 1).getNumber()));
                        O.set(index, temp);
                        System.out.println("here set " + O.get(index).getNumber() +" at " +index);
                        System.out.println("here remove " + O.get(index+1).getNumber()+" from "+(index+1)+" and remove "+ A.get(index)+" from "+index);
                        O.remove(index + 1);
                        A.remove(index);
                    } else {
                        int index = A.indexOf("-");
                        System.out.print(index + " - ");
                        System.out.println(O.get(index).getNumber()
                                + "-"
                                + O.get(index + 1).getNumber()
                                + "="
                                + (O.get(index).getNumber() - O.get(index + 1)
                                        .getNumber()));
                        temp.setDecimal((O.get(index).getNumber()
                                - O.get(index + 1).getNumber()));
                        O.set(index, temp);
                        System.out.println("here set " + O.get(index).getNumber() +" at " +index);
                        System.out.println("here remove " + O.get(index+1).getNumber()+" from "+(index+1)+" and remove "+ A.get(index)+" from "+index);
                        O.remove(index + 1);
                        A.remove(index);
                    }
                }
                // if (A.contains("×")) {
                // int index = A.indexOf("×");
                // System.out.print(index + " × ");
                // System.out.println(O.get(index).getNumber() + "*"
                // + O.get(index + 1).getNumber() + "="
                // + O.get(index).getNumber()
                // * O.get(index + 1).getNumber());
                // temp.setDecimal(O.get(index).getNumber()
                // * O.get(index + 1).getNumber());
                // temp.setSign(0);
                // O.set(index, temp);
                // O.remove(index + 1);
                // A.remove(index);
                // } else if (A.contains("÷")) {
                // int index = A.indexOf("÷");
                // System.out.print(index + " ÷ ");
                // System.out.println(O.get(index).getNumber() + "/"
                // + O.get(index + 1).getNumber() + "="
                // + O.get(index).getNumber()
                // / O.get(index + 1).getNumber());
                // temp.setDecimal(O.get(index).getNumber()
                // / O.get(index + 1).getNumber());
                // temp.setSign(0);
                // O.set(index, temp);
                // O.remove(index + 1);
                // A.remove(index);
                // }
                // else if (A.contains("+")) {
                // int index = A.indexOf("+");
                // System.out.print(index + " + ");
                // System.out.println(O.get(index).getNumber() + "+"
                // + O.get(index + 1).getNumber() + "="
                // + O.get(index).getNumber()
                // + O.get(index + 1).getNumber());
                // temp.setDecimal(O.get(index).getNumber()
                // + O.get(index + 1).getNumber());
                // temp.setSign(0);
                // O.set(index, temp);
                // O.remove(index + 1);
                // A.remove(index);
                // } else if (A.contains("-")) {
                // int index = A.indexOf("-");
                // System.out.print(index + " - ");
                // System.out.println(O.get(index).getNumber()
                // + "-"
                // + O.get(index + 1).getNumber()
                // + "="
                // + (O.get(index).getNumber() - O.get(index + 1)
                // .getNumber()));
                // temp.setDecimal(O.get(index).getNumber()
                // - O.get(index + 1).getNumber());
                // temp.setSign(0);
                // O.set(index, temp);
                // O.remove(index + 1);
                // A.remove(index);
                // }
                else
                    System.out.println("error!!");
                O.trimToSize();
                A.trimToSize();
            }
            return O.get(0);
        }
    
        public RN GA(int number) {// Generation algorithm 生成算式
            RN[] Operand = new RN[number];
            String[] AS = new String[number];
            AS[number - 1] = "=";
            for (int i = 0; i < number; i++) {// 生成 number 个操作数
                Operand[i] = new RN();
                switch (new Random().nextInt(2) + 1) {// 這裏設置了Int(2)+1略過 小數
                // ifInt(3)就會出現小數
                case 0:// 小数[0~1.0) *100
                    Operand[i]
                            .setDecimal(((int) (new Random().nextDouble() * 9900)) / 100.0 + 1);
                    Operand[i].setSign(0);
    
                    break;
                case 1:// 整数[0~n)
                    Operand[i].setInteger(new Random().nextInt(99) + 1);
                    Operand[i].setSign(1);
                    break;
                case 2:// 分数
                    Operand[i].setMolecular(new Random().nextInt(99) + 1);
                    Operand[i].setDenominator(new Random().nextInt(99) + 1);
                    Operand[i].setSign(2);
                    break;
                default:
                    System.exit(0);
                }
                if (i > 0)
                    switch (new Random().nextInt(4)) {
                    case 0:
                        AS[i - 1] = "+";
                        // Answer += Operand[i].getNumber();// 答案有待修改
                        break;
                    case 1:
                        AS[i - 1] = "-";
                        // Answer -= Operand[i].getNumber();
                        break;
                    case 2:
                        AS[i - 1] = "×";
                        // Answer *= Operand[i].getNumber();
                        break;
                    case 3:
                        AS[i - 1] = "÷";
                        // Answer /= Operand[i].getNumber();
                        break;
                    default:
                        System.exit(0);
                    }
                // if (new Random().nextBoolean())
                // Operand[i] *= -1;
            }
    
            for (int i = 0; i < number; i++)
                System.out.print(Operand[i].toString() + " " + AS[i] + " ");
            return getAnswer(Operand, AS);// !!
        }
    
        public static void main(String[] args) {
            SE2_1 test = new SE2_1();
            Scanner in = new Scanner(System.in);
            int count = 0;
            int Num;
            do {
                System.out.println("請輸入出題的個數(不超過5):");
                Num = in.nextInt();
            } while ((Num > 5 || Num < 1) ? true : false);
            for (int i = 0; i < Num; i++) {
                RN Answer = test.GA(new Random().nextInt(4) + 2);// 确定操作数个数 1~5
                // 个RN Answer =
                System.out.println("Answer is:" + Answer.getNumber());
                double Input = in.nextDouble();
                if (Input == Answer.getNumber()
                        || Input == Math.round(Answer.getNumber() * 100) / 100.0) {
                    System.out.println("答對了!");
                    count++;
                } else
                    System.out.println("答錯了!答案爲:"
                            + Math.round(Answer.getNumber() * 100) / 100.0);
            }
            System.out
                    .println("一共" + Num + "道題,答對了" + count + "道題,答錯了"
                            + (Num - count) + "道題,正確率爲:" + (double) count * 100
                            / Num + "%");
        }
    
        public class F {// Fraction 分數
            int Molecular;// 分子
            int Denominator = 0;// 分母
    
            void setMolecular(int Molecular) {
                this.Molecular = Molecular;
            }
    
            int getMolecular() {
                return Molecular;
            }
    
            void setDenominator(int Denominator) {
                this.Denominator = Denominator;
            }
    
            int getDenominator() {
                return Denominator;
            }
    
            double getAll() {
                return (double) Molecular / Denominator;
            }
    
            public String toString() {
                return Molecular + "/" + Denominator;
            }
        }
    
        public class RN {// Random Number 隨機數
            double Decimal;// 小數
            int Integer;// 整數
            F Fraction = new F();// 分數
    
            int Sign;// 標誌該對象中存儲的是什麼數
    
            void setDecimal(double Decimal) {
                this.Decimal = Decimal;
            }
    
            double getDecimal() {
                return Decimal;
            }
    
            void setInteger(int Integer) {
                this.Integer = Integer;
            }
    
            int getInteger() {
                return Integer;
            }
    
            void setMolecular(int Molecular) {
                this.Fraction.setMolecular(Molecular);
            }
    
            int getMolecular() {
                return this.Fraction.getMolecular();
            }
    
            void setDenominator(int Denominator) {
                this.Fraction.setDenominator(Denominator);
            }
    
            int getDenominator() {
                return this.Fraction.getDenominator();
            }
    
            void setSign(int Sign) {
                this.Sign = Sign;
            }
    
            double getNumber() {
                switch (Sign) {
                case 0:
                    return Decimal;
                case 1:
                    return Integer;
                case 2:
                    return Fraction.getAll();
                default:
                    return -1;
                }
            }
    
            public String toString() {
                return Fraction.getDenominator() == 0 ? (Integer == 0 ? Decimal
                        + "" : Integer + "") : Fraction.toString();
            }
        }
    }


    現在只是一個過程,還不是所謂的完整版,所以,還是希望能有高人指點下.

        目前我知道存在的問題是:

          1.運算時,結果是由式子從左到用運算了,沒考慮到優先問題.例如:1+2*3=(1+2)*3=9 而真的答案是 7

            ...這個暫時想到棧處理,但是還在觀望中,看下還有沒更簡單的處理方法,我很嫌麻煩的.

        還有很多功能還沒加進來,我的做法是循序漸進,由簡至繁.如果有什麼比較高明的建議的話.請務必指導下

        已解決:

          現在已經解決上面1的問題,但是還有小錯誤.就是ArrayList set 功能感覺有點問題.還要研究下.

  • 相关阅读:
    [CareerCup] 4.6 Find Next Node in a BST 寻找二叉搜索树中下一个节点
    Android 接入支付宝支付实现
    Android 设置软键盘搜索键以及监听搜索键点击事件
    Android 应用监听自身卸载,弹出用户反馈调查
    ndk制作so库,ndk-build不是内部或外部命令。。。的错误
    Error: Your project contains C++ files but it is not using a supported native build system
    Android开发之——依赖冲突Program type already present
    基于Python的开源人脸识别库:离线识别率高达99.38%
    Android5.0以后,materialDesign风格的加阴影和裁剪效果
    Android 5.0 以上监听网络变化
  • 原文地址:https://www.cnblogs.com/kazehanaai/p/4389629.html
Copyright © 2020-2023  润新知