问题评估后缀评估 [英] problem to evaluate postfix evaluate

查看:73
本文介绍了问题评估后缀评估的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对第126行有疑问
怎么了请帮助我

hi i have problem with line 126
whats wrong plz help me

package itp2;
import java.util.*;
import java.io.*;
class StackX
{
private int maxSize;
private char[] stackArray;
private int[] stackArray1;
private int top;
 
public StackX(int s)
{
maxSize = s;
stackArray = new char[maxSize];
top = -1;
}
 
public void push(char j)
{ stackArray[++top] = j; }
 
public void push1(int j)
{ stackArray1[++top] =j ; }
 
public char pop()
{ return stackArray[top--]; }
public int pop1()
{ return stackArray[top--]; }
public char peek()
{ return stackArray[top]; }
 
public boolean isEmpty()
{ return (top == -1); }
 
public int size()
{ return top+1; }
 
public char peekN(int n)
{ return stackArray[n]; }
 
 
 
} // end class StackX used from lab1
 
class InToPost // infix to postfix conversion borrowed from book
{
private StackX theStack;
private String input;
private String output = "";
 
public InToPost(String in) // constructor
{
input = in;
int stackSize = input.length();
theStack = new StackX(stackSize);
}
 
public String doTrans() // algorithm that does translation with cases
{
for(int j=0; j<input.length(); j++)
{
char ch = input.charAt(j);
 
switch(ch)
{case '+':gotOper(ch, 1); break;
case '-':gotOper(ch, 1); break;
case '*':gotOper(ch, 2); break;
case '/':gotOper(ch, 2); break;
default :output = output + ch; break; //writes to output
} // end of switch
} // end for loop
while( !theStack.isEmpty() ) // pop remaining operators at the end
{
output = output + theStack.pop(); // write to output
}
return output; // return postfix
}
//--------------------------------------------------------------
public void gotOper(char opThis, int prec1)
{
while( !theStack.isEmpty() )
{
char opTop = theStack.pop();
 
if(1==1 )
{
int prec2;
 
if(opTop=='+' || opTop=='-') // find new op prec
prec2 = 1;
else
prec2 = 2;
if(prec2 < prec1)
{
theStack.push(opTop);
break;
}
else
output = output + opTop;
}
} // end while loop
theStack.push(opThis);
}
}
public class ITP2 {
public static void main(String[] args) throws IOException
{
String output;
String input;
while(true)
{
System.out.print("Enter infix: ");
System.out.flush();
input = getString();
if( input.equals("") )
break;
 
InToPost theTrans = new InToPost(input);
output = theTrans.doTrans();
 
 
 
System.out.println("Evaluated expression: " + (output));
// Eval ev = new Eval(output);
//  System.out.println("Evaluated expression: " + evaluate(theTrans.doTrans(output)));
 
System.out.println("Postfix is " + evaluate(output)) ;
}
}
 public class Eval{
private StackX operatorStack ;
private StackX operandStack;
private String output;

public int evaluate( String output)
{
StringTokenizer s = new StringTokenizer(output);//divides into tokens
 
int value;
String symbol;
while (s.hasMoreTokens())
{
symbol = s.nextToken();
if (Character.isDigit(symbol.charAt(0)))// if its a number push it
 
{
Integer operand = new Integer(Integer.parseInt(symbol));
operandStack.push1(operand);
}
else // if it's an operator, operate on the previous two popped operandStack items
{
int op2 = ((Integer)operandStack.pop1()).intValue();
int op1 = ((Integer)operandStack.pop1()).intValue();
int result = 0;
switch(symbol.charAt(0))
{
case '*': {result = op1 * op2; break;}
case '+': {result = op1 + op2; break;}
case '-': {result = op1 - op2; break;}
case '/': {result = op1 / op2; break;}
case '%': {result = op1 % op2; break;}
}
Integer operand = new Integer(result);
operandStack.push1(operand);
}
}
value = ((Integer)operandStack.pop1()).intValue();
return value;
}
 
}
 
public static String getString() throws IOException
{
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
String s = br.readLine();
return s;
}
 
}

推荐答案

http: //www.daniweb.com/software-development/java/threads/168876 [ ^ ]


这篇关于问题评估后缀评估的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆