Write a program to convert Infix to Postfix using stack
Example 1:
Input: infix="A+B*C+(E/F)*G^H"
Output: postfix = "ABC*+EF/GH^*+"
Approach :
Java
import java.util.HashMap;import java.util.Stack;public class InfixToPostfix {static HashMap<Character, Integer> precedence =new HashMap<Character, Integer>();public static void main(String[] args) {// infixString infix = "A+B*C+(E/F)*G^H";// precedence initializationoperatorPrecedence();// infix to postfixString postFIx = infixToPostfix(infix);System.out.println("Postfix is " + postFIx);}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;}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(){// infixstring infix = "A+B*C+(E/F)*G^H";// precedence initializationoperatorPrecedence();// infix to postfixstring postFIx = infixToPostfix(infix);cout<<"Postfix is ";cout<<postFIx<<"\n";return 0;}
No comments:
Post a Comment