0% found this document useful (0 votes)
29 views8 pages

Exp 5

This document contains code to convert an infix expression to postfix notation and then evaluate the postfix expression. It defines functions for operations like pushing and popping from a stack, checking operator precedence, and evaluating the postfix expression. The code takes an infix expression as input, converts it to postfix, and then prints the postfix form and results of evaluating it.

Uploaded by

Sunil Motihar
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views8 pages

Exp 5

This document contains code to convert an infix expression to postfix notation and then evaluate the postfix expression. It defines functions for operations like pushing and popping from a stack, checking operator precedence, and evaluating the postfix expression. The code takes an infix expression as input, converts it to postfix, and then prints the postfix form and results of evaluating it.

Uploaded by

Sunil Motihar
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 8

//Exp-5(infix-to-postif)

#include<stdio.h>

#include<conio.h>

#include<string.h>

#define Blank ' '

#define Tab '\t'

#define MAX 50

long int pop ();

long int eval_post();

char infix[MAX], postfix[MAX];

long int stack[MAX];

int top;

main()

clrscr();

long int value;

char choice='y';

while(choice == 'y')

top = 0;

printf("Enter infix : ");

fflush(stdin);

gets(infix);
infix_to_postfix();

printf("Postfix : %s\n",postfix);

value=eval_post();

printf("Value of expression : %ld\n",value);

printf("Want to continue(y/n) : ");

scanf("%c",&choice);

}/*End of main()*/

infix_to_postfix()

int i,p=0,type,precedence,len;

char next ;

stack[top]='#';

len=strlen(infix);

infix[len]='#';

for(i=0; infix[i]!='#';i++)

if( !white_space(infix[i]))

switch(infix[i])

case '(':
push(infix[i]);

break;

case ')':

while((next = pop()) != '(')

postfix[p++] = next;

break;

case '+':

case '-':

case '*':

case '/':

case '%':

case '^':

precedence = prec(infix[i]);

while(stack[top]!='#' && precedence<= prec(stack[top]))

postfix[p++] = pop();

push(infix[i]);

break;

default: /*if an operand comes */

postfix[p++] = infix[i];

}/*End of switch */

}/*End of if */

}/*End of for */

while(stack[top]!='#')
postfix[p++] = pop();

postfix[p] = '\0' ; /*End postfix with'\0' to make it a string*/

}/*End of infix_to_postfix()*/

/* This function returns the precedence of the operator */

prec(char symbol )

switch(symbol)

case '(':

return 0;

case '+':

case '-':

return 1;

case '*':

case '/':

case '%':

return 2;

case '^':

return 3;

}/*End of switch*/

}/*End of prec()*/

push(long int symbol)

{
if(top > MAX)

printf("Stack overflow\n");

exit(1);

else

top=top+1;

stack[top] = symbol;

}/*End of push()*/

long int pop()

if (top == -1 )

printf("Stack underflow \n");

exit(2);

else

return (stack[top--]);

}/*End of pop()*/

white_space(char symbol)

{
if( symbol == Blank || symbol == Tab || symbol == '\0')

return 1;

else

return 0;

}/*End of white_space()*/

long int eval_post()

long int a,b,temp,result,len;

int i;

len=strlen(postfix);

postfix[len]='#';

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

if(postfix[i]<='9' && postfix[i]>='0')

push( postfix[i]-48 );

else

a=pop();

b=pop();

switch(postfix[i])

case '+':
temp=b+a; break;

case '-':

temp=b-a;break;

case '*':

temp=b*a;break;

case '/':

temp=b/a;break;

case '%':

temp=b%a;break;

case '^':

temp=pow(b,a);

}/*End of switch */

push(temp);

}/*End of else*/

}/*End of for */

result=pop();

return result;

getch();

}/*End of eval_post */

output

Enter infix : 5+4*2

Postfix : 542*+
Value of expression : 13

Want to continue(y/n) : n

You might also like