计算中缀表达式的值

计算中缀表达式的值

#include<iostream>
#include<stack>
#include<string>
#include<stdlib.h>
using namespace std;
int priority(int state,char a){
//计算操作符优先级的函数,注意state表示运算符状态:
//state=1表示还未进栈,state=0表示栈内优先级,注意
//这只对‘(’起作用
    int result;
    switch (a){
    case '+':
    case '-':
        result = 1;
        break;
    case '*':
    case '/':
        result = 2;
        break;
    case '(':
        if (state == 0)
            result = 3;
        else
            result = 0;
        break;
    case '#':
        result = 0;
        break;
    default:
        break;
    }
    return result;
}
double calculate(char op, double op1, double op2){
    double result;
    switch (op){
    case '+':
        result = op1 + op2;
        break;
    case '-':
        result = op1 - op2;
        break;
    case '*':
        result = op1*op2;
        break;
    case '/':
        result = op1 / op2;
        break;
    default:
        break;
    }
    return result;
}
int main(){
    string s;
    while (cin >> s){//测试多组数据
        stack<char> operation;//存放操作符的栈
        stack<double> operand;//存放操作数的栈
        operation.push('#');//先将‘#’压栈
        string num;//存放操作数
        for (int i = 0; i < s.length(); i++){
            if (isdigit(s[i])){//出现数字
                while (isdigit(s[i]) || s[i] == '.'){//将操作数提取完全
                    num.push_back(s[i]);
                    i++;
                }
                double a = atof(num.c_str());//string->double
                operand.push(a);//操作数入栈
                num.clear();//num清空以备下次使用
                i--;//位置还原
            }
            else if (s[i] == '+' || s[i] == '-' || s[i] == '*' || s[i] == '/' || s[i] == '('){//出现运算符
                if (priority(0, s[i])>priority(1, operation.top()))//优先级比较
                    operation.push(s[i]);//>,直接入栈
                else{
                    while (priority(0, s[i]) <= priority(1, operation.top())){//<,出栈并进行计算直至>
                        char temp = operation.top();
                        operation.pop();
                        double op2 = operand.top();
                        operand.pop();
                        double op1 = operand.top();
                        operand.pop();
                        operand.push(calculate(temp, op1, op2));
                    }
                    operation.push(s[i]);//不要忘了最后操作符入栈
                }
            }
            else if (s[i] == ')'){//扫描到‘)’
                while (operation.top() != '('){//出栈直至‘(’
                    char temp = operation.top();
                    operation.pop();
                    double op2 = operand.top();
                    operand.pop();
                    double op1 = operand.top();
                    operand.pop();
                    operand.push(calculate(temp, op1, op2));
                }
                operation.pop();//‘(’出栈
            }
            else{//非法字符的处理
                cout << "error!" << endl;
                return 0;
            }
        }
        while (operation.top() != '#'){//扫尾工作
            char temp = operation.top();
            operation.pop();
            double op2 = operand.top();
            operand.pop();
            double op1 = operand.top();
            operand.pop();
            operand.push(calculate(temp, op1, op2));
        }
        cout << operand.top() << endl;//输出结果
    }
    return 0;
}
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值