Data Structure: Unit II (a): Stacks

Evaluation of Arithmetic Expression

Example Stacks Operations with Example C Programs | ADT Data Structure

As we have seen how to convert given infix expression to postfix form. It's the time to learn an evaluation of postfix expression. Again use of stack is necessary in postfix expression evaluation.

Evaluation of Arithmetic Expression

As we have seen how to convert given infix expression to postfix form. It's the time to learn an evaluation of postfix expression. Again use of stack is necessary in postfix expression evaluation. But here we will use the stack for storing operands rather than operators. Operands are taken as numeric values. Let us see an algorithm first,

Algorithm for evaluation of postfix.

1. Read the postfix expression from left to right.

2.  If the input symbol read is an operand then push it on to the stack.

3. If the operator is read POP two operands and perform arithmetic operations if operator is

+ then result = operand 1 + operand 2

- then result = operand 1 - operand 2

* then result = operand 1 * operand 2

/ then result = operand 1 / operand 2

4.. Push the result onto the stack.

5. Repeat steps 1-4 till the postfix expression is not over.

Ex. 2.8.1: Evaluate given postfix expression. ABC-BA + C $ for A = 1, B = 2 and C = 3

Sol. Now as per the algorithm of postfix expression evaluation, we scan the input from left to right. If operand comes we push them onto the stack and if we read any operator, we must pop two operands and perform the operation using that operator. Here $ is taken as exponential operator.

Ex. 2.8.2 Program for evaluation of postfix expression.

Sol. : /*****************************************************

Program to evaluate a given postfix expression.

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

#include <stdio.h>

#include <conio.h>

#include <stdlib.h>

#include <string.h>

#include <math.h>

#define size 80

/*declaration of stack data structure*/

struct stack

{

double s[size];

int top;

}st;

void main()

{

char exp[size];

int len;

double Result;

double post();

clrscr();

printf("Enter the postfix Expression\n");

scanf("%s", exp);

len = strlen(exp);

exp[len] = '$'; /* Append $ at the end as a endmarker*/

Result = post(exp);

printf("The Value of the expression is %f\n", Result);

getch();

exit(0);

}

double post(char exp[])

{

char ch,*type;

double result, val, op1, op2;

void push(double);

double pop();

int i;

st.top = 0;

i=0;

ch= exp[i];

while (ch!= '$')

{

if (ch >= '0' && ch <= '9')

type="operand";

else if (ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == ('^')

type="operator";

if(strcmp(type,"operand")==0)/*if the character is operand*/

{

val = ch - 48;

push(val);

}

else

{

if (strcmp(type, "operator")==0)/*if it is operator*/ .

op2 = pop();

op1 = pop();

switch(ch)

}

case '+' : result = op1 + op2;

break;

case '-' : result = op1 - op2;

break;

case '*': result = op1 * op2;

break;

case '/' : result = op1 / op2;

break;

case '^' : result =

pow(op1,op2);

break;

}/* switch */

push(result);

}

i++;

ch=exp[i];

} /* while */

result = pop(); /*pop the result*/

return(result);

}

void push(double val)

{

if (st.top+1 >= size)

printf("\nStack is Full\n");

st.top++;

st.s[st.top] = val;

}

double pop()

{

double val;

if(st.top == -1)

printf("\nStack is Empty\n");

val = st.s[st.top];

st.top--;

return(val);

}

Output

Enter the postfix Expression

12+34*+

The Value of the expression is 15.0000

Logic of evaluation of postfix expression [Refer the above program]

Let us take some example of postfix expression and try to evaluate it 123+*

Step 1: Assume the array exp[ ] contains the input

The $ symbol is used as an end marker. Read from first element of the array if it is operand push it onto the stack.

Step 2: If the operator isv read pop two operands

Step 3:

Again pop two operands and perform the operation.

Step 4:

At this point the stack is empty and the input is read as $. So here stop the evaluation procedure and return the result = 5.

Ex. 2.8.3: Write an algorithm to convert a infix expression into postfix expression. Consider following arithmatic expression in postfix notation:

752 + * 415 -/-

i) Find the value of the expression.

ii) Find the equivalent prefix form of above expression.

Sol.:Algorithm to convert postfix expression to infix - Refer section 2.7.

i) Let, the given postfix expression be

752+*415-/-

ii) Let 752 + * 415 -/-

Step 1: Read expression from left to right. Go on pushing operands onto the stack. When operator is read, pop two operands and form prefix expression push it onto the stack.

Ex. 2.8.4 Evaluate the following expression using stack 5 6 2 + * 84/-.

Sol.:

The result of evaluation is 38.

Ex. 2.8.5 Write the procedure to convert the infix expression to postfix expression and steps involved in evaluating the postfix expression. Convert the expression A-(B/C+(D%E*F)/F)*H to postfix form. Evaluate the given postfix expression 9 3 4 8 +4/

Sol. Procedure to convert infix to postfix: Refer section 2.7.

 Conversion of A-(B/C+(D%E*F)/F)*H

The result of evaluation is 4

Review Question

1. Discuss any two applications of stack with relevant examples.

Data Structure: Unit II (a): Stacks : Tag: : Example Stacks Operations with Example C Programs | ADT Data Structure - Evaluation of Arithmetic Expression