I have the following code and I am getting a runtime error, can anybody help me find the bug.
#include
#include
#include<math.h>
#include
#include<strings.h>
using namespace std;
void infixToPostfix(string st);
int pr(char s);
int main() {
int t;
cin>>t;
while(t–)
{
string st;
cin>>st;
infixToPostfix(st);
}
return 0;
}
int pr(char s) // precedence checking
{
if(s == ‘^’)
{
return 4;
}
else if(s == ‘*’)
{
return 3;
}
else if(s == ‘/’)
{
return 3;
}
else if(s == ‘+’)
{
return 2;
}
else if(s == ‘-’)
{
return 2;
}
else {
return 0;
}
}
void infixToPostfix(string st)
{
stackop; //stack for operators and bracket
st += “n”; //finding end of string
int l = st.size();
op.push('0');
string fst;
for(int x = 0;x<l;x++)
{
if(st[x] == '(')
{
op.push(st[x]);
}
else if(st[x] == ')')
{
while(op.top() != '(' && !op.empty())
{
fst +=op.top();
op.pop();
}
op.pop();
}
else if(st[x] == '+' || st[x] == '-' || st[x] == '*' ||st[x] == '/' ||st[x] == '^')
{
if(pr(st[x]) <= pr(op.top()))
{
fst += op.top();
op.pop();
op.push(st[x]);
}
else{
op.push(st[x]);
}
}
else if(st[x] == 'n'){
while(op.size() != 0)
{
if(op.top() != '0')
{fst += op.top();}
op.pop();
}
}
else
{
fst+= st[x];
}
}
printf("%s\n",fst.c_str());
}