So, I was doing Data Structure and ALgorithm using C and STL, I was trying to implement Infix to postfix using stack. I am not sure what is this issue with the code? There is no compile error and when the code runs it returns -1073741510. I have rechecked the whole code, couldn't found any issues
#include<iostream>
#include<stack>
#include<string>
using namespace std;
int isOperator(char ch)
{
if(ch==' ' || ch == '-' || ch == '*' || ch == '/')
return 1;
else
return 0;
}
int precedence(char ch)
{
//for limited input only
if(ch == '*' || ch == '/')
return 3;
else if(ch==' ' || ch == '-' )
return 2;
else
return 0;
}
string infixtopostfix(string infix)
{
stack <char> st;
int i=0;
string postfix;
while(infix[i]!='\0')
{
if(!isOperator(infix[i]))
{
postfix.push_back(infix[i]);
i ;
}
else
{
if(precedence(infix[i])>precedence(st.top()))
{
st.push(infix[i]);
i ;
}
else{
postfix.push_back(st.top());
st.pop();
}
}
}
while(!st.empty())
{
postfix.push_back(st.top());
st.pop();
}
return postfix;
}
int main()
{
string infix= "a b";
cout<<"Postfix-->"<<infixtopostfix(infix)<<endl;
return 0;
}
CodePudding user response:
Your postfix
string has a size of zero. This means that any attempt to access the characters of that string is an error. If you want to add characters to a string use push_back
.
postfix[j] = infix[i];
should be
postfix.push_back(infix[i]);
and (twice)
postfix[j] = st.top();
should be
postfix.push_back(st.top());
and
postfix[j]='\0';
should be removed.
There is no need to nul terminate std::string
. Once you have made all these changes you will also see that the j
variable can be removed. A std::string
knows it's own size, you don't need a separate variable to keep track. It seems that you are programming a std::string
as if it's like a C string.
It seems to be a very common misunderstanding that std::string
(or a std::vector
) automatically grows when you subscript it. This is not true.
EDIT
You have another error here,
if(precedence(infix[i])>precedence(st.top()))
The stack maybe empty when executing this statement, leading to a crash. I'm guessing the code should read
if(st.empty() || precedence(infix[i])>precedence(st.top()))
With that change your code works for me.
CodePudding user response:
Thank You everyone for the help. Here is the final answer which is running without any error.
#include<iostream>
#include<stack>
#include<string>
using namespace std;
int isOperator(char ch)
{
if(ch==' ' || ch == '-' || ch == '*' || ch == '/')
return 1;
else
return 0;
}
int precedence(char ch)
{
//for limited input only
if(ch == '*' || ch == '/')
return 3;
else if(ch==' ' || ch == '-' )
return 2;
else
return 0;
}
string infixtopostfix(string infix)
{
stack <char> st;
int i=0;
string postfix;
while(infix[i]!='\0')
{
if(!isOperator(infix[i]))
{
postfix.push_back(infix[i]);
i ;
}
else{
if(st.empty() || precedence(infix[i])>precedence(st.top()))
{
st.push(infix[i]);
i ;
}
else{
postfix.push_back(st.top());
st.pop();
}
}
}
while(!st.empty())
{
postfix.push_back(st.top());
st.pop();
}
return postfix;
}
int main()
{
string infix= "a b";
cout<<"Postfix-->"<<infixtopostfix(infix)<<endl;
return 0;
}