Guest User

Untitled

a guest
Aug 29th, 2022
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.70 KB | None | 0 0
  1. std::string ex = std::string("3+4");
  2. int pos = ex.find_first_of("+-*/"); // 1
  3.  
  4. while (pos != std::string::npos) {
  5.     std::string prenxn = ex.substr(pos + 1); // 4
  6.  
  7.     int pnf = prenxn.find_first_of("+-*/"); // std::string::npos
  8.  
  9.     if (pnf != std::string::npos)
  10.         prenxn = prenxn.substr(0, pnf);
  11.  
  12.     int nxn = std::stoi(prenxn);
  13.  
  14.     switch (ex[pos]) {
  15.         case '+':
  16.             res += nxn;
  17.  
  18.         case '-':
  19.             res -= nxn;
  20.  
  21.         case '*':
  22.             res *= nxn;
  23.  
  24.         case '/':
  25.             res /= nxn;
  26.  
  27.         default:
  28.             char* expos = &ex[pos];
  29.             throw std::runtime_error(expos);
  30.     }
  31.  
  32.     pos = ex.find_first_of("+-*/", pos + 1);
  33. }
Advertisement
Add Comment
Please, Sign In to add comment