源代码如下import java.io.*;public class InorderExpression{

    public static void main(String args[]){

StackArray OperatorStack=new StackArray();
StackArray OperandStack= new StackArray();

String Expression="";
char Temp='0';
int Position=0;
int Evaluate=0;

BufferedReader BR= new BufferedReader(new InputStreamReader(System.in) );

int Operator,Operand1,Operand2;



System.out.println("Please Input the Inorder Expression!");
try{

Expression= BR.readLine();
}catch(IOException e){

}

Position=0;
while(Position<Expression.length()){

Temp=Expression.charAt(Position);

if(OperatorStack.isOperator((int)Temp)){

if(OperatorStack.Top==-1){

OperatorStack.push((int)Temp);

}else{

if(OperatorStack.setPriority((int)Temp)<=OperatorStack.setPriority(OperatorStack.peek())){

Operand2=OperandStack.pop();
Operand1=OperandStack.pop();
Operator=OperatorStack.pop();
OperandStack.push(OperandStack.twoResult(Operator,Operand1,Operand2));
}else{

OperatorStack.push((int)Temp);

}
}
}else{

OperandStack.push((int)Temp-48);
}


Position++;
}


while(OperatorStack.Top!=-1){

Operator=OperatorStack.pop();
Operand2=OperandStack.pop();
Operand1=OperandStack.pop();

OperandStack.push(OperandStack.twoResult(Operator,Operand1,Operand2));
}

Evaluate=OperandStack.pop();

System.out.println("The Expression ["+Expression+"] result is "+Evaluate);





}
}
class StackArray{

int MaxSize=20;
int AStack[]=new int[20];
int Top=-1;


//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
public void push(int Value){

if(Top==MaxSize-1)
System.out.println("The Stack is full!");
else{

Top++;
AStack[Top]=Value;
}

}


//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////

    public int pop(){
    
     int Temp=0;
    
     if(Top==-1){
     System.out.println("The Stack is empty!");
     return -1;
     }
    
     else{
    
     Temp=AStack[Top];
     Top--;
     return Temp;
     }
    }

//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////

public boolean isOperator(int Operator){

switch(Operator){

case 43:

case 45:

case 42:

case 47:

return true;

default:

return false;
}

}
//////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////// public int setPriority(int Operator){

switch(Operator){

case 43:

case 45:

return 1;


case 42:

case 47:

return 2;


default:
return 0;

}

}
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////

public int twoResult(int Operator,int Operand1,int Operand2){

switch(Operator){

case 43 :
return Operand1+Operand2;

case 45 :
return Operand1-Operand2;


case 42 :
return Operand1*Operand2;

case 47 :
return Operand1/Operand2;

default :
return -1;
}

}



//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////

public int peek(){

if(Top==-1){

System.out.println("The Stack is Empty!");
return -1;
}
else{

return AStack[Top];

}

}

}

解决方案 »

  1.   

    if(OperatorStack.setPriority((int)Temp)<=OperatorStack.setPriority(OperatorStack.peek())){
    Operand2=OperandStack.pop();
    Operand1=OperandStack.pop();
    Operator=OperatorStack.pop();
    OperandStack.push(OperandStack.twoResult(Operator,Operand1,Operand2));
    }//else{

    OperatorStack.push((int)Temp);

    //}这里把else去掉
    当优先级高时,做了运算,但低优先级的运算符没有压栈
      

  2.   

    说的有点问题,当
    OperatorStack.setPriority((int)Temp)<=OperatorStack.setPriority(OperatorStack.peek())
    时此时只运算而不将运算浮压栈,所以错误
      

  3.   

    果然是这样子的!!!太谢谢你了!!
    马上加分!
    都说csdn上面高手多!这次真让我见识到了!程序才贴上来不到半个小时就被你把错误看出来了,我刚才一直挑了一下午还没有挑出来呢