This is my code:
#include "stdafx.h"
#include <iostream>
#include <conio.h>
#include <stdio.h>
#define max 30
char pop(void);
void push(char);
int Isunder(void);
int Isover(void);
int prio(char);
int top=-1,i=0,j=0;
char stk[max];
char infix[max];
char postfix[max];
void main()
{
cout << "Please enter your expression in infix form :";
cin >> infix ;
for(i=0;infix[i]!='\0';i++)
{
switch(infix[i])
{
case '(' :
push( '(' );
break;
case ')' :
while(stk[top]!='(')
postfix[j++]=pop();
if (stk[top]=='(')
top--;
break;
case '/':
case '*':
case '+':
case '-':
case '^':
if (!Isunder())
{
if (prio(infix[i]) > prio(stk[top]) )
{
push(infix[i]);
}
else
{
postfix[j++]=pop();
push(infix[i]);
}
}
else
push(infix[i]);
break;
default :
postfix[j++] = infix[i] ;
}
}
while (!Isunder())
postfix[j++]=pop();
postfix[j]='\0';
cout << endl << "Your postfix expression is :" << postfix << endl ;
}
//// pop opertation
char pop()
{
if (!Isunder())
return stk[top--];
}
//// push operation
void push(char ch)
{
if (!Isover())
stk[++top]=ch;
else
cout << "Stack is full";
}
//// overflow
int Isover()
{
if (top == max-1)
return 1;
else
return 0;
}
//// underflow
int Isunder()
{
if (top == -1)
return 1;
else
return 0;
}
/////////////////////////////
int prio(char ch)
{
switch(ch)
{
case '(' :
return 1;
break;
case '-' :
return 2;
break;
case '+' :
return 3;
break;
case '*' :
return 4;
break;
case '/' :
return 5;
break;
case '^':
return 6;
}
}