后缀表达式求值-C++实现

一、思路

给出一个后缀表达式(以字符串数组的形式,每一项要么是一个操作数字符串,要么是一个运算符),计算过程中要将操作数从string形式转换为int形式,在计算表达式运算结果时,需要设置一个栈 num 保存操作数,计算步骤如下:

1)若当前项是操作数,则将该操作数压入num栈中;

2)若当前项是运算符,则从操作数栈中出栈两个操作数(栈顶的数作为第二个操作数,次栈顶的数作为第一个操作数),计算两个操作数进行运算的结果,然后将结果压入操作数栈num中。

遍历完后缀表达式数组后,操作数栈num中必定只剩下一个数(表达式正确的情况下),这个数就是表达式的运算结果。

二、代码

int solve(vector<string> postexp;) {
        //postexp是后缀表达式,每一项是一个操作数串或者运算符
        stack<int> num;  //操作数栈
        int len = postexp.size();
        int i = 0;
        while(i < len){  //对后缀表达式求值
            if(postexp[i] != "+" && postexp[i] != "-" && postexp[i] != "*"){
                int tmp = atoi(postexp[i].c_str()) ; //操作数(string类型转换为int型)
                num.push(tmp); //将操作数压入栈中
                i++;
            }
            else{  //每遇到一个操作符,就将两个操作数出栈,运算完后将结果压栈
                int opnum1 = num.top(); 
                num.pop();
                int opnum2 = num.top();
                num.pop();
                int res = 0;  //运算结果
                char op= postexp[i][0]; //字符串的第一个字符(实际上也只有一个字符)
                switch(op){ //注意两个操作数的顺序,栈顶元素作为第二个操作数
                    case '+':
                        res = opnum2 + opnum1;
                        break;
                    case '-':
                        res = opnum2 - opnum1;
                        break;
                    case '*':
                        res = opnum2 * opnum1;
                        break;
                    case '/':  
                        res = opnum2 / opnum1;
                        break;
                    default:
                        break;
                }
                num.push(res);
                i++;
            }
        }
        return num.top();  //最后栈中必定只剩下一个数,就是运算结果
    }

  • 12
    点赞
  • 71
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
好的,链栈实现后缀表达式求值的思路如下: 1. 定义一个链栈结构体,包含数据域和指向下一个节点的指针域。 2. 遍历后缀表达式,如果是数字则入栈,如果是运算符则弹出栈顶的两个数字进行运算,并将结果入栈。 3. 最后栈中剩下的数字即为表达式的结果。 以下是代码实现: ```c++ #include <iostream> #include <string> using namespace std; struct Node { int data; Node* next; }; class LinkedStack { public: LinkedStack() { top = nullptr; } ~LinkedStack() { while (top != nullptr) { Node* temp = top; top = top->next; delete temp; } } void push(int x) { Node* newNode = new Node; newNode->data = x; newNode->next = top; top = newNode; } int pop() { if (top == nullptr) { return -1; // 栈为空 } int x = top->data; Node* temp = top; top = top->next; delete temp; return x; } int peek() { if (top == nullptr) { return -1; // 栈为空 } return top->data; } bool isEmpty() { return top == nullptr; } private: Node* top; }; int evaluatePostfix(string postfix) { LinkedStack stack; for (char c : postfix) { if (isdigit(c)) { stack.push(c - '0'); } else { int operand2 = stack.pop(); int operand1 = stack.pop(); switch (c) { case '+': stack.push(operand1 + operand2); break; case '-': stack.push(operand1 - operand2); break; case '*': stack.push(operand1 * operand2); break; case '/': stack.push(operand1 / operand2); break; } } } return stack.pop(); } int main() { string postfix = "23+4*"; int result = evaluatePostfix(postfix); cout << "Result: " << result << endl; return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值