Implement a basic calculator to evaluate a simple expression string.
The expression string contains only non-negative integers, '+', '-', '*', '/' operators, and open '(' and closing parentheses ')'. The integer division should truncate toward zero.
You may assume that the given expression is always valid. All intermediate results will be in the range of [-231, 231 - 1].
Note: You are not allowed to use any built-in function which evaluates strings as mathematical expressions, such as eval().
Example 1:
Input: s = "1+1"
Output: 2
Example 2:
Input: s = "6-4/2"
Output: 4
Example 3:
Input: s = "2*(5+5*2)/3+(6/2+8)"
Output: 21
Constraints:
1 <= s <= 104
s consists of digits, '+', '-', '*', '/', '(', and ')'.
classSolution{public:// Define an operation function that performs mathematical operations based on the operatorintoperate(intb,charch,inta){// Note the order of abswitch(ch){case'+':returna+b;// Additioncase'-':returna-b;// Subtractioncase'*':returna*b;// Multiplicationcase'/':returna/b;// Divisiondefault:break;}return0;// Default return 0, handle invalid operators}// Calculate the value of the string expressionintcalculate(strings){intpreority[250];// Operator precedence arraypreority['+']=1;preority['-']=1;preority['*']=2;preority['/']=2;preority['(']=0;preority[')']=0;stack<char>op;// Operator stackstack<int>num;// Operand stackintstringsize=s.size();// Length of the stringinti=0;charch;// Traverse the stringfor(;i<stringsize;i++){ch=s[i];if(ch==' '){continue;// Skip spaces}if(ch>='0'&&ch<='9'){intrealnum=ch-'0';// Convert character to number// Handle multi-digit numberswhile(s[i+1]>='0'&&s[i+1]<='9'){i++;realnum*=10;realnum+=s[i]-'0';}num.push(realnum);// Push the number onto the stack}else{// Handle operatorsif(op.empty()||ch=='('||preority[ch]>preority[op.top()]){// Special case, handle the first character being '-' or '+'if(num.empty()&&(ch=='-'||ch=='+')){num.push(0);}op.push(ch);// Push the operator onto the stack// Handle expressions inside parenthesesif(ch=='('){intj=i;while(j+1<stringsize){// Preprocess the first operator inside the parenthesesif(s[j+1]=='-'||s[j+1]=='+'){num.push(0);}if(s[j+1]!=' '){break;}j++;}}}elseif(ch==')'){// Handle right parenthesescharch2=')';ch2=op.top();op.pop();while(ch2!='('){inta=num.top();num.pop();intb=num.top();num.pop();num.push(operate(a,ch2,b));// Calculate and push the resultch2=op.top();op.pop();}}elseif(preority[ch]<=preority[op.top()]){// Handle cases where the precedence is less than or equal to the top of the stackcharch2;ch2=op.top();while(!op.empty()&&preority[ch]<=preority[op.top()]&&ch2!='('){op.pop();inta=num.top();num.pop();intb=num.top();num.pop();num.push(operate(a,ch2,b));// Calculate and push the resultif(!op.empty()){ch2=op.top();}else{break;}}op.push(ch);// Push the current operator onto the stack}}}// Handle the remaining expressions in the stackwhile(!op.empty()){ch=op.top();op.pop();inta=num.top();num.pop();intb=num.top();num.pop();num.push(operate(a,ch,b));// Calculate and push the result}returnnum.top();// Return the final result}};