2 Replies - 2973 Views - Last Post: 30 October 2009 - 10:58 AM Rate Topic: -----

#1 ykelpsis89   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 20-November 08

Infix to Postfix Question

Post icon  Posted 29 October 2009 - 11:18 PM

Hello,

I am working on converting Infix to Postfix and then evaluating the Postfix expression. However, when I compile the code I get a few error messages. I am not sure what I am doing wrong. Your input will be greatly appreciated. I've tried several things but it doesn't seem to make it work the way it should.


import java.util.*;


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



Is This A Good Question/Topic? 0
  • +

Replies To: Infix to Postfix Question

#2 krizjaz   User is offline

  • D.I.C Head
  • member icon

Reputation: 20
  • View blog
  • Posts: 99
  • Joined: 07-October 07

Re: Infix to Postfix Question

Posted 30 October 2009 - 10:52 AM

View Postykelpsis89, on 29 Oct, 2009 - 10:18 PM, said:

Hello,

I am working on converting Infix to Postfix and then evaluating the Postfix expression. However, when I compile the code I get a few error messages. I am not sure what I am doing wrong. Your input will be greatly appreciated. I've tried several things but it doesn't seem to make it work the way it should.


import java.util.*;


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



I think i have the same program as yours. I would like to share it and please study. . This has GUI and can only accept 1 digit number. If two digits are entered, the answer will become wrong due to unimplementation of two digit numbers. I still have to work with it though. I've made this program 1 & 1/2 years ago (i'm still noob with java). Please bear with my coding. . :)
2 files:
http://www.megaupload.com/?d=LHP8N3H1
http://www.megaupload.com/?d=OQ4SOGTG

This post has been edited by krizjaz: 30 October 2009 - 11:03 AM

Was This Post Helpful? 0
  • +
  • -

#3 Fuzzyness   User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Infix to Postfix Question

Posted 30 October 2009 - 10:58 AM

Please post your errors and the line of code that it reffers to in order for us to help you.

At first glance at the green part like so:
//	public calculator(String in) {
	//	input = in;
	//	int SIZE = input.length();

//	}

You didnt not instantiate the object input anywhere(create it) so it will say cannot find symbol for that most likely.
As for the int SIZE, you have created it inside of the consructor, therefore cannot be used outside of the constructor; get what im saying? You would have to create the object outside of it and give it a value inside the constructor.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1