四则运算器

整个项目我并没有采取web的形式做,因为那样比较复杂,我选择了相对来说更为简单的ui,而且也改进了许多原来的四则运算项目所存在的不足的地方,原来的四则运算就做的很草率,这里我们采用中缀转后缀的方式。

这是我们ui界面

public UI_Cmd() {
    this.setLayout((LayoutManager)null);
    this.setSize(600, 500);
    this.lable1.setBounds(30, 30, 90, 30);
    this.numberOfProblems.setBounds(120, 35, 50, 20);
    this.lable3.setBounds(30, 70, 90, 30);
    this.numberOfOperators.setBounds(120, 75, 50, 20);
    this.lable2.setBounds(30, 110, 90, 30);
    this.lowerBound.setBounds(120, 115, 50, 20);
    this.upperBound.setBounds(180, 115, 50, 20);
    this.isEasyBox.setBounds(200, 30, 90, 30);
    this.isBracketBox.setBounds(200, 70, 90, 30);
    this.preToCore.setBounds(450, 30, 90, 40);
    this.preToDo.setBounds(350, 30, 90, 40);
    this.finish.setBounds(350, 30, 90, 40);
    this.finish.setVisible(false);
    this.finish.setEnabled(false);
    this.nextOne.setBounds(350, 360, 90, 40);
    this.nextOne.setEnabled(false);
    this.nextOne.setVisible(false);
    this.labelTime.setBounds(100, 370, 150, 40);
    this.labelRight.setBounds(100, 400, 150, 40);
    this.answer.setBounds(220, 300, 60, 20);
    this.labelAnswer.setBounds(100, 300, 130, 30);
    this.labelRight.setEnabled(false);
    this.labelRight.setVisible(false);
    this.labelTime.setEnabled(false);
    this.labelTime.setVisible(false);
    this.answer.setEnabled(false);
    this.answer.setVisible(false);
    this.labelAnswer.setEnabled(false);
    this.labelAnswer.setVisible(false);
    this.upLoadQuestion.setBounds(400, 90, 100, 40);
    this.lableStr.setBounds(100, 250, 500, 60);
    this.lableStr.setFont(new Font("楷体", 1, 15));
    this.add(this.lable1);
    this.add(this.numberOfProblems);
    this.add(this.lable2);
    this.add(this.lowerBound);
    this.add(this.upperBound);
    this.add(this.lable3);
    this.add(this.numberOfOperators);
    this.add(this.isBracketBox);
    this.add(this.isEasyBox);
    this.add(this.lableStr);
    this.add(this.preToCore);
    this.add(this.preToDo);
    this.add(this.finish);
    this.add(this.nextOne);
    this.add(this.upLoadQuestion);
    this.add(this.labelTime);
    this.add(this.labelRight);
    this.add(this.answer);
    this.add(this.labelAnswer);
    this.add(this.labelRight);
    this.preToCore.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            UI_Cmd.this.labelTime.setEnabled(false);
            UI_Cmd.this.labelTime.setVisible(false);
            UI_Cmd.this.labelRight.setEnabled(false);
            UI_Cmd.this.labelRight.setVisible(false);
            UI_Cmd.this.answer.setEnabled(false);
            UI_Cmd.this.answer.setVisible(false);
            String[] args = new String[9];

            try {
                int count = 0;
                int operator = 1;
                int n = Integer.parseInt(UI_Cmd.this.numberOfProblems.getText());
                if(!UI_Cmd.this.numberOfOperators.getText().equals("")) {
                    operator = Integer.parseInt(UI_Cmd.this.numberOfOperators.getText());
                }

                int lowerBoundNumber = Integer.parseInt(UI_Cmd.this.lowerBound.getText());
                int uppperBoundNumber = Integer.parseInt(UI_Cmd.this.upperBound.getText());
                Boolean isBracket = Boolean.valueOf(UI_Cmd.this.isBracketBox.isSelected());
                Boolean isEasy = Boolean.valueOf(UI_Cmd.this.isEasyBox.isSelected());
                if(n < 1 || n > 10000 || operator < 1 || operator > 10 || lowerBoundNumber > uppperBoundNumber || lowerBoundNumber < 1 || lowerBoundNumber > 100 || uppperBoundNumber < 50 || uppperBoundNumber > 1000) {
                    throw new Exception();
                }

                int var12 = count + 1;
                args[count] = "-n";
                args[var12++] = String.valueOf(n);
                args[var12++] = "-m";
                args[var12++] = String.valueOf(lowerBoundNumber);
                args[var12++] = String.valueOf(uppperBoundNumber);
                if(!UI_Cmd.this.numberOfOperators.getText().equals("")) {
                    args[var12++] = "-o";
                    args[var12++] = String.valueOf(operator);
                }

                if(isBracket.booleanValue()) {
                    args[var12++] = "-b";
                }

                if(isEasy.booleanValue()) {
                    args[var12++] = "-c";
                }

                for(int i = 0; i < 9; ++i) {
                    if(args[i] == null) {
                        args[i] = "NotExit";
                    }
                }

                Command.main(args);
                JOptionPane.showMessageDialog((Component)null, "成功生成文件", "亲爱的学生您好:", 0);
            } catch (Exception var11) {
                var11.printStackTrace();
                JOptionPane.showMessageDialog((Component)null, "出错了", "请重新设置参数", 0);
            }

        }
    });
    this.preToDo.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            UI_Cmd.this.time = Calendar.getInstance().getTimeInMillis();
            if(UI_Cmd.this.url.equals("")) {
                UI_Cmd.this.url = "result.txt";
            }

            File file = new File(UI_Cmd.this.url);
            if(!file.exists()) {
                JOptionPane.showMessageDialog((Component)null, "出错了", "您需要先上传题目", 0);
            } else {
                UI_Cmd.this.labelRight.setEnabled(false);
                UI_Cmd.this.labelRight.setVisible(false);
                UI_Cmd.this.answer.setEnabled(false);
                UI_Cmd.this.answer.setVisible(false);
                UI_Cmd.this.preToDo.setVisible(false);
                UI_Cmd.this.preToDo.setEnabled(false);
                UI_Cmd.this.finish.setVisible(true);
                UI_Cmd.this.finish.setEnabled(true);
                UI_Cmd.this.nextOne.setEnabled(true);
                UI_Cmd.this.nextOne.setVisible(true);
                UI_Cmd.this.upLoadQuestion.setVisible(false);
                UI_Cmd.this.upLoadQuestion.setEnabled(false);
                BufferedReader reader = null;

                try {
                    reader = new BufferedReader(new FileReader(file));
                    String tempString = null;
                    int line = 1;
                    UI_Cmd.this.arr[0] = "2016012040";

                    while((tempString = reader.readLine()) != null) {
                        if(!tempString.equals("2016012040")) {
                            UI_Cmd.this.arr[line] = "第" + line + "道题:" + tempString;
                            ++line;
                        }
                    }

                    reader.close();
                } catch (IOException var14) {
                    var14.printStackTrace();
                } finally {
                    if(reader != null) {
                        try {
                            reader.close();
                        } catch (IOException var13) {
                            ;
                        }
                    }

                }

            }
        }
    });
    this.finish.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            UI_Cmd.this.finish.setVisible(false);
            UI_Cmd.this.finish.setEnabled(false);
            UI_Cmd.this.preToDo.setVisible(true);
            UI_Cmd.this.preToDo.setEnabled(true);
            UI_Cmd.this.nextOne.setVisible(false);
            UI_Cmd.this.nextOne.setEnabled(false);
            UI_Cmd.this.upLoadQuestion.setVisible(true);
            UI_Cmd.this.upLoadQuestion.setEnabled(true);
            UI_Cmd.this.preToCore.setVisible(true);
            UI_Cmd.this.preToCore.setEnabled(true);
            UI_Cmd.this.lableStr.setText("准备开始做题");
            String str = "您做题一共花了" + (Calendar.getInstance().getTimeInMillis() - UI_Cmd.this.time) / 1000L + "秒";
            UI_Cmd.this.labelTime.setText(str);
            UI_Cmd.this.labelTime.setEnabled(true);
            UI_Cmd.this.labelTime.setVisible(true);
            UI_Cmd.this.labelAnswer.setEnabled(false);
            UI_Cmd.this.labelAnswer.setVisible(false);
            UI_Cmd.this.answer.setEnabled(false);
            UI_Cmd.this.answer.setVisible(false);
            UI_Cmd.this.labelRight.setText("您一共做对" + UI_Cmd.this.rightAnswer + "道题");
            UI_Cmd.this.labelRight.setEnabled(true);
            UI_Cmd.this.labelRight.setVisible(true);
            UI_Cmd.this.count = 1;
            UI_Cmd.this.rightAnswer = 0;
            UI_Cmd.this.arr = new String[10010];
            File file = new File(UI_Cmd.this.url);
            if(file.exists()) {
                file.delete();
            }

        }
    });
    this.nextOne.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if(UI_Cmd.this.count > Integer.parseInt(UI_Cmd.this.numberOfProblems.getText())) {
                UI_Cmd.this.answer.setEnabled(false);
                UI_Cmd.this.answer.setVisible(false);
                UI_Cmd.this.labelAnswer.setVisible(true);
                UI_Cmd.this.labelAnswer.setEnabled(true);
                UI_Cmd.this.labelRight.setEnabled(true);
                UI_Cmd.this.labelRight.setVisible(true);
                String str = "您做题一共花了" + (Calendar.getInstance().getTimeInMillis() - UI_Cmd.this.time) / 1000L + "秒";
                UI_Cmd.this.labelTime.setText(str);
                UI_Cmd.this.labelTime.setEnabled(true);
                UI_Cmd.this.labelTime.setVisible(true);
                UI_Cmd.this.lableStr.setText("已经把题做完了");
                UI_Cmd.this.labelRight.setText("您一共做对" + UI_Cmd.this.rightAnswer + "道题");
                JOptionPane.showMessageDialog((Component)null, "出错了", "请重新生成题目", 0);
                UI_Cmd.this.nextOne.setEnabled(false);
                UI_Cmd.this.nextOne.setVisible(false);
            } else {
                UI_Cmd.this.labelAnswer.setEnabled(true);
                UI_Cmd.this.labelAnswer.setVisible(true);
                UI_Cmd.this.answer.setEnabled(true);
                UI_Cmd.this.answer.setVisible(true);
                UI_Cmd.this.labelTime.setVisible(true);
                UI_Cmd.this.labelTime.setEnabled(true);
                if(UI_Cmd.this.count != 1 && UI_Cmd.this.answer.getText().equals("")) {
                    JOptionPane.showMessageDialog((Component)null, "未回答问题", "出错了", 0);
                    return;
                }

                UI_Cmd.this.lableStr.setText(UI_Cmd.this.arr[UI_Cmd.this.count].substring(0, UI_Cmd.this.arr[UI_Cmd.this.count].lastIndexOf(32)));
                if(UI_Cmd.this.count != 1 && (UI_Cmd.this.arr[UI_Cmd.this.count - 1].substring(0, UI_Cmd.this.arr[UI_Cmd.this.count - 1].indexOf(61)) + "= " + UI_Cmd.this.answer.getText()).equals(UI_Cmd.this.arr[UI_Cmd.this.count - 1])) {
                    UI_Cmd.this.rightAnswer = UI_Cmd.this.rightAnswer + 1;
                }
            }

            UI_Cmd.this.count = UI_Cmd.this.count + 1;
            UI_Cmd.this.answer.setText("");
        }
    });
    this.upLoadQuestion.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            JFileChooser jfc = new JFileChooser();
            jfc.setFileSelectionMode(2);
            jfc.showDialog(new JLabel(), "选择");
            File file = jfc.getSelectedFile();
            if(file.isDirectory()) {
                JOptionPane.showMessageDialog((Component)null, "只能选择文件", "提示", 0);
            } else if(file.isFile()) {
                UI_Cmd.this.url = file.getAbsolutePath();
            }

            if(file == null) {
                JOptionPane.showMessageDialog((Component)null, "文件不能为空", "提示", 0);
            }

        }
    });
}

protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.drawLine(0, 170, 600, 170);
    String welcome = "欢迎您开始做题";
    g.setFont(new Font("楷体", 1, 30));
    g.drawString(welcome, 50, 230);
}

这里用了之前项目没有用的中缀转后缀

public ToSuffix() {
}

public String toSuffix(String infix) {
    List queue = new ArrayList();
    List stack = new ArrayList();
    char[] charArr = infix.trim().toCharArray();
    String standard = "*÷+-()";
    boolean ch = true;
    int len = 0;

    for(int i = 0; i < charArr.length; ++i) {
        char chi = charArr[i];
        int size;
        if(Character.isDigit(chi)) {
            ++len;
        } else if(Character.isLetter(chi)) {
            ++len;
        } else if(chi == 46) {
            ++len;
        } else {
            if(Character.isSpaceChar(chi)) {
                if(len > 0) {
                    queue.add(String.valueOf(Arrays.copyOfRange(charArr, i - len, i)));
                    len = 0;
                }
                continue;
            }

            if(standard.indexOf(chi) != -1) {
                if(len > 0) {
                    queue.add(String.valueOf(Arrays.copyOfRange(charArr, i - len, i)));
                    len = 0;
                }

                if(chi == 40) {
                    stack.add(Character.valueOf(chi));
                    continue;
                }

                if(!stack.isEmpty()) {
                    size = stack.size() - 1;

                    boolean flag;
                    for(flag = false; size >= 0 && chi == 41 && ((Character)stack.get(size)).charValue() != 40; flag = true) {
                        queue.add(String.valueOf(stack.remove(size)));
                        --size;
                    }

                    while(size >= 0 && !flag && ((Integer)basic.get(stack.get(size))).intValue() >= ((Integer)basic.get(Character.valueOf(chi))).intValue()) {
                        queue.add(String.valueOf(stack.remove(size)));
                        --size;
                    }
                }

                if(chi != 41) {
                    stack.add(Character.valueOf(chi));
                } else {
                    stack.remove(stack.size() - 1);
                }
            }
        }

        if(i == charArr.length - 1) {
            if(len > 0) {
                queue.add(String.valueOf(Arrays.copyOfRange(charArr, i - len + 1, i + 1)));
            }

            for(size = stack.size() - 1; size >= 0; --size) {
                queue.add(String.valueOf(stack.remove(size)));
            }
        }
    }

    return (String)queue.stream().collect(Collectors.joining(","));
}

public String suffixToArithmetic(String str, int lowerBound, int upperBound) {
    String[] arr = str.split(",");
    List list = new ArrayList();

    for(int i = 0; i < arr.length; ++i) {
        int size = list.size();
        String var8;
        switch((var8 = arr[i]).hashCode()) {
            case 42:
                if(var8.equals("*")) {
                    double c = Double.parseDouble((String)list.remove(size - 2)) * Double.parseDouble((String)list.remove(size - 2));
                    if(c == (double)((int)c) && c <= (double)upperBound && c >= (double)lowerBound) {
                        list.add(String.valueOf(c));
                        continue;
                    }

                    return "-1";
                }
                break;
            case 43:
                if(var8.equals("+")) {
                    double a = Double.parseDouble((String)list.remove(size - 2)) + Double.parseDouble((String)list.remove(size - 2));
                    if(a == (double)((int)a) && a <= (double)upperBound && a >= (double)lowerBound) {
                        list.add(String.valueOf(a));
                        continue;
                    }

                    return "-1";
                }
                break;
            case 45:
                if(var8.equals("-")) {
                    double b = Double.parseDouble((String)list.remove(size - 2)) - Double.parseDouble((String)list.remove(size - 2));
                    if(b == (double)((int)b) && b <= (double)upperBound && b >= (double)lowerBound) {
                        list.add(String.valueOf(b));
                        continue;
                    }

                    return "-1";
                }
                break;
            case 247:
                if(var8.equals("÷")) {
                    double d = Double.parseDouble((String)list.remove(size - 2)) / Double.parseDouble((String)list.remove(size - 2));
                    if(d == (double)((int)d) && d <= (double)upperBound && d >= (double)lowerBound) {
                        list.add(String.valueOf(d));
                        continue;
                    }

                    return "-1";
                }
        }

        list.add(arr[i]);
    }

    return list.size() == 1?(String)list.get(0):"运算失败";
}

这是项目的核心
public class Core {
private long time;
private static final char[] operator = new char[]{’+’, ‘-’, ‘*’, ‘÷’};
private String pattern;
private boolean brackets;
private int numberOfOperator;
private int lowerNumber;
private int upperNumber;
private int n;

public Core() {
    this.setPattern("easy");
    this.setBrackets(false);
    this.setNumberOfOperator(1);
}

public int getN() {
    return this.n;
}

public void setN(int n) {
    try {
        if(n <= 0 || n > 10000) {
            throw new Exception();
        }

        this.n = n;
    } catch (Exception var3) {
        System.out.println("Variable of seting the count of problems must be among 1 and 10000");
        var3.printStackTrace();
        this.setTime(0L);
    }

}

public int getLowerNumber() {
    return this.lowerNumber;
}

public void setLowerNumber(int lowerNumber) {
    try {
        if(lowerNumber > 100 || lowerNumber < 1) {
            throw new Exception();
        }

        this.lowerNumber = lowerNumber;
    } catch (Exception var3) {
        System.out.println("The lower bound of the parameter is among 1 and 100");
        var3.printStackTrace();
        this.setTime(0L);
    }

}

public int getUpperNumber() {
    return this.upperNumber;
}

public void setUpperNumber(int upperNumber) {
    try {
        if(upperNumber > 1000 || upperNumber < 50) {
            throw new Exception();
        }

        this.upperNumber = upperNumber;
    } catch (Exception var3) {
        System.out.println("The upper bound of the parameter is among 50 and 1000");
        var3.printStackTrace();
        this.setTime(0L);
    }

}

public String getPattern() {
    return this.pattern;
}

public void setPattern(String pattern) {
    try {
        if(!pattern.equals("-c") && !pattern.equals("easy")) {
            throw new Exception();
        }

        this.pattern = pattern;
    } catch (Exception var3) {
        System.out.println("Wrong command!The legal directive of setting '*' or '÷' is \"-c\" ");
        var3.printStackTrace();
        this.setTime(0L);
    }

}

public boolean isBrackets() {
    return this.brackets;
}

public void setBrackets(boolean brackets) {
    this.brackets = brackets;
}

public int getNumberOfOperator() {
    return this.numberOfOperator;
}

public void setNumberOfOperator(int numberOfOperator) {
    try {
        if(numberOfOperator < 1 || numberOfOperator > 10) {
            throw new Exception();
        }

        this.numberOfOperator = numberOfOperator;
    } catch (Exception var3) {
        System.out.println("Illegal the number of operator is among 1 and 10!Please input again");
        var3.printStackTrace();
        this.setTime(0L);
    }

}

public long getTime() {
    return this.time;
}

public void setTime(long time) {
    this.time = time;
}

public String[] getCaculate(int n) {
    this.setN(n);
    String[] arr = new String[10011];
    String str = null;

    for(int i = 0; i < this.n; ++i) {
        int rOperator;
        do {
            do {
                rOperator = (int)(Math.random() * (double)(this.getNumberOfOperator() * 2 + 1) + 1.0D);
            } while(rOperator % 2 != 1);
        } while(rOperator == 1);

        double result;
        do {
            int rLowerBrackets = (int)(Math.random() * (double)(rOperator - 2) + 1.0D);
            int rUpperBrackets = (int)(Math.random() * (double)(rOperator - 2) + 3.0D);
            if(rLowerBrackets >= rUpperBrackets || rLowerBrackets % 2 == 0 || rUpperBrackets % 2 == 0) {
                do {
                    do {
                        rLowerBrackets = (int)(Math.random() * (double)(rOperator - 2) + 1.0D);
                        rUpperBrackets = (int)(Math.random() * (double)(rOperator - 2) + 3.0D);
                    } while(rUpperBrackets <= rLowerBrackets);
                } while(rLowerBrackets % 2 != 1 || rUpperBrackets % 2 != 1);
            }

            str = "";
            int temp = 0;

            for(int j = 1; j <= rOperator; ++j) {
                int count = 0;
                int[] rTemp = new int[100];
                int random = (int)(Math.random() * (double)(this.getUpperNumber() - this.getLowerNumber() + 1) + (double)this.getLowerNumber());
                if(j == rLowerBrackets && this.isBrackets()) {
                    str = str + '(';
                }

                int r;
                if(j % 2 == 1) {
                    boolean flag = true;
                    if(j != 1 && str.charAt(str.length() - 1) == 247) {
                        for(r = 2; r < temp; ++r) {
                            if(temp % r == 0) {
                                rTemp[count++] = r;
                            }
                        }

                        r = (int)(Math.random() * (double)count);
                        str = str + String.valueOf(rTemp[r]);
                        flag = false;
                        temp = rTemp[r];
                    }

                    if(j != 1 && str.charAt(str.length() - 1) == 42) {
                        r = (int)(Math.random() * 10.0D + 3.0D);
                        str = str + String.valueOf(r);
                        flag = false;
                        temp = r;
                    }

                    if(flag) {
                        str = str + String.valueOf(random);
                        temp = random;
                    }
                }

                if(j == rUpperBrackets && this.isBrackets()) {
                    str = str + ')';
                }

                if(j % 2 == 0) {
                    if(this.getPattern().equals("-c")) {
                        r = (int)(Math.random() * 4.0D);
                        str = str + operator[r];
                    } else {
                        r = (int)(Math.random() * 2.0D);
                        str = str + operator[r];
                    }
                }
            }

            ToSuffix tx = new ToSuffix();
            result = Double.parseDouble(tx.suffixToArithmetic(tx.toSuffix(str), this.getLowerNumber(), this.getUpperNumber()));
        } while(result < 0.0D || result > 10000.0D);

        if(this.getPattern().equals("-c")) {
            arr[i] = str + " = " + (int)result;
        } else {
            arr[i] = str + " = " + (int)result;
        }
    }

    return arr;
}

你可能感兴趣的:(四则运算器)