问题描述
javacodepackagecounter;importjava.util.Stack;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjavax.swing.JButton;importjavax.swing.JFrame;importjavax.swing.JTextField;classMiddleToLast{publicintprior(charop){if(op=='+'||op=='-')return1;if(op=='*'||op=='/')return2;return0;}publicStringmiddleToLast(Stringmiddle){Stackop=newStack();Stringans=newString();charc[]=newchar[50];String[]s=newString[50];Stringms=newString();intj=0;for(inti=0;i<middle.length();i++){charch=middle.charAt(i);if(ch>='0'&&ch<='9'){c[j]=ch;j++;}else{if(ch=='('){op.push(Character.valueOf(ch));}else{if(ch==')'){while(((Character)op.peek()).charValue()!='('){c[j]=((Character)op.peek()).charValue();j++;op.pop();}op.pop();}elseif(ch=='+'||ch=='-'||ch=='*'||ch=='/'){if(op.empty())op.push(Character.valueOf(ch));else{if(prior(ch)>prior(((Character)op.peek()).charValue())){op.push(Character.valueOf(ch));}else{while(!op.empty()&&prior(ch)<=prior(((Character)op.peek()).charValue())){c[j]=((Character)op.peek()).charValue();j++;op.pop();}op.push(Character.valueOf(ch));}}}}}}while(!op.empty()){c[j]=((Character)op.peek()).charValue();j++;op.pop();}ans=String.valueOf(c,0,j);returnans;}}classCompute{privateStrings=newString();privateMiddleToLastmtl=newMiddleToLast();publicCompute(){this("");}publicCompute(Strings){this.s=s;}publicvoidsetComputeString(Strings){this.s=s;}publicStringgetComputeString(){returnthis.s;}publicStringgetLastComputeString(){returnthis.mtl.middleToLast(this.s);}publicintgetResult(){intright=0,left=0,result=0;Stacknumber=newStack();number.push(Character.valueOf('='));charc;Stringlast=this.mtl.middleToLast(this.s);for(inti=0;i<last.length();i++){c=last.charAt(i);if(!isOperateSign(c))number.push(Integer.valueOf(c-48));if(isOperateSign(c)){right=((Integer)number.pop()).intValue();left=((Integer)number.pop()).intValue();result=this.ComputeResult(right,left,c);number.push(Integer.valueOf(result));}}returnresult;}publicintComputeResult(intright,intleft,charop){intresult=0;switch(op){case'+':result=left+right;break;case'-':result=left-right;break;case'*':result=left*right;break;case'/':result=left/right;break;}returnresult;}publicbooleanisOperateSign(charc){if(c=='+'||c=='-'||c=='/'||c=='*')returntrue;elsereturnfalse;}}publicclasstestextendsJFrameimplementsActionListener{privatestaticfinallongserialVersionUID=1L;privateJTextFieldjtf;privateJButtonjb;privateStrings=newString();Computecp;publictest(){super("姚健的计算器");jtf=newJTextField();jtf.setSize(200,30);jtf.setLocation(5,5);jb=newJButton("计算");jb.addActionListener(this);jb.setSize(70,30);jb.setLocation(5,50);setLayout(null);add(jtf);add(jb);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setBounds(getToolkit().getScreenSize().width/2-150,getToolkit().getScreenSize().height/2-150,300,300);setVisible(true);}publicvoidactionPerformed(ActionEvente){this.s=jtf.getText();intresult=this.count(s);System.out.println("s"+"="+result);this.jtf.setText(s);}publicintcount(Strings){intresult=0;Computecp=newCompute(s);result=cp.getResult();returnresult;}publicstaticvoidmain(String[]args){newtest();}}可以实现个位数的加减乘除,但是多位数的运算就没则了,请求各位大大们帮忙解决