<p>Given a string <code>s</code> which represents an expression, <em>evaluate this expression and return its value</em>.&nbsp;</p>

<p>The integer division should truncate toward zero.</p>

<p>You may assume that the given expression is always valid. All intermediate results will be in the range of <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>.</p>

<p><strong>Note:</strong> You are not allowed to use any built-in function which evaluates strings as mathematical expressions, such as <code>eval()</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "3+2*2"
<strong>Output:</strong> 7
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = " 3/2 "
<strong>Output:</strong> 1
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> s = " 3+5 / 2 "
<strong>Output:</strong> 5
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= s.length &lt;= 3 * 10<sup>5</sup></code></li>
	<li><code>s</code> consists of integers and operators <code>(&#39;+&#39;, &#39;-&#39;, &#39;*&#39;, &#39;/&#39;)</code> separated by some number of spaces.</li>
	<li><code>s</code> represents <strong>a valid expression</strong>.</li>
	<li>All the integers in the expression are non-negative integers in the range <code>[0, 2<sup>31</sup> - 1]</code>.</li>
	<li>The answer is <strong>guaranteed</strong> to fit in a <strong>32-bit integer</strong>.</li>
</ul>

class Solution {
private:
    class stack_entry {
    public:
        int val;
        enum op {
            NONE,
            ADD,
            SUB,
            MUL,
            DIV
        } op;

        stack_entry() = default;

        stack_entry(int v, enum op operation) {
            val = v;
            op = operation;
        }
    };

    int _calculate(int opnd1, enum stack_entry::op op, int opnd2) {
        int res;

        switch (op) {
        case stack_entry::ADD:
            res = opnd1 + opnd2;
            break;
        case stack_entry::SUB:
            res = opnd1 - opnd2;
            break;
        case stack_entry::MUL:
            res = opnd1 * opnd2;
            break;
        case stack_entry::DIV:
            res = opnd1 / opnd2;
            break;
        default:
            throw exception();
        }

        return res;
    }

public:
    int calculate(string s) {
        int i, j, n;
        int idx;
        int res;
        stack_entry *cur, *next;
        stack_entry *op;
        stack_entry *opnd1, *opnd2;
        vector<stack_entry> stk;

        static const enum stack_entry::op tbl[] = {
            [(unsigned char)'+'] = stack_entry::ADD,
            [(unsigned char)'-'] = stack_entry::SUB,
            [(unsigned char)'*'] = stack_entry::MUL,
            [(unsigned char)'/'] = stack_entry::DIV
        };

        n = s.length();

        stk.resize(n * 2);

        op = opnd1 = nullptr;
        i = 0;
        idx = 0;
        for (;;) {
            if (i == n)
                break;
            if (s[i] == ' ') {
                ++i;
                continue;
            }
            if (isdigit(s[i])) {
                int val;

                for (j = i + 1; j < n && isdigit(s[j]); j++)
                    ;
                val = stoi(string(begin(s) + i, begin(s) + j), nullptr, 10);
                stk[idx] = stack_entry(val, stack_entry::NONE);
                cur = &stk[idx++];
                if (op == nullptr || (op->op != stack_entry::MUL && op->op != stack_entry::DIV))
                    opnd1 = cur;
                else {
                    opnd2 = cur;
                    idx -= 3;
                    stk[idx] = stack_entry(_calculate(opnd1->val, op->op, opnd2->val), stack_entry::NONE);
                    opnd1 = &stk[idx++];
                }
                i = j;
            } else {
                stk[idx] = stack_entry(-1, tbl[(unsigned char)s[i]]);
                op = &stk[idx++];
                ++i;
            }
        }

        op = nullptr;
        res = stk[0].val;
        for (i = 1; i < idx; i++) {
            cur = &stk[i];
            if (cur->op != stack_entry::NONE)
                op = cur;
            else {
                opnd2 = cur;
                res = _calculate(res, op->op, opnd2->val);
            }
        }

        return res;
    }
};
21578