Results 1 to 4 of 4

Thread: InfixToPostfix

  1. #1
    ak13qa is offline Member
    Join Date
    Mar 2012
    Posts
    2
    Rep Power
    0

    Default InfixToPostfix

    how to put the precedence class to this coding.


    Infix2Postfix.java
    Java Code:
    import java.util.Scanner;
    import java.util.Stack;
    
    public class Infix2Postfix {
    
    	private Stack<String> stack;
    	private String infixExp;
    	private String postfixExp = "";
    
    	public Infix2Postfix() {}
    
    	public void convertToPostfix(String exp){
    
    		String str = "";
    		infixExp = exp;
    		stack = new Stack<String>();
    
    		for (int i=0;i<infixExp.length();i++){
    
    			str = infixExp.substring(i,i+1);
    			if(str.matches("[a-zA-Z]|\\d"))
    				postfixExp += str;
    			else if (isOperator(str)){
    				if (stack.isEmpty()){
    					stack.push(str);
    				}
    				else{
    					String stackTop = stack.peek();
    					while (precedence(stackTop,str).equals(stackTop)&& !(stack.isEmpty())){
    						postfixExp += stack.pop();
    						if (!(stack.isEmpty()))
    							stackTop = stack.peek();
    					}
    					stack.push(str);
    				}
    			}
    		}
    
    		System.out.println("The postfix form of the expression you entered is: " + postfixExp);
    	}
    
    	private boolean isOperator(String ch){
    
    		String operators = "*/%+-";
    		if (operators.indexOf(ch) != -1)
    			return true;
    		else
    			return false;
    	}
    
    	private String precedence(String op1, String op2){
    
    		String multiplicativeOps = "*/%";
    		String additiveOps = "+-";
    
    		if ((multiplicativeOps.indexOf(op1) != -1) && (additiveOps.indexOf(op2) != -1))
    			return op1;
    		else if ((multiplicativeOps.indexOf(op2) != -1) && (additiveOps.indexOf(op1) != -1))
    			return op2;
    		else if((multiplicativeOps.indexOf(op1) != -1) && (multiplicativeOps.indexOf(op2) != -1))
    			return op1;
    		else
    			return op1;
    	}
    
    	public void getInfix(String infixExp)
    	{
    		this.infixExp = infixExp;
    	}
    
    	public String showInfix()
    	{
    		return infixExp;
    	}
    
    	public String showPostfix()
    	{
    		return postfixExp;
    	}
    
    }

    TestProgInfix2Postfix.java
    Java Code:
    import java.util.Scanner;
    import java.util.Stack;
    
    public class TestProgInfix2Postfix
    
    {
    
    	public static void main(String[] args)
    	{
    
    		int count = 0;
    		while(count < 5)
    		{
    			System.out.println("Enter an expression in the Infix form:");
    			Scanner scanner = new Scanner(System.in);
    
    			String expression = scanner.nextLine();
    			Infix2Postfix x = new Infix2Postfix();
    
    			x.convertToPostfix(expression);
    
    			x.getInfix(expression);
    			System.out.println();
    
    			System.out.println("Trying to view infix: "+x.showInfix());
    			System.out.println("Trying to view postfix: "+x.showPostfix());
    
    			System.out.println();
    			count++;
    		}
    	}
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    18,332
    Rep Power
    27

    Default Re: InfixToPostfix

    how to put the precedence class to this coding.
    Can you explain?
    What is the "precedence class"?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    ak13qa is offline Member
    Join Date
    Mar 2012
    Posts
    2
    Rep Power
    0

    Default Re: InfixToPostfix

    precedence: precedence between two operators. If the first operator is of higher or equal precedence than the second operator, its returns the value true, otherwise its return value false.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,043
    Blog Entries
    7
    Rep Power
    23

    Default Re: InfixToPostfix

    The essential part of a stack based algorithm for infix -> posfix expressions is the priority/precedence of the operator on the stack top, compared to the priority/precedence of the operator to be entered on the stack. There's a nice explanation in Sahni and Horowitz "Data Structures" but I'm sure there's an answer on Google as well.

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •