Janki
infix to post fix convertion
1 Answer(s)      2 years and 11 months ago
Posted in : Java Beginners

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

Advertisement
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
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
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
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
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
please fix the error
please fix the error  org.apache.jasper.JasperException: Unable to compile class for JSP: An error occurred at line: 14 in the jsp file: /doctor.jsp Invalid character constant 11: Connection con
post get
post get  what is post and get
post data
post data  what is PHP
post get
post get  what is ajax
POST or GET
POST or GET  hello, Which will execute faster on php POST or GET?   hii, The POST metod is much more faster then GET ..because in GET... more execution time for processing.In the POST method no information is given
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
need to fix errors please help
need to fix errors please help  it does have 2 errors what should i fix? import java.io.*; class InputName static InputStreamReader reader = new InputStreamReader(system.in); static BufferedReader input = new BufferedReader
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
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
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
Fix Scrollbar position in a checkboxList after postback
Fix Scrollbar position in a checkboxList after postback  Hi, I have a CheckBoxList in which have lot of items and i set AutoPostBack="True".I am using Scrollbar on this list.Now when I am checking any item then after postback
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