中缀表达式转后缀表达式 求值

#include<cstdio>
#include<iostream>
#include<string>
#include<stack>
#include<cmath>
#include<cstring>
using namespace std;

void Get(string a, char b[]);//中缀表达式转后缀表达式
bool Com(stack <char> s, char c);//运算符比较
int Get_V(char b[]);//后缀表达式求值

int main()
{
    string a;
    char b[100] = {0};
    cin >> a;
    Get(a,b);
    printf("%s\n", b);
    int value = Get_V(b);
    cout << a << '=' << value << endl;
    system("pause");
    return 0;
}
void Get(string a,char b[])
{
    int l = a.size();
    int i,j;
    i = j = 0;
    stack <char> s;
    char c;
    while (i < l)
    {
        c = a[i];
        if (c >= '0'&&c <= '9')//运算对象
        {
            b[j] = c;
            i++;
            j++;
        }
        else//运算符
        {
            if (Com(s,c))//栈顶操作符优先级低
            {
                s.push(c);
                i++;
            }
            else
            {
                if (s.top() == '(')
                {
                    s .pop();
                    i++;
                }
                else
                {
                    b[j] = s.top();
                    s.pop();
                    j++;
                }
            }
        }   
    }
    while (!s.empty())
    {
        b[j++] = s.top();
        s.pop();
    }
    b[j] = '\0';
}
bool Com(stack <char> s, char c)
{

    if (s.empty())
        return true;
    else
    {
        char a = s.top();
        if (c == '+')
        {
            switch (a)
            {
            case '+': return false;
            case '-': return false;
            case '*': return false;
            case '/': return false;
            case '^': return false;
            case '(': return true;

            }
        }
        if (c == '-')
        {
            switch (a)
            {
            case '+': return false;
            case '-': return false;
            case '*': return false;
            case '/': return false;
            case '^': return false;
            case '(': return true;

            }
        }
        if (c == '*')
        {
            switch (a)
            {
            case '+': return true;
            case '-': return true;
            case '*': return false; 
            case '/': return false;
            case '^': return false;
            case '(': return true;
            }
        }
        if (c == '/')
        {
            switch (a)
            {
            case '+': return true;
            case '-': return true;
            case '*': return false;
            case '/': return false;
            case '^': return false;
            case '(': return true;
            }
        }
        if (c == '^')
        {
            switch (a)
            {
            case '+': return true;
            case '-': return true;
            case '*': return true;
            case '/': return true;
            case '^': return false;
            case '(': return true;
            }
        }
        if (c == '(')
        {
            switch (a)
            {
            case '+': return true;
            case '-': return true;
            case '*': return true;
            case '/': return true;
            case '^': return true;
            case '(': return true;
            case ')': return false;
            }
        }
        if (c == ')')
        {
            switch (a)
            {
            case '+': return false;
            case '-': return false;
            case '*': return false;
            case '/': return false;
            case '^': return false;
            case '(': return false;
            }
        }
    }
}
int Get_V(char b[])
{
    int l = strlen(b);
    int i = 0;
    int ans;
    stack <int> a;
    char c;
    while (i<l)
    {
        c = b[i];
        if (c >= '0'&&c <= '9')
        {
            a.push(c - '0');
            i++;
        }
        else
        {
            int x2 = a.top();
            a.pop();
            int x1 = a.top();
            a.pop();
            switch (c)
            {
            case '+': ans = x1 + x2; break;
            case '-': ans = x1 - x2; break;
            case '*': ans = x1 * x2; break;
            case '/': ans = x1 / x2; break;
            case '^': ans = pow(x2, x2); break;
            }
            a.push(ans);
            i++;
        }
    }
    ans = a.top();
    a.pop();
    return ans;
}

猜你喜欢

转载自blog.csdn.net/zero_1778393206/article/details/79747138