You are on page 1of 17

67

Program Name : Computer Engineering Academic Year: 2022-2023


Program Code : CO3I

Course Name : Data Structure using C Semester: III


Course Code : 22317

A STUDY ON

INFIX TO PREFIX AND PREFIX TO INFIX CONVERSION

MICRO PROJECT REPORT


Submitted in 2022 by the group of 5 Students
Sr. Enrollment Exam Seat
Roll No Full name of Student
No No No
1 30 Chaitali Ankush Ingle 2100590077 317335
2 34 Tanmay Sanjay Jirekar 2100590081 317339
3 56 Mirza Mohd. Ibrahim Baig 2100590106 317361
4 60 Rohit Narendra More 2100590110 317365
5 61 Soham Rajendra More 2100590111 317366

Under the Guidance of


Ms. S. H. Patil
in
Three Years Diploma Programming in Engineering & Technology of
Maharashtra StateBoard of Technical Education, Mumbai (Autonomous)
ISO 9001: 2008 (ISO/IEC-27001:2013)
at
0059 - Shri Shivaji Vidya Prasarak Sanstha’s
Bapusaheb Shivajirao Deore Polytechnic, Vidyanagari , Deopur, Dhule-424005.
MAHARASHTRA STATE BOARD OF TECHNICAL
EDUCATION, MUMBAI

Certificate
This is to certify that,
Sr. Enrollment Exam Seat
Roll No Full name of Student
No No No
1 30 Chaitali Ankush Ingle 2100590077 317335
2 34 Tanmay Sanjay Jirekar 2100590081 317339
3 56 Mirza Mohd. Ibrahim Baig 2100590106 317361
4 60 Rohit Narendra More 2100590110 317365
5 61 Soham Rajendra More 2100590111 317366

students of Third Semester, Diploma Programme in Engineering &

Technology at 0059 - Shri Shivaji Vidya Prasarak Sanstha’s Bapusaheb

Shivajirao Deore Polytechnic- Dhule, has completed the Micro Project

satisfactorily in Subject Data Structure using C (22317) in the academic year

2022-2023 as prescribed in the MSBTE curriculum of I Scheme.

Place: Dhule
Date: 22 / 12 / 2022

Project Guide Head of the Department Principal

Institute
Part A :Micro-Project Proposal
Infix To Prefix and Prefix To Infix conversion

1.0 Introduction:
To convert the expression from infix into prefix in the data structure using stack , first reverse
the infix expression given in the problem. scan the expression from left to right. Whenever operands
arrive , print them. An expression is called infix expression if the operator lies in between the operands
in the expression. An expression is called prefix expression if the operator lies before the operands.

2.0 Aim Of The Project:


To convert the expression infix into prefix in data structure using stack.

3.0 Intended Course Outcomes:


a. Perform Basic operation on array.

4.0 Literature Review

To solve the given expression into prefix form, first expression is reversed .note that for
reversing the opening and closing parenthesis will also reversed. For example: A+B*(C - D)
After reversing this expression will be: ) D – C(*B+A So we need to convert opening
parenthesis to closing parenthesis and vice versa. After that, postfix expression form by using
infix to postfix algorithm. Again the postfix expression is reversed and we get prefix expression.

5.0 Resources Required:

Sr.No Name Of Resources Specification Qty


Intel(R) Core(TM) i5-6200U CPU
1. Computer system with broad 1
@ 2.30GHz 2.40 GHz
specification
2.9 GHz
1GB DDR RAM,3 MB L2

2. Software VS code and 1


MinGW compiler
6.0 Action Plan:

Sr. Details Of Activity Planned Planned Finish Date Name Of Team


No Start Members
Date
1. Define Problem For Project 21/10/2022 26/10/2022 Chaitali Ingle
and Mirza
Ibrahim

2. Gather The Requirement 26/10/2022 30/10/2022 Tanmay Jirekar

3. Designing The Microproject 30/10/2022 3/11/2022 Soham More and


Rohit More

4. Testing 3/11/2022 9/11/2022 Mirza Ibrahim


and Tanmay
Jirekar
5. Coding 9/11/2022 12/11/2022 Rohit More and
Soham More
Part B : Format For Report
Infix To Prefix and Prefix To Infix conversion

1.0 : Rationale

Polish Notation:

Polish notation is a notation form for expressing arithmetic, logic and algebraic equations. Its most
basic distinguishing feature is that operators are placed on the left of their operands. If the operator has
a defined fixed number of operands, the syntax does not require brackets or parenthesis to lessen
ambiguity.

Polish notation is also known as prefix notation, prefix Polish notation, normal Polish notation,
Warsaw notation and Lukasiewicz notation.

Infix Notation:

An infix notation is a notation in which an expression is written in a usual or normal format. It is a


notation in which the operators lie between the operands. The examples of infix notation are A+B,
A*B, A/B, etc.

As we can see in the above examples, all the operators exist between the operands, so they are infix
notations. Therefore, the syntax of infix notation can be written as:

Prefix Notation:

A prefix notation is another form of expression but it does not require other information such as
precedence and associativity, whereas an infix notation requires information of precedence and
associativity. It is also known as polish notation. In prefix notation, an operator comes before the
operands. The syntax of prefix notation is given below:

For example, if the infix expression is 5+1, then the prefix expression corresponding to this infix
expression is +51.
* Priority of Operator:

I. Exponential Operator: ^ , $.

II. Multiplication, Divide and Modulus: : *, /, %.

III. Subtraction and addition: + , -

IV. Other Operator:’ (’,’)’,’[’,’]’.

2.0 Course Outcome Addressed

a. Perform Basic operation on array.

3.0 : Actual Methodology

1] Algorithm:

Step 1: start
Step 2: declare variable char infix[30],prefix[30],c,str1[30]
[30],opr1[30],opr2[30],expression[30],str2[30],pre[30]
Step 3: read choice from user 1.Infix to Prefix 2.Prefix to infix 3.Exit
Step 4: if user enter choice 1 then read infix expression
Step 5: initialise top1=-1
Reverse the infix expression by using strrev(expression)
then call function intoprefix(); Function is written for convert expression into prefix.
Step 6 : adding left parenthesis ‘(‘ at beginning and right parenthesis ‘)’ at end.
Step 7: Read A from right to left and repeat steps 3-6.for each element of A until stack is empty.
Step 8: if right parenthesis ‘)’ is encounter then push it on the stack.
Step 9: if an Operand is encountered first then add it into str1[];
A] repeatedly pop from stack and add to str[];.each operator from the stack which Higher
priority then incoming operator.
B] Remove the right parenthsis ‘)’from stack.

Step 10: reverse the expression of given output prefix expression.


Step 11: if user enter choice 2
First initialize top=-1.
Step 12: Read prefix expression from user
Step 13: call function convert (); which Is written for convert into prefix.
Step 14: Scan all the symbol from right to left to given prefix expression
Step 15: If the reading symbol is an operand push it into the stack.
Step 16: if the reading symbol is operand then
A] Pop to expression form stack operand 1 And operand 2 which is operand for the current
operator.
B] Push ‘(’ + operand +operand +operand ’)’into the stack.
Where “+” denoted the concatenation of string
Step 17: If there is no symbol Is left then stop the process. Top of the stack will required the stack
will have required the infix expression.
Step 18: display the output of infix expression.
Step 19: display enter your choice
1. Infix to prefix
2. Prefix to infix
3. Exit
Step 20: if user enter 3, then immediately exit.
Step 21: stop.

2] Flowchart: Start

Declare variables infix[30],prefix[30],c,str1[30][30],op1[30],op2[30],str2[30]

Display menu 1.infix to prefix 2.perfix to infix 3.exit Enter choice

Ch1 Ch2 Ch3

exit
Top=-1;

Enter infix experssion

Enter prefix expression

len=strlen(exp) Len=strlen(prefix)

For(i=0;i<
=len;i++)
For(i=len- 1;i>=0;i--
)

Infix[i+1]=exp[i]

C=prefix[i];

For(i=strlen (infix)1;i>= 0;i++)

Infix[0]=c Infix[1]=’\0’

c=infix[i]
If(isalnum( c))

If(isalnum(c))

Push(infix)

Pre[k++]=c
else
Else if(c==’s’||c==’^’
||c==’/’||c==’*‘|| c==’%’||c==’+’|| c==’-‘)

Exp[0]=’\0’
Strcpy(op1,pop(1) Strcpy(op2,pop(1) Strcpy(exp,’(‘) Strcpy(exp,op1) Strcat(exp,infix) Strcat(exp,op2) St

While(top!=- 1&&priority( s+1[top1]>pr iority(c))

Push(exp)

Print(\t\t\t%s%c%s%c%s\n, op1c,op2)

Pre[k++]=pop1()

Push1(c)

Else if(c==’)’
||c==’]’)
Push(c)

Else if(c==’(‘)

Char x=pop1()

While( x!=’)’)

Pre[k++]=x X=pop1()

Else if(c==’(‘)

Char x=pop1()
While( x!=’]’)

Pre[k++]=x
X=pop1()
Print (%c\f\t%s\t\t%s,c,st1,pre))

3] Program:

#include <stdio.h>
#define SIZE 30

// Global declaration of variables

char infix[SIZE], prefix[SIZE], c, str[SIZE][SIZE], opr1[SIZE], opr2[SIZE], expression[SIZE],


str1[SIZE];
int top, top1, i, length, k = 0;

// prefix to infix
push(char *x)
{
printf("\n| %s\t|\n--------", x);
strcpy(str[++top], x);
}

char *pop()
{
return str[top--];
}
// infix to prefix
push1(char x)
{
str1[++top1] = x;
}
char pop1()
{
char c1 = str1[top1];
str1[top1] = '\0';
top1--;
return c1;
}

int priority(char x)
{
switch (x)
{
case '$':
case '^':
return 4;
case '*':
case '/':
case '%':
return 3;
case '+':
case '-':
return 2;
case ')':
case ']':
return 1;
}
return 0;
}

intopre()
{
push1(')');

length = strlen(expression);
infix[0] = '(';

for (i = 0; i <= length; i++)


infix[i + 1] = expression[i];

for (i = strlen(infix) - 1; i >= 0; i--)


{
c = infix[i];
if (isalnum(c))
{
prefix[k++] = c;
printf("\n%c\t\t%s\t\t%s", c, str1, prefix);
}
else if (c == '$' || c == '^' || c == '/' || c == '*' || c == '%' || c == '+' || c == '-')
{
while (top != -1 && priority(str1[top1]) > priority(c))
{
prefix[k++] = pop1();

printf("\n\t\t%s\t\t%s", str1, prefix);


}
printf("\n%c\t\t%s\t\t%s", c, str1, prefix);
push1(c);
}
else if (c == ')' || c == ']')
push1(c);
else if (c == '(')
{
char x = pop1();
while (x != ')')
{
prefix[k++] = x;

x = pop1();

printf("\n%c\t\t%s\t\t%s", c, str1, prefix);


}
}
else if (c == '[')
{
char x = pop1();
while (x != ']')
{
prefix[k++] = x;
x = pop1();

printf("\n%c\t\t%s\t\t%s", c, str1, prefix);


}
}
}
}

convert()
{
length = strlen(prefix);
for (i = length - 1; i >= 0; i--)
{
c = prefix[i];
infix[0] = c;
infix[1] = '\0';

if (isalnum(c))
push(infix);
else
{
expression[0] = '\0';
strcpy(opr1, pop());
strcpy(opr2, pop());
strcpy(expression, "(");
strcat(expression, opr1);
strcat(expression, infix);
strcat(expression, opr2);
strcat(expression, ")");
push(expression);
}
}
}

main()
{
int ch;

while (1)
{
printf("\n\n 1: Infix to Prefix \n 2: Prefix to Infix \n 3: Exit");
printf("\n Enter your choice : ");
scanf("%d", &ch);

switch (ch)
{
case 1:
top1 = -1;
printf("\n Enter Infix Expression");
scanf("%s", expression);
printf("\n Infix expression %s\n", expression);
printf("\nToken\t\tStack\t\tPrefix\n");
intopre();
printf("\n\n REVERSE THE STRING TO GET PREFIX STRING.");
printf("\n\n Prefix expression %s", strrev(prefix));
printf("\n\n");
break;

case 2:
top = -1;
printf(" \n\nEnter prefix expression : ");
scanf("%s", prefix);
printf("\n Prefix expression : %s", prefix);
printf("\n\nStack \n");
convert();
printf("\n Infix expression :- %s", pop());
printf("\n\n");
break;

case 3:
exit(0);
}
}
}

4.0 Output
5.0 Actual Resource Used

Sr.No Name Of Resources Specification Qty

1. Computer system with broad 1


specification Esys make,Intel e7500 Core 2
Duo
2.9 GHz 1GB DDR RAM,3 MB L2
Cache,160 GB HDD
2. Software VS Code and 1
MinGW

6.0 Skills Developed


convert an infix to prefix and prefix to infix notations
Support for basic mathematical operations

7.0 Application of Microproject:

You can use infix-expression-parser as a library or as a CLI tool. Below are some examples explaining
how to use the package in each case.

8.0 Area of future improvement

A sub-command for evaluating expressions in the command line may come in future iterations of this package

Support for other mathematical operations, functions and symbols may come in future iterations of this package

You might also like