package yjq.com.cn; 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 MyCaculator implements MenuListener, ActionListener { JPanel mainp,p1,p2,p3,p4; JTextField jt1; JMenuItem mnuCopy; JMenuItem mnuPaste; JButton bM; boolean isDouble = false;// 是否为实数 double op1 = 0, op2 = 0;// 操作数1,操作数2 double n3; boolean op2IsEmpty = true; boolean op1IsEmpty = true; MyStack<Integer> opPool = new MyStack<Integer>();//操作符堆栈 StringBuffer buf = new StringBuffer(20); StringBuffer copyBoard = new StringBuffer(20);// 剪贴板 StringBuffer memory = new StringBuffer(20);// M系列 StringBuffer str = new StringBuffer();//缓冲器 public MyCaculator() { 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.setSelectedTextColor(Color.yellow); jt1.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.add(mnuNotepad); p1.add(jt1); 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.length() != 1 && str.length() != 0) { str.setLength(str.length() - 1); jt1.setText(str.toString()); } else { jt1.setText("0."); str.setLength(0); } } } else if(s.equals("CE")){ jt1.setText("0."); } else if(s.equals("C")){ isDouble = false; jt1.setText("0."); op1 = op2 = 0; str.replace(0,str.length(),""); opPool.clear(); op2IsEmpty = true; op1IsEmpty = true; } 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){ jt1.setText("" + Math.sqrt(dtemp)); str.setLength(0); str.append(jt1.getText().trim()); op2 = Double.parseDouble(jt1.getText().trim()); op1 = op2; op1IsEmpty = false; op2IsEmpty = true; } else{ JOptionPane.showMessageDialog(null, "sqrt的操作数不能为负数"); }; } else if(s.equals("+/-")){ op2 = Double.parseDouble(jt1.getText().trim()); if(op1IsEmpty) { op1 = -op2; op1IsEmpty = false; op2 = 0; op2IsEmpty = true; jt1.setText("" + op1); } else{ op2 = -op2; jt1.setText("" + op2); } } else if(s.equals("%")){ if(isDouble){ n3 = Double.parseDouble(jt1.getText().trim())*100; jt1.setText(""+n3+"%"); } } else if(s.equals("1/x")){ String temp = jt1.getText().trim(); double dtemp = Double.parseDouble(temp); op2 = dtemp; if(op2 != 0){ op2 = 1/op2; str.setLength(0); str.append("" + op2); jt1.setText(str.toString()); op1 = op2; op2IsEmpty = true; } else{ JOptionPane.showMessageDialog(null, "1/x的操作数不能为0"); } } else if(s.equals("+")){ if(op1IsEmpty) { op1 = op2; op1IsEmpty = false; op2IsEmpty = true; } if (opPool.isEmpty()||op2IsEmpty){ opPool.clear(); opPool.push(1); jt1.setText("" + op1); } else { switch (opPool.pop()) { case 1: op1 += op2; jt1.setText("" + op1); break; case 2: op1 -= op2; jt1.setText("" + op1); break; case 3: op1 *= op2; jt1.setText("" + op1); break; case 4: if(op2 != 0) op1 /= op2; else JOptionPane.showMessageDialog(null, "除数不能为0"); jt1.setText("" + op1); break; } opPool.push(1); op2IsEmpty= true; } str.setLength(0); } else if(s.equals("-")){ if(op1IsEmpty&&!op2IsEmpty) { op1 = op2; op1IsEmpty = false; op2 = 0; op2IsEmpty = true; } if (opPool.isEmpty()||op2IsEmpty){ opPool.clear(); opPool.push(2); jt1.setText("" + op1); } else { switch (opPool.pop()) { case 1: op1 += op2; jt1.setText("" + op1); break; case 2: op1 -= op2; jt1.setText("" + op1); break; case 3: op1 *= op2; jt1.setText("" + op1); break; case 4: if(op2 != 0) op1 /= op2; else JOptionPane.showMessageDialog(null, "除数不能为0"); jt1.setText("" + op1); break; } opPool.push(2); op2 = 0; op2IsEmpty = true; } str.setLength(0); } else if(s.equals("*")){ if(op1IsEmpty&&!op2IsEmpty) { op1 = op2; op1IsEmpty = false; op2 = 0; op2IsEmpty = true; } str.setLength(0); if (opPool.isEmpty()||op2IsEmpty){ opPool.clear(); opPool.push(3); jt1.setText("" + op1); } else { switch (opPool.pop()) { case 1: op1 += op2; jt1.setText("" + op1); break; case 2: op1 -= op2; jt1.setText("" + op1); break; case 3: op1 *= op2; jt1.setText("" + op1); break; case 4: if(op2 != 0) op1 /= op2; else JOptionPane.showMessageDialog(null, "除数不能为0"); jt1.setText("" + op1); break; } opPool.push(3); op2 = 0; op2IsEmpty = true; } str.setLength(0); } else if(s.equals("/")){ if(op1IsEmpty&&!op2IsEmpty) { op1 = op2; op1IsEmpty = false; op2 = 0; op2IsEmpty = true; } if (opPool.isEmpty()||op2IsEmpty){ opPool.clear(); opPool.push(4); jt1.setText("" + op1); } else { switch (opPool.pop()) { case 1: op1 += op2; jt1.setText("" + op1); break; case 2: op1 -= op2; jt1.setText("" + op1); break; case 3: op1 *= op2; jt1.setText("" + op1); break; case 4: if(op2 != 0) op1 /= op2; else JOptionPane.showMessageDialog(null, "除数不能为0"); jt1.setText("" + op1); break; } opPool.push(4); op2IsEmpty = true; } str.setLength(0); } else if(s.equals(".")){ if(jt1.getText().trim().indexOf(".") == -1 ){ str.append(s); jt1.setText(str.toString()); } isDouble = true; } else if(s.equals("=")){ if(op1IsEmpty&&!op2IsEmpty) { op1 = op2; op1IsEmpty = false; op2 = 0; op2IsEmpty = true; } if (opPool.isEmpty()||op2IsEmpty){ opPool.clear(); jt1.setText("" + op1); } else { switch (opPool.pop()) { case 1: op1 += op2; jt1.setText("" + op1); break; case 2: op1 -= op2; jt1.setText("" + op1); break; case 3: op1 *= op2; jt1.setText("" + op1); break; case 4: if(op2 != 0) op1 /= op2; else JOptionPane.showMessageDialog(null, "除数不能为0"); jt1.setText("" + op1); break; } op2 = 0; op2IsEmpty = true; } str.setLength(0); } else if (s.equals("0")){ // 如果选择的是"0"这个数字键 if (jt1.getText().trim().equals("0.")) { str.replace(0,str.length(),"0."); if(isDouble){ //为浮点数时 0加到 str 中去 jt1.setText(str.append(s).toString()); op2 = Double.parseDouble(str.toString().trim()); op2IsEmpty = false; } else{ } } else { jt1.setText(str.append(s).toString()); op2 = Double.parseDouble(str.toString().trim()); op2IsEmpty = false; } } else{ jt1.setText(str.append(s).toString()); op2 = Double.parseDouble(str.toString().trim()); op2IsEmpty = false; } }
public static void main(String args[]){ new MyCaculator(); } }
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;} 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()) {throw new EmptyStackException();} return pool.size(); } }