| | |
Infix to Postfix (HELP NEEDED)
![]() |
•
•
Join Date: Oct 2009
Posts: 1
Reputation:
Solved Threads: 0
Hello,
I am having some difficulty in trying to figure out where I am going wrong in my program. I am trying to convert infix to postfix and from there I am evaluating the postfix. However, it is not compiling correctly. I've tried several things but nothing seems to work correctly for me.
I am having some difficulty in trying to figure out where I am going wrong in my program. I am trying to convert infix to postfix and from there I am evaluating the postfix. However, it is not compiling correctly. I've tried several things but nothing seems to work correctly for me.
Java Syntax (Toggle Plain Text)
import java.util.*; import java.io.*; public class calculator { private String infix; private String postfix = " "; // public calculator(String in) { // input = in; // int SIZE = input.length(); // } public String toPostfix(String infix) { String expression; String postfix = " "; Stack operatorStack = new Stack(); StringTokenizer s = new StringTokenizer(infix); while (s.hasMoreTokens()) { expression = s.nextToken(); if (Character.isDigit(expression.charAt(0))) postfix = postfix + " " + (Integer.parseInt(expression)); else if (expression.equals("(")) { Character operator = new Character('('); operatorStack.push(operator); } else if (expression.equals(")")) { while (((Character) operatorStack.peek()).charValue() != '(') { postfix = postfix + " " + operatorStack.pop(); } operatorStack.pop(); } else { while (!operatorStack.isEmpty() && !(operatorStack.peek()).equals("(") && prec(expression.charAt(0)) <= prec(((Character) operatorStack .peek()).charValue())) postfix = postfix + " " + operatorStack.pop(); Character operator = new Character(expression.charAt(0)); operatorStack.push(operator); } } while (!operatorStack.isEmpty()) postfix = postfix + " " + operatorStack.pop(); return postfix; } private static int prec(char x) { if (x == '+' || x == '-') return 1; if (x == '*' || x == '/' || x == '%') return 2; return 0; } public int evaluate(String postfix) { int a; int b; int result = 0; Stack<Integer> myStack = new Stack<Integer>(); for (int i = 0; i < postfix.length(); i++) { char ch = postfix.charAt(i); if (ch >= '0' && ch <= '9') { myStack.push((int) (ch - '0')); } else { switch (ch) { case '+': a = myStack.pop(); b = myStack.pop(); result = a + b; myStack.push(result); break; case '-': a = myStack.pop(); b = myStack.pop(); result = a - b; myStack.push(result); break; case '*': a = myStack.pop(); b = myStack.pop(); result = a * b; myStack.push(result); break; case '/': a = myStack.pop(); b = myStack.pop(); result = a / b; myStack.push(result); break; } myStack.push(result); } } myStack.push(result); return result; } }
0
#3 27 Days Ago
Try searching the forum, this question gets posted all the time. In fact my first post ever was regarding converting infix to postfix using stack. Also, I know there is a code snppet for converting infix to postfix in java. Give it a try.
Check out my new band URL on facebook. I'm the bass player. :) Become a fan and leave comments if you like.
URL on facebook!
URL on facebook!
•
•
Join Date: Nov 2008
Posts: 332
Reputation:
Solved Threads: 53
0
#4 23 Days Ago
•
•
•
•
I know there is a code snppet for converting infix to postfix in java
method
•
•
•
•
public String toPostfix(String infix)
method
•
•
•
•
public int evaluate(String postfix)
In addition little stack tracer
Java Syntax (Toggle Plain Text)
Stack<Integer> myStack = new Stack<Integer>() { public Integer push(Integer i) { Integer j = super.push(i); System.out.println("push " + i); return j; } public Integer pop() { Integer i = super.pop(); System.out.println("pop " + i); return i; } };
Last edited by quuba; 23 Days Ago at 6:22 am.
![]() |
Other Threads in the Java Forum
- Previous Thread: java applets
- Next Thread: Input an integer number and display its largest factor
| Thread Tools | Search this Thread |
911 actionlistener addressbook android api append applet application array arrays automation binary block bluetooth character chat class client code component consumer csv database desktop developmenthelp eclipse error fractal ftp game gameprogramming givemetehcodez graphics gui html ide image integer j2me j2seprojects japplet java javaarraylist javac javaee javaprojects jni jpanel julia lego linked linux list loops mac map method methods mobile netbeans newbie number objects online oriented panel printf problem program programming project projects properties recursion replaydirector reporting researchinmotion rotatetext rsa scanner se server set singleton sms sort sql string swing test textfields threads time title tree tutorial-sample ubuntu update windows working





