Write a program that will read postfix expression that may contain only integer
ID: 3731960 • Letter: W
Question
Write a program that will read postfix expression that may contain only integer numbers and operators (consider only binary operators: /, *, -, and + ) which are delimited by single or more white space. Then the program will calculate the value of the expression and print it.
(If input postfix expression is not valid appropriate error message will be printed such as missing operand, missing operator or invalid symbol in the expression)
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:
1)Initialize pfx to an empty expression and also initialize the stack.
2)While there are symbols in the infx expression, Get the next symbol, sym, from infx.
a)If sym is an operand, append sym to pfx.
b)If is (, push sym into the stack.
c)If sym is ), pop and append all the symbols from the stack until the most recent left parenthesis. Pop and discard the left parenthesis.
d)If sym is an operator:
d.1)If s is not empty ,Pop and append all the operators from the stack to pfx that are above the most recent left parenthesis and have precedence greater than or equal to sym.
d.2)Push sym onto the stack.
3)After processing infx, some operators might be left in the stack. Pop and append to pfx everything from the stack.
In this program, you will consider the (binary) arithmetic operators: +, -, *, and /, operands are single letters. In valid infix expression operands and operators are delimited with one or more white space.
You may assume that the expressions you will process are error free.
Design a class that stores the infix and postfix strings. The class must include the following operations:
-infix: string
-postfix: string
+getInfix—Stores the infix expression
+showInfix—Outputs the infix expression
+showPostfix—Outputs the postfix expression
Some other operations that you might need are the following:
+convertToPostfix—Converts the infix expression into a postfix expression. The resulting postfix expression is stored in pfx.
-precedence—Determines the precedence between two operators. If the first operator is higher or equal precedence than the second operator, it returns the value true; otherwise, it returns the value false.
Include the constructors and destructors for automatic initialization and dynamic memory deallocation.
Test your program on the following five expressions:
A + B - C;
(A + B ) * C;
(A + B) * (C - D);
A + ((B + C) * (E - F) - G) / (H - I);
A + B * (C + D ) - E / F * G + H;
For each expression, your answer must be in the following form:
Infix Expression: A + B - C;
Postfix Expression: A B + C -
Explanation / Answer
This is the C++ code to convert infix to postfix
#include<stack>
int length(char input[]){
int i=0;
while(input[i])
i++;
return i;
}
//Function to return priority of operators
int priority(char c)
{
if(c == '*' || c == '/')
return 2;
else if(c == '+' || c == '-')
return 1;
else
return -1;
}
// returns the value when a specific operator operates on two operands
int evaluate(int op1, int op2, char operate) {
switch (operate) {
case '*': return op2 * op1;
case '/': return op2 / op1;
case '+': return op2 + op1;
case '-': return op2 - op1;
default : return 0;
}
}
// convert infix expression to postfix expression
void infixToPostfix(char *input)
{
stack<char> s;
int len = length(input),index=0;
char output[1000];
for(int i = 0; i < len; i++)
{
if((input[i] >= '0' && input[i] <= '9')) {
output[index++] = input[i];
} else if(input[i] == '(') {
s.push('(');
} else if(input[i] == ')')
{
// If the scanned character is an ‘)’, pop and to output string from the stack until
an ‘(‘ is encountered.
while(!s.empty() && s.top() != '(')
{
char c = s.top();
s.pop();
output[index++] = c;
}
if(s.top() == '(')
{
char c = s.top();
s.pop();
}
} else{
while(!s.empty() && priority(input[i]) <= priority(s.top()))
{
char c = s.top();
s.pop();
output[index++]= c;
}
s.push(input[i]);
}
}
//Pop all the remaining elements from the stack
while(!s.empty())
{
char c = s.top();
s.pop();
output[index++] = c;
}
output[index] = '';
cout << output << " ";
//copying to change input expression from calling function evaluatePostfix
int i=0;
while(i < length(output)){
input[i] = output[i];
i++;
}
input[i] = '';
}
void evalPostfix(char postfix[]) {
stack<int> s;
int i = 0;
char ch;
int val;
int len = length(postfix);
while (i < len) {
ch = postfix[i];
if (isdigit(ch)) {
s.push(ch-'0');
}
else {
int op1 = s.top();
s.pop();
int op2 = s.top();
s.pop();
val = evaluate(op1, op2, ch);
s.push(val);
}
i++;
}
cout << val;
}
void evaluatePostfix(char exp[]) {
infixToPostfix(exp);
evalPostfix(exp);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.