0

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;
}
  • 1
    You are writing to arbitrary indices of `postfix`, which is an empty string. If you need to append characters, use the `push_back` method. – paddy Sep 15 '22 at 08:23
  • 3
    `#include` -- You actually learned to do this from following class instructions? If so, that class is not teaching you C++ properly. -- *There is no compile error* -- There is when I take your code and compile it using the latest version of Visual C++, all because of that non-standard header. – PaulMcKenzie Sep 15 '22 at 08:24
  • The call `precedence(st.top() || st.empty())` is also very sketchy. I think you meant `if(st.empty() || precedence(infix[i]) > precedence(st.top()))` – paddy Sep 15 '22 at 08:26
  • `if(precedence(infix[i])>precedence(st.top() || st.empty()))` -- Explain what this line of code is supposed to do. – PaulMcKenzie Sep 15 '22 at 08:27
  • @PaulMcKenzie Actually *any* version of MSVC and alike clang, borland, and whichever other compiler. The header is exclusively GCC-specific – and even there *not* intended for direct inclusion. – Aconcagua Sep 15 '22 at 08:28
  • Rolling together all recommendations from the comments: https://godbolt.org/z/69fYoddKv – paddy Sep 15 '22 at 08:29
  • About [using namespace std](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice)… – Aconcagua Sep 15 '22 at 08:29
  • [Why should I not #include ?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) – Jesper Juhl Sep 15 '22 at 09:29

2 Answers2

0

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.

john
  • 85,011
  • 4
  • 57
  • 81
0

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;
}
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – primo Sep 21 '22 at 04:35