• 后缀算术表达式


    后缀表达式的特点如下:

    1、后缀表达式的操作数与中缀表达式的操作数先后次序相同,而运算符的先后次序不同。

    2、后缀表达式中没有括号,而且运算符没有优先级。

    3、后缀表达式计算过程严格按照从左到右的顺序进行。

    例如:

    算术表达式a+(b-c)*d的后缀式是:abc-d*+

    在输入的时候出了问题:

    string s;

    cin>>s;  不能输入空格

    所以要用getline 

    getline(cin,s);

    头文件 string 或者istream

    #include<iostream>
    #include<string> 
    #include<istream>
    #include<cstring>
    #include<iomanip>
    using namespace std;
    typedef struct StackNode *LStack;
    struct StackNode {
        double nu;
        LStack next;
    };
    void Init(LStack &s)
    {
        s = new StackNode;
        s = NULL;
    }
    void Pushnu(LStack &s, double c)
    {
        LStack p = new StackNode;
        p->nu = c;
        p->next = s;
        s = p;
    }
    void Pop(LStack &s)
    {
        LStack p;
        p = s;
        if (!s->next)
            s = NULL;
        else
            s = s->next;
        delete p;
    }
    double GetTopnu(LStack &s)
    {
        return s->nu;
    }
    int main()
    {
        string s;
        s = "";
        cout << setiosflags(ios::fixed) << setprecision(2);
        while (s != "=")
        {
            getline(cin, s);
            char *ptr;
            int length = 0;
            LStack p;//s1是数字,s2是运算符
            Init(p);
            for (int i = 0; i <s.size(); i++)
            {
                if (s[i] >= '0'&&s[i] <= '9')
                {
                    Pushnu(p, s[i] - 48);
                }
                else
                    if (s[i] == '+')
                    {
                        double a, b;
                        a = GetTopnu(p);
                        Pop(p);
                        b = GetTopnu(p);
                        Pop(p);
                        double c;
                        c = a + b;
                        Pushnu(p, c);
                    }
                    else
                    {
                        if (s[i] == '-')
                        {
                            double a, b;
                            a = GetTopnu(p);
                            Pop(p);
                            b = GetTopnu(p);
                            Pop(p);
                            double c;
                            c = b - a;
                            Pushnu(p, c);
                        }
                        else
                            if (s[i] == '*')
                            {
                                double a, b;
                                a = GetTopnu(p);
                                Pop(p);
                                b = GetTopnu(p);
                                Pop(p);
                                double c;
                                c = b * a;
                                Pushnu(p, c);
                            }
                            else
                            {
                                if (s[i] == '/')
                                {
                                    double a, b;
                                    a = GetTopnu(p);
                                    Pop(p);
                                    b = GetTopnu(p);
                                    Pop(p);
                                    double c;
                                    c = b / a;
                                    Pushnu(p, c);
                                }
                                else
                                    if (s[i] == '=')
                                        break;
                                    else
                                        if (s[i] == ' ')
                                        {
                                            
                                            continue;
                                        }
    
                            }
    
                    }
            }
            if (s == "=")
                return 0;
            cout << p->nu << endl;
        }
        return 0;
    }
  • 相关阅读:
    centos7下源码编译方式安装httpd
    转-centos7下安装apache服务器httpd的yum方式安装
    centos7下安装mysql
    centos7下安装tomcat7
    centos7下安装jdk7
    centos7 下安装eclipse
    mysql-用命令导出、导入表结构或数据
    mysql用户管理
    mysql错误总结-ERROR 1067 (42000): Invalid default value for TIMESTAMP
    Linux kernel启动log显示时间戳
  • 原文地址:https://www.cnblogs.com/h694879357/p/11767445.html
Copyright © 2020-2023  润新知