标签:key tde import static bounds etl getc 简单 oca
package Calculator_JCoder; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.text.DecimalFormat; import javax.swing.*; import java.util.*; import java.math.*; public class Calculator extends JFrame implements ActionListener { static Font F = new Font("宋体",Font.BOLD,25); static Font _F = new Font("宋体",Font.BOLD,40); static DecimalFormat DF = new DecimalFormat("0.0000000000"); static String IN = ""; static JTextField Text_Res = new JTextField("0"); static JTextField Text_Now = new JTextField(""); static JButton Keys[] = new JButton[21]; static String KeysName[] = { "7", "8", "9", "4", "5", "6", "1", "2", "3", "0", ".", "+", "-", "*", "/", "=", "CE", "(", ")", "^", "C" }; static int CMP(char a){ if(a == ‘#‘){return 0;} if(a == ‘(‘){return 1;} if(a == ‘+‘ || a == ‘-‘){return 3;} if(a == ‘*‘ || a == ‘/‘){return 4;} if(a == ‘^‘){return 5;} if(a == ‘)‘){return 6;} return -1; } static String Change(String in){ in += "#"; int L = in.length(); String t = ""; String NPR = ""; Stack<Character> S = new Stack(); for(int i = 0;i < L;i ++){ if(in.charAt(i) >= ‘0‘ && in.charAt(i) <= ‘9‘){t += in.charAt(i);} else if(in.charAt(i) == ‘.‘){t += in.charAt(i);} else if(in.charAt(i) == ‘#‘){ if(t.length() != 0){NPR += t + " ";} t = ""; } else if(in.charAt(i) == ‘+‘ || in.charAt(i) == ‘-‘ || in.charAt(i) == ‘*‘ || in.charAt(i) == ‘/‘ || in.charAt(i) == ‘^‘){ if(t.length() != 0){NPR += t + " ";} t = ""; if(S.size() == 0){ S.push(in.charAt(i)); } else if(CMP(S.peek()) < CMP(in.charAt(i))){ S.push(in.charAt(i)); } else if(CMP(S.peek()) >= CMP(in.charAt(i))){ NPR += S.peek() + " "; S.pop(); S.push(in.charAt(i)); } } else if(in.charAt(i) == ‘(‘){ if(t.length() != 0){NPR += t + " ";} t = ""; S.push(in.charAt(i)); } else if(in.charAt(i) == ‘)‘){ if(t.length() != 0){NPR += t + " ";} t = ""; while(S.peek() != ‘(‘){ NPR += S.peek() + " "; S.pop(); } S.pop(); } } while(S.size() != 0){ NPR += S.peek() + " "; S.pop(); } //System.out.println(NPR); return NPR; } static double Solve(double a,double b,char c){ double out = 0.0; if(c == ‘+‘){ out = a + b; } else if(c == ‘-‘){ out = a - b; } else if(c == ‘*‘){ out = a * b; } else if(c == ‘/‘){ out = a / b; } else if(c == ‘^‘){ out = Math.pow(a,b); } return out; } static double Cal(String now){ String Sp[] = now.split("\\ "); Stack<Double> S = new Stack(); for(int i = 0;i < Sp.length;i ++){ if(Sp[i].length() == 0){continue;} if(Sp[i].charAt(0) <= ‘9‘ && Sp[i].charAt(0) >= ‘0‘){ S.push(Double.valueOf(Sp[i])); } else{ double b = S.peek();S.pop(); double a = S.peek();S.pop(); double c = Solve(a,b,Sp[i].charAt(0)); S.push(c); } } double ans = S.peek(); return ans; } static String SetS(int x){ if(x == 1){return "+";} if(x == 2){return "-";} if(x == 3){return "*";} if(x == 4){return "/";} return "0"; } public void init() { JPanel KeysP = new JPanel(); KeysP.setLayout(null); KeysP.setSize(500,500); for(int i = 0;i <= 20;i ++){ Keys[i] = new JButton(KeysName[i]); KeysP.add(Keys[i]); Keys[i].setFont(F); } Keys[0].setBounds(20,20,60,60); Keys[1].setBounds(85,20,60,60); Keys[2].setBounds(150,20,60,60); Keys[3].setBounds(20,85,60,60); Keys[4].setBounds(85,85,60,60); Keys[5].setBounds(150,85,60,60); Keys[6].setBounds(20,150,60,60); Keys[7].setBounds(85,150,60,60); Keys[8].setBounds(150,150,60,60); Keys[9].setBounds(20,215,125,60); Keys[10].setBounds(150,215,60,60); Keys[11].setBounds(215,20,60,60); Keys[12].setBounds(280,20,60,60); Keys[13].setBounds(215,85,60,60); Keys[14].setBounds(280,85,60,60); Keys[15].setBounds(215,150,125,60); Keys[16].setBounds(215,215,125,60); Keys[17].setBounds(345,20,60,60); Keys[18].setBounds(345,85,60,60); Keys[19].setBounds(345,150,60,60); Keys[20].setBounds(345,215,60,60); Text_Res.setHorizontalAlignment(JTextField.RIGHT); Text_Now.setHorizontalAlignment(JTextField.RIGHT); Text_Res.setEditable(false); Text_Now.setEditable(false); Text_Res.setBackground(Color.WHITE); Text_Now.setBackground(Color.WHITE); JPanel TextP = new JPanel(); TextP.setLayout(null); TextP.setSize(500,100); TextP.add(Text_Res); Text_Res.setBounds(20, 60, 385, 60); TextP.add(Text_Now); Text_Now.setBounds(20, 20, 385, 40); Text_Res.setFont(_F); Text_Now.setFont(F); JPanel BigP = new JPanel(); BigP.setSize(800,600); BigP.setLayout(null); BigP.add(KeysP); BigP.add(TextP); KeysP.setBounds(0, 120, 600, 600); TextP.setBounds(0, 0, 500, 200); getContentPane().setLayout(null); getContentPane().add(BigP); for (int i = 0; i <= 20; i ++) { Keys[i].addActionListener(this); } } public void actionPerformed(ActionEvent e) { String Get = e.getActionCommand(); if("0123456789.+-*/^()".indexOf(Get) >= 0){ IN = IN + Get; Text_Now.setText(IN); } else if("=".indexOf(Get) >= 0){ double show = Cal(Change(IN)); //System.out.println(show); //System.out.println(show); String t1 = String.valueOf(show); String t2 = String.valueOf(DF.format(show)); Text_Res.setText(t1); IN = t2; } else if("CE".compareTo(Get) == 0){ int L = IN.length(); if(L != 0){IN = IN.substring(0,L - 1);} Text_Now.setText(IN); } else if("C".compareTo(Get) == 0){ IN = ""; Text_Now.setText(IN); Text_Res.setText("0"); } } public Calculator(){ super(); init(); this.setTitle("Calculator By-J_Coder"); this.setResizable(false); this.setLocation(100,100); this.setSize(440,450); } public static void main(String[] args) { Calculator window = new Calculator(); window.setVisible(true); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }
标签:key tde import static bounds etl getc 简单 oca
原文地址:https://www.cnblogs.com/love-fromAtoZ/p/9704113.html