Share on Google+Share on Google+

Janki
infix to postfix covertion
1 Answer(s)      4 years and 8 months ago
Posted in : Java Beginners

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

Ads

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
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
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: To demonstrate the difference between prefix and postfix operator we... on the console and then applying the postfix increment operator and display
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
The increment/decrement operators can be a prefix or a postfix .In a prefix expression (++ x or -- x), an operator is applied before an operand while in a postfix...; Although there is a major difference between a prefix and a postfix
Free Linux Distribution in India
Mozilla 1.0.1 Postfix 1.1.11... Apache 2, Samba 2.2.7a, MySQL 4. 0.11, ProFTPD 1.2.7, Postfix
Core Java Interview Question, Interview Question
. Question: What is the difference between the prefix and postfix forms... ofthe increment operation. The postfix form returns the current value all
Using Operators in JSP
of operators.   postfix operator   
We are providing Linux CD's for free.
Apache 2, Samba 2.2.7a, MySQL 4. 0.11, ProFTPD 1.2.7, Postfix 2.0.6, OpenSSH
We are providing Linux CD's for free.
gphoto2 2.1.0 KDE 3.0.3 MrProject 0.6 Mozilla 1.0.1 Postfix 1.1.11
We are providing Linux CD's for free.
MrProject 0.6 Mozilla 1.0.1 Postfix 1.1.11 Perl 5.8.0 XFree86 4.2.0
Operators
 postfix  expr++,, expr--  
Operators
 postfix  expr++,, expr--  
Operators
 postfix  expr++,, expr--  
Operators
 postfix  expr++,, expr--  
Operators
 postfix  expr++,, expr--  
Operator Precedence
;postfix  expr++  expr--  unary
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
Open Source Project Management
-source projects such as OpenACS, AOLServer, PostgreSQL, CygWin, CVS, Postfix
Best Open Source Software
within Zimbra - Linux?, Apache?, MySQL?, Postfix?, OpenLDAP, and so
Open Source web mail
most popular open source mail packages--sendmail, qmail, and postfix
Beginners Java Tutorial
In this example we are demonstrating the significance of prefix and postfix operators
Tutorials   
Java Spring Hibernate Struts Training Retrieve database from the table dynamically in jsp from oracle using servlet What does core Java include? java What are some way to learn Java quickly? Is it required to learn Java before learning Java Script? Is it necessary to learn java script before learning PHP? Are HTML5 and Java Script boosts Java career? Is Java object oriented? Fingerprint application with Java Uninstall Oracle Virtual Box JSON to HashMap Free Java online Training I want example of Control Statement in Java ANSI Color Codes with Python Create a Program that Calculates Input What is difference between JDK,JRE and JVM? How to see ubuntu version on server? How to get Page Source in Selenium (WebDriver) using Java? The path to the driver executable must be set by the webdriver.gecko.driver system property parse data from a link in java Java Program Qns using BlueJ How to fix HAX Kernel Module Is Not Installed error? Installing Audacity Looking for code Logic to check track changes & Coments in MSWord, MSWordx, MSExcel, MSExcelx is ON/OFF Installing JDK on Mac SAX Parser exception ERROR 601 (42P00): Syntax error. Encountered Thread java.lang.NoClassDefFoundError: org/apache/commons/fileupload/FileItemFactory How to install Ubuntu 16.04 LTS? HttpServletRequest cannot be resolved to a type in eclipse - Solved Unhandled event loop exception GC overhead limit exceeded Spring Data jpa with apache phoenix Caused by: java.lang.IllegalArgumentException: Not a host:port pair: PBUF o.a.h.h.z.RecoverableZooKeeper - Possibly transient ZooKeeper, quorum= com.thinkaurelius.titan.diskstorage.hbase.HBaseStoreManager class not found com.thinkaurelius.titan.diskstorage.hbase.HBaseStoreManager not found How to download and install Java 8 on Windows? How to uninstall JDK 7? How to install gtk-doc-tools package in Ubuntu? How to install Oracle JDK 8 on Ubuntu? ejabberd_ctl.beam not found - Solved How to convert date to UTC format in Java? How to install autoconf, automake and libtool in Ubuntu 15.10? How to convert current date to mm dd yyyy format in Java? How to convert current date to dd mm yyyy format in Java? How to stop window closing in "internalFrameClosing" event. How to find list of all index in Neo4j? neo4j-server.properties file location SASLError using PLAIN: not-authorized

Ads

 
Advertisement null

Ads