Write a program to Prefix to Postfix conversion using stack
Example: 1
Input: prefix="+A+*BC*/EF^GH"
Output: postfix = "ABC*+EF/GH^*+"
Approach:
Java
import java.util.HashMap;import java.util.Stack;public class PrefixToPostfix {static HashMap<Character, Integer> precedence =new HashMap<Character, Integer>();public static void main(String[] args) {// prefixString prefix = "+A+*BC*/EF^GH";// precedence initializationoperatorPrecedence();// prefix to infixString infix = prefixToInfix(prefix);// infix to postfixString postFIx = infixToPostfix(infix);System.out.println("Postfix is " + postFIx);}private static String prefixToInfix(String prefix) {String infix = "";// stack for hold operatorStack<String> stack = new Stack<>();// Iterate till given infix lengthfor (int i = prefix.length() - 1; i >= 0; i--) {// hold char from postFIxchar c = prefix.charAt(i);// If letter then add into stackif (Character.isLetter(c)) {stack.add(String.valueOf(c));} else {// rule = first+operator+secondString a = stack.pop();String b = stack.pop();stack.add(a + "" + c + "" + b);}}// iterate till stack is emptywhile (!stack.isEmpty()) {infix += stack.pop();}return infix;}private static String infixToPostfix(String infix) {String postFix = "";// stack for hold operatorStack<Character> stack = new Stack<>();// Iterate till given infix lengthfor (int i = 0; i < infix.length(); i++) {// hold char from infixchar c = infix.charAt(i);// If letter then add into postfixif (Character.isLetter(c)) {postFix += infix.charAt(i);} else if (c == '(') {stack.add(c);} else if (c == '^') {stack.add(c);} else if (c == ')') { // if closing ) then// iterate till ( and add into postfixwhile (!stack.isEmpty() && stack.peek() != '(') {postFix += stack.pop();}// if last char is '(' into stack then popif (!stack.isEmpty() && stack.peek() == '(')stack.pop();} else {// if precedence of c is greater then// top stack then addif (stack.isEmpty() || precedence.get(c) >precedence.getOrDefault(stack.peek(), 0)) {stack.add(c);} else {// iterate till precedence is less then equal and addwhile (!stack.isEmpty() && precedence.get(c)<= precedence.get(stack.peek())) {postFix += stack.pop();}// add coming charstack.add(c);}}}// iterate till stack is emptywhile (!stack.isEmpty()) {postFix += stack.pop();}return postFix;}private static HashMap<Character, Integer> operatorPrecedence() {precedence.put('^', 10);precedence.put('*', 9);precedence.put('/', 9);precedence.put('-', 8);precedence.put('+', 8);return precedence;}}
C++
#include <bits/stdc++.h>using namespace std;map<char,int> precedence;map<char,int> operatorPrecedence(){precedence['^']=10;precedence['*']=9;precedence['/']=9;precedence['-']=8;precedence['+']=8;return precedence;}//function to convert from//prefix to infixstring prefixToInfix(string prefix){string infix = "";// stack for hold operatorstack<string> stack;// Iterate till given prefix lengthfor (int i = prefix.length() - 1; i >= 0; i--){// hold char from postFIxchar c = prefix[i];// If letter then add into stackif (isalpha(c)){//convert character//to stringstring strc(1,c);stack.push(strc);}else{// rule = first+operator+secondstring a = stack.top();stack.pop();string b = stack.top();stack.pop();//convert from character//to stringstring strc(1,c);stack.push(a + "" + strc + "" + b);}}// iterate till stack is emptywhile (!stack.empty()) {{infix += stack.top();stack.pop();}}return infix;}string infixToPostfix(string infix){string postFix = "";// stack for hold operatorstack<char> stack;// Iterate till given infix lengthfor (int i = 0; i < infix.size(); i++){// hold char from infixchar c = infix[i];// If letter then add into postfixif (isalpha(c)) {postFix += c;}else if (c == '('){stack.push(c);}else if (c == '^'){stack.push(c);}else if (c == ')'){ // if closing ) then// iterate till ( and add into postfixwhile (!stack.empty() && stack.top() != '(') {{postFix += stack.top();stack.pop();}}// if last char is '(' into stack then popif (!stack.empty() && stack.top() == '(')stack.pop();} else {// if precedence of c is greater then//top stack then addif (stack.empty() || precedence[c] >precedence[stack.top()]) {stack.push(c);} else {// iterate till precedence is less then equal and addwhile (!stack.empty() && precedence[c]<= precedence[stack.top()]) {{postFix += stack.top();stack.pop();}}// add coming charstack.push(c);}}}// iterate till stack is emptywhile (!stack.empty()) {{postFix += stack.top();stack.pop();}}return postFix;}int main(){// prefixstring prefix = "+A+*BC*/EF^GH";// precedence initializationoperatorPrecedence();// prefix to infixstring infix=prefixToInfix(prefix);//infix to postfixstring postFIx = infixToPostfix(infix);cout<<"Postfix is ";cout<<postFIx<<"\n";return 0;}
No comments:
Post a Comment