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

#include <iostream>
#include <string>
#include <stack>
using namespace std;
class Solution {
public:
    int calculate(string s) {
        // All The Calculations are Pushed And Done On A Stack
        stack<string> stack;
        bool preparing_for_calculation = false;
        bool is_operator_ready = false;
        bool negative_val = false;

        // Example String: s = "( 1+(10+10+2)-3)+(6+8)"
        for (int i = 0; i < s.size(); i++) {
            // Special Case Only For The "-" Operator. For Reliability Just Check If The Previous In Stack Is A Number.
            if (stack.empty() && s[i] == '-') {
                negative_val = true;
                stack.push(string(1, s[i]));
                continue;
            }
            // Ensures Multiple Integer Digit Get Saved In The Same Stack Container
            if ((i != 0 && isdigit(s[i]) && isdigit(s[i - 1]))|| negative_val ==true && isdigit(s[i])) {
                // Update The Last Stack Value To Add The Newest Character
                string temp_string = stack.top();
                stack.pop();
                temp_string += s[i];
                stack.push(temp_string);
                negative_val = false;
                continue;
            }
            // Skip Over All Spaces Manually To Save Computation Time
            if (s[i] == ' ') {
                continue;
            }
            // Adds Up While Traveling The Stack
            if ((s[i] == '+' || s[i] == '-') && preparing_for_calculation == true && is_operator_ready == true) {
                int temp_val1 = stoi(stack.top());// Retrieving the Second Value
                stack.pop();
                string Operator = string(stack.top());// Retrieving Operator.
                stack.pop();
                int temp_val2 = stoi(stack.top()); // Retrieving First Value
                stack.pop();
                string result = to_string(doing_math(temp_val2, temp_val1, Operator));
                stack.push(result); // Pushing In The Result 
                stack.push(string(1, s[i]));// Pushing In The Operator.
                continue;
            }

            if (isdigit(s[i])) {
                stack.push(string(1,s[i]));
                preparing_for_calculation = true;
                continue;
            }
            // The Values Inside () can either be one value or (num+num).
            if (s[i] == ')') {
                string result = "";
                int first_value = stoi(stack.top());
                int second_value = 0;
                stack.pop();
                //Start Popping Stacks Until '(' is reached
                while (stack.top() != "(") {
                    if (result != "") {
                        first_value = stoi(result);
                    }
                    string Operator = stack.top();
                    stack.pop();
                    if (stack.top() == "(") {
                        result = to_string(doing_math(second_value, first_value, Operator));
                        continue;
                    }
                    int second_value = stoi(stack.top());
                    stack.pop();
                    result = to_string(doing_math(second_value, first_value, Operator));
                    continue;
                }
                if (result == "") {
                    result = to_string(first_value);
                }

                stack.pop();// For The "(" Bracket.
                // Checking If Operator Outside Is Negative.
                if (!stack.empty() && stack.top() == "-") {
                    if (result[0] == '-') {
                        result = result.substr(1);
                    }
                    else {
                        result = "-" + result;
                    }
                    stack.pop();// Removing The Negative Value
                    if (stack.size() > 0) {
                        stack.push("+");
                    }
                    stack.push(result);
                }
                else {
                    stack.push(result);// Pushes The Actual Value In
                }
                is_operator_ready = false;
                continue;
            }
            if (s[i] == '(') {
                preparing_for_calculation = false; // Reset The Flag
                is_operator_ready = false;
                negative_val = false;
                stack.push(string(1, s[i]));
                continue;
           }
            else {
                stack.push(string(1,s[i])); // Adds Up The Operators "+" , "-".
                is_operator_ready = true;
            }

        }
        // Add Up Any Remaining Values In The Stack
        int result = stoi(stack.top());
        stack.pop();
        while (!stack.empty()) {
            string Operator = stack.top();
            stack.pop();
            int second_val = 0;
            if (!stack.empty()) {
                second_val = stoi(stack.top());
                stack.pop();
            }
            result = doing_math(second_val, result, Operator);
            
        }

        return result;
    }
    // Does The Actual Math Part When Called By calculate Function
    int doing_math(int num1, int num2, string Operator) {
        if (Operator == "+") {
             return num1 + num2;
        }
        if (Operator == "-") {
            return num1 - num2;
        }
        cout << "False Operators Found";
        return 0;
    }
};
27181