#include<iostream>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;
class stackk
{
public :
int top;
char arr[15];
stackk()
{
top =-1;
for(int i=0; i<=15; i++)
{
arr[i]=0;
}
}
bool isOperator(char c)
{
if(c == '^' || c == '/' || c =='*' || c=='+' || c=='-')
{
return true;
}
else
{
return false;
}
}
int precedence(char c)
{
if(c == '^')
{
return 3;
}
else if(c =='*' || c=='/')
{
return 2;
}
else if(c =='+' || c=='-')
{
return 1;
}
else
{
return -1;
}
}
bool empty()
{
if(top == -1)
{
return true;
}
else
{
return false;
}
}
bool full()
{
if(arr[top]==15)
{
return true;
}
else
{
return false;
}
}
char pop()
{
if(empty())
{
cout<<" cant pop stack is already empty"<<endl;
return -1;
}
else
{
char pop_value = arr[top];
arr[top] = 0;
top--;
return pop_value;
}
}
void push(char val)
{
if(full())
{
cout<<" cant push stack is already full "<<endl;
}
else
{
top++;
arr[top] = val;
}
}
char topf()
{
return arr[top];
}
string infixtoPrefix(char arr , string in , int n)
{
string prefix;
reverse(in.begin() , in.end());
for(int i=0; i<n; i++)
{
if(in[i]=='(')
{
in[i]==')';
}
else if(in[i]==')')
{
in[i]=='(';
}
}
for(int i =0; i<n; i++)
{
if((in[i] >= 'a' && in[i]<='z') || (in[i] >='A' && in[i]<='Z' ))
{
prefix += in[i];
}
else if( in[i]=='(')
{
push(in[i]);
}
else if(in[i]==')')
{
while((topf() != '(') && (!empty()))
{
prefix += topf();
pop();
}
if(topf() == '(')
{
pop();
}
}
else if(isOperator(in[i]))
{
if(empty())
{
push(in[i]);
}
else
{
if(precedence(in[i])>precedence(topf()))
{
push(in[i]);
}
else if ((precedence(in[i]) == precedence(topf()))
&& ( in[i]=='^'))
{
while(( precedence(in[i]) == precedence(topf()))
&&(in[i] == '^'))
{
prefix += topf();
pop();
}
push(in[i]);
}
else if(precedence(in[i]) == precedence(topf()))
{
push(in[i]);
}
else
{
while((!empty()) && (precedence(in[i]) < precedence(topf())))
{
prefix += topf();
pop();
}
push(in[i]);
}
}
}
}
while(!empty())
{
prefix += topf();
pop();
}
reverse(prefix.begin(), prefix.end());
return prefix;
}
};
int main()
{
stackk S;
string input;
cout<<" enter the infix axpression : "<<endl;
cin>>input;
int n = input.length();
cout<<" prefix expression is : " << S.infixtoPrefix(S.arr[15] , input , n);
return 0;
}