Beispiel #1
0
//Recursive
int evalRPN(vector<string> &n) {
    string s = n.back(); n.pop_back();
    if ( s== "*" || s=="/" || s=="+" || s == "-" ){
        int r2 = evalRPN(n);
        int r1 = evalRPN(n);
        if ( s=="*") return r1*r2;
        if ( s=="/") return r1/r2;
        if ( s=="+") return r1+r2;
        if ( s=="-") return r1-r2;
    }
    else
        return atoi(s.c_str());
}
 int evalRPN(vector<string> &tokens) {
     int x, y;
     auto token = tokens.back();  tokens.pop_back();
     if (is_operator(token))  {
         y = evalRPN(tokens);
         x = evalRPN(tokens);
         if (token[0] == '+')       x += y;
         else if (token[0] == '-')  x -= y;
         else if (token[0] == '*')  x *= y;
         else                       x /= y;
     } else  {
         size_t i;
         x = stoi(token, &i);
     }
     return x;
 }
Beispiel #3
0
double evaluate(const std::string &expr) {
    std::queue<std::string> output;
    parse(expr, output);

    double result = evalRPN(output);
    return result;
}
    int evalRPN(vector<string> &tokens) {
        if(!tokens.size()) return 0;
        if(tokens.size() == 1)
            return stoi(tokens.front());
            
        string op;
        int num1, num2;
        op = tokens.back();
        tokens.pop_back();
        
        int idx = tokens.size(), expectNum = 1;
        vector<string>::iterator it = tokens.end();
        vector<string> aux;
        while(true){
            it--;
            idx--;
            if(isOperator(tokens[idx])){
                expectNum += 1;
            }else if(--expectNum == 0){
                aux.insert(aux.begin(), it, tokens.end());
                tokens.erase(it, tokens.end());
                num1 = evalRPN(tokens);
                num2 = evalRPN(aux);
                break;
            }
        }

        if(op == "+"){
            return num1 + num2;
        }else if(op == "-"){
            return num1 - num2;
        }else if(op == "*"){
            return num1 * num2;
        }else if(op == "/"){
            return num1 / num2;
        }else{
            cout << "wrong operator\n";
        }
    }
	int evalRPN(vector<string> &tokens) {
		stack<int> s;
		for (vector<string>::const_iterator it = tokens.begin(); it != tokens.end(); ++it) {
			//如果是数字
			if (isNumber(*it))
				s.push(stoi(*it)); //将数字压入栈中
			//如果是运算符
			else if (*it == "+" || *it == "-" || *it == "*" || *it == "/") {
				if (!s.empty()) {	//如果栈中有数字
					int opnd2 = s.top();	//获得表达式中的右边的操作数
					s.pop();	//弹出
					int opnd1 = s.top();	//获得左边的操作数
					s.pop();	//弹出
					s.push(calculate(opnd1, it->c_str(), opnd2));	//计算表达式,将结果再压入栈中
				}
			}
			//如果是表达式
			else {
				vector<string> anotherTokens;
				s.push(evalRPN(anotherTokens = convertToRPN(*it)));	//如果是表达式那么转化成逆波兰式,在调用evalRPN,所得结果压入栈中
			}
		}
		return s.top();	//返回最终结果
	}
	void test() {
		vector<string> tokens = { "-1", "1", "*", "-1", "+" };
		int result = evalRPN(tokens);
	}
 int calculate(string s) 
 {
     vector <string> sets = postfix(s);
     
     return evalRPN(sets);
 }