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

60.1K

Verified Solution

Question

Programming

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

#include

#include

#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 ornot.

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 meanshigher 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

     {

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

     {

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

  postfix += pop(st);

  push(st,infix[i]);

     }

     elseif(infix[i] == '(')

  push(st,infix[i]);

  

     elseif(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: \" <

  

  return 0;

}

Answer & Explanation Solved by verified expert
3.6 Ratings (613 Votes)
include    See Answer
Get Answers to Unlimited Questions

Join us to gain access to millions of questions and expert answers. Enjoy exclusive benefits tailored just for you!

Membership Benefits:
  • Unlimited Question Access with detailed Answers
  • Zin AI - 3 Million Words
  • 10 Dall-E 3 Images
  • 20 Plot Generations
  • Conversation with Dialogue Memory
  • No Ads, Ever!
  • Access to Our Best AI Platform: Flex AI - Your personal assistant for all your inquiries!
Become a Member

Other questions asked by students