In: Computer Science
C++ OOP
•Write a program that evaluates a postfix expression (assume it’s valid) such as
6 2 + 5 * 8 4 / -
•The program should read a postfix expression consisting of digits and operators into a string.
•The algorithm is as follows:
1.While you have not reached the end of the string, read the expression from left to right.
–If the current character is a digit, Push its integer value onto the stack (the integer value of a digit character is its value in the computer’s character set minus the value of '0' in the computer’s character set).
–Otherwise, if the current character is an operator, Pop the two top elements of the stack into variables x and y.
–Calculate y operator x.
–Push the result of the calculation onto the stack.
2.When you reach the end of the string, pop the top value of the stack. This is the result of the postfix expression.
–[Example: In Step 2 above, if the operator is '/', the top of the stack is 2 and the next element in the stack is 8, then pop 2 into x, pop 8 into y, evaluate 8 / 2 and push the result, 4, back onto the stack. This note also applies to operator '–'.]
–The arithmetic operations allowed in an expression are
•+ addition
•– subtraction
•* multiplication
•/ division
•^ exponentiation
•% modulus
•[Note: We assume left-to-right associativity for all operators for the purpose of this exercise.] The stack should be maintained with stack nodes that contain an int data member and a pointer to the next stack node. You may want to provide the following functional capabilities:
a.function evaluatePostfixExpression that evaluates the postfix expression
b.function calculate that evaluates the expression (op1 operator op2)
c.function push that pushes a value onto the stack
d.function pop that pops a value off the stack
e.function isEmpty that determines if the stack is empty
f.function printStack that prints the stack
SAMPLE OUTPUT:
INPUT POSTFIX NOTATION: 23+2*
INFIX NOTATION: (2+3)*2
RESULT: 10
Postfix notation: It is a notation for writing arithmetic expressions in which the operands appear before their operators. /* C++ Program to Evaluate an Expression using Stacks*/
#include <iostream>
#include <conio.h>
#include <string.h>
using namespace std;
// Stack
struct node
{
int data;
node *next;
}*p = NULL, *top = NULL, *save = NULL, *ptr;
/* function capabilities*/
// function push()
void push(int x)
{
p = new node;
p->data = x;
p->next = NULL;
if (top == NULL)
{
top = p;
}
else
{
save = top;
top = p;
p->next = save;
}
}
// function pop()
char pop()
{
if (top == NULL)
{
cout<<"underflow!!";
}
else
{
ptr = top;
top = top->next;
return(ptr->data);
delete ptr;
}
}
//function IsEmpty()
bool pop()
{
if (top == NULL)
{
cout<<"Empty!!";
return true;
}
else
{
return false;
}
}
// function printStack()
char* printStack()
{
char a[100];
int i=0;
while (top!=null)
{
ptr = top;
top = top->next;
a[i++] = ptr->data;
}
return(a);
}
// Main function explaining the above algorithm
int main()
{
char x[30];
int a, b;
cout<<"enter the balanced expression\n";
cin>>x;
for (int i = 0; i < strlen(x); i++)
{
if (x[i] >= 48 && x[i] <= 57)
push(x[i]-'0');
else if (x[i] >= 42 && x[i] <= 47)
{
a=pop();
b=pop();
switch(x[i])
{
case '+':
push(a+b);
break;
case '-':
push(a-b);
break;
case '*':
push(a*b);
break;
case '/':
push(a/b);
break;
}
}
}
cout<<"ans is "<<pop()<<endl;
getch();
}