infix to post fix convertion

Ads
 

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


  } 
}

Ads









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
convertion before downloading
convertion before downloading  convert zip file which is in server to doc while before downloading
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
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
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
post get
post get  what is post and get
post get
post get  what is ajax
post data
post data  what is PHP
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
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
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
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
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
date convertion
Datatype convertion
php form post to mysql
php form post to mysql  How to post data into mysql database from the PHP post data form
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 ,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
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
mockmvc post json string
mockmvc post json string  Hi, I am writing Spring Test cases. I want to post json to webservice. How to post post json string in Spring testing... post json string instead of regular parameters. Following code can be used
HTTP_POST_VARS is deprecated
HTTP_POST_VARS is deprecated  HTTPPOSTVARS is deprecated. Which function to be used? Thanks   Hi, You should use: $id=$_GET["id"]; or $id=$_POST["id"]; thanks

Ads