fork(1) download
  1. #include <map>
  2. #include <iostream>
  3.  
  4. using Operator = double(double, double);
  5.  
  6. double plus(double x, double y) {
  7. return x+y;
  8. }
  9.  
  10. double minus(double x, double y) {
  11. return x-y;
  12. }
  13.  
  14. double divide(double x, double y) {
  15. return x/y;
  16. }
  17.  
  18. double multiply(double x, double y) {
  19. return x*y;
  20. }
  21.  
  22. int main()
  23. {
  24. std::map<char, Operator&> operators = {
  25. {'+', plus},
  26. {'-', minus},
  27. {'/', divide},
  28. {'*', multiply}
  29. };
  30. double x = 0;
  31. double y = 0;
  32. char op = 0;
  33. double result = 0;
  34. std::cout << "Input number, operation (+, -, /, *) and number, please: ";
  35. std::cin >> x >> op >> y;
  36. const auto operation = operators.find(op);
  37. if (operation != operators.cend())
  38. std::cout << "Result: " << (*operation->second)(x, y) << '\n';
  39. else
  40. std::cout << "Operation " << op << " not found!\n";
  41. return 0;
  42. }
Success #stdin #stdout 0s 3464KB
stdin
123 / 3
stdout
Input number, operation (+, -, /, *) and number, please: Result: 41