infix to postfix covertion

infix to postfix covertion

(a+b)*(d/e) convert in post fix notation step by step......

View Answers

August 8, 2012 at 5:59 PM

Here is an example of converting infix expression to its postfix. The code accepts the expression from the user and display its postfix expression.

import java.util.Scanner;
import java.util.Stack;

public class InfixToPostfix {
  private String infix;
  private String postfix = "";

  public void convertString(String a){
    String str = "";
    infix = a;
    Stack<String> stack = new Stack<String>();

    for(int i = 0; i < infix.length(); i++){
      str = infix.substring(i,i+1);
      if(str.matches("[a-zA-Z]|\\d"))
        postfix += str;
      else if (isOperator(str)){
        if (stack.isEmpty()){
          stack.push(str);
        }
        else{
          String stackTop = stack.peek();
          while (getPrecedence(stackTop,str).equals(stackTop)&& !(stack.isEmpty())){
            postfix += stack.pop();
            if (!(stack.isEmpty()))
              stackTop = stack.peek();
          }
          stack.push(str);
        }
      }
    }
    while(!(stack.isEmpty()))
      postfix += stack.pop();
    System.out.println("Postfix of the expression is: " +  postfix);
  }

  private boolean isOperator(String ch){
    String operators = "*/%+-";
    if (operators.indexOf(ch) != -1)
      return true;
    else
      return false;
  }

  private String getPrecedence(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 static void main(String[] args) {

    System.out.println("Enter an expression in the Infix form:");
    Scanner scanner = new Scanner(System.in);

    String expression = scanner.nextLine();
    new InfixToPostfix().convertString(expression);


  } 
}









Related Tutorials/Questions & Answers:
infix to postfix covertion
infix to postfix covertion  (a+b)*(d/e) convert in post fix notation step by step
Prefix / Postfix to Infix
to infix and another one that converts postfix to infix. I recently arrived here...Prefix / Postfix to Infix  Hi, I'm currently a high school student studying Computer Science 3 in the USA. We are currently learning prefix, postfix
Advertisements
Java Convert Infix to Postfix
Java Convert Infix to Postfix In this tutorial, you will learn how to convert expression from infix to postfix. The given code accepts the expression from... { private String infix; private String postfix = ""; public void
ModuleNotFoundError: No module named 'infix'
ModuleNotFoundError: No module named 'infix'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'infix' How to remove the ModuleNotFoundError: No module named 'infix'
infix to post fix convertion
infix to post fix convertion  a+(bc-(d/e^f))*h
Infix to Prefix - Java Beginners
Infix to Prefix  Hello, I needed help on converting from an infix... infix) { StringBuffer sb = new StringBuffer(infix); int index...(); } public static void main(String[] args) { String infix
ModuleNotFoundError: No module named 'django-postfix'
ModuleNotFoundError: No module named 'django-postfix'  Hi, My... named 'django-postfix' How to remove the ModuleNotFoundError: No module named 'django-postfix' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'letsencrypt-postfix'
ModuleNotFoundError: No module named 'letsencrypt-postfix'  Hi, My... named 'letsencrypt-postfix' How to remove the ModuleNotFoundError: No module named 'letsencrypt-postfix' error? Thanks   Hi
ModuleNotFoundError: No module named 'letsencrypt-postfix'
ModuleNotFoundError: No module named 'letsencrypt-postfix'  Hi, My... named 'letsencrypt-postfix' How to remove the ModuleNotFoundError: No module named 'letsencrypt-postfix' error? Thanks   Hi
ModuleNotFoundError: No module named 'postfix-stats'
ModuleNotFoundError: No module named 'postfix-stats'  Hi, My... 'postfix-stats' How to remove the ModuleNotFoundError: No module named 'postfix-stats' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'certbot-postfix'
ModuleNotFoundError: No module named 'certbot-postfix'  Hi, My... named 'certbot-postfix' How to remove the ModuleNotFoundError: No module named 'certbot-postfix' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'django-postfix'
ModuleNotFoundError: No module named 'django-postfix'  Hi, My... named 'django-postfix' How to remove the ModuleNotFoundError: No module named 'django-postfix' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'lets-encrypt-postfix'
ModuleNotFoundError: No module named 'lets-encrypt-postfix'  Hi...: No module named 'lets-encrypt-postfix' How to remove the ModuleNotFoundError: No module named 'lets-encrypt-postfix' error? Thanks   Hi
ModuleNotFoundError: No module named 'lets-encrypt-postfix'
ModuleNotFoundError: No module named 'lets-encrypt-postfix'  Hi...: No module named 'lets-encrypt-postfix' How to remove the ModuleNotFoundError: No module named 'lets-encrypt-postfix' error? Thanks   Hi
ModuleNotFoundError: No module named 'modoboa-postfix-autoreply'
ModuleNotFoundError: No module named 'modoboa-postfix-autoreply'  Hi...: No module named 'modoboa-postfix-autoreply' How to remove the ModuleNotFoundError: No module named 'modoboa-postfix-autoreply' error? Thanks  
ModuleNotFoundError: No module named 'modoboa-postfix-autoreply'
ModuleNotFoundError: No module named 'modoboa-postfix-autoreply'  Hi...: No module named 'modoboa-postfix-autoreply' How to remove the ModuleNotFoundError: No module named 'modoboa-postfix-autoreply' error? Thanks  
ModuleNotFoundError: No module named 'postfix-mta-sts-resolver'
ModuleNotFoundError: No module named 'postfix-mta-sts-resolver'  Hi...: No module named 'postfix-mta-sts-resolver' How to remove the ModuleNotFoundError: No module named 'postfix-mta-sts-resolver' error? Thanks  
ModuleNotFoundError: No module named 'kalabash-postfix-autoreply'
ModuleNotFoundError: No module named 'kalabash-postfix-autoreply'  Hi...: No module named 'kalabash-postfix-autoreply' How to remove the ModuleNotFoundError: No module named 'kalabash-postfix-autoreply' error? Thanks
write a java program to solve the infix expression
write a java program to solve the infix expression  ==>> solve the below expression using java program: 2+5*2-5+6/2 using stack operations or any other process in java
c postfix - Framework
c postfix  q: write a c program to read apostfix expression store it in array of character then evaluate this expression using eval and get_token function   Hi Ashraf I am sending a link, where lots of examples
Auto Increment and Decrement
of prefix and postfix operators by incrementing and decrementing the value... of program:ADS_TO_REPLACE_1 To demonstrate the difference between prefix and postfix... and display the value on the console and then applying the postfix increment operator
java program - Development process
java program  hi, i need a simple program for converting given infix expression to postfix expression  Hi friend, Code to solve...(); System.out.println("Infix is " + inStr + '\n'); System.out.println
Java operator
Java operator  What is the difference between the prefix and postfix forms of the ++ operator
calculator in java with stack
in java and in interface there is button called postfix ,,, when the user enter opertions and numbers first check if is vaild or not then convert to postfix... * 1 2 3 \ 0 . = - postfix
jAVA BEGINNER PROBLEMS
jAVA BEGINNER PROBLEMS  I need the program that takes from standard input an expression without left parenthesis and prints the equivalent infix expression with the parenthesis inserted??? Like EXAMPLE: 1+2)3-4)5-6
Unary Operators
be a prefix or a postfix .In a prefix expression (++ x or -- x), an operator is applied before an operand while in a postfix expression (x ++ or x... difference between a prefix and a postfix expressions. In a prefix expression
Free Linux Distribution in India
Mozilla 1.0.1 Postfix 1.1.11.... 0.11, ProFTPD 1.2.7, Postfix 2.0.6, OpenSSH 3.5 KDE 3.1
We are providing Linux CD's for free.
gphoto2 2.1.0 KDE 3.0.3 MrProject 0.6 Mozilla 1.0.1 Postfix
We are providing Linux CD's for free.
1.2.7, Postfix 2.0.6, OpenSSH 3.5 KDE 3.1, GNOME 2.2, IceWM 1.2
We are providing Linux CD's for free.
Mozilla 1.0.1 Postfix 1.1.11 Perl 5.8.0 XFree86 4.2.0 Availability
Using Operators in JSP
of operators.  ADS_TO_REPLACE_1 postfix
Core Java Interview Question, Interview Question
may be tested. Question: What is the difference between the prefix and postfix... operation and returns the value ofthe increment operation. The postfix form
Operators
 Precedence  postfix  expr++,, expr
Operators
 Precedence  postfix  expr++,, expr
Operators
 Precedence  postfix  expr++,, expr
Operators
 Precedence  postfix  expr++,, expr
Operators
 Precedence  postfix  expr++,, expr
Operator Precedence
; .  postfix  expr++  expr--  
Open Source Exchange
, and sits upon familiar open source components like a MySQL database, a Postfix... functionality. The OSER Platform uses Postfix and Courier IMAP for email, Jabber
Best Open Source Software
within Zimbra - Linux?, Apache?, MySQL?, Postfix?, OpenLDAP, and so
Open Source Project Management
-source projects such as OpenACS, AOLServer, PostgreSQL, CygWin, CVS, Postfix
Open Source web mail
most popular open source mail packages--sendmail, qmail, and postfix
Beginners Java Tutorial
the significance of prefix and postfix operators by incrementing and decrementing

Ads