1. Enter postfix expression
2. Differentiate operand and operator
3. Push operand onto stack till we get operator
4.Whenever we get operator from expression then pop two operand which are already pushed onto stack in variable A And B respectively.
5.Apply operator onto operand A and B and again push result onto stack.
6. Repeat whole procedure till we get end of expression (here we use '.' for end of expression)
#include<stdio.h>
#include<ctype.h>
# define MAX 50
# define Expsize 50
void push(int item);
int pop();
void EvalPostfix(char postfix[]);
int stack[MAX];
int top = -1 ;
int main()
{
int i ;
char postfix[Expsize];
printf("Assume: four operators(*, /, +, -) in an expression and operand is single digit only.\n");
printf( " \nEnter postfix expression,\npress full stop '.' for end expression : ");
//Scan Expression
for (i = 0 ; i <= Expsize - 1 ; i++)
{
scanf("%c", &postfix[i]);
if ( postfix[i] == '.' )
{ break; }
}
EvalPostfix(postfix);
return 0;
}
void push(int item)
{
if(top >= MAX -1)
{
printf("stack over flow");
return;
}
else
{
top = top + 1 ;
stack[top]= item;
}
}
/* define pop operation */
int pop()
{
int item;
if(top <0)
{
printf("stack under flow");
}
else
{
item = stack[top];
top = top - 1;
return item;
}
}
/* define function that is used to input postfix expression and to evaluate it */
void EvalPostfix(char postfix[])
{
int i ;
char ch;
int val;
int A, B ;
/* evaluate postfix expression */
for (i = 0 ; postfix[i] != '.'; i++)
{
ch = postfix[i];
if (isdigit(ch))
{
/* Push operand i.e. the digit onto stack
ch - '0' is used for getting digit rather than ASCII code of digit */
push(ch - '0');
}
else if (ch == '+' || ch == '-' || ch == '*' || ch == '/')
{
/* Ch is an operator
* pop top element A and next-to-top elemnet B
* from stack and compute B operator A
*/
A = pop();
B = pop();
switch (ch)
{
case '*':
val = B * A;
break;
case '/':
val = B / A;
break;
case '+':
val = B + A;
break;
case '-':
val = B - A;
break;
}
/* push the result get from above onto the stack */
push(val);
}
}
printf( " \n Result of expression evaluation : %d \n", pop()) ;
}
/*Output
Assume: four operators(*, /, +, -) in an expression and operand is single digit only.
Enter postfix expression,
press full stop '.' for end expression : 359*+.
Result of expression evaluation : 48
*/
2. Differentiate operand and operator
3. Push operand onto stack till we get operator
4.Whenever we get operator from expression then pop two operand which are already pushed onto stack in variable A And B respectively.
5.Apply operator onto operand A and B and again push result onto stack.
6. Repeat whole procedure till we get end of expression (here we use '.' for end of expression)
#include<stdio.h>
#include<ctype.h>
# define MAX 50
# define Expsize 50
void push(int item);
int pop();
void EvalPostfix(char postfix[]);
int stack[MAX];
int top = -1 ;
int main()
{
int i ;
char postfix[Expsize];
printf("Assume: four operators(*, /, +, -) in an expression and operand is single digit only.\n");
printf( " \nEnter postfix expression,\npress full stop '.' for end expression : ");
//Scan Expression
for (i = 0 ; i <= Expsize - 1 ; i++)
{
scanf("%c", &postfix[i]);
if ( postfix[i] == '.' )
{ break; }
}
EvalPostfix(postfix);
return 0;
}
void push(int item)
{
if(top >= MAX -1)
{
printf("stack over flow");
return;
}
else
{
top = top + 1 ;
stack[top]= item;
}
}
/* define pop operation */
int pop()
{
int item;
if(top <0)
{
printf("stack under flow");
}
else
{
item = stack[top];
top = top - 1;
return item;
}
}
/* define function that is used to input postfix expression and to evaluate it */
void EvalPostfix(char postfix[])
{
int i ;
char ch;
int val;
int A, B ;
/* evaluate postfix expression */
for (i = 0 ; postfix[i] != '.'; i++)
{
ch = postfix[i];
if (isdigit(ch))
{
/* Push operand i.e. the digit onto stack
ch - '0' is used for getting digit rather than ASCII code of digit */
push(ch - '0');
}
else if (ch == '+' || ch == '-' || ch == '*' || ch == '/')
{
/* Ch is an operator
* pop top element A and next-to-top elemnet B
* from stack and compute B operator A
*/
A = pop();
B = pop();
switch (ch)
{
case '*':
val = B * A;
break;
case '/':
val = B / A;
break;
case '+':
val = B + A;
break;
case '-':
val = B - A;
break;
}
/* push the result get from above onto the stack */
push(val);
}
}
printf( " \n Result of expression evaluation : %d \n", pop()) ;
}
/*Output
Assume: four operators(*, /, +, -) in an expression and operand is single digit only.
Enter postfix expression,
press full stop '.' for end expression : 359*+.
Result of expression evaluation : 48
*/
No comments:
Post a Comment