0% found this document useful (0 votes)
3 views

DSA02-postfixevaluation

The document outlines a lab experiment for evaluating postfix expressions using a stack in C programming. It includes a code implementation that reads a postfix expression, processes it, and outputs the result. The example provided demonstrates the evaluation of the expression '(2 2 +)', resulting in an output of 4.

Uploaded by

dhageatharv06
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

DSA02-postfixevaluation

The document outlines a lab experiment for evaluating postfix expressions using a stack in C programming. It includes a code implementation that reads a postfix expression, processes it, and outputs the result. The example provided demonstrates the evaluation of the expression '(2 2 +)', resulting in an output of 4.

Uploaded by

dhageatharv06
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Shri Vile Parle Kelavani Mandal's

INSTITUTE OF TECHNOLOGY
DHULE (M.S.)
DEPARMENT OF COMPUTER ENGINEERING
Remark
Subject : Data Syructure Lab

Name : Dhage Atharv Vijaykumar Roll No. : 30

Class :S.Y. Computer Batch : S2 Division: B


Signature
Expt. No. :02 Date :

Title : Postfix Evaluation

code : Evaluate the Postfix expression

#include <stdio.h>
#include <stdlib.h>

#define STACKSIZE 100


#define ARRYSIZE 100

int stack[STACKSIZE];
int top = -1;

void push(int x)
{

if (top >= STACKSIZE - 1) {


printf("stack over flow");
return;
}
else {
top = top + 1;
stack[top] = x;
}
}

int pop()
{
int y;
if (top < 0) {
printf("stack under flow");
}
else {
y = stack[top];
top = top - 1;
return y;
}
}

void EvaluatePostfix(char postfix[])


{

int i;
char ch;
int C;
int A, B;

for (i = 0; postfix[i] != ')'; i++) {


ch = postfix[i];
if (isdigit(ch)) {

push(ch - '0');
}
else if (ch == '+' || ch == '-' || ch == '*' || ch == '/') {
A = pop();
B = pop();

switch (ch)
{
case '*':
C = B * A;
break;

case '/':
C = B / A;
break;

case '+':
C = B + A;
break;

case '-':
C = B - A;
break;
}

push(C);
}
}
printf(" \n The Ans is : %d \n", pop());
}

int main()
{

int i;

char postfix[ARRYSIZE];
printf("Enter postfix experssion and also enter opening and
closing brackets.\n");
for (i = 0; i <= ARRYSIZE - 1; i++) {
scanf("%c", &postfix[i]);

if (postfix[i] == ')')
{
break;
}
}

EvaluatePostfix(postfix);

return 0;
}

Output:

Enter postfix experssion and also enter opening and closing


brackets.
(
2
2
+
)
The Ans is : 4

You might also like