赞
踩
/************************************************************************* > File Name: ex14.44.cpp > Author: > Mail: > Created Time: Tue 09 Jul 2024 10:37:09 AM CST ************************************************************************/ #include<iostream> #include<vector> #include<functional> #include<algorithm> #include<map> using namespace std; int add(int a, int b){ return a + b; } auto mod = [](int a, int b){ return a % b; }; struct divide{ int operator()(int a, int b){ return a / b; } }; int main(){ map<string, function<int(int, int)>> binops = { {"+", add}, {"-", std::minus<int>()}, {"/", divide()}, {"*", [](int a, int b){ return a * b; }}, {"%", mod} }; int a, b; cout<<"Enter a and b: "; cin>>a>>b; cout<<"a + b = "<<binops["+"](a, b)<<endl; cout<<"a - b = "<<binops["-"](a, b)<<endl; cout<<"a * b = "<<binops["*"](a, b)<<endl; cout<<"a / b = "<<binops["/"](a, b)<<endl; cout<<"a % b = "<<binops["%"](a, b)<<endl; return 0; }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。