源代码如下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];
}
}
}
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];
}
}
}
解决方案 »
- 关于字符串处理的一个问题。。
- 在线等..小弟刚学java...遇到点问题..望高手们止步帮我看看。不胜感激
- 诚恳请教关于JTree的实时更新问题!(可以追加给分!急!!非常感谢)
- 各位兄弟们帮个忙....
- 这几行代码为什么可以由域名得到IP,但由IP得不到域名?麻烦大侠帮看看,谢谢先!
- 求救:急需JAVA RMI渗透防火墙的例子,或者RMI也可,或提供信息也可!!!UP有分(现只能给这么多)
- ImageIcon类是不是不支持打开BMP类型的图像?
- 刚刚学习java便遇到问题, 请求帮助。
- 如何由程序员向系统分析员转变?
- 在座各位有没有用过Forte呢?
- 关于Applet载入图片时不显示的问题
- sdk为何安装不了呢?
Operand2=OperandStack.pop();
Operand1=OperandStack.pop();
Operator=OperatorStack.pop();
OperandStack.push(OperandStack.twoResult(Operator,Operand1,Operand2));
}//else{
OperatorStack.push((int)Temp);
//}这里把else去掉
当优先级高时,做了运算,但低优先级的运算符没有压栈
OperatorStack.setPriority((int)Temp)<=OperatorStack.setPriority(OperatorStack.peek())
时此时只运算而不将运算浮压栈,所以错误
马上加分!
都说csdn上面高手多!这次真让我见识到了!程序才贴上来不到半个小时就被你把错误看出来了,我刚才一直挑了一下午还没有挑出来呢