0% found this document useful (0 votes)
15 views4 pages

Experiment No 2

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)
15 views4 pages

Experiment No 2

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

Experiment no 2

Aim – Write a program to evaluate a given postfix expression using stacks.

#include<stdio.h>

#include<conio.h>

void sum();

void diff();

void mult();

void div();

void power();

int stack [50],top=-1;

main ( )

char st[30];

int i=0;

printf("\n enter a postfix expression:");

scanf("%[^\n]s",st);

for(i=0;st[i]!='\0';i++)

switch(st[i])

case'+' : sum();

break;

case'-' : diff();

break;
case'*' : mult();

break;

case'/' : div();

break;

case'^': power();

break;

default:

top++;

stack[top]=st[i]-48;

printf("\n the result is =%d",stack[top]);

getch();

void sum()

int res,op1,op2;

op1=stack[top];

top--;

op2=stack[top];

top--;

res=op2+op1;

top++;

stack[top]=res;

void diff()
{

int res,op1,op2;

op1=stack[top];

top--;

op2=stack[top];

top--;

res=op2-op1;

top++;

stack[top]=res;

void mult()

int res,op1,op2;

op1=stack[top];

top--;

op2=stack[top];

top--;

res=op2*op1;

top++;

stack[top]=res;

void div()

int res,op1,op2;

op1=stack[top];

top--;

op2=stack[top];

top--;

res=op2/op1;

top++;

stack[top]=res;
}

void power()

int res=1,op1,op2,i;

op1=stack[top];

top--;

op2=stack[top];

top--;

for(i=0;i<op1;i++)

res=res*op2;

top++;

stack[top]=res;

Input – Enter the postfix expression = 6523+8*+3+*

Output – The result is 288.

You might also like