2024/12/1日工作总结

完成pta实验7-2 栈实现表达式求值

使用键盘输入数学表达式(含数字,四种运算符+、-、、/和小括号,其中运算数都是一位数(0~9)),将数学表达式转化成后缀表达式输出,利用后缀表达式求表达式的值并输出。

输入格式:
输入正确的表达式(可以有空格)后回车,得到后缀表达式和结果。输入括号缺失的表达式,输出"ERROR:缺少括号"。输入两个除括号外运算符连续的表达式,输出"ERROR:表达式缺操作数"。

输出格式:
请在这里描述输出格式。例如:对每一组输入,在一行中输出A+B的值。

输入样例:
在这里给出一组输入。例如:

5*(8-(3+2))
输出样例:
在这里给出相应的输出。例如:

5832+-*
15

点击查看代码
#include <iostream>
#include <stack>
#include <string>
using namespace std;

int precedence(char op){
    if (op == '+' || op == '-'){
        return 1;
    }
    if (op == '*' || op == '/'){
        return 2;
    }
    return 0;
}

string toPostfix(const string& expression){
    stack<char> stack;
    string postfix;
    bool numRequired = true;

    for (char ch : expression){
        if (isdigit(ch)){
            postfix += ch;
            numRequired = false;
        } else if (ch == '('){
            stack.push(ch);
            numRequired = true;
        } else if (ch == ')'){
            while (!stack.empty() && stack.top() != '('){
                postfix += stack.top();
                stack.pop();
            }
            if (stack.empty()){
                return "ERROR:缺少括号";
            }
            stack.pop();
            numRequired = false;
        } else if (ch == '+' || ch == '-' || ch == '*' || ch == '/'){
            if (numRequired){
                return "ERROR:表达式缺操作数";
            }
            while (!stack.empty() && precedence(stack.top()) >= precedence(ch)){
                postfix += stack.top();
                stack.pop();
            }
            stack.push(ch);
            numRequired = true;
        }
    }
    
    while (!stack.empty()){
        char top = stack.top();
        if (top == '('){
            return "ERROR:缺少括号";
        }
        postfix += top;
        stack.pop();
    }
    
    return postfix;
}

int evaluatePostfix(const string& postfix){
    stack<int> stack;
    
    for (char token : postfix){
        if (isdigit(token)){
            stack.push(token - '0');
        }else{
            int b = stack.top();
            stack.pop();
            int a = stack.top();
            stack.pop();
    
            switch (token){
                case '+':
                    stack.push(a + b);
                    break;
                case '-':
                    stack.push(a - b);
                    break;
                case '*':
                    stack.push(a * b);
                    break;
                case '/':
                    stack.push(a / b);
                    break;
                default:
                    break;
            }
        }
    }
    
    return stack.top();
}

int main(){
    string expression;
    getline(cin, expression);
    
    string postfix = toPostfix(expression);
    if(postfix.substr(0,5)=="ERROR"){
        cout<<postfix<<endl;
    }else{
        int result = evaluatePostfix(postfix);
        cout<<postfix<<endl;
        cout<<result<<endl;
    }
    return 0;
}


posted @ 2024-12-18 11:13  vivi_vimi  阅读(17)  评论(0)    收藏  举报