<p>Given a string <code>s</code> representing a valid expression, implement a basic calculator to evaluate it, and return <em>the result of the evaluation</em>.</p>

<p><strong>Note:</strong> You are <strong>not</strong> 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 = &quot;1 + 1&quot;
<strong>Output:</strong> 2
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> s = &quot; 2-1 + 2 &quot;
<strong>Output:</strong> 3
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;(1+(4+5+2)-3)+(6+8)&quot;
<strong>Output:</strong> 23
</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 digits, <code>&#39;+&#39;</code>, <code>&#39;-&#39;</code>, <code>&#39;(&#39;</code>, <code>&#39;)&#39;</code>, and <code>&#39; &#39;</code>.</li>
	<li><code>s</code> represents a valid expression.</li>
	<li><code>&#39;+&#39;</code> is <strong>not</strong> used as a unary operation (i.e., <code>&quot;+1&quot;</code> and <code>&quot;+(2 + 3)&quot;</code> is invalid).</li>
	<li><code>&#39;-&#39;</code> could be used as a unary operation (i.e., <code>&quot;-1&quot;</code> and <code>&quot;-(2 + 3)&quot;</code> is valid).</li>
	<li>There will be no two consecutive operators in the input.</li>
	<li>Every number and running calculation will fit in a signed 32-bit integer.</li>
</ul>

class Solution {
public:

    enum class CalcType
    {
        Number,
        Plus,
        Minus,
        OpenBracket,
        CloseBracket
    };

    struct CalcOp
    {
        CalcType type;
        int value;
    };

    vector<CalcOp> opList;

    void printOps()
    {
        for (int i = 0; i < opList.size(); ++i)
        {
            CalcOp& op = opList[i];
            if (op.type == CalcType::Plus) printf(" Plus\n");
            if (op.type == CalcType::Minus) printf(" Minus\n");
            if (op.type == CalcType::Number) printf(" Number: %d\n", op.value);
            if (op.type == CalcType::OpenBracket) printf(" Open Bracket\n");
            if (op.type == CalcType::CloseBracket) printf(" Close Bracket\n");
        }
    }

    int executeOps(vector<CalcOp> ops)
    {
        bool addState = false;
        bool subState = false;
        int tmpNum = 0;

        for (int i = 0; i < ops.size(); ++i)
        {
            CalcOp& op = ops[i];

            if (op.type == CalcType::Plus)
            {
                //printf("Add encounted.\n");
                addState = true;
                subState = false;
            }

            if (op.type == CalcType::Minus)
            {
                //printf("Minus encounted.\n");
                addState = false;
                subState = true;
            }

            if (op.type == CalcType::Number)
            {
                //printf("Number encounted %d\n", op.value);

                if (addState)
                {
                    //printf("Adding %d + %d\n", tmpNum, op.value);
                    tmpNum += op.value;
                    addState = false;
                }
                else if (subState)
                {
                    //printf("Subtracting %d - %d\n", tmpNum, op.value);
                    tmpNum -= op.value;
                    subState = false;
                    
                }
                else 
                {
                    tmpNum = op.value;
                    //printf("Setting tmp: %d\n", tmpNum);
                }
            }
        }

        return tmpNum;
    }

    // Walks backward in the list and processes everything up until
    // an opening bracket.
    void processBracketClose()
    {
        vector<CalcOp> bracketOps;

        int opIndex = opList.size() - 1;
        for (; opIndex >= 0; opIndex--)
        {
            CalcOp& op = opList[opIndex];

            if (op.type == CalcType::OpenBracket)
            {
                break;
            } 
            else 
            {
                bracketOps.insert(bracketOps.begin(), op);
            }
        }

        //printf("Ops before erase:\n");
        //printOps();
        opList.erase(opList.begin() + opIndex, opList.end());
        int result = executeOps(bracketOps);
        //printf("Ops after erase:\n");
        //printOps();

        CalcOp newOp;
        newOp.type = CalcType::Number;
        newOp.value = result;
        opList.push_back(newOp);

        //printf("Finished bracket pushing result %d\n", result);
    }

    int calculate(string s) 
    {
        string pendingNumber = "";
        bool collectingNumber = false;

        for (int i = 0; i < s.length(); ++i)
        {
            bool nonNumber = false;
            bool closingBracket = false;
            bool addOp = false;
            CalcOp opToAdd;

            char c = s[i];
            if (c == ' ')
            {
                nonNumber = true;
            }
            else if (c == '+')
            {
                opToAdd.type = CalcType::Plus;
                opToAdd.value = 0;
                addOp = true;
                nonNumber = true;
            }
            else if (c == '-')
            {
                opToAdd.type = CalcType::Minus;
                opToAdd.value = 0;
                addOp = true;
                nonNumber = true;
            }
            else if (c == '(')
            {
                opToAdd.type = CalcType::OpenBracket;
                opToAdd.value = 0;
                addOp = true;
                nonNumber = true;
            }
            else if (c == ')')
            {
                opToAdd.type = CalcType::CloseBracket;
                opToAdd.value = 0;
                addOp = true;
                nonNumber = true;
                closingBracket = true;
            }
            else 
            {
                nonNumber = false;
                collectingNumber = true;
                pendingNumber += c;
            }

            if (collectingNumber && nonNumber)
            {
                CalcOp newOp;
                newOp.type = CalcType::Number;
                newOp.value = std::stoi(pendingNumber);
                opList.push_back(newOp);
                //printf("Collected number: %d\n", newOp.value);
                collectingNumber = false;
                pendingNumber = "";
            }

            if (addOp)
            {
                opList.push_back(opToAdd);
            }

            if (closingBracket)
            {
                processBracketClose();
            }
        }   

        if (pendingNumber != "")
        {
            CalcOp newOp;
            newOp.type = CalcType::Number;
            newOp.value = std::stoi(pendingNumber);
            opList.push_back(newOp);
        }

        return executeOps(opList);
    }
};
25306