JButton btn0 = new JButton(); JButton btn1 = new JButton(); JButton btn2 = new JButton(); JButton btn3 = new JButton(); JButton btn4 = new JButton(); JButton btn5 = new JButton(); JButton btn6 = new JButton(); JButton btn7 = new JButton(); JButton btn8 = new JButton(); JButton btn9 = new JButton();
//特殊按钮 JButton btnClear = new JButton(); //清除 JButton btnEqual = new JButton(); //等于 JButton btnPlus = new JButton(); //+操作符 JButton btnMinus = new JButton(); //-操作符 JButton btnMultiply = new JButton(); //*操作符 JButton btnDivide = new JButton(); //除操作符
class IllegalExpressionException extends Exception{ public IllegalExpressionException(String exp){ super(exp); } }
public class expression{ public String exp = null, texp = null; public String op1, op2; public String operator;
public expression(String exp){ this.exp = new String(exp); texp = new String(exp); op1 = new String(); op2 = new String(); operator = new String(); }
public void setExpression(String exp){ this.exp = new String(exp); texp = new String(exp); op1 = new String(); op2 = new String(); operator = new String(); }
public String getExpression(){ return exp.toString(); }
private void getOp1(){ int index = 0; for(index=0; index<texp.length(); index++) { char c = texp.charAt(index); if ((c>='0' && c<='9') || (c=='.') || (c=='e')|| (c=='E')) {} else break; }
operator = operator.trim(); if (operator.length()!=1) { throw new IllegalExpressionException("错误的运算符"); }
c= operator.charAt(0);
switch(c){ case '+': re = dop1 + dop2; break; case '-': re = dop1 - dop2; break; case '*': re = dop1 * dop2; break; case '/': re = dop1 / dop2; break; default: throw new IllegalExpressionException("错误的运算符"); }
return re; }
public static void main(String[] argvs){
expression e = new expression("3.1 + 4+"); try{ System.out.println(e.getResult()); System.out.println(e.op1); System.out.println(e.operator); System.out.println(e.op2);