Advertisement
alfps

Untitled

Mar 1st, 2021
538
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.00 KB | None | 0 0
  1. #include <kickstart/all.hpp>        // https://github.com/alf-p-steinbach/kickstart/
  2. using namespace kickstart::all;     // input, to_vector_, split_on_whitespace, out, endl
  3.  
  4. #include <unordered_map>
  5. using std::unordered_map;
  6.  
  7. void cppmain()
  8. {
  9.     using Op = function<auto( double, double ) -> double>;
  10.     const unordered_map<string_view, Op> ops =
  11.     {
  12.         { "+", std::plus() }, { "-", std::minus() },
  13.         { "*", std::multiplies() }, { "/", std::divides() }
  14.     };
  15.  
  16.     const string    op_symbol       = input( "Enter an operator: " );
  17.     const Op        op              = ops.at( op_symbol );
  18.  
  19.     const string    number_specs    = input( "Enter two numbers: " );
  20.     const auto      numbers         = to_vector_<double>( split_on_whitespace( number_specs ) );
  21.  
  22.     const auto [a, b] = array{numbers.at( 0 ), numbers.at( 1 )};
  23.     out << a << " " << op_symbol << " " << b << " = " << op( a, b ) << "." << endl;
  24. }
  25.  
  26. auto main() -> int { return with_exceptions_displayed( cppmain ); }
  27.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement