hey guys i m having a problem with infix and prefix
here is the code
it says in line 72import java.util.StringTokenizer; import static java.lang.Math.pow; import java.util.Stack; public class Calculator { static int precedence(char op) { switch(op) { case '+' : case '-' : return 5; case '*' : case '/' : return 10; default : throw new IllegalArgumentException("λαθος εκφραση"); } } public static String infixToPostfix(String infix) { StringTokenizer tokenizer = new StringTokenizer (infix); String postfix = ""; Stack opStack = new Stack (); while (tokenizer.hasMoreTokens()) { String token = tokenizer.nextToken(); char c = token.charAt(0); if (Character.isDigit(c)){ postfix +=(token + " "); } else { while (!opStack.empty()){ char top = ((Character)opStack.peek()).charValue(); if (precedence(top) >= precedence(c)) { postfix += (top + " "); opStack.pop(); } else { break; } } opStack.push(new Character (c)); } } while (!opStack.empty()) { char top = ((Character)opStack.pop()).charValue(); postfix += (top + " "); } return postfix; } public static int evalPostfix(String postfix) { StringTokenizer tokenizer = new StringTokenizer(postfix); Stack valStack = new Stack(); while(tokenizer.hasMoreTokens()) { String token = tokenizer.nextToken(); char c = token.charAt(0); if (Character.isDigit(c)) { valStack.push(new Integer(token)); } else { int rightVal = ((Integer)valStack.pop()).intValue(); int leftVal = ((Integer)valStack.pop()).intValue(); int rslt; switch(c) { case '+' : rslt = leftVal + rightVal;break; case '-' : rslt = leftVal - rightVal;break; case '*' : rslt = leftVal * rightVal;break; case '/' : rslt = leftVal / rightVal;break; case '^' : rslt = pow(leftVal,rightVal);break; default: throw new IllegalArgumentException("λαθος εκφραση"); } valStack.push(new Integer(rslt)); } } int rslt = ((Integer)valStack.pop()).intValue(); if (!valStack.empty()){ throw new IllegalArgumentException("λαθος εκφαρση"); } return rslt; } public static void main(String[] args) { String infix = args[0]; String postfix = infixToPostfix(infix); System.out.println("postfix"+postfix); int value = evalPostfix(postfix); System.out.println("value: " + value); } }
possible loss of precision
required: int
found: double
the pow only takes int values?not doubles?how do i fix it?