簡單混合運算的計算器

One Life C發表於2020-11-29

實現效果
在這裡插入圖片描述
程式碼

Main

package calc;

public class Main {
    public static void main(String[] args) {
        new MyJFrame();
    }
}

MyJFrame

package calc;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


public class MyJFrame extends JFrame implements ActionListener {
    /*結果文字框和輸入文字框*/
    private JTextField resultText;
    private JTextField input;

    /*退格、清空*/
    private JPanel middlePanel;
    private JButton backspaceBtn;
    private JButton clearBtn;
    private JButton unknownBtn;

    /*數字皮膚*/
    private JPanel mainPanel;

    /*運算式*/
    private Expression expression;

    /*記錄點選的數字,方便多位數的存入*/
    private String tempNum;
    /*計算結果*/
    private String result;


    public MyJFrame() {
        this.setTitle("計算器");
        this.setLayout(new FlowLayout());
        init();
        this.setBounds(200, 200, 350, 350);
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public void init() {
        expression = new Expression();
        tempNum = "";
        result = "";

        resultText = new JTextField(25);
        resultText.setBorder(null);
        resultText.setHorizontalAlignment(JTextField.RIGHT);
        input = new JTextField(25);
        input.setHorizontalAlignment(JTextField.RIGHT);
        /*Border lineBorder = BorderFactory.createLineBorder(Color.blue);
        resultText.setBorder(lineBorder);*/

        middlePanel = new JPanel();
        backspaceBtn = new JButton("backspace");
        clearBtn = new JButton("clear");
        unknownBtn = new JButton("(");
        backspaceBtn.addActionListener(this);
        clearBtn.addActionListener(this);
        unknownBtn.addActionListener(this);
        middlePanel.add(backspaceBtn);
        middlePanel.add(clearBtn);
        middlePanel.add(unknownBtn);

        mainPanel = new JPanel();
        mainPanel.setLayout(new GridLayout(4, 4, 30, 20));

        JButton btn = null;
        int n = 7;
        for (int i = 1; i <= 16; i++) {
            if (i == 4) {
                btn = new JButton("/");
                n -= 6;
            } else if (i == 8) {
                btn = new JButton("*");
                n -= 6;
            } else if (i == 12) {
                btn = new JButton("-");
                n -= 6;
            } else if (i == 13) {
                btn = new JButton("0");
            } else if (i == 14) {
                btn = new JButton(".");
            } else if (i == 15) {
                btn = new JButton("=");
            } else if (i == 16) {
                btn = new JButton("+");
            } else {
                btn = new JButton(String.valueOf(n++));
            }
            btn.addActionListener(this);
            mainPanel.add(btn);
        }

        this.add(resultText);
        this.add(input);
        this.add(middlePanel);
        this.add(mainPanel);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        JButton button=(JButton)e.getSource();

        String click = button.getText();

        if (click.equals("1") || click.equals("2") || click.equals("3") || click.equals("4") || click.equals("5")
                || click.equals("6") || click.equals("7") || click.equals("8") || click.equals("9") || click.equals("0")
                || click.equals(".")) {
            this.tempNum += click;
        }

        if (click.equals("+") || click.equals("-") || click.equals("*") || click.equals("/") || click.equals("=")) {
            expression.add(new NumSign(this.input.getText(), click.charAt(0)));
            this.tempNum = "";

        }

        if (click.equals("backspace")) {
            this.tempNum = this.tempNum.substring(0, this.tempNum.length() - 1);
        }

        if (click.equals("clear")) {
            this.expression.clear();
            this.input.setText("");
            this.tempNum = "";
        }

        // 顯示輸入框內容
        this.input.setText(tempNum);

        // 顯示結果框內容
        result = expression.toString();
        this.resultText.setText(result);



        // 計算結果
        if (click.equals("=")) {
            expression.calculate();
            this.resultText.setText(result + this.expression.get(0).getStrNum());
            this.expression.clear();
            this.result = "";
            this.tempNum = "";
        }
    }
}

Expression

package calc;

import java.util.LinkedList;

public class Expression extends LinkedList<NumSign> {
    @Override
    public String toString() {
        String str = "";
        for (NumSign numSign : this) {
            str += numSign.getStrNum() + numSign.getSign();
        }
        /*if (this.size() != 0) {
            return this.get(0).getStrNum();
        }*/

        return str;
    }

    public double calculate() {
        this.calMulAndDiv();
        this.calAddAndSub();
        return this.get(0).getNum();
    }

    private void calMulAndDiv() {
        for (int i = 0; i < this.size(); i++) {
            if (this.size() == 1)   return;
            if (this.get(i).getSign() == '*') {
                double temp = this.get(i).getNum() * this.get(i + 1).getNum();
                this.get(i + 1).setNum(temp);
                this.remove(i);
                i = -1;
                continue;
            }
            if (this.get(i).getSign() == '/') {
                double temp = this.get(i).getNum() / this.get(i + 1).getNum();
                this.get(i + 1).setNum(temp);
                this.remove(i);
                i = -1;
                continue;
            }
        }
    }

    private void calAddAndSub() {
        for (int i = 0; i < this.size(); i++) {
            if (this.size() == 1) return;
            if (this.get(i).getSign() == '+') {
                double temp = this.get(i).getNum() + this.get(i + 1).getNum();
                this.get(i + 1).setNum(temp);
                this.remove(i);
                i = -1;
                continue;
            }
            if (this.get(i).getSign() == '-') {
                double temp = this.get(i).getNum() - this.get(i + 1).getNum();
                this.get(i + 1).setNum(temp);
                this.remove(i);
                i = -1;
                continue;
            }
        }
    }
}

NumSign

package calc;

public class NumSign {
    private String strNum;
    private char sign;

    public NumSign() {
    }

    public NumSign(String strNum, char sign) {
        this.strNum = strNum;
        this.sign = sign;
    }

    public String getStrNum() {
        return strNum;
    }

    public void setStrNum(String strNum) {
        this.strNum = strNum;
    }

    public char getSign() {
        return sign;
    }

    public void setSign(char sign) {
        this.sign = sign;
    }

    public double getNum() {
        return Double.parseDouble(this.strNum);
    }

    public void setNum(double num) {
        this.strNum = String.valueOf(num);
    }

    @Override
    public String toString() {
        return "NumSign{" +
                "strNum='" + strNum + '\'' +
                ", sign=" + sign +
                '}';
    }
}

相關文章