首页 > 技术文章 > 后缀算术表达式

h694879357 2019-10-30 20:16 原文

后缀表达式的特点如下:

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;
}

 

推荐阅读