我的解答
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| #include <string> |
| #include <iostream> |
| int calculate(std::string str); |
| auto zero() {return '0';} |
| auto one() {return '1';} |
| auto two() {return '2';} |
| auto three() {return '3';} |
| auto four() {return '4';} |
| auto five() {return '5';} |
| auto six() {return '6';} |
| auto seven() {return '7';} |
| auto eight() {return '8';} |
| auto nine() {return '9';} |
| auto zero(std::string str){return calculate(zero()+str);} |
| auto one(std::string str){return calculate(one()+str);} |
| auto two(std::string str){return calculate(two()+str);} |
| auto three(std::string str){return calculate(three()+str);} |
| auto four(std::string str){return calculate(four()+str);} |
| auto five(std::string str){return calculate(five()+str);} |
| auto six(std::string str){return calculate(six()+str);} |
| auto seven(std::string str){return calculate(seven()+str);} |
| auto eight(std::string str){return calculate(eight()+str);} |
| auto nine(std::string str){return calculate(nine()+str);} |
| int calculate(std::string str){ |
| if(str.size()!=3){ |
| std::cout<<"error"<<std::endl; |
| return 0; |
| }else{ |
| switch(str[1]){ |
| case '+':return (str[0]-'0')+(str[2]-'0'); |
| case '-':return (str[0]-'0')-(str[2]-'0'); |
| case '*':return (str[0]-'0')*(str[2]-'0'); |
| case '/':return (str[2]=='0')?0:((str[0]-'0')/(str[2]-'0')); |
| } |
| } |
| } |
| std::string plus(char right) { |
| return std::string("+")+right; |
| } |
| std::string minus(char right) { |
| return std::string("-")+right; |
| } |
| std::string times(char right) { |
| return std::string("*")+right; |
| } |
| std::string divided_by(char right) { |
| return std::string("/")+right; |
| } |
| int main(){ |
| std::cout<<zero(plus(one()))<<std::endl; |
| std::cout<<seven(times(five()))<<std::endl; |
| std::cout<<four(plus(nine()))<<std::endl; |
| std::cout<<eight(divided_by(two()))<<std::endl; |
| std::cout<<six(minus(three()))<<std::endl; |
| return 0; |
| } |
学习别人的代码
| |
| |
| |
| |
| #include <functional> |
| #include <iostream> |
| using op = std::function<int(int)>; |
| |
| |
| int id(int n) { return n; } |
| |
| |
| |
| int zero (op func = id) { |
| return func(0); |
| |
| |
| } |
| int one (op func = id) { return func(1); } |
| int two (op func = id) { return func(2); } |
| int three(op func = id) { return func(3); } |
| int four (op func = id) { return func(4); } |
| int five (op func = id) { return func(5); } |
| int six (op func = id) { return func(6); } |
| int seven(op func = id) { return func(7); } |
| int eight(op func = id) { return func(8); } |
| int nine (op func = id) { return func(9); } |
| |
| |
| |
| |
| op plus (int n) { return [=](int m) { return m + n; }; } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| op minus (int n) { return [=](int m) { return m - n; }; } |
| |
| op times (int n) { return [=](int m) { return m * n; }; } |
| op divided_by(int n) { return [=](int m) { return m / n; }; } |
| int main(){ |
| std::cout<<zero(plus(one()))<<std::endl; |
| std::cout<<seven(times(five()))<<std::endl; |
| std::cout<<four(plus(nine()))<<std::endl; |
| std::cout<<eight(divided_by(two()))<<std::endl; |
| std::cout<<six(minus(three()))<<std::endl; |
| return 0; |
| } |
验证是否掌握的默写
| |
| |
| |
| #include <functional> |
| #include <iostream> |
| using op = std::function<int(int)>; |
| int id(int n){return n;} |
| |
| int zero(op func = id){return func(0);} |
| int one(op func = id){return func(1);} |
| int two(op func = id){return func(2);} |
| int three(op func = id){return func(3);} |
| int four(op func = id){return func(4);} |
| int five(op func = id){return func(5);} |
| int six(op func = id){return func(6);} |
| int seven(op func = id){return func(7);} |
| int eight(op func = id){return func(8);} |
| int nine(op func = id){return func(9);} |
| |
| op plus(int right){ |
| return [=](int left){return left+right;}; |
| } |
| op minus(int right){ |
| return [=](int left){return left-right;}; |
| } |
| op times(int right){ |
| return [=](int left){return left*right;}; |
| } |
| op divided_by(int right){ |
| return [=](int left){return right==0?0:left/right;}; |
| } |
| |
| int main(){ |
| std::cout<<zero(plus(one()))<<std::endl; |
| std::cout<<seven(times(five()))<<std::endl; |
| std::cout<<four(plus(nine()))<<std::endl; |
| std::cout<<eight(divided_by(two()))<<std::endl; |
| std::cout<<six(minus(three()))<<std::endl; |
| return 0; |
| } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现