Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions C-C++/Rebecca Aurelia Dsouza/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Infix To Postfix conversion - Rebecca Dsouza
Infix expressions are readable and solvable by humans.
We can easily distinguish the order of operators, and also can use the parenthesis to solve that part first during solving mathematical expressions.
The computer cannot differentiate the operators and parenthesis easily, that’s why postfix conversion is needed.

To convert infix expression to postfix expression, we will use the stack data structure.
By scanning the infix expression from left to right, when we will get any operand, simply add them to the postfix form, and for the operator and parenthesis, add them in the stack maintaining the precedence of them.
153 changes: 153 additions & 0 deletions C-C++/Rebecca Aurelia Dsouza/infix_to_postfix.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
/*Conversion from infix to postfix - Rebecca Aurelia Dsouza*/

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
/*#include"charstack.h"*/
#define size 50

char stack[size];
int top = -1;

void conversion(char * , char * );
int getprecedence( char o);
void push(char s)
{
if(top== size-1)
{
printf("\n Stack is FULL!!! Insertion is not possible!");
}
else
{
top++;
stack[top] = s;
}
}

char pop()
{
if( top == -1)
{
printf("\n Stack is EMPTY!!! Deletion is not possible!");
stack[top] = ' ';
}
else
{
top--;
}
return stack[top+1];
}

char peep()
{
if( top == -1)
{
printf("\n Stack is EMPTY!!!");
stack[top] = ' ';
}

return stack[top];
}

int main()
{
char infix[50];
char postfix[50];

printf("\n Enter the infix expression: \n ");
gets(infix);

conversion(infix,postfix);

printf("\n The postfix expression is: \n ");
puts(postfix);

return 0;
}

void conversion( char infix[50], char postfix[50])
{
int length;
int i=0;
int j=0;
int op;

length = strlen(infix);
infix[length] = ')';
infix[length + 1] = '\0';

push('(');

while(infix[i] != '\0')
{
if(infix[i] == '(')
{
push(infix[i]);
}
else if( isalpha(infix[i]) || isdigit(infix[i]) )
{
postfix[j] = infix[i];
j++;
}
else if(infix[i] == ')')
{
while(peep() != '(')
{
op = pop();
postfix[j] = op;
j++;
}
pop();
}
else
{
if( peep() == '(')
{
push(infix[i]);
}
else
{
if( getprecedence(peep()) >= getprecedence(infix[i]) )
{
op = pop();
postfix[j] = op;
j++;
}
push(infix[i]);
}
}
i++;
}

postfix[j] = '\0';
}

int getprecedence( char o)
{
if( o == '/' || o == '*')
{
return 2;
}

if( o == '+' || o == '-')
{
return 1;
}
}