Do this usign STL STACK The operators shall include +,-,*,/,(,),++,--, **, mod,
ID: 3554299 • Letter: D
Question
Do this usign STL STACK
The operators shall include +,-,*,/,(,),++,--, **, mod, div
Write a main.cpp that creates a few test example mathExp objects that demonstrates the working
of your mathExp class. (a sample is loaded in the attachments, add more expressions if you implement other operators, e.g. exp).
Challenge:
(Infix to Postfix) Write a program that converts an infix expression into an equivalent postfix expression. The rules to convert an infix expression into an equivalent postfix expression are as follows: Suppose infx represents the infix expression and pfx represents the postfix expression. The rules to convert infx into pfx are as follows:Explanation / Answer
#define SIZE 50 /* Size of Stack */
#include <ctype.h>
char s[SIZE];
int top = -1; /* Global declarations */
push(char elem) { /* Function for PUSH operation */
s[++top] = elem;
}
char pop() { /* Function for POP operation */
return (s[top--]);
}
int pr(char elem) { /* Function for precedence */
switch (elem) {
case '#':
return 0;
case '(':
return 1;
case '+':
case '-':
return 2;
case '*':
case '/':
return 3;
}
}
main() { /* Main Program */
char infx[50], pofx[50], ch, elem;
int i = 0, k = 0;
printf(" Enter the Infix Expression ? ");
scanf("%s", infx);
push('#');
while ((ch = infx[i++]) != '') {
if (ch == '(')
push(ch);
else if (isalnum(ch))
pofx[k++] = ch;
else if (ch == ')') {
while (s[top] != '(')
pofx[k++] = pop();
elem = pop(); /* Remove ( */
} else { /* Operator */
while (pr(s[top]) >= pr(ch))
pofx[k++] = pop();
push(ch);
}
}
while (s[top] != '#') /* Pop from stack till empty */
pofx[k++] = pop();
pofx[k] = ''; /* Make pofx as valid string */
printf(" Given Infix Expn: %s Postfix Expn: %s ", infx, pofx);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.