4 Replies - 894 Views - Last Post: 16 November 2012 - 05:02 AM Rate Topic: -----

#1 chocologo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 15-November 12

Infix to Postfix?!

Posted 15 November 2012 - 04:42 PM

So I've been trying to write this infix to postfix program (new to programming). And at least it compiles and without parenthesis it works. But things start to get a little wacky when I put in the parenthesis in the infix. :(/>/>/>/> Need help please. THANK YOU :]

Things that I've tried:
Thought there was something wrong with line 40 and line 43 in that rather than: sub.equals("(") and sub.equals(")") I should use sub == "(" and sub == ")". And then I realized that you can't that because they are not in the same memory (?).
Really have no idea what could be wrong. I've spent an hour just looking at it. Maybe I'm just going at the problem wrong.


import java.io.*;
import java.util.*;

public class infix2postfix{
	public static void main(String [] args){
		System.out.println("Enter an expression in the Infix form:");
		Scanner scanner = new Scanner(System.in); //makes new scanner
		
		String infix = scanner.nextLine();//creates variable infix and puts in the user input
		converter firstExp = new converter(); //makes a new object
		firstExp.input(infix); //user input is put into the converter class
		String postfix = firstExp.convert(); //converts the user input and returns the postfix
		System.out.println(postfix); 
		
	//* variables : infix, postfix
	//* create scanner
	//* infix = user input
	//* postfix = converter(user input);
	//eval
	}
}
class converter{
	String postfixstr= ""; //holds the postfix string
	String sub = ""; //to hold the letter/number/digit	
	Stack<String> operator = new Stack<String>();
	String infixstr;
	
	void input (String str){
		infixstr = str;
	}
	String convert(){
		for(int x=0 ; x<infixstr.length() ; x++){
			//cut the string
			sub = infixstr.substring(x,x+1);
			//numbers & operators are separated
			if (isOperator(sub)){
				if (operator.isEmpty()){
					operator.push(sub); //because the stack is empty and you dont need to compare just push into operator
				}
				else if(sub.equals("(")){
					operator.push(sub); //push into operator  stack ( 
				}
				else if(sub.equals(")")){
					postfixstr += operator.pop(); //pops the last operator into the string
					operator.pop(); //removes the ( in the stack
				}
				else{ //stack is not empty and the operators must be compared
					String prec = precedence(operator.peek(), sub);
					if (prec.equals(operator.peek())){ //higher priority is in the stack and the lower priority is the one being evaluated
						postfixstr += operator.pop(); //pop the higher priority into the string
						operator.push(sub); // push the lower priority into stack
					}
					else //the lower priority is in the stack and the higher priority is being evaluated
						operator.push(sub);//push the higher priority into stack
				}	
			}
			else{
				postfixstr += sub; //immediately add onto the final string
			}
		}
		while (!operator.isEmpty()){//don't forget to pop the rest of the operators in the stack
			postfixstr += operator.pop();
		}
		return postfixstr;		
	}
	private boolean isOperator(String chr){
		if(chr.equals("(")||chr.equals(")")||chr.equals("^")||chr.equals("*")||chr.equals("/")||chr.equals("+")||chr.equals("-")){
			return true;
		}
		else{
			return false;
		}
	}
	private String precedence(String a, String B)/>/>/>/>/>{
		String one = "^";
		String two = "*/";
		String three = "+-";
		
		if ((one.indexOf(a) != -1)&&(two.indexOf(B)/>/>/>/>/> != -1)) // if the operators are ^ and */ return ^
			return a;
		else if ((one.indexOf(B)/>/>/>/>/> != -1)&&(two.indexOf(a) != -1)) // if the operators are */ and ^ return ^
			return b;
		else if ((one.indexOf(a) != -1)&&(three.indexOf(B)/>/>/>/>/> != -1)) // if the operators are ^ and +- return ^
			return a;
		else if ((one.indexOf(B)/>/>/>/>/> != -1)&&(three.indexOf(a) != -1)) // if the operators are +- and ^ return ^
			return b;
		else if ((two.indexOf(a) != -1)&&(three.indexOf(B)/>/>/>/>/> != -1)) // if the operators are */ and +- return */
			return a;
		else if ((two.indexOf(B)/>/>/>/>/> != -1)&&(three.indexOf(a) != -1)) // if the operators are +- and */ return */
			return b;	
		else //since both the operators must be the same level return the one that comes first
			return a;
	}
}


Is This A Good Question/Topic? 0
  • +

Replies To: Infix to Postfix?!

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Infix to Postfix?!

Posted 15 November 2012 - 05:00 PM

The hell if I know why Programming Languages teachers still give this type of homework.
Infix to Postfix... this is the most threorical and useless problem ever invented

This post has been edited by pbl: 15 November 2012 - 05:01 PM

Was This Post Helpful? 0
  • +
  • -

#3 chocologo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 15-November 12

Re: Infix to Postfix?!

Posted 15 November 2012 - 05:22 PM

View Postpbl, on 15 November 2012 - 05:00 PM, said:

The hell if I know why Programming Languages teachers still give this type of homework.
Infix to Postfix... this is the most threorical and useless problem ever invented



ahaha I know right? um examples if you want me to refresh your mind?

4 + 5 * ( 3 - 2 )
4 5 3 2 - * +

stack: + * ( -
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Infix to Postfix?!

Posted 15 November 2012 - 05:26 PM

HeHeHe I used such an HP calculator in the 70's
http://www.dreaminco...rpn-calculator/
Was This Post Helpful? 0
  • +
  • -

#5 Xupicor  Icon User is offline

  • Nasal Demon
  • member icon

Reputation: 249
  • View blog
  • Posts: 582
  • Joined: 31-May 11

Re: Infix to Postfix?!

Posted 16 November 2012 - 05:02 AM

Is it really, pbl?
I'd see a couple of reasons why it's not so bad:
- implementing it makes you write code that's a bit longer than 10-liners which forces some order and thought, at least it should after first few miserable fails. ;P
- you get to parse text, which is always fun,
- you get to make use of some data structures, here it's just a stack (you could also make Token derivative classes), but:
- while searching info on this you are bound to find something along AST, which could interest you in trees if you don't know them yet,
- you get to set your own rules for operator precedence, making you understand what it is better,
- you get to create new operators if you want,
- it's very easy to implement variables, which leads to:
- if you got that AST interest - maybe you get to write something that is not just advanced calc with variables, but a simple interpreter for your own new scripting language.

In my own time I didn't even have an assignment like that, I made such a calc with variables and custom operators just for the heck of it. And learned a lot while doing that.

Sorry for offtopic rant. ;P

This post has been edited by Xupicor: 16 November 2012 - 05:07 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1