0% found this document useful (0 votes)
13 views5 pages

9 (B) - Stack Application - Postfix Evaluation

Uploaded by

gokula1310
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)
13 views5 pages

9 (B) - Stack Application - Postfix Evaluation

Uploaded by

gokula1310
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/ 5

EXP. NO.

: 9 - B
STACK APPLICATION –
POSTFIX EVALUATION
DATE: 22 - 10 - 2024

AIM:
To write a C program to evaluate postfix expression using stack.

ALGORITHM:

STEP – 1: Start the program and include required header files.


STEP – 2: Define a macro 'MAX' of '100'
STEP – 3: Declare a integer array 'stack' of size 'MAX' and variable 'top' and initialize with '-
1'.
STEP – 4: Define a 'push(int x)' function which acts as a push function of stack.
STEP – 5: Define a 'pop()' function which acts as a pop function of stack.
STEP – 6: Open the main()
STEP – 7: Declare a character array 'exp' of size 'MAX'.
STEP – 8: Get the postfix expression from user and store it in 'exp'.
STEP – 9: Call 'evaluatePostfix()' function with argument 'exp' and display the result.
STEP – 10: Stop the program.

evaluatePostfix()

STEP – 1: Declare an integer variable 'i' and initialize with '0'.


STEP – 2: Run the while loop until 'exp[i]' becomes '\0'. If true go to step 3 , else go to step
7.
STEP – 3: Call 'isdigit()' with argument 'exp[i]. if true call 'push()' with ' exp[i] - '0' ' and go
to step 6 , else go to step 4.
STEP – 4: Call 'pop()' twice , and store the results in 'val2' and 'val1'.
STEP – 5: Pass 'exp[i]' as a condition to switch statement, if it is '+' , then call 'push()' with
argument 'val1 + val2 and goto step 6. Same for rest of the operators.
STEP – 6: Increment i by 1 and go to step 2.
STEP – 7: Return 'pop()' and exit the function.
PROGRAM :

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

int s[20];
int t = -1;

void push(int x)
{
s[++t] = x;
}

int pop()
{
if(t == -1)
return -1;
else
return s[t--];
}

bool isdigit(char x)
{
if(x >= '0' && x <= '9')
return true;
return false;
}

int main()
{
char p[25] = {'\0'} , e;
int i , n1 , n2 , ans;
printf("\nEnter Postfix Expression : ");
scanf("%s" , p);
printf("\nPostfix Expression : %s" , p);
i = 0;
while(i <= (strlen(p) - 1))
{
e = p[i];
if(isdigit(e))
{
push(atoi(e));
}
else
OUTPUT:
{
n1 = pop();
n2 = pop();
switch(e)
{
case '^':
ans = pow(n2 , n1);
break;
case '/':
ans = n2 / n1;
break;
case '*':
ans = n2 * n1;
break;
case '-':
ans = n2 - n1;
break;
case '+':
ans = n2 + n1;
break;
}
push(ans);
}
i++;
printf("\nAnswer : %d" , pop());
}
printf(“\n\nProgram done by : “);
printf(“\nName : Gokula Krishnan”);
printf(“\nRegister no. : 2303717710421013);
return 0;
}

PREPARATION 15
OBSERVATION 15
IMPLEMENTATION AND OUTPUT 20
VIVA - VOCE 15
RECORD 10
TOTAL 75

RESULT:
Thus, the C program to evaluate postfix expression using stack is successfully
developed and executed.

You might also like