Java四则混合运算 图形化计算器

最近在学习数据结构,学到Stack栈,想到用栈求表达式的值.为了看起来更好看一点,就写了一个GUI界面.Java四则混合运算 图形化计算器_第1张图片

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 ops = new Stack();
	Stack num = new Stack();
	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;

	}
}

 

 

 

 

 

你可能感兴趣的:(数据结构,Java)