Professional Documents
Culture Documents
APAssignment 3
APAssignment 3
-----------------------------------------------------------------------------------
Write a program where the input is an infix expression kept in a string,
convert it into a postfix expression and evaluate it. The operands'
values are also taken as user input after receiving the string.
ALGORITHM 1:
1) Here the name of the array is exp (refered as stack).
2) Traverse the infix epression from left to right
3) For(I=0;exp[I];++I)
K=exp[I]
4) Compare the character k with the operators and do the
respective function
5) If k = ‘(‘ : push it onto the stack
6) If k = ‘)’ : pop all the operators in the stack untill ‘)’
7) If k = ‘+’ or ‘-’ or ‘*’ or ‘/’ or ‘^’ : check the precedence. If higher
precendence operator is present in the stack then pop it, if not
then push the current operator into the stack.
8) If k = any operand: push it onto the stack
int i,j;
for (i = 0,j = -1; exp[i]; ++i)
{
char k = exp[i];
switch (k)
{
case '(': //leftparanthesis
push(k);
break;
case ')': //rightparanthesis
exp[++j] = '\0';
printf("The Postfix Expression is: ");
printf( "%s", exp);
printf("\n");
return 0;
}
ALGORITHM 2: