Infix To Postfix Code
Infix To Postfix Code
//#include<bits/stdc++.h>
#include<iostream>
class stack {
public:
char a[50];
char mytop;
stack()
mytop = -1;
bool isempty();
bool isfull();
void pop();
char top();
};
if (isfull() == true)
else
mytop++;
a[mytop] = num;
}
bool stack::isempty()
if (mytop == -1)
return true;
else
return false;
bool stack::isfull()
if (mytop == 49)
return true;
else
return false;
void stack::pop()
if (isempty() == true)
else
a[mytop] = 0;
mytop--;
}
}
char stack::top()
return a[mytop];
int prec(char c)
return 2;
return 1;
else
return -1;
void infixToPostfix(string s)
stack st;
int l = s.length();
string ns;
int z=0;
string strStack="";
if(s[i] == '(')
strStack+=s[i];
st.push('(');
cout << "Token: '" << ns<< "'\tpush '" << s[i]<<"'\t\tStack: "<<strStack<< "\n";
char c = st.top();
z=strStack.length()-1;
string ss =strStack;
strStack = "";
strStack += ss[i];
ns += c;
st.pop();
if(st.top() == '(')
char c = st.top();
st.pop();
z=strStack.length()-1;
string ss =strStack;
strStack = "";
strStack += ss[i];
else if (s[i]=='+'||s[i]=='-'||s[i]=='*'||s[i]=='/')
char c = st.top();
z=strStack.length()-1;
string ss =strStack;
strStack = "";
strStack += ss[i];
ns += c;
cout << "Token: '" << ns << "'\tpop & Display '" << st.top();
st.pop();
strStack+=s[i];
st.push(s[i]);
cout << "Token: '" << ns << "'\tpush '"<< s[i] << "'\t\tStack: " << strStack << "\n";
else
{
ns+=s[i];
cout << "Token: '" << ns << "'\tDisplay '"<<s[i]<<"'\t\tStack: " << strStack << "\n";
while(st.isempty() != true)
char c = st.top();
z=strStack.length()-1;
string ss =strStack;
strStack = "";
strStack += ss[i];
st.pop();
ns += c;
cout << "Token: '" << ns << "'\tpop & Display '" << st.top();
st.pop();
int main()
string exp;
cout<<"enter string"<<endl;
cin>>exp;
infixToPostfix(exp);
return 0;
//((A*(B+D)/E)-F*(G+H/K)))
//ABD+*E/FGHK/+*-