You are on page 1of 17

Infix to Prefix Coversion DSU 17330, Sem III

PART-A PLAN
1.0 Brief Introduction
____________________________________________________
In this Micro project our group has made a program to convert Infix Expressions to Prefix
Expressions. We have used Array, Stack and many things like Loops to make the program. In
Infix Expressions the operators are in between operands. In Prefix Expression the operators are
placed before the operators. In Postfix Expressions the operators are placed after the operands.

2.0 AIM of Micro-Project


____________________________________________________
 Main aim of this micro project is to create a program to convert Infix Expressions to the
prefix Expressions.
 To convert A+B(C-D)*(O/P)+S (Infix Expression) to Prefix Expression.

1
VIVA COLLEGE OF DIPLOMA ENGG & TECH, COMPUTER ENGG. :
Infix to Prefix Coversion DSU 17330, Sem III

3.0 Action Plan


____________________________________________________
Sr. Details of Activity Planne Planne Name of Responsible Team
No d Start d Members
Date Finish
Date
1 Project selection 3/7/18 17/7/18 Harshay F, Adarsh S, Shubham B,
Yashar S, Vaishnavi B.
2 Identifying project outcomes 17/7/18 31/8/18 Harshay F

3 Identifying resources required 31/8/18 14/8/18 Adarsh S.

4 Algorithm & implementation 14/8/18 28/8/18 Yashar S, Shubham B.

5 Final outcome 28/8/18 11/9/18 Vaishnavi B.

6 Documentation 11/9/18 18/9/18 Yashar S, Vaishnavi B.

7 Seminer and viva-vose 18/9/18 25/9/18 Harshay F, Adarsh S, Shubham B,


Yashar S, Vaishnavi B.
8 Final submission of 25/9/18 3/10/18 Harshay F, Adarsh S, Shubham B,
Microproject Yashar S, Vaishnavi B.

2
VIVA COLLEGE OF DIPLOMA ENGG & TECH, COMPUTER ENGG. :
Infix to Prefix Coversion DSU 17330, Sem III

4.0 Resources Required


____________________________________________________

Sr. No Name of Resource Specification Qty Remarks

1 Computer 500GB HDD, 1


4 Gb RAM,
AMD processor,
Windows 7 OS
2 Turbo C Yes 1

3
VIVA COLLEGE OF DIPLOMA ENGG & TECH, COMPUTER ENGG. :
Infix to Prefix Coversion DSU 17330, Sem III

PART-B OUTCOME

1.0 Brief Description


____________________________________________________
When you write an arithmetic expression such as B * C, the form of the expression provides you
with information so that you can interpret it correctly. In this case we know that the variable B is
being multiplied by the variable C since the multiplication operator * appears between them in
the expression. This type of notation is referred to as infix since the operator is in between the
two operands that it is working on.
Consider another infix example, A + B * C. The operators + and * still appear between the
operands, but there is a problem. Which operands do they work on? Does the + work on A and B
or does the * take B and C? The expression seems ambiguous.
In fact, you have been reading and writing these types of expressions for a long time and they do
not cause you any problem. The reason for this is that you know something about the operators +
and *. Each operator has a precedence level. Operators of higher precedence are used before
operators of lower precedence.
Let’s interpret the troublesome expression A + B * C using operator precedence. B and C are
multiplied first, and A is then added to that result. (A + B) * C would force the addition of A and
B to be done first before the multiplication. In expression A + B + C, by precede the leftmost +
would be done first.
Although all this may be obvious to you, remember that computers can't understand the order
and precedence. So there are two other ways to write an expression that computer can
understand. i.e Prefix and Postfix.
In Prefix Expressions, operators are written before the operands and in Postfix expressions
operators are written after the operands.
+AB is the example of the prefix expression and AB+ is the example of the postfix expressions.

Infix Prefix Postfix


A+B +AB AB+
A+B*C +A*BC ABC*+

4
VIVA COLLEGE OF DIPLOMA ENGG & TECH, COMPUTER ENGG. :
Infix to Prefix Coversion DSU 17330, Sem III

2.0 AIM of Micro-Project


____________________________________________________
Main aim of this micro project is to create a program to convert Infix Expressions to the prefix
Expressions.
To convert A+B(C-D)*(O/P)+S (Infix Expression) to Prefix Expression

3.0 Course Outcomes (CO)

c. Implement basic operations on stack to convert Infix Expression To Prefix Expression.

5
VIVA COLLEGE OF DIPLOMA ENGG & TECH, COMPUTER ENGG. :
Infix to Prefix Coversion DSU 17330, Sem III

4.0 Procedure Followed

Algorithm

Step 1: Declare S, Ch and element as stack char and char respectively.


Step 2: start the while loop and run it until Tokens are available.
Step 3: In while loop - ch=Read Token.
Step 4: Initialize If statement and print Ch if it is operand.
Step 5: Inside Else while(Priority(ch) <= Priority(Top Most Stack)) excecute element = Pop(S);
and print element.
Step 6: Push(S,ch)
Step 7: End the while loop.
Step 8: while(!Empty(S)) execute element = Pop(S) Print(element).
Step 9: Stop.

6
VIVA COLLEGE OF DIPLOMA ENGG & TECH, COMPUTER ENGG. :
Infix to Prefix Coversion DSU 17330, Sem III

Flow Chart

7
VIVA COLLEGE OF DIPLOMA ENGG & TECH, COMPUTER ENGG. :
Infix to Prefix Coversion DSU 17330, Sem III

5.0 Resources Used


____________________________________________________

Sr. No Name of Resource Specification Qty Remarks

1 Computer 500GB HDD, 1


4 Gb RAM,
AMD processor,
Windows 7 OS
2 Turbo C Turbo C7 1

8
VIVA COLLEGE OF DIPLOMA ENGG & TECH, COMPUTER ENGG. :
Infix to Prefix Coversion DSU 17330, Sem III

6.0 Outputs of Micro-Projects


____________________________________________________

C Code
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<string.h>
#include <stdlib.h>
#include<string.h>

9
VIVA COLLEGE OF DIPLOMA ENGG & TECH, COMPUTER ENGG. :
Infix to Prefix Coversion DSU 17330, Sem III

 #define MAX 20
 void push(int);
char pop();
void in2prefix();
int priority ( char );
char stk[20],infix[20],postfix[20];
int top=-1;
int main()
{
    int cont;
    printf("\n Enter the Infix Expression : ");
    scanf("%s",infix);
    in2prefix();
    return 0;
}
 // method that pushes the elements onto the character stack
void push(int pos)
{
 
    if(top==MAX-1)
    {
        printf("stackfull!!!!");
    }
 
    else {
        top++;
        stk[top] = infix[pos];
    }
}
// method that removes character from stack and returns them
char pop()
10
VIVA COLLEGE OF DIPLOMA ENGG & TECH, COMPUTER ENGG. :
Infix to Prefix Coversion DSU 17330, Sem III

{
    char ch;
 
    if(top < 0)
    {
        printf("stackempty!!!!");
        exit(0);
    }
    else
    {
        ch=stk[top];
        stk[top]='\0';
        top--;
        return(ch);
    }
    return 0;
}
 // method that converts String from infix to prefix
// all the strings are assumed to be valid expressions
void in2prefix()
{
    int i=0,j=0;
 
    strrev(infix);
  
    while(infix[i]!='\0')
    {
        // if an alphabet is found then copy it to the output string
        if(infix[i]>='a' && infix[i]<='z')          /*case when alphabet is found*/
        {
            postfix[j]=infix[i];
11
VIVA COLLEGE OF DIPLOMA ENGG & TECH, COMPUTER ENGG. :
Infix to Prefix Coversion DSU 17330, Sem III

            j++;
            i++;
        }
// as we have reversed the string closing bracket will be found first
        // if an closing bracket is found then put it in stack
        else if(infix[i]==')' || infix[i]=='}'  || infix[i]==']')      
            {
                push(i);
                i++;
            }
// as we have reversed the string opening bracket will be found after the closing bracket
        // if an opening bracket is found then
        // keep removing the operators from the stack and add them to prefix string until you find the
corresponding opening bracket
        else if(infix[i]=='(' || infix[i]=='{'  || infix[i]=='[')       /*when closing bracket is found*/
        {
            if(infix[i]=='(')
            {
                while(stk[top]!=')')          /*pop till corresponding opening bracket is found*/
                {
                    postfix[j]=pop();
                    j++;
                }
                pop();
                i++;
            }
 
            else if(infix[i]=='[')
            {
                while(stk[top]!=']')      /*pop till corresponding opening bracket is found*/
                {
12
VIVA COLLEGE OF DIPLOMA ENGG & TECH, COMPUTER ENGG. :
Infix to Prefix Coversion DSU 17330, Sem III

                    postfix[j]=pop();
                    j++;
                }
                pop();
                i++;
            }
 
            else if(infix[i]=='{')
            {
            while(stk[top]!='}')      /*pop till corresponding opening bracket is found*/
                {
                    postfix[j]=pop();
                    j++;
                }
                pop();
                i++;
            }
        }
    
        // if none of the above cases are satisfied then we surely have an operator
        else            
        {
            // if the stack if empty then we simply put the operator in stack
            if(top==-1)        
            {
                push(i);
                i++;
            }
 
            // if the priority of current operator is less than the stack top then
            // pop the stack top and add it to the prefix string
13
VIVA COLLEGE OF DIPLOMA ENGG & TECH, COMPUTER ENGG. :
Infix to Prefix Coversion DSU 17330, Sem III

            else if( priority(infix[i]) < priority(stk[top]))
            {
                postfix[j]=pop();
                j++;
        
                // now if you have an operator that has priority greater than current operator then pop
                while(priority(stk[top]) > priority(infix[i])){
                    postfix[j] = pop();
                    j++;
                    if(top < 0) {
                        break;
                    }
                }
                push(i);
                i++;
            }
            // if the priority of current operator is greater than or equal to the stack top then push it onto
the stack
            else if(priority(infix[i]) >= priority(stk[top]))
            {
                push(i);
                i++;
            }
        }
    }
 
    // at the end remove all the operators from the stack
    while(top!=-1)
    {
        postfix[j]=pop();
        j++;
14
VIVA COLLEGE OF DIPLOMA ENGG & TECH, COMPUTER ENGG. :
Infix to Prefix Coversion DSU 17330, Sem III

    }
  
    // reverse the final string before output
    strrev(postfix);
    postfix[j]='\0';
    printf("The converted postfix string is : %s ",postfix);
}
 
// method that returns priority for operators according to their precedence
int priority ( char alpha )
{
    if(alpha == '+' || alpha =='-')
    {
        return(1);
    }
 
    if(alpha == '*' || alpha =='/')
    {
        return(2);
    }
 
    if(alpha == '$')
    {
        return(3);
    }
    return 0;
}

15
VIVA COLLEGE OF DIPLOMA ENGG & TECH, COMPUTER ENGG. :
Infix to Prefix Coversion DSU 17330, Sem III

7.0 Skill Developed


_____________________________________________________________________________________

Thus, we have studied how to convert Infix Expressions to Prefix Expression and also
understood how to make a C program using stack.

16
VIVA COLLEGE OF DIPLOMA ENGG & TECH, COMPUTER ENGG. :

You might also like