#include<iostream>
using namespace std;
char stack[100]; //
char postfix[100];
char exp[100]; // mathematical expression entered by the
user
int top=-1;
/**********************************************/
void push1(char newitem)
{
if(top=='\0')
{
cout<<"Stack
overflow"<<endl;
}
else
{
exp[++top]=newitem;
}
}
/**********************************************/
bool ArePair(char opening,char closing)
{
if(opening
== '(' && closing == ')') return true;
else
if(opening == '{' && closing == '}') return true;
else
if(opening == '[' && closing == ']') return true;
return
false;
}
/**********************************************/
void pop1()
{
if(top==-1)
{
cout<<"Cannot
pop. The stack does not exist"<<endl;
}
else
{
top--;
}
}
/**********************************************/
int rtop()
{
if(top!=-1)
{
return
exp[top];
}
else
return false;
}
/**********************************************/
bool isempty()
{
if(top==-1)
{
return true;
}
else
{
return false;
}
}
/**********************************************/
bool AreParanthesesBalanced(char exp[]) // This function will check that
parantheses are balanaced or not. If it if balance it will return true.
{
for(int
i =0;exp[i]!= '\0';i++)
{
if(exp[i]
== '(' || exp[i] == '{' || exp[i] == '[')
{
push1(exp[i]);
}
else
if(exp[i] == ')' || exp[i] == '}' || exp[i] == ']')
{
if(isempty()
|| !ArePair(rtop(),exp[i]))
{
return false;
}
else
{
pop1();
}
}
}
return
isempty();
}
/**********************************************/
int precedence(int ch)
{
if(ch=='(')
return 0;
else
if(ch=='+'||ch=='-')
return 1;
else
if(ch=='*'||ch=='/'||ch=='%')
return 2;
return -1;
}
/**********************************************/
char pop()
{
if(top==-1)
{
cout<<"\n stack is Empty";
}
else
{
char
ch=stack[top];
top--;
return (ch);
}
}
/**********************************************/
void push(int ch)
{
if(top!=sizeof(stack)-1)
{
top++;
stack[top]=ch;
}
else
cout<<"Stack Overflow"<<endl;
}
/************************************************/
bool IsOperator(char C)
{
if(C ==
'+' || C == '-' || C == '*' || C == '/' || C== '$')
return
true;
return
false;
}
/************************************************/
bool IsOperand(char C)
{
if(C
>= '0' && C <= '9') return true;
if(C
>= 'a' && C <= 'z') return true;
if(C
>= 'A' && C <= 'Z') return true;
return
false;
}
/**********************************************/
void postfixfun(char exp[])
{
int k=0;
int j=0;
int p=0;
while(exp[k]!='\0')
{
/*------------------------------------------------------------*/
if(IsOperand(exp[k])==true)
{
postfix[j]=exp[k];
j++;
}
/*----------------------------------------------------------------*/
else
if(exp[k]=='(')
{
push(exp[k]);
}
/*-------------------------------------------------------------------*/
else
if(IsOperator(exp[k])==true)
{
while(precedence(stack[top])>=precedence(exp[k]))
{
postfix[j]=pop();
j++;
}
push(exp[k]);
}
/*-------------------------------------------------------------------*/
else
if(exp[k]=')')
{
while(stack[top]!='(')
{
postfix[j]=stack[top];
j++;
pop();
}
pop();
}
k++;
}
while(top!=-1)
{
postfix[j]=pop();
j++;
}
}
/************************************************/
int main()
{
cout<<"Enter your mathematic expression:"<<endl;
cin>>exp;
int size;
size=sizeof(exp);
if(AreParanthesesBalanced(exp))
{
cout<<"Expression is balanced \n" <<endl;
cout<<"Postfix Expression="<<endl;
postfixfun(exp);
for(int
i=0;i<size;i++)
{
cout<<postfix[i];
}
}
else
{
cout<<"Not balanced"<<endl;
}
return 0;
}