infix to post fix convertion

infix to post fix convertion

a+(bc-(d/e^f))*h

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 post fix convertion
infix to post fix convertion  a+(bc-(d/e^f))*h
infix to postfix covertion
infix to postfix covertion  (a+b)*(d/e) convert in post fix notation step by step
Advertisements
post
post method in PHP  Write an example of the post method in PHP to send the data to another page or simply update the database.   in post values in the url will be hidden   in post   post
post
post  what is post   vxvxcvxcv   Please visit the following link: Post in PHP
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'
convertion
convertion
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
Prefix / Postfix to Infix
, and infix and have been tasked with creating a program that converts prefix to infix and another one that converts postfix to infix. I recently arrived here
convertion before downloading
convertion before downloading  convert zip file which is in server to doc while before downloading
ModuleNotFoundError: No module named 'Temu-Convertion'
ModuleNotFoundError: No module named 'Temu-Convertion'  Hi, My... named 'Temu-Convertion' How to remove the ModuleNotFoundError: No module named 'Temu-Convertion' error? Thanks   Hi, In your python
please how fix this error?
How to Fix Error   for this following code : $var1 = $_POST["name"]; $var2 = $_POST["id"]; $st = 'INSERT INTO USERNAME(id,name) VALUES(:var1,:var2)'; $query = ociparse($con,$st); OCIBindByName($st ,':va1',$var1
post get
post get  what is post and get
post get
post get  what is ajax
post data
post data  what is PHP
convertion of string entered in the web interface
convertion of string entered in the web interface  hi i am entering some values in the textbox of web interface.how i can access that values and convert to particular type.in short how to link web interface values with my java
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
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
POST or GET
POST or GET  hello, Which will execute faster on php POST or GET?   hii,ADS_TO_REPLACE_1 The POST metod is much more faster then GET... in the URl which takes more execution time for processing.In the POST method
get and post
get and post  what is php and get   PHP get and post method
To delete post
To delete post  How can I delete my old post answer. I've posted 2 same answers by mistake
ModuleNotFoundError: No module named 'fix'
ModuleNotFoundError: No module named 'fix'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'fix' How to remove the ModuleNotFoundError: No module named 'fix' error
post back
post back  I am developing a C# asp.net application. I have a webform which contains 2 dropdowns and a textbox with type="password". On "SelectedIndexChanged" event of the first dropdown, there is a postback, in order to fill
hex to binary convertion in java - Java Beginners
hex to binary convertion in java  HI, im doin a application wch requires to send a binary message to other mobiles, so i need to convert the hex values to binary format.So the binary format could either be a ringtone
hex to binary convertion in java - Java Beginners
hex to binary convertion in java  HI, im doin a application wch requires to send a binary message to other mobiles, so i need to convert the hex values to binary format.So the binary format could either be a ringtone
hex to binary convertion in java - Java Beginners
hex to binary convertion in java  HI, im doin a application wch requires to send a binary message to other mobiles, so i need to convert the hex values to binary format.So the binary format could either be a ringtone
HTTP Post PHP
HTTP Post PHP  HTTP post method example in PHP
php form post to mysql
php form post to mysql  How to post data into mysql database from the PHP post data form
post ,get data in the database
post ,get data in the database  post ,get data in the database  Please visit the following links: PHP get post data PHP Tutorials
date convertion
Datatype convertion
PHP Pass Post Data
PHP Pass Post Data  How to pass POST data to the PHP-CGI
post function php
post function php  when should i use post function in PHP
ModuleNotFoundError: No module named 'post'
ModuleNotFoundError: No module named 'post'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'post' How to remove the ModuleNotFoundError: No module named 'post' error
GET and POST methods
GET and POST methods   What are the differences between GET and POST methods in form submitting, give the case where we can use GET and we can use POST methods
Ajax post example
Ajax post example  Hi, I am looking for ajax post example. Thanks   Hi, You can use jQuery to jQuery Post Data. Thanks
jquery post data error
jquery post data error  getting post data error in JQuery ..any idea why?   Please visit the following link: JQuery Post Data
Version of com.intersult>jpa-fix dependency
List of Version of com.intersult>jpa-fix dependency
ModuleNotFoundError: No module named 'colout-fix'
ModuleNotFoundError: No module named 'colout-fix'  Hi, My Python... 'colout-fix' How to remove the ModuleNotFoundError: No module named 'colout-fix' error? Thanks   Hi, In your python environment you
ModuleNotFoundError: No module named 'colout-fix'
ModuleNotFoundError: No module named 'colout-fix'  Hi, My Python... 'colout-fix' How to remove the ModuleNotFoundError: No module named 'colout-fix' error? Thanks   Hi, In your python environment you
ModuleNotFoundError: No module named 'cpca-fix'
ModuleNotFoundError: No module named 'cpca-fix'  Hi, My Python... 'cpca-fix' How to remove the ModuleNotFoundError: No module named 'cpca-fix... to install padas library. You can install cpca-fix python with following
ModuleNotFoundError: No module named 'cpca-fix'
ModuleNotFoundError: No module named 'cpca-fix'  Hi, My Python... 'cpca-fix' How to remove the ModuleNotFoundError: No module named 'cpca-fix... to install padas library. You can install cpca-fix python with following
ModuleNotFoundError: No module named 'django-tables2-wsgi-fix'
ModuleNotFoundError: No module named 'django-tables2-wsgi-fix'  Hi...: No module named 'django-tables2-wsgi-fix' How to remove the ModuleNotFoundError: No module named 'django-tables2-wsgi-fix' error? Thanks   
ModuleNotFoundError: No module named 'fakeredis-fix'
ModuleNotFoundError: No module named 'fakeredis-fix'  Hi, My... 'fakeredis-fix' How to remove the ModuleNotFoundError: No module named 'fakeredis-fix' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'mailcap-fix'
ModuleNotFoundError: No module named 'mailcap-fix'  Hi, My Python... 'mailcap-fix' How to remove the ModuleNotFoundError: No module named 'mailcap-fix' error? Thanks   Hi, In your python environment
ModuleNotFoundError: No module named 'pcap-fix'
ModuleNotFoundError: No module named 'pcap-fix'  Hi, My Python... 'pcap-fix' How to remove the ModuleNotFoundError: No module named 'pcap-fix... to install padas library. You can install pcap-fix python with following
ModuleNotFoundError: No module named 'pycocotools-fix'
ModuleNotFoundError: No module named 'pycocotools-fix'  Hi, My... named 'pycocotools-fix' How to remove the ModuleNotFoundError: No module named 'pycocotools-fix' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'pycocotools-fix'
ModuleNotFoundError: No module named 'pycocotools-fix'  Hi, My... named 'pycocotools-fix' How to remove the ModuleNotFoundError: No module named 'pycocotools-fix' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'pyhocon-fix'
ModuleNotFoundError: No module named 'pyhocon-fix'  Hi, My Python... 'pyhocon-fix' How to remove the ModuleNotFoundError: No module named 'pyhocon-fix' error? Thanks   Hi, In your python environment
ModuleNotFoundError: No module named 'pyrrd-fix'
ModuleNotFoundError: No module named 'pyrrd-fix'  Hi, My Python... 'pyrrd-fix' How to remove the ModuleNotFoundError: No module named 'pyrrd-fix' error? Thanks   Hi, In your python environment you

Ads