0% found this document useful (0 votes)
81 views3 pages

Convert Prefix To Post Fix

This lab report describes converting infix notation arithmetic expressions to postfix notation using a stack. It defines infix and postfix notation, provides an algorithm to perform the conversion using a stack, and includes C code implementing the algorithm. The code takes an infix expression as input, converts it to postfix notation using functions for pushing/popping from a stack and checking operator precedence, and outputs the postfix expression.

Uploaded by

Mian Tauseef
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
81 views3 pages

Convert Prefix To Post Fix

This lab report describes converting infix notation arithmetic expressions to postfix notation using a stack. It defines infix and postfix notation, provides an algorithm to perform the conversion using a stack, and includes C code implementing the algorithm. The code takes an infix expression as input, converts it to postfix notation using functions for pushing/popping from a stack and checking operator precedence, and outputs the postfix expression.

Uploaded by

Mian Tauseef
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Lab Report no.

11

Convert Infix to Postfix Expression


Objective:
To implement stack and use it to convert infix to postfix expression.
Introduction:
One of the applications of Stack is in the conversion of arithmetic expressions in high-level
programming languages into machine readable form. As our computer system can only
understand and work on a binary language, it assumes that an arithmetic operation can take
place in two operands only e.g., A+B, C*D,D/Aetc. But in our usual form an arithmetic
expression may consist of more than one operator and two operands
e.g. (A+B)*C(D/(J+D)).These complex arithmetic operations can be converted into polish
notation using stacks which then can be executed in two operands and an operator form.
Infix Expression:
It follows the scheme of <operand><operator><operand> i.e. an <operator> is preceded
and succeeded by an <operand>. Such an expression is termed infix expression. E.g., A+B
Postfix Expression:
It follows the scheme of <operand><operand><operator> i.e. an <operator> is succeeded
by both the <operand>. E.g., AB+
Algorithm to convert Infix To Postfix:
Let, X is an arithmetic expression written in infix notation. This algorithm finds the
equivalent postfix expression Y.
 Push “(“onto Stack, and add “)” to the end of X.
 Scan X from left to right and repeat Step 3 to 6 for each element of X until the Stack
is empty.
 If an operand is encountered, add it to Y.
 If a left parenthesis is encountered, push it onto Stack.
 If an operator is encountered ,then:
 Repeatedly pop from Stack and add to Y each operator (on the top of Stack) which
has the same precedence as or higher precedence than operator.
 Add operator to Stack.
[End of If]
 If a right parenthesis is encountered ,then:
 Repeatedly pop from Stack and add to Y each operator (on the top of Stack) until a
left parenthesis is encountered.
 Remove the left Parenthesis.
[End of If]
[End of If]
 END.
Code of convert infix to postfix expression using Stack:
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#define SIZE 30
void push(char);
char pop();
int preced(char);
char stack[SIZE], postfix[SIZE], infix[SIZE];
int top = -1;
Roll no. 16-ELE-03

53
Lab Report no.11

int main(){
char ch;
int i, k, p = 0;
for(i=0; i<SIZE; i++){
stack[i] = '\0';}
printf("\nEnter an infix expression: ");
scanf("%s",infix);
for(i=0; infix[i] != '\0'; i++){
ch = infix[i];
if (isalnum(ch)){
postfix[p++] = ch;}
else if (ch == '('){
push(ch);}
else if (ch == ')'){
while(stack[top] != '('){
postfix[p++] = pop();}
pop();}
else if (ch == '/' || ch == '*' || ch == '+' || ch == '-'){
if (preced(ch) > preced(stack[top])){
push(ch);}
else{
while (preced(ch) <= preced(stack[top])){
postfix[p++] = pop();}}}
postfix[p] = '\0';
while(top > -1)
postfix[p++] = pop();
printf("\nThe postfix expression is: %s\n",postfix);
return 0;}
void push(char el){
if (top < SIZE-1){
stack[++top] = el;}
else{
printf("\nError! Stack is full.\n");
exit(-1);}}
char pop(){
char ch;
if (top > -1){
ch = stack[top];
stack[top--] = '\0';
return ch;}
else{
printf("\nError! Stack is empty.\n");
exit(-1);}}

Roll no. 16-ELE-03

54
Lab Report no.11

int preced(char ch){


if(ch == '(' || ch == ')')
return 0;
else if (ch == '+' || ch == '-')
return 1;
else if (ch== '/' || ch == '*')
return 2; }
Output:

Roll no. 16-ELE-03

55

You might also like