Question

i want similar for this code to solve two questions : 1- Write a program to...

i want similar for this code to solve two questions :

1- Write a program to convert a postfix expression to infix expression

2-Write a program to convert an infix expression to prefix expression

each question in separate code ( so will be two codes )

#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;

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

1)//function to convert postfix expression to infix expression

string PostToInfix(Stack *st, string postfix) {

  string infix = "";

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

     {

//If postfix[i]) is operator follow the given lines

//precedence is not needed in postfix expressions

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

     {

  while(st->top!=-1)

string op1;

string op2;

op1=st->top;

pop(st);

op2=st->top;

pop(st);

infix='('+op2+postfix[i]+op1+')';

  push(st,infix[i]);

     }

else

//this condition runs if postfix[i] is an operand

   push(st,postfix[i]);

    }

return infix;

}

int main() {

  Stack st;

  st.top = -1;

  string postfix;

cout << "Enter an postfix expression: ";

  getline(cin, postfix);

  cout << "Infix expression is: " << PostToInfix(&st,postfix);

  return 0;

}

2)//function to convert infix expression to prefix expression

string infixToprefix(Stack *st, string infix) {

  string prefix = "";

reverse(infix.begin(),infix.end());

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

{

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

infix[i] == ')';

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

infix[i] == '(';

}

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

     {

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

     {

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

push(st,infix[i]);

else if (precd(infix[i])==precd(st->s[st->top])&& (infix[i]=='^'))

{ prefix += pop(st);

pop(st);

}

else

   push(st,infix[i]);

     }

     

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

{

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

{

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

{   prefix += infix[i];

pop(st);

}

}

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

  push(st,infix[i])

return prefix;

}

}

int main() {

  Stack st;

  st.top = -1;

  string infix;

  cout << "Enter an infix expression: ";

  getline(cin, infix);

  cout << "Prefix expression is: " << infixToprefix(&st, infix);

  

  return 0;

}

Add a comment
Know the answer?
Add Answer to:
i want similar for this code to solve two questions : 1- Write a program to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • This code in C converts infix to postfix and evaluates it. The problem is that it...

    This code in C converts infix to postfix and evaluates it. The problem is that it only evaluates one digit expressions. I need to fix it so that it can evaluate 2 digits expressions as well. #include <stdio.h> #include <ctype.h> #include <string.h> #include <math.h> #define SIZE 100 char s[SIZE]; int top=-1; void infixToPostfix(char *infix, char *postfix); void postfixEvaluation(char *postfix); void push(char elem){ s[++top]=elem; } char pop(){ return(s[top--]); } int pr(char elem){ // Order of precedence switch (elem) { case '(':...

  • Help me to fix this code in C language. This code converts infix expressions to postfix and then evaluate the expression...

    Help me to fix this code in C language. This code converts infix expressions to postfix and then evaluate the expression. Right now, it works with single digits. I need to modify it so that it can evaluate expressions with also 2 digits, example: (60+82)%72. Additionally I need to display an error when the parenthesis don't match like (89+8(. I have muted some line that would print the postfix expression with a space like: 22 8 + when the input...

  • Finish function to complete code. #include <stdio.h> #include <stdlib.h> #include<string.h> #define Max_Size 20 void push(char S[],...

    Finish function to complete code. #include <stdio.h> #include <stdlib.h> #include<string.h> #define Max_Size 20 void push(char S[], int *p_top, char value); char pop(char S[], int *p_top); void printCurrentStack(char S[], int *p_top); int validation(char infix[], char S[], int *p_top); char *infix2postfix(char infix[], char postfix[], char S[], int *p_top); int precedence(char symbol); int main() { // int choice; int top1=0; //top for S1 stack int top2=0; //top for S2 stack int *p_top1=&top1; int *p_top2=&top2; char infix[]="(2+3)*(4-3)"; //Stores infix string int n=strlen(infix); //length of...

  • I need assistance with this code. Is there any way I can create this stack class (dealing with infix to postfix then postfix evaluation) without utilizing <stdio.h> and <math.h>? ________...

    I need assistance with this code. Is there any way I can create this stack class (dealing with infix to postfix then postfix evaluation) without utilizing <stdio.h> and <math.h>? ____________________________________________________________________________________________ C++ Program: #include <iostream> #include <string> #include <stdio.h> #include <math.h> using namespace std; //Stack class class STACK { private: char *str; int N; public: //Constructor STACK(int maxN) { str = new char[maxN]; N = -1; } //Function that checks for empty int empty() { return (N == -1); } //Push...

  • Stacks are used by compilers to help in the process of evaluating expressions and generating machine...

    Stacks are used by compilers to help in the process of evaluating expressions and generating machine language code.In this exercise, we investigate how compilers evaluate arithmetic expressions consisting only of constants, operators and parentheses. Humans generally write expressions like 3 + 4and 7 / 9in which the operator (+ or / here) is written between its operands—this is called infix notation. Computers “prefer” postfix notation in which the operator is written to the right of its two operands. The preceding...

  • Return a method as an expression tree Hi guys. I need to return a method as...

    Return a method as an expression tree Hi guys. I need to return a method as an expression tree, it's currently returning null. public static ExpressionTree getExpressionTree(String expression) throws Exception {       char[] charArray = expression.toCharArray(); Node root = et.constructTree(charArray); System.out.println("infix expression is"); et.inorder(root); return et; } In the above method, et needs to have a value. -- Take a look at the complete class below. Kindly assist. ; public class ExpressionTree extends BinaryTree { private static final String DELIMITERS...

  • Total point: 15 Introduction: For this assignment you have to write a c program that will...

    Total point: 15 Introduction: For this assignment you have to write a c program that will take an infix expression as input and display the postfix expression of the input. After converting the postfix expression, the program should evaluate the expression from the postfix and display the result. What should you submit? Write all the code in a single file and upload the .c file. Problem We as humans write math expression in infix notation, e.g. 5 + 2 (the...

  • Write a program (InfixToPostfix function) to convert an infix expression that includes +,-, * and /...

    Write a program (InfixToPostfix function) to convert an infix expression that includes +,-, * and / to postfix. int main(int argc, const char argv[l) cout <"Final Result : "<< InfixToPostfix("(5+3)*4" return The above main function should display the result below: Final Result 53+4* Here are some uiliy functions that you may find usefal int precedence(char ch) switch (ch) case case- return 1; case case ソ': return 2 '^': return 3; case

  • JAVA, please You must write a robust program meaning that your program should not crash with...

    JAVA, please You must write a robust program meaning that your program should not crash with any given data. Data validation must be done any time that user enters an input. Write a program that 1. Gets an infix expression form the user and evaluate the expression using stack ADT a. Finds the postfix equivalent of the given infix expression b. Evaluate the created postfix expression. c. Note: your program should not crash when you enter an invalid expression such...

  • We as humans write math expression in infix notation, e.g. 5 + 2 (the operators are...

    We as humans write math expression in infix notation, e.g. 5 + 2 (the operators are written in-between the operands). In a computer’s language, however, it is preferred to have the operators on the right side of the operands, i.e. 5 2 +. For more complex expressions that include parenthesis and multiple operators, a compiler has to convert the expression into postfix first and then evaluate the resulting postfix. Write a program that takes an “infix” expression as input, uses...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT