`C++`

C++

#include <iostream> #include <map> #include <cmath> #include <functional> using namespace std; using binary_map = map<int, pair < string, function<double (double, double)> > >; using unary_map = map<int, pair < string, function<double (double)> > >; void show_menu( const binary_map & bm, const unary_map &um); double apply( function<double (double, double)> bin_fun); double apply( function<double (double)> una_fun); int main() { binary_map bm = { { 1, { "division", [](double a, double b){ return a/b; } } }, { 2, {"multiplication", [](double a, double b){ return a*b; } } }, //... }; unary_map um = { { 6, { "square root ", [](double x ){return sqrt(x);} } }, { 7, {"sin", [](double x) { return sin(x);} } }, //... }; for (;;) { show_menu(bm, um); int choice; cin >> choice; bool found = false; if ( ! found ) { auto it = bm.find(choice); if ( it != bm.end()) { auto result = apply(it->second.second); cout << "result is " << result << "\n"; found = true; } } if ( ! found ) { auto it = um.find(choice); if ( it != um.end() ) { auto result = apply(it->second.second); cout << "result is " << result << "\n"; found = true; } } if ( ! found ) break; } } void show_menu( const binary_map & bm, const unary_map &um) { cout << "\nchoose your poison\n"; for ( const auto & [key, value] : bm ) cout << key << " - " << value.first << endl; for ( const auto & [key, value] : um ) cout << key << " - " << value.first << endl; } double apply( function<double (double, double)> bin_fun) { double x,y; cout << "please enter the two operands\n"; cin >> x >> y; return bin_fun(x,y); } double apply( function<double (double)> una_fun) { double x; cout << "please enter the operand\n"; cin >> x ; return una_fun(x); }