Tuesday, July 22, 2014
Today I am going to show how to convert a postfix expression to an infix expression using stack in Java. In an earlier post here we ahve shown how to convert an infix expression to postfix using stack. Here we will just try to do the reverse. Here in our example we will be able to convert any postfix expression to infix irrespective of the operators. A number of parenthesis may be generated extra which were not there in original infix expression. The parenthesis that may be generated extra will have no impact on actual expression, they are just for better understanding. The process of postfix to infix conversion is summarised below -
1. Create an empty stack that can hold string values.
2. Scan the postfix expression from left to right
2a. If operand then push into stack
2b. If operator then
1. Pop first two elements
2. Now make a string with "(" + operand2 + operator + operand1 + ")"
3. Push the new string onto stack
3. Pop the element on stack.
------------------------------------------------------------------------------------------------------
Java Source Code
------------------------------------------------------------------------------------------------------
import java.util.Scanner; import java.util.Stack; public class PostfixToInfix { /** * Checks if the input is operator or not * @param c input to be checked * @return true if operator */ private boolean isOperator(char c){ if(c == '+' || c == '-' || c == '*' || c =='/' || c == '^') return true; return false; } /** * Converts any postfix to infix * @param postfix String expression to be converted * @return String infix expression produced */ public String convert(String postfix){ Stack<String> s = new Stack<>(); for(int i = 0; i < postfix.length(); i++){ char c = postfix.charAt(i); if(isOperator(c)){ String b = s.pop(); String a = s.pop(); s.push("("+a+c+b+")"); } else s.push(""+c); } return s.pop(); } public static void main(String[] args) { PostfixToInfix obj = new PostfixToInfix(); Scanner sc =new Scanner(System.in); System.out.print("Postfix : "); String postfix = sc.next(); System.out.println("Infix : "+obj.convert(postfix)); } }
------------------------------------------------------------------------------------------------------
DOWNLOAD the source from Mediafire
DOWNLOAD the source from 4shared
Output
------------------------------------------------------------------------------------------------------
Postfix : ABC^^ Infix : (A^(B^C))------------------------------------------------------------------------------------------------------
Download Links
------------------------------------------------------------------------------------------------------DOWNLOAD the source from Mediafire
DOWNLOAD the source from 4shared
Labels:Algorithms
Subscribe to:
Post Comments
(Atom)
Total Pageviews
Followers
Labels
- Algorithms (7)
- Annotation (3)
- Files (6)
- Generics (3)
- Graphics2D (5)
- Graphics2D-Images (7)
- Inheritance (2)
- J2EE (9)
- Java 8 (4)
- Java FAQs (19)
- JDBC (3)
- Networking (2)
- Packages (1)
- Reflection (4)
- Security (7)
- Sorting (2)
- Swing (3)
- Threads (3)
- Utils (3)
Popular Posts
-
Today I will show you how you can implement Bankers algorithm in Java. The Banker's algorithm is a resource allocation and deadlock a...
-
------------------------- UPDATE ------------------------- I have updated the code on request of some followers so that they can directly...
-
Today I am going to show how to convert a postfix expression to an infix expression using stack in Java. In an earlier post here we ...
-
Today in this article I will tell you how to convert an infix expression to postfix expression using stack. This is an important applicat...
-
--------------------UPDATE------------------- I have updated my post so that now it can detect IE 11. This modification was necessary as t...
-
Today I am going to show you how you can generate and validate captcha. A CAPTCHA (an acronym for "Completely Automated Public Turin...
-
Today I am going to post a program that will be able to produce all the mColorings of a given graph G. What is mColoring : The problem st...
-
Today in this article I will show you how to create or develop a Tower of Hanoi game in Java. The Tower of Hanoi is a famous problem tha...
How will you convert a postfix to infix with expression tree?
ReplyDeleteThis code does not produce the right results. If the postfix expression was 359+-23*/, your code produces ((3-(5+9))/(2*3)), when it should be (3-5+9)/(2*3).
ReplyDeleteIn regards to K.O., both those answers produce the same answer, the difference is simply the amount of parentheses. The problem with wanting to eliminate parentheses is that it would destroy the result of the program if say a higher order operator were where the subtraction symbol is.
ReplyDeleteWhy are you creating a PostfixToInfix object with no constructor?
ReplyDeleteIn regards to K.O., you are right - they certainly do NOT produce the same answer.
ReplyDeletethanks for posting this code. It was very helpfull for my project. very simple to understand.
ReplyDelete