简单计算器实现-中缀表达式转后缀表达式求值

时间:2022-02-28 11:29:48
#include <iostream>
#include <stack>
using namespace std;

int priority(char signal)  //优先级
{
    int prior = 0;
    switch(signal)
    {
        case '+':
        {
            prior = 1;
            break;
        }
        case '-':
        {
            prior = 1;
            break;
        }
        case '*':
        {
            prior = 2;
            break;
        }
        case '/':
        {
            prior = 2;
            break;
        }
        case '(':
        {
            prior = 3;
            break;
        }
        case ')':
        {
            prior = 3;
            break;
        }
        default:
            return 0;
    }
    return prior;
}
char* InfixToPostfix(const string &str)
{
    long len = str.size();
    char *goal = new char[len];
    stack<char> s;
    int j = 0;
    for(int i = 0;i<len;i++) //遍历一次字符串
    {
        if(priority(str[i])==0) //如果是操作数 直接输出
        {
            cout<<str[i];
            goal[j++] = str[i];
        }
        else
        {
            if(s.empty())     //栈为空,将当前操作符压入栈
                s.push(str[i]);
            else
            {
                if((priority(str[i])>priority(s.top())||s.top() == '(')&&str[i] != ')') //如果当前操作符优先级大于栈顶优先级,压入栈
                    s.push(str[i]);                                                     //如果栈顶元素为(,将操作符压入栈
                else
                {
                    if(str[i] == ')')      //遇到右括号')' 弹出所有操作符直到与之匹配的左括号'('
                    {
                        while(s.top() != '(')
                        {
                            cout<<s.top();
                            goal[j++] = s.top();
                            s.pop();
                        }
                        s.pop();
                    }
                    else
                    {
                        if(!s.empty())                 //如果当前操作符优先级不大于栈顶优先级,则弹出栈元素直到栈顶优先级小于当前操作符
                        {
                            while(!s.empty()&&s.top() != '('&&priority(str[i])<=priority(s.top()))
                            {
                                cout<<s.top();
                                goal[j++] = s.top();
                                s.pop();
                            }
                            s.push(str[i]);
                        }
                    }
                }
            }
        }
    }
    while(!s.empty())   //最后将栈内操作符全部弹出
    {
        cout<<s.top();
        goal[j++] = s.top();
        s.pop();
    }
    cout<<endl;
    goal[j] = '\0';
    return goal;
}

int operation(int a,int b,char symbol)
{
    switch (symbol)
    {
        case '+':
        {
            return a + b;
            break;
        }
        case '-':
        {
            return a - b;
            break;
        }
        case '*':
        {
            return a * b;
            break;
        }
        case '/':
        {
            return a/b;
        }
        default:
            return 0;
    }
}
int calculator(string postFix) //计算后缀表达式。遇到操作数就入栈,遇到操作符就弹出两个元素AB,然后B<op>A,将结果压入栈
{
    long len = postFix.size();
    stack<char> s;
    for(int i = 0;i<len;i++)
    {
        if(priority(postFix[i])==0)
            s.push(postFix[i]);
        else
        {
            int operand1 = s.top() - '0';
            s.pop();
            int operand2 = s.top() - '0';
            s.pop();
            int t = operation(operand2, operand1,postFix[i]);
            char tmp = (char)(t + '0');
            s.push(tmp);
        }
    }
    return s.top() - '0';
}

int main()
{
    string infix = "2+3*(4-2)-6/3";
    cout<<infix<<endl;
    string goal = InfixToPostfix(infix);
    //cout<<goal<<endl;
    cout<<calculator(goal)<<endl;
    return 0;
}