Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners infix to postfix covertion

Have Programming Question? Ask it here!
 
 
 


Janki
infix to postfix covertion
1 Answer(s)      2 years and a month ago
Posted in : Java Beginners


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


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 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
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
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
Using Operators in JSP
of operators.   postfix operator   
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
Best Open Source Software
within Zimbra - Linux?, Apache?, MySQL?, Postfix?, OpenLDAP, and so
Open Source Project Management
-source projects such as OpenACS, AOLServer, PostgreSQL, CygWin, CVS, Postfix
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
Advertisements
 

 

 

DMCA.com