JAVA实现计算器
1 import java.awt.Color;
2 import java.awt.Container; 3 import java.awt.GridLayout; 4 import java.awt.TextField; 5 import java.awt.event.ActionEvent; 6 import java.awt.event.ActionListener; 7 8 import javax.jws.HandlerChain; 9 import javax.swing.JButton; 10 import javax.swing.JFrame; 11 import javax.swing.JPanel; 12 import javax.swing.JTextField; 13 14 import java.util.Stack; 15 16 public class Main extends JFrame implements ActionListener{ 17 18 private String expression;//记录表达式 19 20 //各个按键的字符 21 private final String[] keys = {"(",")","C","Backspace", 22 "7","8","9","+", 23 "4","5","6","-", 24 "1","2","3","*", 25 ".","0","=","/"}; 26 private JTextField result = new JTextField("0"); 27 28 //按键 29 private JButton[] button = new JButton[20]; 30 31 Main(){ 32 33 34 //计算器布局 35 JPanel calckeysPanel = new JPanel(); 36 //布局先分为六行,第一行为文本显示,二到五行为按键 37 calckeysPanel.setLayout(new GridLayout(6,1)); 38 39 calckeysPanel.add(result); 40 result.setFont(result.getFont().deriveFont((float)(30))); 41 Container[] containers = new Container[5]; 42 for (int i = 0;i < 5;++i){ 43 containers[i] = new Container(); 44 containers[i].setLayout(new GridLayout(1,4)); 45 calckeysPanel.add(containers[i]); 46 } 47 48 //按钮 49 for (int i = 0;i < button.length;++i){ 50 button[i] = new JButton(keys[i]); 51 button[i].setFont(button[i].getFont().deriveFont((float)(30))); 52 containers[i/4].add(button[i]); 53 button[i].addActionListener(this); 54 if (i == 2 || i == 3) button[i].setForeground(Color.red); 55 } 56 getContentPane().add(calckeysPanel); 57 58 //表达式赋值为空 59 expression = ""; 60 } 61 62 63 64 @Override 65 public void actionPerformed(ActionEvent e) { 66 // TODO Auto-generated method stub 67 String action = e.getActionCommand(); 68 69 70 if (action.equals(keys[2])){ 71 //用户按下C 72 handleC(); 73 }else if (action.equals(keys[3])){ 74 //用户按下退格 75 handleBackspace(); 76 }else if (action.equals(keys[18])){ 77 //用户按下= 78 handleCalc(); 79 }else{ 80 //用户输入表达式 81 handleExpression(action); 82 } 83 } 84 85 //处理按下C的事件 86 private void handleC(){ 87 expression = ""; 88 result.setText(""); 89 } 90 91 //处理按下退格的事件 92 private void handleBackspace(){ 93 expression = expression.substring(0,expression.length() - 1); 94 result.setText(expression); 95 } 96 97 //处理按下等号的事件 98 private void handleCalc(){ 99 result.setText(Calculator.calrp(Calculator.getrp(expression))); 100 expression = ""; 101 } 102 103 //处理用户按下数字或运算符的事件 104 private void handleExpression(String action){ 105 expression += action; 106 result.setText(expression); 107 } 108 109 public static void main(String[] args){ 110 Main calculator = new Main(); 111 calculator.setSize(600,800); 112 calculator.setVisible(true); 113 } 114 115 } 116 117 class Calculator { 118 //计算中缀表达式 119 static Stack<Character> op = new Stack<>(); 120 121 public static Float getv(char op, Float f1, Float f2){ 122 if(op == '+') return f2 + f1; 123 else if(op == '-') return f2 - f1; 124 else if(op == '*') return f2 * f1; 125 else if(op == '/') return f2 / f1; 126 else return Float.valueOf(-0); 127 } 128 129 public static String calrp(String rp){ 130 Stack<Float> v = new Stack<>(); 131 char[] arr = rp.toCharArray(); 132 int len = arr.length; 133 for(int i = 0; i < len; i++){ 134 Character ch = arr[i]; 135 136 if(ch >= '0' && ch <= '9') v.push(Float.valueOf(ch - '0')); 137 138 else v.push(getv(ch, v.pop(), v.pop())); 139 } 140 return v.pop().toString(); 141 } 142 143 public static String getrp(String s){ 144 char[] arr = s.toCharArray(); 145 int len = arr.length; 146 String out = ""; 147 148 for(int i = 0; i < len; i++){ 149 char ch = arr[i]; 150 if(ch == ' ') continue; 151 152 153 if(ch >= '0' && ch <= '9') { 154 out+=ch; 155 continue; 156 } 157 158 if(ch == '(') op.push(ch); 159 160 if(ch == '+' || ch == '-'){ 161 while(!op.empty() && (op.peek() != '(')) 162 out+=op.pop(); 163 op.push(ch); 164 continue; 165 } 166 167 if(ch == '*' || ch == '/'){ 168 while(!op.empty() && (op.peek() == '*' || op.peek() == '/')) 169 out+=op.pop(); 170 op.push(ch); 171 continue; 172 } 173 174 if(ch == ')'){ 175 while(!op.empty() && op.peek() != '(') 176 out += op.pop(); 177 op.pop(); 178 continue; 179 } 180 } 181 while(!op.empty()) out += op.pop(); 182 return out; 183 } 184 }
用Java实现的简易计算器
运行效果图如下: