Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Programming Languages Course) ((Explain this code in details with drawing the stack)) Lab Exercise Postfix Expression #include using namespace std; int max_length = 10 ;

Programming Languages Course)
((Explain this code in details with drawing the stack))
Lab Exercise
Postfix Expression
#include
using namespace std;
int max_length = 10 ;
class postfix
{
private :
int stack[10] ;
int top,y, x ;
char *t ;
public :
postfix( ) ;
void setexpr ( char *str ) ;
void push ( int item ) ;
int pop( ) ;
void calculate( ) ;
void show( ) ;
} ;
postfix :: postfix( )
{
top = -1 ;
}
void postfix :: setexpr ( char *str )
{
t = str ;
}
void postfix :: push ( int item )
{
if ( top == max_length - 1 )
cout << endl << "Stack is full" ;
else
{
top++ ;
stack[top] = item ;
}
}
int postfix :: pop( )
{
if ( top == -1 )
{
cout << endl << "Stack is empty" ;
return NULL ;
}
int data = stack[top] ;
top-- ;
return data ;
}
void postfix :: calculate( )
{
int a, b, z ;
while ( *t )
{
if ( *t == ' ' || *t == ' ')
{
t++ ;
continue ;
}
if ( isdigit ( *t ) )
{
x = *t - '0' ;
push ( x ) ;
}
else
{
a = pop( ) ;
b = pop( ) ;
switch ( *t )
{
case '+' :
z =b + a ;
break ;
case '-' :
z = b- a ;
break ;
case '/' :
z = b / a ;
break ;
case '*' :
z = b * a ;
break ;
default :
cout << "This operator is unknown " ;
exit ( 1 ) ;
}
push ( z ) ;
}
t++ ;
}
}
void postfix :: show( )
{
y= pop ( ) ;
cout << "the result after the calculation is: " << y <
}
int main( )
{
char expr[10];
cout << " Enter the postfix expression that you want: " ;
cin.getline ( expr, max_length ) ;
postfix x ;
x.setexpr ( expr ) ;
x.calculate( ) ;
x.show( ) ;
system(pause)
return 0;
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Application Development And Design

Authors: Michael V. Mannino

1st Edition

0072463678, 978-0072463675

More Books

Students also viewed these Databases questions

Question

What is an odds ratio?

Answered: 1 week ago