value_stack.push(0); // for some occasions that starts with a negative digit
std::size_t index = 0; while (index < s.size()) { // process digit with high priority if ('0' <= s[index] && s[index] <= '9') { int num = 0; while (index < s.size() && ('0' <= s[index] && s[index] <= '9')) { num = num * 10 + (s[index] - '0'); ++index; }
value_stack.push(value1 / value2); // Note that it is NOT a strict division! break; } default: { // impossible in fact break; } }
// Note: // `sign_stack.pop()` is called outside `handle()` so that the iteration of loop looks more understandable. }
voidclear(){ // clear stack with trick since stack does't provide `clear()` std::stack<int>().swap(value_stack); std::stack<char>().swap(sign_stack); }
// invalid test cases // ASSERT("1/0", -1); // terminate as expected // // Only support division with result as an integer, // hence multiplication and division are not actually perfectly reciprocal. // ASSERT("1/2*2", 1);