Professional Documents
Culture Documents
A STUDY ON
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
Place: Dhule
Date: 22 / 12 / 2022
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.
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.
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:
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: ^ , $.
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.
2] Flowchart: Start
exit
Top=-1;
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];
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
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
// 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] = '(';
x = pop1();
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
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.
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