zoukankan      html  css  js  c++  java
  • 算法训练 表达式计算

    #include <iostream>
    #include <string>
    #include <sstream>
    #include <stack>
    #include <cctype>
    
    using namespace std;
    
    stack<int> stack_num;
    stack<char> stack_op;
    
    bool f(char ch1, char ch2){
        // 1-2+3     1*2/3
        if ((ch1 == '-'||ch1 == '+') && (ch2 == '+'|| ch2 == '-') )
            return false;
        if ((ch1 == '*' || ch1 == '/') && (ch2 == '*' || ch2 == '/'))
            return false;
    
        // 1*2-3
        if ((ch1 == '*' || ch1 == '/') && (ch2 == '+' || ch2 == '-'))
            return false;
    
        // 1+2*3
        if ((ch1 == '+' || ch1 == '-') && (ch2 == '*' || ch2 == '/'))
            return true;
    }
    
    int calc(int num1, int num2, char op){
        int ret = 0;
        switch (op)
        {
        case '+': ret = num1 + num2; break;
        case '-': ret = num1 - num2; break;
        case '*': ret = num1 * num2; break;
        case '/': ret = num1 / num2; break;
        default:
            break;
        }
        return ret;
    }
    // 5*((1-2)*(6-7+8*9-(34-12*1)))
    // 1-2+3*(4-5)
    int main(){
        string str;
        cin >> str;
    
        for (unsigned int i = 0; i < str.length(); i++){
            if (isdigit(str[i])){
                string tmp = "";
                stringstream ss;
                int k;
                while (isdigit(str[i])){
                    tmp += str[i];
                    i++;
                }
                i-=1;
                ss << tmp;
                ss >> k;
                stack_num.push(k);
            }else{
                if (stack_op.empty() || str[i] == '('){
                    stack_op.push(str[i]);
                }else{
                    if (str[i] == ')'){
                       while (stack_op.top() != '(')
                       {
                           int num1, num2;
                           if (!stack_num.empty()){
                               num2 = stack_num.top();
                               stack_num.pop();
                           }else{
                               num2 = 0;
                           }
                           if (!stack_num.empty()){
                               num1 = stack_num.top();
                               stack_num.pop();
                           }else{
                               num1 = 0;
                           }
                           char op = stack_op.top();
                           stack_op.pop();
                           stack_num.push(calc(num1, num2, op));
                       }
                       stack_op.pop(); // 除去stack_op 中的 (
                    }else{
                        char ch1 = stack_op.top();
                        char ch2 = str[i];
                        if (f(ch1, ch2)){
                            stack_op.push(str[i]);
                        }else{
                            int num2 = stack_num.top();
                            stack_num.pop();
                            int num1 = stack_num.top();
                            stack_num.pop();
                            int s = calc(num1, num2, stack_op.top());
                            stack_op.pop();
                            stack_num.push(s);
                            stack_op.push(str[i]);
                        } // end if (f(ch1,ch2))
                    }
                }
            }
        }
    
        // 清空尾数据
        while (!stack_op.empty()){
            int num1, num2;
            if (!stack_num.empty()){
                num2 = stack_num.top();
                stack_num.pop();
            }else{
                num2 = 0;
            }
            if (!stack_num.empty()){
                num1 = stack_num.top();
                stack_num.pop();
            }else{
                num1 = 0;
            }
            char op = stack_op.top();
            stack_op.pop();
            stack_num.push(calc(num1, num2, op));
        }
        cout << stack_num.top() << endl;
        return 0;
    }
  • 相关阅读:
    【bzoj2079】[Poi2010]Guilds 构造结论题
    【bzoj1899】[Zjoi2004]Lunch 午餐 dp
    【bzoj1345】[Baltic2007]序列问题Sequence 单调栈
    【bzoj1047】[HAOI2007]理想的正方形 二维RMQ
    【bzoj1044】[HAOI2008]木棍分割 二分+dp
    【bzoj5037】[Jsoi2014]电信网络 最大权闭合图
    【bzoj5018】[Snoi2017]英雄联盟 背包dp
    【bzoj5020】[THUWC 2017]在美妙的数学王国中畅游 泰勒展开+LCT
    【bzoj2213】[Poi2011]Difference dp
    【bzoj2161】布娃娃 权值线段树
  • 原文地址:https://www.cnblogs.com/laohaozi/p/8266491.html
Copyright © 2011-2022 走看看