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.

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

properly write constructor
reply , what does mean parameter "String in" in contructor?
What information is transmitted to a class?

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.

I know there is a code snppet for converting infix to postfix in java

here is! , and annunfanwen is an author

method

public String toPostfix(String infix)

adds usefull spaces as separator L28,38,48,54

method

public int evaluate(String postfix)

don't know about it . Also can't collect digits together to form number

In addition little stack tracer

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;
            }
        };
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.