Hey, i dont get any compile errors on this code but when i run it i dont get an evaluated value, what have i missed???
import java.util.Scanner;
import java.util.StringTokenizer;
import java.lang.Math;
public class main
{
static Stack<String> stack = new NodeStack<String>();
static final String operators = "+-/*^";
static Scanner key = new Scanner(System.in);
public static void main(String[] args)
{
System.out.print("Enter an expression: ");
String exp = key.nextLine();
Queue<String> q = infixToPostfix(exp);
while (!q.isEmpty())
System.out.print(q.dequeue() + " ");
System.out.println();
while (!stack.isEmpty())
System.out.print("The value of " + exp + " is: " + evalToken(q));
System.out.println();
}
// methods
public static Queue<String> infixToPostfix(String exp)
{
Queue<String> q = new NodeQueue<String>();
StringTokenizer ST = new StringTokenizer(exp, " +-/*^()", true);
while(ST.hasMoreTokens())
{
String aToken = ST.nextToken();
if (aToken.equals(" "))
; // Ignore this
else if (aToken.equals("(")) // if it is open parenthesis
stack.push("(");
else if(aToken.equals(")")) // if it is close parenthesis
{
while(stack.top() != "(")
{
q.enqueue(stack.pop());
}
if(stack.top() == "(")
stack.pop();
}
else if (operators.indexOf(aToken) != -1) // if it is operator
{
/* pop all stack symbols until a symbol of lower precedence
* or a right-associative symbol of equal precedence appears
* then push the operator
*/
if (stack.isEmpty())
stack.push(aToken);
else
{
while(!stack.isEmpty())
{
if(isHigherPrecedence(stack.top(),aToken)) // if higher precedence
{
q.enqueue(stack.pop());
}
else
{
stack.push(stack.pop());
stack.push(aToken);
break;
}
}
if(stack.isEmpty())
stack.push(aToken);
}
}
else // if it is operand
{
q.enqueue(aToken);
}
}
while(!stack.isEmpty())
{
q.enqueue(stack.pop());
}
return q;
}
public static boolean isHigherPrecedence(String op1,String op2)
{
if (equalsOp(op1,op2))
return true;
else
{
if(op1.equals("^")) // make the power the highest precidence
{
if (op2.equals("^"))
return false;
else
return true;
}
if(op1.equals("(")) // make the open bracket lower precidence
return false;
else
{
if(op1.equals("+") | op1.equals("-"))
return false;
else if(op1.equals("*") | op1.equals("/"))
if (op2.equals("^"))
return false;
else
return true;
else
return true;
}
}
}
public static boolean equalsOp(String op1, String op2)
{ if(op2.equals("^")||op1.equals("^"))
return false;
else if (op1.equals(op2))
return true;
else if(op1.equals("+") & op2.equals("-") || op2.equals("+") & op1.equals("-"))
return true;
else if(op1.equals("/") & op2.equals("*") || op2.equals("*") & op1.equals("/"))
return true;
else
return false;
}
// ################### EVALUATION ############################
public static String evalToken(Queue q) // evaluates the tokens
{
while (!q.isEmpty())
{
String token = (String)q.dequeue();
if (token.equals(" "))
//;
if (operators.indexOf(token) != -1)
{
double op1 = Double.parseDouble(stack.pop());
double op2 = Double.parseDouble(stack.pop());
//pop two from stacks, as rhs and lhs
if (token.equals("+"))
// do plus
stack.push(Double.toString(op1 + op2));
else if (token.equals("-"))
// do minus
stack.push(Double.toString(op1 - op2));
else if (token.equals("*"))
// do multiply
stack.push(Double.toString(op1 * op2));
else if (token.equals("/"))
// do division
stack.push(Double.toString(op1 / op2));
else
// do power
stack.push(Double.toString(Math.pow(op1,op2)));
}
else
//push token into stack
stack.push(token);
}
return (stack.pop());
}
}