思路

查看代码注解

实现

代码

package calculator;
import javax.swing.*;
import java.awt.*;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Stack;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class calculator extends JFrame implements ActionListener {
	/**
 * 
 */
private static final long serialVersionUID = 1L;
	String[] buttonNum= {"7","8","9","+","4","5","6","-","1","2","3","*",".","0","=","/"};
	JButton key[]=new JButton[16];
	JTextField jtf;
	String input="";
	JPanel jp1,jp2,jp3;
	JButton jb1,jb2;
	
public static void main(String[] args) {
	new calculator();

}
public calculator() {
	Container cr=getContentPane();
	jp1=new JPanel();
	jp2=new JPanel();
	jp3=new JPanel();
	jb1=new JButton("C");
	jb2=new JButton("BS");
	GridLayout gl=new GridLayout(4,4,5,5);
	jp1.setLayout(new BorderLayout());
	jp2.setLayout(gl);
		
	jp3.setLayout(new GridLayout(1,2,5,5));
	
		jtf=new JTextField();
	jtf.setPreferredSize(new Dimension(380,50));
	jtf.setHorizontalAlignment(SwingConstants.LEFT);
	cr.add(jp1, BorderLayout.NORTH);
	jp1.add(jtf,BorderLayout.WEST);
	jp1.add(jp3,BorderLayout.EAST);
	jp3.add(jb1);
	jp3.add(jb2);
	jb1.setBackground(Color.LIGHT_GRAY);
	jb2.setBackground(Color.LIGHT_GRAY);
	jb1.addActionListener(this);
	jb2.addActionListener(this);
	for (int i=0;i<buttonNum.length;i++) {
		key[i]=new JButton(buttonNum[i]);
		jp2.add(key[i]);
		key[i].addActionListener(this);
	}
	cr.add(jp2,BorderLayout.CENTER);
	this.setVisible(true);
	this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	this.setSize(500,450);
	this.setLocation(300, 300);
	this.setTitle("Davis简易计算器");
			
}
@Override
public void actionPerformed(ActionEvent e) {
	int p=0;
	String k=e.getActionCommand();
	if(k.equals("+")||k.equals("-")||k.equals("*")||k.equals("/")) {
//			遇到运算符,就在运算符前后分别加上一个空格,为后面的分界字符串做准备.
		input+=" "+k+" ";
	}else if (k.equals("C")) {
		input="";
	}else if(k.equals("BS")) {
//	检测字符窜的最后一个字符是否为空格空格
		if((input.charAt(input.length()-1))==' ') {
//				如果是删除末尾3个字否则删除1个字符字符
			input=input.substring(0, input.length()-3);
			
		}else {
			input=input.substring(0,input.length()-1);
		}
	}
	else if(k.equals("=")) {
		input=compute(input);
		jtf.setText(input);
		input="";
		p=1;
	}else {
		input+=k;
		if(p==0) {
			jtf.setText(input);
			
		}
	}
}
	private String compute(String st) {
		String array[];
		array=st.split(" ");
		Stack<Double> w=new Stack<Double>();
		Double a=Double.parseDouble(array[0]);
		w.push(a);
		for (int i=1;i<array.length;i++) {
			if (i%2==1) {
				if(array[i].compareTo("+")==0) {
					double b=Double.parseDouble(array[i+1]);
					double c=w.pop();
					c*=b;
					w.push(c);
				}
				if (array[i].compareTo("/")==0) {
					double b=Double.parseDouble(array[i+1]);
					double c=w.peek();
					w.pop();
					c/=b;
					w.push(c);
				}
			}
		}
		double sum=0;
		while(!w.isEmpty()) {
			sum+=w.pop();
		}
		String result=String.valueOf(sum);
		return result;
	}
	
}