Exp 5
Exp 5
#include<stdio.h>
#include<conio.h>
#include<string.h>
#define MAX 50
int top;
main()
clrscr();
char choice='y';
while(choice == 'y')
top = 0;
fflush(stdin);
gets(infix);
infix_to_postfix();
printf("Postfix : %s\n",postfix);
value=eval_post();
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 ')':
postfix[p++] = next;
break;
case '+':
case '-':
case '*':
case '/':
case '%':
case '^':
precedence = prec(infix[i]);
postfix[p++] = pop();
push(infix[i]);
break;
postfix[p++] = infix[i];
}/*End of switch */
}/*End of if */
}/*End of for */
while(stack[top]!='#')
postfix[p++] = pop();
}/*End of infix_to_postfix()*/
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()*/
{
if(top > MAX)
printf("Stack overflow\n");
exit(1);
else
top=top+1;
stack[top] = symbol;
}/*End of push()*/
if (top == -1 )
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()*/
int i;
len=strlen(postfix);
postfix[len]='#';
for(i=0;postfix[i]!='#';i++)
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
Postfix : 542*+
Value of expression : 13
Want to continue(y/n) : n