fork(1) download
  1. #include <iostream>
  2. #include <map>
  3. #include <functional>
  4.  
  5. int
  6. main(int argc, char** argv)
  7. {
  8. std::map<char, std::function<double(double, double)>> ch2f{
  9. {'+', [] (double lhs, double rhs) -> auto { return lhs + rhs; }},
  10. {'*', [] (double lhs, double rhs) -> auto { return lhs * rhs; }},
  11. {'/', [] (double lhs, double rhs) -> auto { return lhs / rhs; }},
  12. {'-', [] (double lhs, double rhs) -> auto { return lhs - rhs; }}
  13. };
  14. double lhs, rhs;
  15. char op;
  16. std::cin >> lhs >> op >> rhs;
  17. std::cout << ch2f[ op ](lhs, rhs) << std::endl;
  18.  
  19. std::cin >> lhs >> op >> rhs;
  20. std::cout << ch2f[ op ](lhs, rhs) << std::endl;
  21. }
Success #stdin #stdout 0s 3420KB
stdin
3+3
9/3
stdout
6
3