基于栈的中缀算术表达式求值

二次信任 提交于 2019-12-02 20:13:20
#include<iostream>
#include<string>
#include<cstring>
#include<iomanip>
using namespace std;
typedef struct StackNode *LStack;
/*
两个栈,一个是字符,一个是数字,
先判断是什么字符,然后调用不同的函数
*/
typedef struct Data {
    double number;
    char op;
};
struct StackNode {
    Data data;
    LStack next;
};
double Function(double a, double b, char op)
{
    if (op == '+')
        return a + b;
    else
        if (op == '-')
            return b - a;
        else
            if (op == '*')
                return b * a;
            else
                if (op == '/')
                    return b / a;
}
char Judge(char a, char b)
{
    if ((a == '('&&b == ')') || (a == '='&&b == '='))//a\b都为等号?
        return '=';
    else
        if (a == '(' || a == '=' || b == '(' || (a == '+' || a == '-') && (b == '*' || b == '/'))
            return '<';
        else
            return '>';
}
void InitStack(LStack &s)
{
    s = NULL;
}
void Creat1(LStack &s, double a)//数字
{
    LStack p = new StackNode;
    p->data.number = a;
    p->next = s;
    s = p;

}
void Creat2(LStack &s, char a)//op
{
    LStack p = new StackNode;
    p->data.op = a;
    p->next = s;
    s = p;

}
void Pop(LStack &s)
{
    LStack q;
    q = s;
    s = s->next;
    delete q;
}
double GetTop1(LStack &s)//number取栈顶元素
{
    return s->data.number;
}
char GetTop2(LStack &s)//op
{
    return s->data.op;
}
int  main()
{
    string c;
    double y;
    while (1)
    {
        cin >> c;
        if (c[0] == '=')
            break;
        LStack s1, s2;
        InitStack(s1);//数字
        InitStack(s2);//运算符
        Creat2(s2, '=');
        int x = 0, e = 0, flag = 0;//??
        for (int i = 0; i < c.size(); i++)
        {
            if (c[i] >= '0' && c[i] <= '9')//还要考虑有小数点呢
            {
                flag = 1;
                x = x * 10 + c[i] - '0';
                if (e)
                {
                    e *= 10;
                }
            }
            else
                if (c[i] == '.')
                {
                    e = 1;
                }
                else
                {
                    if (flag)
                    {
                        if (e)
                        {
                            y = x * 1.0 / e;
                        }
                        else
                            y = x * 1.0;
                        Creat1(s1, y);
                        x = 0;
                        e = 0;
                        flag = 0;
                    }
                    while (1)
                    {
                        if (Judge(GetTop2(s2), c[i]) == '<')
                        {
                            Creat2(s2, c[i]);
                            break;
                        }
                        else
                            if (Judge(GetTop2(s2), c[i]) == '>')
                            {
                                double a, b;
                                a = GetTop1(s1);
                                Pop(s1);
                                b = GetTop1(s1);
                                Pop(s1);
                                char op;
                                op = GetTop2(s2);
                                Pop(s2);
                                y = Function(a, b, op);
                                Creat1(s1, y);
                            }
                            else
                            {
                                Pop(s2);
                                break;
                            }

                    }
                }

        }
        cout << setiosflags(ios::fixed) << setprecision(2);
        cout << s1->data.number << endl;
    }
    return 0;

}

有一些不明白的:有小数点的部分没看明白呢。。

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!