开发者

a code for infix to postfix conversion

开发者 https://www.devze.com 2023-04-11 14:37 出处:网络
I have written a code for infix to postfix conversion,This piece of code is not encountering any kind of compile time error but after taking the input infix expression it is giving some runtime errors

I have written a code for infix to postfix conversion,This piece of code is not encountering any kind of compile time error but after taking the input infix expression it is giving some runtime errors which i am unable to understand these errors are something related to string as the message says.

#include<iostream>
#include<string>

#define N 50
using namespace std;

class stack
{
private:
    char arr[N];
    int tos;

public:
    void push(char p)
    {
        if (tos != N)
            arr[++tos] = p;
        else
            cout << "stack full";
    }

    char pop()
    {
        if (tos == -1)
            cout << "stack Empty";
        else
            return (arr[tos--]);
    }

    bool isempty()
    {
        if (tos == -1)
            return (true);
        else 
            return (false);
    }

    char top()
    {
        return arr[tos];
    }

    stack()
    {
        tos = -1;
    }
};

int pres(char sym)
{
    if (sym == '^')
        return 3;
    else if (sym == '*' || '/')
        return 2;
    else if (sym == '+' || '-')
        return 1;
    else if开发者_StackOverflow社区 (sym == '(')
        return 0;
} 

bool isoperator(char op)
{
    if (op=='+' || op=='-' || op=='/' || op=='*' || op=='^')
        return true;
    else
        return false;
}

int main()
{
    string infix, postfix;
    stack s;
    int in=0;
    int post=0;

    cout << "Enter an infix expression: ";
    cin >> infix;
    s.push('(');
    infix.append(")");
    char temp;

    while (!(s.isempty()))
    {
        if (isdigit(infix[in]))
            postfix[post++] = infix[in];
        else if (infix[in] == '(')
            s.push(infix[in]);
        else if (infix[in] == ')')
        {
            while (1)
            {
                temp = s.pop();
                if (temp == '(')
                    break;
                else
                    postfix[post] = infix[in];
            }
        }
        else if (isoperator(infix[in]))
        {
            while (pres(s.top()) >= pres(infix[in]))
                postfix[post++] = s.pop();

            s.push(infix[in]);
        }
        in++;
    }

    cout << "Postfix expression is: " << postfix;
    system("pause");

}

I m unable to get what's wrong with it. Can any one help??


I found the following logical errors in your code:

  • the result string postfix is empty at the beginning, but you're writing to single character positions using postfix[post++]=. This is not valid and is propably causing the "string related" errors. You should only use postfix.push_back() to add characters to the output string.
  • In the first inner while loop (while(1)) the last statement should read

    postfix.push_back(temp);

    since you want to append the operators from the stack to the output.

  • Your code falsely accept input with unbalanced additional closing parents like "1+4)". Personally, I would put the input position as outer loop condition and verify that the stack is empty after the loop (and check for empty stack in the pop() function) for detecting input errors.


The biggest error is in his pres() function, it should be:

else if (sym == '*' || sym == '/')
else if (sym == '+' || sym == '-')

I have noticed some of the errors mentioned by MartinStettner.

0

精彩评论

暂无评论...
验证码 换一张
取 消