Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Implement the following function. You may use the stack template class and the s

ID: 3673579 • Letter: I

Question

Implement the following function. You may use the stack template class and the stack operations of push, pop, peek, is_empty, and size. You may also use cin.peek( ) and use "cin>>i" to read an integer. int evaluate_postfix_from_cin( ) // Precondition (Which is not checked): The next input line of cin is a // properly formed postfix expression consisting of integers and // the binary operations // Postcondition: The function has read the next input line (including // the newline) and returned the value of the postfix expression. Test it in the main()

the pictures show an example

Postfix Expression Infix expression is the form AOB - A and B are numbers or also infix expression O is operator (+, -, *, /) Postfix expression is the form ABO - A and B are numbers or also postfix expression -O is operator (+, -, *,/) From Postfix to Answer . The reason to convert infix to postfix expression is that we can compute the answer of postfix expression easier by

Explanation / Answer

Good wishes,

First let me give the code to convert a given infix notation to postfix notation.

PROGRAM TO CONVERT INFIX TO POSTFIX:

Next let me give you the code to evaluate a postfix expression.

FUNCTION TO EVALUATE A POSTFIX EXPRESSION:

EXPLAINATION:

Initially I stored the user input which is an infix expression in variable infix_exp.

printf(" Enter Infix expression in parentheses: ");
gets(infix_exp);

Then I traversed each element in the infix expression

item=infix_exp[i++];
while(item != '')
{

when '(' is encounted I placed in the stack named 'stack',

if(item == '(')
{
push(item);
}

if an integer is encountered i placed it in the stack

else if((item>=0)&&(item<=9))
{
postfix_exp[j++] = item;
}

if an operator is encountered then i checked its precedence with those already placed in tha stack and placed it accordingly in the stack.

else if(is_operator(item) == 1)
{
x=pop();
while(is_operator(x) == 1 && precedence(x)
>= precedence(item))
{
postfix_exp[j++] = x;
x = pop();
}
push(x);
push(item);          
}

if ')' is encounted then I repeatedly popped from the stack until '(' is encountrred

and placed them in post fix expression called postfix_exp

else if(item == ')')
{
x = pop();
while(x != '(')
{
postfix_exp[j++] = x;
x = pop();
}
}

Finally when the stack is empty

the value in postfix_exp is the postfix expression

postfix_exp[j++] = '';
printf(" Arithmetic expression in Postfix notation: ");
puts(postfix_exp);

Then using calculator() function

I traversed each element in the postfix_exp and then when an operator is encountered

when a integer is encountered I pushed it into the stack

when an operator is encountered

I popped the top two elements in the stack and then performed the operation and stored the result in top of the stack.

This is done repeatedly until the postfix_exp is empty and the result is printed.

Hope this is clear.