Question

In: Computer Science

this is my code I want the opposite i want to convert a postfix expression to...

this is my code I want the opposite i want to convert a postfix expression to infix expression

#include <iostream>

#include <string>

#define SIZE 50

using namespace std;

// structure to represent a stack

struct Stack {

  char s[SIZE];

  int top;

};

void push(Stack *st, char c) {

  st->top++;

  st->s[st->top] = c;

}

char pop(Stack *st) {

  char c = st->s[st->top];

  st->top--;

  //(A+B)*(C+D)

  return c;

}

/* function to check whether a character is an operator or not.

this function returns 1 if character is operator else returns 0 */

int isOperator(char c) {

  switch (c)

  {

  case '^':

  case '+':

  case '-':

  case '*':

  case '/':

  case '%':

  return 1;

  default:

  return 0;

  }

}

/* function to assign precedence to operator.

In this function we assume that higher integer value means higher precedence */

int precd(char c) {

  switch (c)

  {

  case '^':

  return 3;

  case '*':

  case '/':

  case '%':

  return 2;

  case '+':

  case '-':

  return 1;

  default:

  return 0;

  }

}

//function to convert infix expression to postfix expression

string infixToPostfix(Stack *st, string infix) {

  string postfix = "";

  for(int i=0; i<infix.length(); i++)

     {

     if(isOperator(infix[i])==1)

     {

  while(st->top!=-1 && precd(st->s[st->top]) >= precd(infix[i]))

  postfix += pop(st);

  push(st,infix[i]);

     }

     else if(infix[i] == '(')

  push(st,infix[i]);

  

     else if(infix[i] == ')')

     {

  while(st->top!=-1 && st->s[st->top] != '(')

  postfix += pop(st);

  pop(st);

     }

     else

  postfix += infix[i];

  

  }

  while(st->top != -1)

  postfix += pop(st);

return postfix;

}

int main() {

  Stack st;

  st.top = -1;

  string infix;

  cout << "Enter an infix expression: ";

  getline(cin, infix);

  cout << "Postfix expression is: " << infixToPostfix(&st, infix);

  

  return 0;

}

Solutions

Expert Solution

#include <bits/stdc++.h>
using namespace std;
bool isOperand(char x) {
   return (x >= 'a' && x <= 'z') || (x >= 'A' && x <= 'Z');
}
string PostfixToInfix(string postfix) {
   stack<string> infix;
   for (int i=0; postfix[i]!='\0'; i++) {
      if (isOperand(postfix[i])) {
         string op(1, postfix[i]);
         infix.push(op);
      } else {
         string op1 = infix.top();
         infix.pop();
         string op2 = infix.top();
         infix.pop();
         infix.push("{"+op2+postfix[i]+op1 +"}");
      }
   }
   return infix.top();
}
int main() {
   string infix, postfix;
   cout<<"Enter a POSTFIX expression:<< postfix << endl;
   cin>> postfix;
   cout<<"POSTFIX Expression: "<< postfix << endl;
   infix = PostfixToInfix(postfix);
   cout<< endl << "INFIX Expression: " << infix;
   return 0;
}

Related Solutions

Convert an infix expression to its postfix representation in JAVA
Convert an infix expression to its postfix representation in JAVA
(Convert infix to postfix) Note: Postfix notation is a way of writing expression without using parentheses....
(Convert infix to postfix) Note: Postfix notation is a way of writing expression without using parentheses. For example, the expression ( 11 + 12 ) * 13 would be written as 11 12 + 13 * Assume that ALWAYS there is a space between operands and operators in the input expression. Use two stacks, one to store the operands and one to store the operators. Your program only accpets following operators : ( ) + - / * Write a...
2. Convert the following infix form expression into the postfix form expression by using a stack:...
2. Convert the following infix form expression into the postfix form expression by using a stack: A+(B*(C-D)+E)/F-G*H Show the stack after each push/pop.
implement the algorithm described in this chapter to convert a prefix expression to postfix form. involve...
implement the algorithm described in this chapter to convert a prefix expression to postfix form. involve the classes that programming problems 4 and 5 describe This is to be written in C++. I cannot provide anymore information, I cannot provide any class information, etc. This is all the problem that book gave me. This is for Data Structures and algorithms.
Python I want to name my hero and my alien in my code how do I...
Python I want to name my hero and my alien in my code how do I do that: Keep in mind I don't want to change my code except to give the hero and alien a name import random class Hero:     def __init__(self,ammo,health):         self.ammo=ammo         self.health=health     def blast(self):         print("The Hero blasts an Alien!")         if self.ammo>0:             self.ammo-=1             return True         else:             print("Oh no! Hero is out of ammo.")             return False     def...
This is my code I want the average temp for the week to be printed when...
This is my code I want the average temp for the week to be printed when the user types : 'week' currently when the user types  'week' it only prints  Monday - Sunday and the average temp for each day. import java.util.Arrays; import java.util.ArrayList; import java.util.Scanner; public class weeklytemps {    public static void main(String[] args) {        Scanner input = new Scanner(System.in);                  ArrayList Day = new ArrayList(Arrays.asList(    "Monday","Tuesday","Wednesday","Thurday","Friday","Saturday","Sunday")); // Stores days of the week...
I want to know the details interpretation of this code for my interview preperation on this...
I want to know the details interpretation of this code for my interview preperation on this code. This is A* algorithm in c++ #include<bits/stdc++.h> using namespace std; #define DISCONNECTED -1 int num_of_node,num_of_edge,graph[30][30],pathCost[30]; void init_Heuristic(); struct Node{     int from,to;     int cost; }; struct CompareNode{     bool operator()(Node& n1, Node& n2){         if(n1.cost > n2.cost)return true;         return false;     } }; map<int,int>Heuristic; priority_queue<Node, vector<Node>, CompareNode> PQ; vector<Node>path; void AStarSearch(int source,int destination){     init_Heuristic();     for(int i = 1; i...
Write the code for postfix expression in C++ using a linked stack that can take numbers...
Write the code for postfix expression in C++ using a linked stack that can take numbers bigger than 9 (any size the user gives) and pushes the final result onto the top of the stack
My code is working fine but after selecting an option and at the end I want...
My code is working fine but after selecting an option and at the end I want user could enter another option again without going back to menu. However, in my code when I enter another option, it prints the whole menu again then I can select option.Could anyone help me on that? #include "stdafx.h" #include #include #include using namespace std; const double pi = 3.14159265358979323846; const double deg_to_rad = (pi / 180); int main() { int option; do { cout...
I want to convert those codes to assembler code // Problem 1 // for loop J=5...
I want to convert those codes to assembler code // Problem 1 // for loop J=5 for(i=1; i<5; i++) {         j-- } // Problem 2 // if - then - else i=4 if (i < 5) then         j = 3 else         j = 2 // Problem 3 //while loop i = 0 j = 0 while(i==0) {   j++   if j = 5 then         i = j }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT