Class Solution {
Public
int calculate (string s) {
Stack<int> num;
Stack<char> symbol;
for (int i=0;i<s.length (); i++) {
if (s[i]== ') continue;
else if (s[i]== ' (' | | | s[i]== ' + ' | | s[i]== '-') Symbol.push (S[i]);
else if (s[i]>= ' 0 ' &&s[i]<= ' 9 ') {
for (int j=i+1;j<s.length (); j + +) {
if (s[j]< ' 0 ' | | S[j]> ' 9 ') {
Num.push (Stoi (S.substr (i,j-i)));
I=j-1;
Break
}
}
}
else if (s[i]== ') ') {
Stack<int> tem_i;
Stack<char> Tem_c;
while (Symbol.top ()! = ' (') {
Tem_i.push (Num.top ());
Num.pop ();
Tem_c.push (Symbol.top ());
Symbol.pop ();
}
Symbol.pop ();
while (!tem_c.empty ()) {
Char c=tem_c.top ();
Tem_c.pop ();
if (c== ' + ') {
int A=tem_i.top ();
Tem_i.pop ();
int B=tem_i.top ();
Tem_i.pop ();
Tem_i.push (A+B);
}
if (c== '-') {
int A=tem_i.top ();
Tem_i.pop ();
int B=tem_i.top ();
Tem_i.pop ();
Tem_i.push (A-B);
}
}
Num.push (Tem_i.top ());
Tem_i.pop ();
}
}
Stack<int> tem_i;
Stack<char> Tem_c;
while (!symbol.empty ()) {
Tem_i.push (Num.top ());
Num.pop ();
Tem_c.push (Symbol.top ());
Symbol.pop ();
}
while (!tem_c.empty ()) {
Char c=tem_c.top ();
Tem_c.pop ();
if (c== ' + ') {
int A=tem_i.top ();
Tem_i.pop ();
int B=tem_i.top ();
Tem_i.pop ();
Tem_i.push (A+B);
}
if (c== '-') {
int A=tem_i.top ();
Tem_i.pop ();
int B=tem_i.top ();
Tem_i.pop ();
Tem_i.push (A-B);
}
}
return Tem_i.top ();
}
int Stoi (string s) {
int res=0;
for (int i=0;i<s.length (); i++) {
res=res*10+ (s[i]-' 0 ');
}
return res;
}
};
Leetcode () Basic Calculator don't know what's wrong.