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

Question 2: Postfix notation (also known as Reverse Polish Notation or RPN in sh

ID: 3713172 • Letter: Q

Question

Question 2:

Postfix notation (also known as Reverse Polish Notation or RPN in short) is a mathematical notation in which operators follow all of its operands. It is different from infix notation in which operators are placed between its operands. The algorithm to evaluate any postfix expression is based on stack and is pretty simple: 1. Initialize empty stack 2. For every token in the postfix expression (scanned from left to right): 1. If the token is an operand (number), push it on the stack 2. Otherwise, if the token is an operator (or function): 1. Check if the stack contains the sufficient number of values (usually two) for given operator 2. If there are not enough values, finish the algorithm with an error 3. Pop the appropriate number of values from the stack 4. Evaluate the operator using the popped values and push the single result on the stack 3. If the stack contains only one value, return it as a final result of the calculation 4. Otherwise, finish the algorithm with an error Consider the following postfix expression: 34 613+ Complete the following statements to evaluate the above expression assuming that the Stack class has been done for you. You can also skip the checking steps in this example. For example: Test Result print(postfix.popO) 5.0 Answer: (penalty regime: 0 %) 1 postfix.push(3) 2 lpostfix.push(4)

Explanation / Answer

//variables names are different ... you can set them as you wish...

//if this helps you .. then pleaseprovide your positive feedback

Algorithm for Evaluation of Postfix Expression

**********************************************************8

program....in "c" //if you want it in other language then write to me in comment section...

****************************************************************************************

solution for question 2:

>>>>>>>>>>>>>>

#include<stdio.h>

#include<stdlib.h>

#define MAX 5 //Maximum number of elements that can be stored

int top=-1,stack[MAX];
void push();
void pop();
void display();

void main()
{
int ch;
  
while(1) //infinite loop, will end when choice will be 4
{
printf(" *** Stack Menu ***");
printf(" 1.Push 2.Pop 3.Display 4.Exit");
printf(" Enter your choice(1-4):");
scanf("%d",&ch);
  
switch(ch)
{
case 1: push();
break;
case 2: pop();
break;
case 3: display();
break;
case 4: exit(0);
  
default: printf(" Wrong Choice!!");
}
}
}

void push()
{
int val;
  
if(top==MAX-1)
{
printf(" Stack is full!!");
}
else
{
printf(" Enter element to push:");
scanf("%d",&val);
top=top+1;
stack[top]=val;
}
}

void pop()
{
if(top==-1)
{

}
else
{
printf(" Deleted element is %d",stack[top]);
top=top-1;
}
}

void display()
{
int i;
  
if(top==-1)
{
printf(" Stack is empty!!");
}
else
{
printf(" Stack is... ");
for(i=top;i>=0;--i)
printf("%d ",stack[i]);
}
}

*********************************

python program for question 1....

*******************************************

question 2:-

class Stack:   

def __init__(self):

****************************************************************************************

solution for question 2:

>>>>>>>>>>>>>>

#include<stdio.h>

#include<stdlib.h>

#define MAX 5 //Maximum number of elements that can be stored

int top=-1,stack[MAX];
void push();
void pop();
void display();

void main()
{
int ch;
  
while(1) //infinite loop, will end when choice will be 4
{
printf(" *** Stack Menu ***");
printf(" 1.Push 2.Pop 3.Display 4.Exit");
printf(" Enter your choice(1-4):");
scanf("%d",&ch);
  
switch(ch)
{
case 1: push();
break;
case 2: pop();
break;
case 3: display();
break;
case 4: exit(0);
  
default: printf(" Wrong Choice!!");
}
}
}

void push()
{
int val;
  
if(top==MAX-1)
{
printf(" Stack is full!!");
}
else
{
printf(" Enter element to push:");
scanf("%d",&val);
top=top+1;
stack[top]=val;
}
}

void pop()
{
if(top==-1)
{

}
else
{
printf(" Deleted element is %d",stack[top]);
top=top-1;
}
}

void display()
{
int i;
  
if(top==-1)
{
printf(" Stack is empty!!");
}
else
{
printf(" Stack is... ");
for(i=top;i>=0;--i)
printf("%d ",stack[i]);
}
}

*********************************

python program for question 1....

  def eval_postfix(text):      s = list()      for symbol in text:          if symbol in "0123456789":              s.append(int(symbol))            plus = None          elif not s.is_empty():              if symbol == "+":                  plus = s.pop() + s.pop()              elif symbol == "-":                  plus = s.pop() - s.pop()              elif symbol == "*":                  plus = s.pop() * s.pop()              elif symbol == "/":                  plus = s.pop() / s.pop()          if plus is not None:              s.append(plus)          else:               raise Exception("unknown value %s"%symbol)      return s.pop()

*******************************************

question 2:-

class Stack:   

def __init__(self):

         self.items = []      def isEmpty(self):          return self.items == []      def push(self, item):          self.items.insert(0,item)      def pop(self):          return self.items.pop(0)      def peek(self):          return self.items[0]      def size(self):          return len(self.items) s = Stack() s.push('hello') s.push('true') print(s.pop())
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote