Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- std::string ex = std::string("3+4");
- int pos = ex.find_first_of("+-*/"); // 1
- while (pos != std::string::npos) {
- std::string prenxn = ex.substr(pos + 1); // 4
- int pnf = prenxn.find_first_of("+-*/"); // std::string::npos
- if (pnf != std::string::npos)
- prenxn = prenxn.substr(0, pnf);
- int nxn = std::stoi(prenxn);
- switch (ex[pos]) {
- case '+':
- res += nxn;
- case '-':
- res -= nxn;
- case '*':
- res *= nxn;
- case '/':
- res /= nxn;
- default:
- char* expos = &ex[pos];
- throw std::runtime_error(expos);
- }
- pos = ex.find_first_of("+-*/", pos + 1);
- }
Advertisement
Add Comment
Please, Sign In to add comment