中缀表达式转化为后缀表达式

#include<iostream>
#include<string>
#include<cstring>
using namespace std;
typedef struct StackNode* LStack;
struct StackNode {
    char data;
    LStack next;
};
void InitStack(LStack &s)
{
    s = NULL;
}
void Creat(LStack &s, char a)
{
    LStack p = new StackNode;
    p->data = a;
    p->next = s;
    s = p;
}
void Pop(LStack &s)
{
    LStack q;
    q = s;
    s = s->next;
    delete q;
}
char GetTop(LStack &s)
{
    if (s == NULL)
        return NULL;
    return s->data;
}
int f(char s)//优先级
{
    int p;
    if (s == '*' || s == '/')
        p = 2;
    else
        if (s == '+' || s == '-')
            p = 1;
    return p;
}
int main()
{
    string s;
    while (cin >> s && s != "=")
    {
        LStack q;
        InitStack(q);//中间结果
        LStack p;//运算符
        InitStack(p);
        for (int i = 0; i < s.size(); i++)
        {
            if (s[i] >= '0'&&s[i] <= '9')
            {
                Creat(q, s[i]);
            }
            else
            {
                if (s[i] == '(')
                    Creat(p, s[i]);
                else
                    if (s[i] == ')')
                    {
                        while (GetTop(p) != '(')
                        {
                            Creat(q, GetTop(p));
                            Pop(p);
                        }
                        Pop(p);
                    }
                    else
                    {
                        if (s[i] == '+' || s[i] == '-' || s[i] == '*' || s[i] == '/')
                        {

                            while (1)
                            {

                                int pp = f(s[i]);//得到优先级
                                if (GetTop(p) == '(' || p == NULL)
                                {
                                    Creat(p, s[i]);
                                    break;
                                }
                                else
                                {
                                    int qq = f(GetTop(p));
                                    if (qq <pp)
                                    {
                                        Creat(p, s[i]);
                                        break;
                                    }
                                    else
                                        if (pp <=qq)
                                        {
                                            Creat(q, GetTop(p));
                                            Pop(p);
                                        }
                                }
                            }
                        }
                        else
                            if (s[i] == '=')
                            {
                                while (p)
                                {
                                    Creat(q, GetTop(p));
                                    Pop(p);
                                }
                            }
                    }
            }

        }
        char t[1000];
        int count = 0;
        for (int i = 0;; i++)
        {
            t[i] = GetTop(q);
            Pop(q);
            count++;
            if (q == NULL)
                break;
        }
        for (int i = count - 1; i > 0; i--)
        {
            cout << t[i];
        }
        cout << t[0] << endl;
    }
    return 0;
}

错误原因:

1、没有考虑到运算符栈空的时候,运算符怎么push

2、当s[i]是运算符的时候,而且和p栈顶元素的优先级一样的话,怎么处理。

原文地址:https://www.cnblogs.com/h694879357/p/11766972.html