Write a program that evaluates an arithmetic expression in postfix notation. The basic algorithm is contained in "Evaluating Postfix Expressions" on page 377. Assume the input 6 7 8 Programming Projects 391 contains numbers (but no variables) as well as the arithmetic operations +, -, *, and /. Your program should allow the user to evaluate additional expressions until the user wants to end the program. You might also enhance your program so that the expression need not be well formed; if it is not well formed, then the user must reenter the expression.

Respuesta :

Answer:

#include<iostream>

#include<cctype>

#include<stack>

using namespace std;

// returns the value when a specific operator

// operates on two operands

int eval(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;

}

}

// evaluates the postfix operation

// this module neither supports multiple digit integers

// nor looks for valid expression

// However it can be easily modified and some additional

// code can be added to overcome the above mentioned limitations

// it's a simple function which implements the basic logic to

// evaluate postfix operations using stack

int evalPostfix(char postfix[], int size) {

stack<int> s;

int i = 0;

char ch;

int val;

while (i < size) {

ch = postfix[i];

if (isdigit(ch)) {

// we saw an operand

// push the digit onto stack

s.push(ch-'0');

}

else {

// we saw an operator

// pop off the top two operands from the

// stack and evalute them using the current

// operator

int op1 = s.top();

s.pop();

int op2 = s.top();

s.pop();

val = eval(op1, op2, ch);

// push the value obtained after evaluating

// onto the stack

s.push(val);

}

i++;

}

return val;

}

// main

int main() {

char postfix[] = {'5','6','8','+','*','2','/'};

int size = sizeof(postfix);

int val = evalPostfix(postfix, size);

cout<<"\nExpression evaluates to "<<val;

cout<<endl;

return 0;

}

or else you can try even this

#include <iostream.h>

#include <stdlib.h>

#include <math.h>

#include <ctype.h>

const int MAX = 50 ;

class postfix

{

private :

int stack[MAX] ;

int top, nn ;

char *s ;

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 )

{

s = str ;

}

void postfix :: push ( int item )

{

if ( top == MAX - 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 n1, n2, n3 ;

while ( *s )

{

if ( *s == ' ' || *s == '\t' )

{

s++ ;

continue ;

}

if ( isdigit ( *s ) )

{

nn = *s - '0' ;

push ( nn ) ;

}

else

{

n1 = pop( ) ;

n2 = pop( ) ;

switch ( *s )

{

case '+' :

n3 = n2 + n1 ;

break ;

case '-' :

n3 = n2 - n1 ;

break ;

case '/' :

n3 = n2 / n1 ;

break ;

case '*' :

n3 = n2 * n1 ;

break ;

case '%' :

n3 = n2 % n1 ;

break ;

case '

ACCESS MORE
:

n3 = pow ( n2 , n1 ) ;

break ;

default :

cout << "Unknown operator" ;

exit ( 1 ) ;

}

push ( n3 ) ;

}

s++ ;

}

}

void postfix :: show( )

{

nn = pop ( ) ;

cout << "Result is: " << nn ;

}

void main( )

{

char expr[MAX] ;

cout << "\nEnter postfix expression to be evaluated : " ;

cin.getline ( expr, MAX ) ;

postfix q ;

q.setexpr ( expr ) ;

q.calculate( ) ;

q.show( ) ;

}

ACCESS MORE