完美计算器(用栈解决运算优先级问题)import java.applet.*;import java.awt.*;import java.util.*;public class Calculator extends Applet{public void init(){setLayout(new BorderLayout());Panel p1=new Panel();p1.setBackground(Color.gray);//设置p1的背景色display=new ExpTextField( " ",30);p1.add(display);p1.add(new Button( "清空"));add( "North ",p1);Panel p2=new Panel();p2.setLayout(new GridLayout(3,6));//网格布局p2.add(new Button( "7 "));p2.add(new Button( "8 "));p2.add(new Button( "9 "));p2.add(new Button( "( "));p2.add(new Button( ") "));p2.add(new Button( "= "));p2.add(new Button( "4 "));p2.add(new Button( "5 "));p2.add(new Button( "6 "));p2.add(new Button( ". "));p2.add(new Button( "+ "));p2.add(new Button( "- "));p2.add(new Button( "1 "));p2.add(new Button( "2 "));p2.add(new Button( "3 "));p2.add(new Button( "0 "));p2.add(new Button( "* "));p2.add(new Button( "/ "));add( "Center ",p2);}public boolean action(Event e,Object a)//按钮事件的action方法{if(a instanceof String){if(a.equals( "清空"))//清空{display.setText( " ");return true;}display.setText(display.getText()+(String)a);if(a.equals( "= "))//当用户按下=键开始计算display.setText(EvaluateExpression.calculate(display.getText()));return true;}elsereturn super.action(e,a);//如果事件没有处理,将事件传输到该类在继承体系中父类去处理}private ExpTextField display;}class ExpTextField extends TextField{public ExpTextField(String s,int n){super(s,n);}public boolean keyDown(Event e,int key){if(key==61)//=键{setText(getText()+ "= ");setText(EvaluateExpression.calculate(getText()));return true;}else if(key==27)//Esc键{setText( " ");return true;}else if((key> =40)&&(key <=57)&&(key!=44)//数字或运算符||(key==8)||(key==127)//Backspace键或delete键||(key==Event.LEFT)||(key==Event.RIGHT)//左右键||(key==Event.HOME)||(key==Event.END))//Home键或End键return super.keyDown(e,key);else //忽略其它键return true;}}class EvaluateExpression //算符优先算法{public static String calculate(String exp){char lastoptr= '= ';char c;char ch;double d1;//操作数double d2;//操作数String opnd= " ";String result;Stack optrstack=new Stack();//运算符栈Stack opndstack=new Stack();//操作数栈optrstack.push(new Character( '= '));try{for(int i=0;i <exp.length();i++){c=exp.charAt(i);//输入的字符if(Character.isDigit(c)||(c== '. '))//操作数{opnd=opnd+c;}else if((c== '+ ')||(c== '- ')||(c== '* ')||(c== '/ ')||(c== '( ')||(c== ') ')||(c== '= '))//运算符{if((c== '- ')&&opnd.equals( " ")&&((lastoptr== '= ')||(lastoptr== '( ')))c= '@ ';//c为负号if(!opnd.equals( " ")){opndstack.push(new Double(opnd));//进操作数栈opnd= " ";}while(getF(((Character)optrstack.peek()).charValue())> getG(c))//比较优先权{ch=((Character)optrstack.pop()).charValue();if(ch== '@ ')//一个操作数的情况{d1=((Double)opndstack.pop()).doubleValue();opndstack.push(new Double(-d1));}else//两个操作数的情况{d2=((Double)opndstack.pop()).doubleValue();d1=((Double)opndstack.pop()).doubleValue();opndstack.push(new Double(cal(d1,d2,ch)));}}if(getF(((Character)optrstack.peek()).charValue()) <getG(c))//比较优先权{optrstack.push(new Character(c));lastoptr=c;continue;}if(getF(((Character)optrstack.peek()).charValue())==getG(c))//比较优先权{optrstack.pop();lastoptr=c;continue;}}elsereturn( "ERROR ");}result=((Double)opndstack.pop()).toString();if((!optrstack.empty())||(!opndstack.empty()))result= "ERROR ";}catch(NumberFormatException e){result= "ERROR ";}catch(ArithmeticException e){result= "ERROR ";}return result;}private static int getF(char c)//运算符栈顶元素优先函数{switch(c){case '+ ':case '- ': return 2;case '* ':case '/ ': return 4;case '@ ': return 5;case '( ': return 0;case ') ': return 6;case '= ': return 0;default: return -1;}}private static int getG(char c)//输入的运算符优先函数{switch(c){case '+ ':case '- ': return 1;case '* ':case '/ ': return 3;case '@ ': return 6;case '( ': return 7;case ') ': return 0;case '= ': return 0;default: return -1;}}private static double cal(double d1,double d2,char c) {switch(c){case '+ ': return(d1+d2);case '- ': return(d1-d2);case '* ': return(d1*d2);case '/ ': return(d1/d2);default: return -1;}}}。