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


    #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栈顶元素的优先级一样的话,怎么处理。

  • 相关阅读:
    云钉一体应用创新:音视频如何带来灵活高效的协同体验
    正则表达式,去除非数字。js动态计算
    使用DbContext实体类访问数据库
    C#字符串去除特殊字符
    实体类赋值给控件,控件赋值给实体类
    List去除重复项。
    分页查询的sql语句
    js页面处理常见问题
    .net Table 导出Excel
    上传文件。普通和ftp
  • 原文地址:https://www.cnblogs.com/h694879357/p/11766972.html
Copyright © 2020-2023  润新知