Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.40 KB | None | 0 0
  1. class Solution {
  2.     int evaluate(std::string& s) {
  3.         int ans=0;
  4.         bool plus=true;
  5.         int ii=0;
  6.         std::string::size_type sz;
  7.         while(ii<s.length()) {
  8.             if (s[ii]=='(' || s[ii]==')') {
  9.                 ++ii;
  10.                 continue;
  11.             }
  12.             if (s[ii]==' '){
  13.                 ++ii;
  14.                 continue;
  15.             }
  16.             if (s[ii]=='+'){
  17.                 if (s[ii+1]=='-') {
  18.                     plus=false;
  19.                     ii+=2;
  20.                 }
  21.                 else {
  22.                 plus=true;
  23.                 ++ii;
  24.                 }
  25.                 continue;
  26.             }
  27.             else if (s[ii]=='-') {
  28.                 if (s[ii+1]=='-') {
  29.                     plus=true;
  30.                     ii+=2;
  31.                 }
  32.                 else{
  33.                 plus=false;
  34.                 ++ii;
  35.                 }
  36.                 continue;
  37.             }
  38.             else {
  39.                 // we have digits here.. get the number..
  40.                 std::string num="";
  41.                 //std::cout<<int(s[ii])<<std::endl;
  42.                 while(s[ii]>=48 && s[ii]<=57) {
  43.                 //std::cout<<s[ii]<<std::endl;
  44.                     num+=s[ii];
  45.                     ++ii;
  46.                 }
  47.                 //digit constructed..
  48.                 int digi = std::stoi (num,&sz);
  49.                 if (plus)
  50.                     ans += digi;
  51.                 else
  52.                     ans -= digi;
  53.             }
  54.         }
  55.         return ans;        
  56.     }
  57. public:
  58.     int calculate(string s) {
  59.         if (s.empty())
  60.             return 0;
  61.         std::stack<string> stk;
  62.         for (int ii=0; ii<s.length(); ++ii) {
  63.             if (s[ii]==')'){
  64.                 // Pop till '(' and create string and send and then push..
  65.                 std::string exp = "";
  66.                 while (!stk.empty() && stk.top()!="("){
  67.                     exp = stk.top()+exp;  
  68.                     stk.pop();
  69.                 }
  70.                 stk.pop();
  71.                 stk.push(std::to_string(evaluate(exp)));
  72.             }
  73.             else {
  74.                 stk.push(std::string(1,s[ii]));
  75.             }
  76.         }
  77.  
  78.         std::string exp = "";
  79.         while (!stk.empty() && stk.top()!="("){
  80.             exp = stk.top()+exp;  
  81.             stk.pop();
  82.         }
  83.        
  84.         return evaluate(exp);
  85.        
  86.        
  87.     }
  88. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement