C++,codewars,Calculating with Functions

我的解答

/*
codewars.com/kata/525f3eda17c7cd9f9e000b39
codewars,Calculating with Functions
要求使用函数来编写计算,例如:
seven(times(five())); // must return 35
four(plus(nine())); // must return 13
eight(minus(three())); // must return 5
six(divided_by(two())); // must return 3
要求定义函数zero()~nine()和plus(),minus(),times(),divided_by()来实现上述功能
除法是整数除法, 返回值向下取整
*/
// You have complete freedom over the function signatures, as long as all the needed functions get defined.
// Do not `using namespace std`, because your `plus` function might clash with `std::plus` :)
/*
先用题目要求的函数生成算式字符串,
最后由一个统一的函数进行计算
*/
#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;
}

学习别人的代码

/*
别人的代码,o2001
学习不懂得地方,加注释笔记
*/
#include <functional>
#include <iostream>
using op = std::function<int(int)>;
//using 可以为类型定义别名,与typedef类似
//例如 using intVec = std::vector<int>; intVec vec1(10);
int id(int n) { return n; }
//默认参数是返回传入值的函数, 用于zero()~nine()无参的情况,返回相应的值0~9
//可以传入的参数是op类型的函数,即返回int值,传入一个int值的函数
int zero (op func = id) {
return func(0);
//若func为id,则0为right, func(0)返回0
//若func为plus(n)返回的匿名函数,则0为left, func(0)返回left+n
}
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); }
//plus()等返回op(std::function<int(int)>)类型的变量,传入一个int值
//返回值为传入一个int值,返回一个int值的lambda函数,是op类型的函数
//返回值可以作为参数传入one()~nine()等函数
op plus (int n) { return [=](int m) { return m + n; }; }
/*
匿名函数的格式
[captures](parameters) -> return_type{
//function body
}
captures: 捕获列表, 用于指定lambda函数可以访问的外部变量
[]:不捕获任何外部变量
[=]:以值方式(复制)捕获所有外部变量
[&]:以引用方式捕获所有外部变量
parameters: 参数列表
return_type: 返回值类型
function body: 函数体
这里[=]表示捕获所有外部变量, 这里外部函数体只有一个return语句,
但是传入参数n是一个外部变量, 因此lambda函数中可以使用变量n,
lambda内部的变量n是一个副本, 不会影响外部变量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;
}
posted @   Kazuma_124  阅读(2)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示