import java.awt.Color; import java.awt.FlowLayout; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.util.ArrayList; import java.util.EmptyStackException; import java.util.List; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JSeparator; import javax.swing.JTextField; import javax.swing.event.MenuEvent; import javax.swing.event.MenuListener; public class MyCaculatorUseStacks implements MenuListener, ActionListener { JPanel mainp,p1,p2,p3,p4; JTextField jt1; JTextField jt2; JMenuItem mnuCopy; JMenuItem mnuPaste; JButton bM; boolean isDouble = false;// 是否为实数 double n3; MyStack<Integer> opPool = new MyStack<Integer>();//操作符堆栈 MyStack<Double> opArg = new MyStack<Double>(); StringBuffer tempsbf =new StringBuffer(); StringBuffer buf = new StringBuffer(); StringBuffer copyBoard = new StringBuffer(40);// 剪贴板 StringBuffer memory = new StringBuffer(40);// M系列 StringBuffer str = new StringBuffer();//缓冲器 public MyCaculatorUseStacks() { p1=new JPanel(); p1.setLayout(new GridLayout(2,1,10,10)); JMenuBar mnuNotepad=new JMenuBar(); JMenu mnuEdit=new JMenu("编辑(E)"); mnuEdit.setMnemonic(KeyEvent.VK_E); JMenu mnuCheck=new JMenu("查看(V)"); mnuCheck.setMnemonic(KeyEvent.VK_V); JMenu mnuHelp=new JMenu("帮助(H)"); mnuCopy=new JMenuItem("复制(C)"); mnuPaste=new JMenuItem("粘贴(P)"); JMenuItem mnuVisit=new JMenuItem("查看帮助(V)"); JMenuItem mnuAbout=new JMenuItem("关于计算器(A)"); JSeparator sep=new JSeparator(); jt1=new JTextField("0."); jt1.setHorizontalAlignment(JTextField.RIGHT); jt1.setEnabled(false); jt2 = new JTextField("运算过程表达式"); jt2.setHorizontalAlignment(JTextField.RIGHT); jt1.setSelectedTextColor(Color.yellow); jt1.setEditable(true); jt2.setSelectedTextColor(Color.red); jt2.setEditable(true); mnuEdit.addMenuListener(this); mnuCheck.addMenuListener(this); mnuHelp.addMenuListener(this); mnuCopy.addActionListener(this); mnuPaste.addActionListener(this); mnuVisit.addActionListener(this); mnuAbout.addActionListener(this); mnuNotepad.add(mnuEdit); mnuNotepad.add(mnuCheck); mnuNotepad.add(mnuHelp); mnuEdit.add(mnuCopy); mnuEdit.add(mnuPaste); mnuEdit.add(sep); mnuHelp.add(mnuVisit); mnuHelp.add(sep); mnuHelp.add(mnuAbout); p1.setLayout(new GridLayout(3,1)); p1.add(mnuNotepad); p1.add(jt1); p1.add(jt2); p2=new JPanel(); p2.setLayout(new FlowLayout(FlowLayout.CENTER)); bM = new JButton(" "); bM.setEnabled(false); p2.add(bM); JButton b25=new JButton("Backspace"); b25.addActionListener(this); JButton b26=new JButton("CE"); b26.addActionListener(this); JButton b27=new JButton("C"); b27.addActionListener(this); p2.add(b25); p2.add(b26); p2.add(b27); p3=new JPanel(); p3.setLayout(new GridLayout(4,6)); JButton button1=new JButton("MC"); button1.addActionListener(this); JButton button2=new JButton("7"); button2.addActionListener(this); JButton button3=new JButton("8"); button3.addActionListener(this); JButton button4=new JButton("9"); button4.addActionListener(this); JButton button5=new JButton("/"); button5.addActionListener(this); JButton button6=new JButton("sqrt"); button6.addActionListener(this); JButton button7=new JButton("MR"); button7.addActionListener(this); JButton button8=new JButton("4"); button8.addActionListener(this); JButton button9=new JButton("5"); button9.addActionListener(this); JButton button10=new JButton("6"); button10.addActionListener(this); JButton button11=new JButton("*"); button11.addActionListener(this); JButton button12=new JButton("%"); button12.addActionListener(this); JButton button13=new JButton("MS"); button13.addActionListener(this); JButton button14=new JButton("1"); button14.addActionListener(this); JButton button15=new JButton("2"); button15.addActionListener(this); JButton button16=new JButton("3"); button16.addActionListener(this); JButton button17=new JButton("-"); button17.addActionListener(this); JButton button18=new JButton("1/x"); button18.addActionListener(this); JButton button19=new JButton("M+"); button19.addActionListener(this); JButton button20=new JButton("0"); button20.addActionListener(this); JButton button21=new JButton("+/-"); button21.addActionListener(this); JButton button22=new JButton("."); button22.addActionListener(this); JButton button23=new JButton("+"); button23.addActionListener(this); JButton button24=new JButton("="); button24.addActionListener(this); p3.add(button1);p3.add(button2); p3.add(button3);p3.add(button4); p3.add(button5);p3.add(button6); p3.add(button7);p3.add(button8); p3.add(button9);p3.add(button10); p3.add(button11);p3.add(button12); p3.add(button13);p3.add(button14); p3.add(button15);p3.add(button16); p3.add(button17);p3.add(button18); p3.add(button19);p3.add(button20); p3.add(button21);p3.add(button22); p3.add(button23);p3.add(button24); p3.setSize(250,250); mainp=new JPanel(); mainp.setLayout(new GridLayout(3,1)); mainp.add(p1); mainp.add(p2); mainp.add(p3); JFrame f = new JFrame(); f.getContentPane().add(mainp); f.setTitle("计算器"); f.setSize(350,350); f.setVisible(true); f.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ System.exit(0); }}); } public void menuCanceled(MenuEvent e) { } public void menuDeselected(MenuEvent e) { } public void menuSelected(MenuEvent e) { } public void actionPerformed(ActionEvent e) { String s= e.getActionCommand(); if(s.equals("复制(C)")){ String temp = jt1.getText().trim(); copyBoard.replace(0, copyBoard.length(), temp); } else if(s.equals("粘贴(P)")){ jt1.setText(copyBoard.toString()); } else if(s.equals("查看帮助(V)")){ } else if(s.equals("关于计算器(A)")){ JOptionPane.showMessageDialog(null, "designed by YJQ!"); } else if(s.equals("Backspace")){ if (!jt1.getText().trim().equals("0.")) { // 如果文本框中有内容 str.setLength(0); str.append(jt1.getText().trim()); if(str.indexOf(".") == (str.length()-1)){ str.setLength(str.length()-1); } if (str.length() > 1){ if(str.indexOf(".")!= -1){ str.setLength(str.length() - 1); jt1.setText(str.toString()); System.out.println(str.toString()); } else { str.setLength(str.length()-1); jt1.setText(str.toString()+"."); System.out.println(str.toString()); } } else { jt1.setText("0."); str.setLength(0); } } } else if(s.equals("CE")){ jt1.setText("0."); jt1.setText("0."); } else if(s.equals("C")){ init(); } else if(s.equals("MC")){ // 如果是MC则清除缓冲区 memory.setLength(0); bM.setText(" "); } else if(s.equals("MR")){ // 如果按键为MR则恢复缓冲区的数到文本框 jt1.setText(memory.toString()); } else if(s.equals("MS")){ // 如果按键为MS则将文本框的数存入缓冲区 try{ n3 = Double.parseDouble(jt1.getText().trim()); memory.setLength(0); memory.append(n3); bM.setText("M"); } catch(Exception ex){ ex.printStackTrace(); JOptionPane.showMessageDialog(null, "文本不是实数,不能被存储!"); }; } else if(s.equals("M+")){ // 如果按键为MS则将文本框值与缓冲区的数相加但不显示结果 String temp1 = jt1.getText().trim(); double dtemp = Double.parseDouble(temp1); String temp2 = memory.toString(); dtemp += Double.parseDouble(temp2); temp1 = String.valueOf(dtemp); memory.replace(0, memory.length(), temp1); } else if(s.equals("sqrt")){ String temp = jt1.getText().trim(); double dtemp = Double.parseDouble(temp); if(dtemp >= 0){ tempsbf.replace(0, tempsbf.length(),("sqrt("+tempsbf+")")); dtemp = Math.sqrt(dtemp); str.setLength(0); str.append(dtemp); if(str.substring(str.length()-2, str.length()).equals(".0")){ str.setLength(str.length()-1); } jt1.setText(str.toString()); } else{ JOptionPane.showMessageDialog(null, "sqrt的操作数不能为负数"); }; } else if(s.equals("+/-")){ String temp = jt1.getText().trim(); double dtemp = Double.parseDouble(temp); tempsbf.replace(0, tempsbf.length(),("(-"+tempsbf+")")); dtemp = -dtemp; str.setLength(0); str.append(""+dtemp); jt1.setText(str.toString()); if(str.substring(str.length()-2, str.length()).equals(".0")){ str.setLength(str.length()-1); } jt1.setText(str.toString()); } else if(s.equals("%")){ if(isDouble){ double dtemp = Double.parseDouble(jt1.getText().trim())*100; jt1.setText(""+dtemp+"%"); } } else if(s.equals("1/x")){ String temp = jt1.getText().trim(); double dtemp = Double.parseDouble(temp); if(dtemp != 0){ tempsbf.replace(0, tempsbf.length(),"1/("+tempsbf+")"); dtemp = 1/dtemp; str.setLength(0); str.append(""+dtemp); if(str.substring(str.length()-2, str.length()).equals(".0")){ str.setLength(str.length()-1); } jt1.setText(str.toString()); } else{ JOptionPane.showMessageDialog(null, "1/x的操作数不能为0"); } } else if(s.equals("+")){ buf.replace(0, buf.length(),""+buf + tempsbf + "+"); tempsbf.setLength(0); if(str.length()!=0){ String temp = str.toString().trim(); double dtemp = Double.parseDouble(temp); opArg.push(dtemp); if (opPool.isEmpty()||opArg.getStackSize()< 2){ if(opPool.isEmpty()){ opArg.clear(); opArg.push(dtemp); opPool.push(1); } else { opPool.clear(); } jt1.setText("" + opArg.getTopObjcet()); } else { operate(opPool,opArg); opPool.push(1); } str.replace(0,str.length(),""+opArg.getTopObjcet()); if(str.substring(str.length()-2, str.length()).equals(".0")){ str.setLength(str.length()-1); } jt1.setText(str.toString()); str.setLength(0); } } else if(s.equals("-")){ buf.replace(0, buf.length(),""+buf+tempsbf + "-"); tempsbf.setLength(0); if(str.length()!= 0){ String temp = str.toString().trim(); double dtemp = Double.parseDouble(temp); if(opArg.getStackSize()<2){ opArg.push(dtemp); } if (opPool.isEmpty()||opArg.getStackSize()==1){ opPool.clear(); opPool.push(2); jt1.setText("" + opArg.getTopObjcet()); } else { operate(opPool,opArg); opPool.push(2); } str.replace(0,str.length(),""+opArg.getTopObjcet()); if(str.substring(str.length()-2, str.length()).equals(".0")){ str.setLength(str.length()-1); } jt1.setText(str.toString()); str.setLength(0); } } else if(s.equals("*")){ buf.replace(0, buf.length(),"("+buf+ tempsbf +")"+"*"); tempsbf.setLength(0); if(str.length() != 0){ String temp = str.toString().trim(); double dtemp = Double.parseDouble(temp); if(opArg.getStackSize()<2){ opArg.push(dtemp); } if (opPool.isEmpty()||opArg.getStackSize()==1){ opPool.clear(); opPool.push(3); jt1.setText("" + opArg.getTopObjcet()); } else { operate(opPool,opArg); opPool.push(3); } str.replace(0,str.length(),""+opArg.getTopObjcet()); if(str.substring(str.length()-2, str.length()).equals(".0")){ str.setLength(str.length()-1); } jt1.setText(str.toString()); str.setLength(0); } } else if(s.equals("/")){ buf.replace(0, buf.length(), "("+buf+tempsbf+")/"); tempsbf.setLength(0); if(str.length() != 0){ String temp = str.toString().trim(); double dtemp = Double.parseDouble(temp); if(opArg.getStackSize()<2){ opArg.push(dtemp); } if (opPool.isEmpty()||opArg.getStackSize()==1){ opPool.clear(); opPool.push(4); jt1.setText("" + opArg.getTopObjcet()); } else { operate(opPool,opArg); opPool.push(4); } str.replace(0,str.length(),""+opArg.getTopObjcet()); if(str.substring(str.length()-2, str.length()).equals(".0")){ str.setLength(str.length()-1); } jt1.setText(str.toString()); str.setLength(0); } } else if(s.equals(".")){ if(str.length()!= 0 && str.toString().trim().indexOf(".") == -1 ){ str.append(s); tempsbf.append(s); jt1.setText(str.toString()); } else if(str.length()== 0){ str.append("0"+s); tempsbf.append("0"+s); } isDouble = true; } else if(s.equals("=")){ if(str.length()!= 0){ double dtemp = Double.parseDouble(str.toString().trim()); opArg.push(dtemp); if (opPool.isEmpty()||opArg.getStackSize()< 2){ if(opPool.isEmpty()){ opArg.clear(); opArg.push(dtemp); } opPool.clear(); jt1.setText("" + opArg.getTopObjcet()); } else { operate(opPool,opArg); } buf.append(tempsbf.toString().trim()); tempsbf.setLength(0); System.out.println(buf+"="+opArg.getTopObjcet()); jt2.setText(buf.toString()+ "="+opArg.getTopObjcet()); buf.setLength(0); str.replace(0,str.length(),""+opArg.getTopObjcet()); if(str.substring(str.length()-2, str.length()).equals(".0")){ str.setLength(str.length()-1); } jt1.setText(str.toString()); str.setLength(0); } } // 如果选择的是"0"这个数字键 else if (s.equals("0")){ if (str.length()== 0){ str.append(s); tempsbf.append(s); } else if(str.toString().equals("0")){ } else if(str.toString().equals("0.")){ if(isDouble){ //为浮点数时 0加到 str 中去 jt1.setText(str.append(s).toString()); tempsbf.append(s); } } else{ jt1.setText(str.append(s).toString()); tempsbf.append(s); } } else{ str.append(s); tempsbf.append(s); if(str.toString().indexOf(".") != -1){ jt1.setText(str.toString()); } else jt1.setText(str.toString()+"."); } } private void operate(MyStack<Integer> opPool,MyStack<Double> opArg){ double opRight = opArg.pop(); double opLeft = opArg.pop(); switch (opPool.pop()) { case 1: opLeft += opRight; break; case 2: opLeft -= opRight; break; case 3: opLeft *= opRight; break; case 4: if(opRight != 0) opLeft /= opRight; else JOptionPane.showMessageDialog(null, "除数不能为0"); break; } opArg.push(opLeft); } private void init() { isDouble = false; jt1.setText("0."); jt2.setText("0."); str.setLength(0); buf.setLength(0); tempsbf.setLength(0); opPool.clear(); opArg.clear(); } public static void main(String args[]){ new MyCaculatorUseStacks(); } private class MyStack<Object>{ private List<Object> pool= new ArrayList<Object>(); public MyStack(){ } public void clear() { pool.clear(); } public boolean isEmpty(){ return pool.isEmpty(); } /** * 获取栈顶元素 * */ public Object getTopObjcet() { if (isEmpty()) return null; else return pool.get(0); } /** * 弹出栈操作 * */ public Object pop(){ if (isEmpty()) {throw new EmptyStackException();} return pool.remove(pool.size() - 1); } /** * 压入栈操作 * */ public void push(Object i){ pool.add(i); } /** * 获取当前栈大小 * */ public int getStackSize(){ if (isEmpty()) return 0; else return pool.size(); } } }