Skip to content

Instantly share code, notes, and snippets.

@AdiPat
Created July 31, 2016 11:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AdiPat/6a4c38dadf71a756146671757b7e3882 to your computer and use it in GitHub Desktop.
Save AdiPat/6a4c38dadf71a756146671757b7e3882 to your computer and use it in GitHub Desktop.
Infix to Postfix conversion (DSA Lab)
/*
* @author: Aditya Patange
*
* Conversion of infix expression to postfix expression using stack.
* -All uppercase and lower case alphabets are treated as operands
* -Valid operators: -,+,/,*,^ in increasing order of precedence.
* -For now only round brackets are valid. Square and curly braces are invalid.
* Example: (A+B+(C+D)) is valid. {A+B+(C+D)} is invalid.
* -The following code simply converts a "valid" infix expression to a postfix expression
* in order to check validity of an infix expression , an additional subroutine must be added.
*
*/
#include <stdio.h>
#include <string.h> // for strlen()
#define MAX 100
#define TRUE 1
#define FALSE 0
/* Stack */
static char stack[MAX];
static int top = -1;
void push(char c);
char pop();
char peek();
int empty();
/* Converts infix to postfix and stores it in postfix */
void infixToPostfix(char* infix, char* postfix);
/* Returns precedence */
int prec(char c);
int main()
{
char s[MAX],t[MAX];
scanf("%s",s); // try: A+B*(C+D-E/F*X+T)^U
infixToPostfix(s,t);
printf("%s\n",t); // out: ABCD+EFX*/-T+U^*+
return 0;
}
/* Infix to postfix conversion */
void infixToPostfix(char* infix, char* postfix)
{
int j=0;
for(int i=0; infix[i] != '\0'; i++)
{
char c = infix[i];
// If c is an operator , append it as it is
if((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
postfix[j++] = c;
// If c is an opening bracket , push
if(c == '(')
push(c);
// If c is a closing bracket , pop till closing bracket is found
if(c == ')')
{
while(peek() != '(') // pop till opening bracket is found
postfix[j++] = pop();
pop(); // discard '('
}
// if c is an operator
if(c == '+' || c == '-' || c == '*' || c == '/' || c == '^')
{
if(empty()) // if stack is empty , push operator
{
push(c);
continue;
}
// check precedence
if(prec(c) >= prec(peek()))
push(c);
else // pop stack till it's empty
{
while(empty() == FALSE){
if(peek() == '(')
break;
postfix[j++] = pop();
}
push(c);
}
}
}
while(empty() == FALSE)
{
char topval = peek();
if(topval == '(')
{
pop();
continue;
}
postfix[j++] += topval;
pop();
}
postfix[j] = '\0';
}
int prec(char c)
{
if(c == '-')
return 1;
if(c == '+')
return 2;
if(c == '/')
return 3;
if(c == '*')
return 4;
if(c == '^')
return 5;
return 0;
}
/* Stack operations */
void push(char c)
{
if(top == MAX-1) // overflow
return;
stack[++top] = c;
}
char pop()
{
if(top == -1) // return null char on underflow
return '\0';
return stack[top--];
}
char peek()
{
if(top == -1)
return '\0';
return stack[top];
}
int empty()
{
return top == -1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment