当前位置: 首页 > news >正文

c++学习:Lambda练习和数组练习

练习一  进行加减乘除

普通方法

#include <iostream>
using namespace std;
int add(int a,int b)
{return a+b;
}
int min(int a,int b)
{return a-b;
}
int mul(int a,int b)
{return a*b;
}
float divRet(int a,int b)
{return (float)a/b;
}
int main() {int a;int b;char calWay;while(1){cout << "请输入两个数:"<< endl;cin >> a;cin >> b;cout<<"请输入运算符号:+ - * /" <<endl;cin >> calWay;switch(calWay){case '+':printf("两数之和是%d\n",add(a,b));break;case '-':printf("两数之差是%d\n",min(a,b));break;case '*':printf("两数之积是%d\n",mul(a,b));break;case '/':printf("两数之余是%f\n",divRet(a,b));break;default:printf("运算符输入错误,请重新输入\n");}}
}

Lambda方法

#include <iostream>
using namespace std;
int calculator(int a, int b, int (*p)(int a, int b))
{cout << "开始计算" << endl;
}
int main()
{int a = 0;int b = 0;char cal;while(1){cout << "请输入两个数:" << endl;cin >> a;cin >> b;cout << "请输入运算符+,-,*,/" << endl;cin >> cal;// auto minu = [a,b]()->int{ return a - b;};switch(cal){case '+':cout << calculator(a,b,[](int a, int b){return a + b;}) << endl;break;case '-':cout << calculator(a,b,[](int a, int b){return a - b;}) << endl;break;case '*':cout << calculator(a,b,[](int a, int b){return a * b;}) << endl;break;case '/':cout << calculator(a,b,[](int a, int b){return a / b;}) << endl;break;}}return 0;
}

练习二 求数组中最大值

#include <iostream>
using namespace std;
void initArry(int *arry, int len)
{for(int i=0; i< len; i++){cout << "请输入第" << i+1 << "个数" << endl;cin >> arry[i];}
}
void printArry(int *arry,int len)
{for(int i=0; i< len; i++){cout << arry[i] << endl;}
}
int getMaxFromArray(int *arry, int len )
{int maxTmp = arry[0];for(int i=0; i< len; i++){if(maxTmp < arry[i])maxTmp = arry[i];}return maxTmp;
}
int main() {int arry[5];int len = sizeof(arry)/sizeof(arry[0]);initArry(arry,len);printArry(arry,len);cout << "最大数是:" << getMaxFromArray(arry,len);
}

http://www.lryc.cn/news/307659.html

相关文章:

  • 数据仓库和数据湖的区别
  • tkinterFrame框架+标签框架LabelFrame+Toplevel窗口的使用
  • C 语言中的 char 关键字详解
  • 信息安全管理与评估赛题解析-应急响应(含环境)
  • 微服务-微服务Spring Security OAuth 2实战
  • 二次供水物联网:HiWoo Cloud助力城市水务管理升级
  • P1015 [NOIP1999 普及组] 回文数
  • 【MATLAB】兔子机器人动力学模型解读(simulink中的simscape的各模块介绍)
  • 小程序配置服务器域名
  • 探究全链路压力测试的含义与重要性
  • 代码随想录算法训练营day64 || 84. 柱状图中最大的矩形
  • 图的简单介绍
  • 【C#小知识】c#中的delegate(委托)和event(事件)
  • 车规级存储芯片SPI NOR Flash
  • CSS轻松学:简单易懂的CSS基础指南
  • 06 Qt自绘组件:Switch动画开关组件
  • 大语言模型LLM分布式训练:大规模数据集上的并行技术全景探索(LLM系列03)
  • 98.验证二叉搜索树
  • 2月21日,每日信息差
  • android.text.BoringLayout.isBoring 的 NullPointerException
  • C++ 高频考点
  • Ubuntu安装SVN服务并结合内网穿透实现公网访问本地存储文件
  • 2月20日,每日信息差
  • Visual Studio清单作用
  • Java中的==和equals()方法的区别是?hashCode()和equals()的关系是什么?
  • yaml-cpp开源库使用
  • 【C++私房菜】序列式容器的迭代器失效问题
  • MySQL 篇-深入了解 DML、DQL 语言(二)
  • 端智能:面向手机计算环境的端云协同AI技术创新
  • PHP函数 “password_hash“ 哈希密码