0

This is my code to convert infix expression to postfix. In the last if else condition, if I am using else then I am getting Segmentation Fault (SIGSEGV) while if I use else if and define the if condition, then the code is working absolutely fine. I cannot figure out why is this happening. This is for the test case (a+(b*c)). Logically, I have covered a-z,A-Z,(,) previously in the if-else conditions and the code should work fine if I use else for the operators. I need help here.

#include <bits/stdc++.h>
using namespace std;

int prt(char s)
{
    if (s == '(')
        return 0;
    if (s == '+' || s == '-')
        return 1;
    if (s == '*' || s == '/')
        return 2;
    if (s == '^')
        return 3;
}

int main()
{
    // your code goes here
    int T;
    cin >> T;
    while (T--) {
        string str;
        cin >> str;
        stack<char> s;

        for (int i = 0; i <= str.length(); i++) {
            if ((str[i] >= 'a' && str[i] <= 'z') || (str[i] >= 'A' && str[i] <= 'Z')) {
                cout << str[i];
            }
            else if (str[i] == '(') {
                s.push('(');
            }
            else if (str[i] == ')') {
                while (s.top() != '(') {
                    cout << s.top();
                    s.pop();
                }
                s.pop();
            }
            else if (str[i] == '+' || str[i] == '-' || str[i] == '*' || str[i] == '/' || str[i] == '^') { //This One
                while (prt(s.top()) >= prt(str[i])) {
                    cout << s.top();
                    s.pop();
                }
                s.push(str[i]);
            }
        }

        while (!s.empty()) {
            cout << s.top();
            s.pop();
        }

        cout << endl;
    }
    return 0;
}

Also, some feedback on code writing style improvement is appreciated.

Alan Birtles
  • 32,622
  • 4
  • 31
  • 60
  • 6
    `for(int i=0;i<=str.length();i++)` -- Using `<=` in a `for` loop is signs of an off-by-one error. – PaulMcKenzie Jul 06 '21 at 16:51
  • 5
    `prt` does not always return a value. This, combined with looking at the nul terminator of the string because of the off-by-one error, results in the fault. – 1201ProgramAlarm Jul 06 '21 at 16:53
  • 1
    `if((str[i] >='a' && str[i]<='z') || (str[i] >='A' && str[i]<='Z'))` -- This is not how to check if a character is alphabetic. The proper check is `if (std::isalpha(static_cast(str[i])))` – PaulMcKenzie Jul 06 '21 at 16:54
  • 1
    [Explanation of why Paul is casting characters to integers before calling is `alpha`](https://en.cppreference.com/w/cpp/string/byte/isalpha#Notes) – user4581301 Jul 06 '21 at 16:58
  • 2
    `#include ` -- [Don't do this](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) – PaulMcKenzie Jul 06 '21 at 16:58
  • 2
    And especially don't if you also [`using namespace std;`](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) Combining the two brings out the other's worst aspects, too often resulting in nigh-inscrutable errors. – user4581301 Jul 06 '21 at 17:00
  • 2
    @PaulMcKenzie • should be `static_cast` if the platform treats `char` as signed by default. – Eljay Jul 06 '21 at 17:01
  • @PaulMcKenzie The mistake was small but the feedback is what helped me the most. thank you – Tushar Agrawal Jul 06 '21 at 17:03
  • @user4581301 The mistake was small but the feedback is what helped me the most. thank you – – Tushar Agrawal Jul 06 '21 at 17:04
  • 1
    @Eljay -- Yes, you're right. – PaulMcKenzie Jul 06 '21 at 22:52

0 Answers0