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 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...
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...
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 }
Write a program that takes an infix expression as input and produces a postfix expression. The...
Write a program that takes an infix expression as input and produces a postfix expression. The infix and postfix expressions are in the form of vectors of strings. We will test with a main method that takes a file name as input from the command line. We will test your implementation using printPostFix() method.   There are sample input and output files in this folder. You may edit the header file. Example 1 infix expression = apple + banana * cat...
Problem 2: Evaluate the following postfix expression, using the rules given in Section I of Lab...
Problem 2: Evaluate the following postfix expression, using the rules given in Section I of Lab 10: 1 5 4 – 3 + * 3.   Computer Science
Postfix Evaluation (JAVA PROGRAMMING) Write class PostfixEva1uator that evaluates a postfix expression such as 6 2...
Postfix Evaluation (JAVA PROGRAMMING) Write class PostfixEva1uator that evaluates a postfix expression such as 6 2 + 5 * 8 4 / - The program should read a postfix expression consisting of single digits and operators into a StringBuilder, The program should read the expression and evaluate it (assume it's valid). The algorithm to evaluate a postfix expression is shown below. Use +, -, *, /, and ^. ^ is the exponent. Append a right parenthesis ') ' to the...
I want my answer typed.                                      I. What i
I want my answer typed.                                      I. What is presbyopia? Hyperopia? Myopia? 2. Which cranial nerves assesses eye function and acoustic function? 3. What is the Rinne test? Weber test? 4. Do you or anyone you know have any of the issues/symptoms/conditions mentioned in the video? Is it due to genetics or aging? How is it being managed?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT