<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>

#include <cctype>
#include <string>
using namespace std;

enum class OperatorType {
    ADD,
    SUBTRACT,
    MULTIPLY,
    DIVIDE,
    INVALID
};
OperatorType getOperator(char c) {
    switch (c) {
        case '+':
            return OperatorType::ADD;
        case '-':
            return OperatorType::SUBTRACT;
        case '*':
            return OperatorType::MULTIPLY;
        case '/':
            return OperatorType::DIVIDE;
        default:
            cout << "Invalid operator!" << endl;
            return OperatorType::INVALID;
    }
}

struct Node {
    int val;  // Used for type=NUMBER
    OperatorType op;  // Used for type=OPERATOR
    Node* next;
    Node(int x) : val(x), next(nullptr) {}
    Node(OperatorType x) : op(x), next(nullptr) {}
    Node(char x) : op(getOperator(x)), next(nullptr) {}  // For convenience
};

class Solution {
public:
    int calculate(string s) {
        string currInt = "";
        Node* head = nullptr;
        Node* curr = nullptr;
        for (char c : s) {
            if (isspace(c)) continue;
            if (isdigit(c)) {
                currInt += c;
                continue;
            }
            if (head == nullptr) {
                head = new Node(stoi(currInt));
                head->next = new Node(c);
                curr = head->next;
                currInt = "";
                continue;
            }
            curr->next = new Node(stoi(currInt));
            curr = curr->next;
            curr->next =new Node(c);
            curr = curr->next;
            currInt = "";
        }
        if (head == nullptr) return stoi(s);
        curr->next = new Node(stoi(currInt));
        curr = curr->next;

        // Multiply and divide first
        Node* p1 = head;
        Node* p2 = head->next;
        Node* p3 = head->next->next;
        while (p3 != nullptr) {
            if (p2->op == OperatorType::ADD || p2->op == OperatorType::SUBTRACT) {
                p1 = p3;
            } else if (p2->op == OperatorType::MULTIPLY) {
                p1->val = p1->val * p3->val;
                p1->next = p3->next;
                delete p2;
                delete p3;
            } else if (p2->op == OperatorType::DIVIDE) {
                p1->val = (int) p1->val / p3->val;
                p1->next = p3->next;
                delete p2;
                delete p3;
            } else {
                cout << "Invalid operation!" << endl;
                break;
            }
            if (p1->next == nullptr) break;
            p2 = p1->next;
            p3 = p1->next->next;
        }
        if (head->next == nullptr) return head->val;

        // Add and subtract
        p1 = head, p2 = head->next, p3 = head->next->next;
        while (p3 != nullptr) {
            if (p2->op == OperatorType::ADD) {
                p1->val = p1->val + p3->val;
                p1->next = p3->next;
                delete p2;
                delete p3;
            } else if (p2->op == OperatorType::SUBTRACT) {
                p1->val = p1->val - p3->val;
                p1->next = p3->next;
                delete p2;
                delete p3;
            } else {
                cout << "Invalid operation." << endl;
                break;
            }
            if (p1->next == nullptr) break;
            p2 = p1->next;
            p3 = p1->next->next;
        }
        return head->val;
    }
};
30680