Click here to Skip to main content
15,916,600 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi i have problem with line 126
whats wrong plz help me

Java
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;
}
 
}
Posted

1 solution

 
Share this answer
 
v2
Comments
DominoBoy 22-Oct-11 16:02pm    
i want to evaluate, and have no problem with, changing infix to postfix
thx
theanil 22-Oct-11 16:11pm    
I have changed the link have a look.
DominoBoy 22-Oct-11 16:22pm    
thx but that threads is open, and its not solved!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900