Home Answers Viewqa Java-Beginners infix to post fix convertion

 
 


Janki
infix to post fix convertion
1 Answer(s)      9 months ago
Posted in : Java Beginners

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 Pages:
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
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
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
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
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
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
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
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
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
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
Please help me fix this code - MobileApplications
Please help me fix this code   Please help me in this area of code. i used three record store for each form I want expenses button to display all the expenses for the selected trip. i want to use image
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
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
Difference between GET and POST
Difference between GET and POST   Difference between GET and POST ?   The difference between a GET and a POST is the way data... this: http://www.roseindia.net/jsp?para1=paraValue With a POST, this information
The $_POST Function
The $_POST Function The $_POST function in PHP is used to gather data of form submit with method="post". When you send data using POST method.... You can change it by setting 'post_max_size' in the 'php.ini'
HTTPClient Post method
HTTPClient Post method    HTTPClient Post method HI friends, I am trying a JAVA API for this i want a Java HTTPClient program. i tried but i am unable to complete. can any one plz help me.......... Plz, PLz help me out
one error but i dont know how to fix it o_O!!!
one error but i dont know how to fix it o_O!!!  where is the mistake here ?? import java.utill.Scanner; public class Time { public static void main (String [] args) { Scanner keyboard = new Scanner (System.in
HTML form methods GET and POST
HTML form methods GET and POST  What is the difference between the HTML form methods GET and POST
POST METHOD
POST METHOD       In this example we are going to show how we can make use of the POST... of the POST. Post is mainly used to send the data to the server and to retrieve
POST AND GET METHOD - JSP-Servlet
POST AND GET METHOD  I AM LITTLE BIT CONFUSED IN GET() AND POST() METHODS. PLZ GIVE ME BREIF INTRODUCTION ABOUT THESE ??:)  Hi Friend, Please visit the following links: http://www.roseindia.net/jsp
post method does not support this url
post method does not support this url  This is Get product servlet.... but I am receiving one error that is post method does not supported by this url...;"); out.println("<form method="post" action='"+res.encodeURL("addProducts")+"'>
where do I find the fix for tree view catalog not expanding and collapsing correctly for roseindia shopping cart 1.1
where do I find the fix for tree view catalog not expanding and collapsing correctly for roseindia shopping cart 1.1  where do I find the fix for tree view catalog not expanding and collapsing correctly for roseindia shopping
HTML Post Radio Button
HTML Post Radio Button       HTML Post attribute used in method, when a user is not able to see the data in the URL of browser. Post method is used for sending, updating

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.