fork download
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. bool isNumber(const std::string& str) {
  6. for (char c : str) {
  7. if (c - '0' > 9 || c - '0' < 0) return false;
  8. }
  9. return true;
  10. }
  11.  
  12. int getMatchingBrace(const std::string& str) {
  13. int i = 1;
  14. int braces = 1;
  15. while (braces && i < str.size()) {
  16. if (str[i] == '(') braces++;
  17. if (str[i] == ')') braces--;
  18. i++;
  19. }
  20. return i-1;
  21. }
  22.  
  23. int eval_op(char op, int first, int second) {
  24. if (op == '+') return first + second;
  25. return first * second;
  26. }
  27.  
  28. int eval(const std::string& str) {
  29. if (isNumber(str)) return std::atoi(str.c_str());
  30. std::vector<std::string> expressions;
  31. int cur_expr_start = 4;
  32. for (int pos = 4; pos < getMatchingBrace(str); pos++) {
  33. if (str[pos] == ' ') {
  34. expressions.push_back(str.substr(cur_expr_start, pos));
  35. cur_expr_start = pos;
  36. }
  37. }
  38. std::cout << expressions[0] << std::endl;
  39. int result = 0;//eval(expressions[0]);
  40. std::cout << str[2] << std::endl;
  41. for (int i = 1; i < expressions.size(); i++) {
  42. std::cout << expressions[i] << std::endl;
  43. //result = eval_op(str[2], result, eval(expressions[i]));
  44. }
  45. return result;
  46. }
  47.  
  48. int main() {
  49. eval("( + 1 1 )");
  50. //std::cout << eval("( + 1 1 )") << std::endl;
  51. return 0;
  52. }
Success #stdin #stdout 0s 4508KB
stdin
Standard input is empty
stdout
1 1 )
+
 1 )