0% found this document useful (0 votes)
23 views2 pages

Dsa Lab 3

Data structures and algorithms lab experiment 3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views2 pages

Dsa Lab 3

Data structures and algorithms lab experiment 3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Experiment-3

// main.c
// PostfixEval

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
#include<math.h>
#define STACK_SIZE 20

void push(int item, int *top, int a[])


{
*top = *top + 1; //a[++*top] = item;
a[*top] = item;
}

int pop(int *top, int a[])


{
int item;
item = a[*top];
*top = *top - 1;
return item;
}

int calculate(int op1, int op2, char symbol)


{
switch(symbol)
{
case '+': return(op1 + op2);
case '-': return(op1 - op2);
case '*': return(op1 * op2);
case '/': return(op1 / op2);
case '%': return(op1 / op2);
case '^': return(pow(op1,op2));
}
}

void main()
{
int stk[STACK_SIZE], i, top = -1, op1, op2, res;
char exp[20], symbol;
printf("Enter postfix expression \n");
scanf("%s",exp);
for(i = 0; i < strlen(exp); i++)
{
symbol = exp[i];
if(isdigit(symbol))
{
push(symbol-'0', &top, stk);
}
else
{
op2 = pop(&top, stk);
op1 = pop(&top, stk);
res = calculate(op1, op2, symbol);
push(res, &top, stk);
}
}
printf("Result of evaluation of expression is %d\n",stk[top]);
}

———————————————————————————……………………………———————————————————————————

Following questions to be answered in lab record:

1. What is the main advantage of using postfix notation for


expression evaluation in computer systems?

2. How do you handle multi-digit numbers during postfix


evaluation?

3. If postfix expressions involve non-integer operands (e.g.,


floating-point numbers or complex numbers), what additional
considerations are necessary?

4. Analyze how the algorithm deals with edge cases, such as a


postfix expression with missing or extra operands.

You might also like