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

Postfix Expression Conversion

postfix expression

Uploaded by

lakshmi shree
Copyright
© © All Rights Reserved
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)
34 views8 pages

Postfix Expression Conversion

postfix expression

Uploaded by

lakshmi shree
Copyright
© © All Rights Reserved
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

As Postfix expression is without parenthesis and can be evaluated as two operands and an

operator at a time, this becomes easier for the compiler and the computer to handle.

Evaluation rule of a Postfix Expression states:

1. While reading the expression from left to right, push the element in the stack if it is an
operand.
2. Pop the two operands from the stack, if the element is an operator and then evaluate it.
3. Push back the result of the evaluation. Repeat it till the end of the expression.

Algorithm

Algorithm

1. Initialize an empty stack.


2. Scan postfix expression from left to right.
1. If the scanned character is an operand, then push it to the stack.
2. If the scanned character is an operator, then pop operands from the stack. Perform
the operation and push the result back to the stack.
3. Repeat step 2 till all characters of infix are read.
4. In the end, the stack will contain only 1 value, that is the answer to the postfix expression.

Let's see an example to better understand the algorithm:

Expression: 456*+
Result: 34

Evaluation of Postfix Expressions Using Stack

/* This program is for evaluation of postfix expression


* This program assume that there are only four operators
* (*, /, +, -) in an expression and operand is single digit only
* Further this program does not do any error handling e.g.
* it does not check that entered postfix expression is valid
* or not.
* */

#include <stdio.h>
#include <ctype.h>

#define MAXSTACK 100 /* for max size of stack */


#define POSTFIXSIZE 100 /* define max number of charcters in postfix expression */

/* declare stack and its top pointer to be used during postfix expression
evaluation*/
int stack[MAXSTACK];
int top = -1; /* because array index in C begins at 0 */
/* can be do this initialization somewhere else */

/* define push operation */


void push(int item)
{

if (top >= MAXSTACK - 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)) {
/* we saw an operand,push 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 == '/') {
/* we saw 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) /* ch is an operator */


{
case '*':
val = B * A;
break;

case '/':
val = B / A;
break;

case '+':
val = B + A;
break;

case '-':
val = B - A;
break;
}

/* push the value obtained above onto the stack */


push(val);
}
}
printf(" \n Result of expression evaluation : %d \n", pop());
}

int main()
{

int i;

/* declare character array to store postfix expression */


char postfix[POSTFIXSIZE];
printf("ASSUMPTION: There are only four operators(*, /, +, -) in an expression and operand
is single digit only.\n");
printf(" \nEnter postfix expression,\npress right parenthesis ')' for end expression : ");

/* take input of postfix expression from user */

for (i = 0; i <= POSTFIXSIZE - 1; i++) {


scanf("%c", &postfix[i]);

if (postfix[i] == ')') /* is there any way to eliminate this if */


{
break;
} /* and break statement */
}

/* call function to evaluate postfix expression */

EvalPostfix(postfix);

return 0;
}

Output

First Run:
Enter postfix expression, press right parenthesis ')' for end expression : 456*+)
Result of expression evaluation : 34

Second Run:
Enter postfix expression, press right parenthesis ')' for end expression: 12345*+*+)
Result of expression evaluation: 47

#include<stdio.h>
int stack[20];
int top = -1;

void push(int x)
{
stack[++top] = x;
}

int pop()
{
return stack[top--];
}

int main()
{
char exp[20];
char *e;
int n1,n2,n3,num;
printf("Enter the expression :: ");
scanf("%s",exp);
e = exp;
while(*e != '\0')
{
if(isdigit(*e))
{
num = *e - 48;
push(num);
}
else
{
n1 = pop();
n2 = pop();
switch(*e)
{
case '+':
{
n3 = n1 + n2;
break;
}
case '-':
{
n3 = n2 - n1;
break;
}
case '*':
{
n3 = n1 * n2;
break;
}
case '/':
{
n3 = n2 / n1;
break;
}
}
push(n3);
}
e++;
}
printf("\nThe result of expression %s = %d\n\n",exp,pop());
return 0;
}
OUTPUT:
Enter the expression :: 245+*

The result of expression 245+* = 18

You might also like