fork download
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <sstream>
  4.  
  5. using namespace std;
  6.  
  7. int main() {
  8. string all("+12+400-500+2:+13-50-510+20-66+20:");
  9. int pos = 0;
  10. for (;;) {
  11. int next = all.find(':', pos);
  12. if (next == string::npos) break;
  13. string expr(all.substr(pos, next+1));
  14. istringstream iss(expr);
  15. int sum = 0;
  16. while (iss) {
  17. int n;
  18. iss >> n;
  19. sum += n;
  20. }
  21. cout << expr.substr(0, expr.size()-1) << " = " << sum << endl;
  22. pos = next+1;
  23. }
  24. return 0;
  25. }
Success #stdin #stdout 0s 2988KB
stdin
Standard input is empty
stdout
+12+400-500+2 = -86
+13-50-510+20-66+20 = -573