• 后缀算术表达式


    后缀表达式的特点如下:

    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;
    }
  • 相关阅读:
    oracle数据库的增长改查
    Delete、truncate、drop删除数据的区别
    oracle数据库分页查询
    Sql语句中的null值
    RestEasy 3.x 系列之三:jsonp
    跨域请求解决方法(JSONP, CORS)
    JdbcUtils 系列1
    RestEasy 3.x 系列之一:Hello world
    JAVA中int、String的类型转换
    struts2 的验证框架validation如何返回json数据 以方便ajax交互
  • 原文地址:https://www.cnblogs.com/h694879357/p/11767445.html
Copyright © 2020-2023  润新知