题解 | #四则运算#

四则运算

http://www.nowcoder.com/practice/9999764a61484d819056f807d2a91f1e

#include <bits/stdc++.h>
#include <iostream>
#include <string>

using namespace std;

string changeKuohao(string& s){
    for(int i = 0; i < s.size(); i++){
        if(s[i] == '{' || s[i] == '['){
            s[i] = '(';
        }
        if(s[i] == '}' || s[i] == ']'){
            s[i] = ')';
        }
    }
    //cout<<s<<endl;
    return s;
}

int calculate(int a, int b, char c){
    switch(c){
        case '+':
            return a + b;
            break;
        case '-':
            return a - b;
            break;
        case '*':
            return a * b;
            break;
        case '/':
            return a / b;
            break;
        default:
            return 0;
            break;
    }
}

int process(string s){
    int flag = 0; //正负号标志,0为无正负号,1为正号,2为负号
    stack<int> numst; //数字栈
    stack<char> symbolst; //符号栈
    
    for(int i = 0; i < s.size(); i++){
        if(isdigit(s[i])){
            int num = 0; //数字的值
            int j = i;
            while(i + 1 < s.size() && isdigit(s[i + 1])) i++;
            string tmp = s.substr(j, i - j + 1);
            
            for(int k = 0; k < tmp.size(); k++){
                num = num * 10 + (tmp[k] - '0');
            }
            if(flag == 2) num = 0 - num;
            
            flag = 0; //复位
            numst.push(num); //入栈
        }  
        else if(s[i] == '*' || s[i] == '/' || s[i] == '('){//为乘除时
            symbolst.push(s[i]);
        }
        else if(s[i] == '+' || s[i] == '-'){//为加减时
            //处理负号
            if(i == 0 || s[i - 1] == '('){
                if(s[i] == '+'){
                    flag = 1;
                }
                else{
                    flag = 2;
                }
            }     
            //堆栈非空时,符号栈弹出符号,并结合数字栈计算
            while(!flag && !symbolst.empty() && symbolst.top() != '('){
                int b = 0, a = 0; char sym_tmp;
                b = numst.top(); numst.pop();
                a = numst.top(); numst.pop();
                sym_tmp = symbolst.top(); symbolst.pop();
                numst.push(calculate(a, b, sym_tmp));//计算结果入栈
            }
            if(!flag) symbolst.push(s[i]); //
        }
        else if(s[i] == ')'){//为右括号时
            while(symbolst.top() != '('){
                int b = 0, a = 0; char sym_tmp;
                b = numst.top(); numst.pop();
                a = numst.top(); numst.pop();
                sym_tmp = symbolst.top(); symbolst.pop();
                numst.push(calculate(a, b, sym_tmp));//计算结果入栈
            }
            symbolst.pop();
        }
        else{
            cout<<"error!"<<endl;
        }
    }

    //循环结束后把剩下的符号弹出,并结合数字栈计算
    while(!symbolst.empty()){
        int b = 0, a = 0; char sym_tmp;
        b = numst.top(); numst.pop();
        a = numst.top(); numst.pop();
        sym_tmp = symbolst.top(); symbolst.pop();
        numst.push(calculate(a, b, sym_tmp));//计算结果入栈
    }
    
    return numst.top();
}

int main(){
    string str = "";
    while(cin>>str){
        str = changeKuohao(str); //将大括号和中括号转成小括号
        int res = process(str);
        cout << res << endl;
    }
    
    return 0;
}
华为题库题解 文章被收录于专栏

牛客华为题库的题解

全部评论

相关推荐

在找内推的六边形战士很想去毕业旅行:不努力不是我兄弟_
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务