4

 #include<stdio.h>

#include<conio.h>

#include<ctype.h>

#include<process.h>

#define MAX_SIZE 25

int s[MAX_SIZE],top=-1;

char expr[25];

void main()

{

void PUSH(int);

int POP();

int i=0,op1,op2,op3;

clrscr();

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

gets(expr);

while(expr[i]!='\0')

{

if(isdigit(expr[i]))

PUSH(expr[i]-'0');

else

{

op2=POP();

op1=POP();

switch(expr[i])

{

case '+':op3=op1+op2;

break;

case '-':op3=op1-op2;

break;

case '*':op3=op1*op2;

break;

case '/':op3=op1/op2;

break;

default: printf("\n invalid operator %c \n",expr[i]);

getch();

exit(0);

}

PUSH(op3);

}

i++;

}

if(top>0)

printf("\n invalid input expression %s \n",expr);

else

printf("\n the result of the expression %s is %d \n",expr,s[top]);

getch();

}

void PUSH(int x)

{

if(top>=MAX_SIZE-1)

{

printf("\n stack overflow \n");

getch();

exit(0);

}

s[++top]=x;

}

int POP()

{

if(top==-1)

{

printf("\n invalid infix expression %s \n",expr);

getch();

exit(0);

}

return s[top--];

}

Comments

Popular posts from this blog

Start system call

Implementation