Java四則混合運算 圖形化計算器
阿新 • • 發佈:2019-02-15
最近在學習資料結構,學到Stack棧,想到用棧求表示式的值.為了看起來更好看一點,就寫了一個GUI介面.
package MyCalculator; import java.awt.BorderLayout; import java.awt.Container; import java.awt.Dimension; import java.awt.Font; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.border.Border; public class MyCalc { public static void main(String[] args) { new myFrame("TWY計算器"); } } class myFrame extends JFrame { JTextField text = new JTextField(); JButton[] bt = new JButton[20]; String key[] = new String[] { "ans", "del", "EC", "C", "1", "2", "3", "+", "4", "5", "6", "-", "7", "8", "9", "*", "0", ".", "=", "/" }; JPanel keyPanel = new JPanel(); boolean finish = false; double ans=0; public myFrame(String title) { super(title); init(); } public void init() { text.setHorizontalAlignment(JTextField.RIGHT); text.setPreferredSize(new Dimension(0, 40)); text.setFont(new Font("宋體", Font.BOLD, 20)); for (int i = 0; i < bt.length; i++) { bt[i] = new JButton(key[i]); } Container pane = this.getContentPane(); this.add(text, BorderLayout.NORTH); keyPanel.setLayout(new GridLayout(5, 4, 3, 3)); this.add(keyPanel, BorderLayout.CENTER); for (int i = 0; i < bt.length; i++) { keyPanel.add(bt[i]); bt[i].addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (finish == true) { text.setText(""); finish = false; } String command = e.getActionCommand(); String s = text.getText(); if ("0123456789.+-*/".indexOf(command) >= 0) text.setText(text.getText() + command); else if (command.equals("del")) text.setText(s.substring(0, s.length() - 1)); else if (command.equals("C") || command.equals("EC")) text.setText(""); else if (command.equals("ans") ) text.setText(text.getText() + ans); else if (command.equals("=")) { ans=new calc(s).result; text.setText(text.getText() + "="+ans); finish = true; } } }); } this.setVisible(true); this.setLocation(500, 200); this.setSize(325, 325); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }
package MyCalculator; import java.util.Stack; class calc { Stack<String> ops = new Stack<String>(); Stack<Double> num = new Stack<Double>(); String exp; double result; public calc(String s) { exp = s; calctor(); } public void calctor() { String s[] = tranform(exp).split(" "); for (String string : s) { if (string.matches("\\d+") || string.matches("\\d+.\\d+")) { double n = Double.parseDouble(string); num.push(n); } else if (string.equals("+") || string.equals("-") || string.equals("/") || string.equals("*")) { double op1 = num.pop(), op2 = num.pop(); switch (string) { case "+": num.push(op1 + op2); break; case "-": num.push(op2 - op1); break; case "*": num.push(op2 * op1); break; case "/": num.push(op2 / op1); break; default: break; } } else { } } result = num.peek(); // System.out.println("ans= " + result); } public String tranform(String s) { s = s.replace("+", " + "); s = s.replace("-", " - "); s = s.replace("*", " * "); s = s.replace("/", " / "); String str[] = s.split(" "); String s1 = ""; for (String string : str) { if (string.matches("\\d+") || string.matches("\\d+.\\d+")) { s1 = s1 + " " + string; } else { if (ops.isEmpty()) { ops.push(string); continue;// 跳過下面 } String temp = ops.peek(); while (priority(temp) >= priority(string)) {// 棧頂元素優先順序高於或等於進棧元素,不斷彈出棧頂元素 s1 = s1 + " " + temp; ops.pop(); if (ops.isEmpty()) break; temp = ops.peek(); } ops.push(string); } } while (!ops.isEmpty()) s1 = s1 + " " + ops.pop(); // System.out.println(s1); return s1; } public int priority(String s) { switch (s) { case "+": case "-": return 1; case "*": case "/": return 2; default: break; } return 0; } }