文章目录
一、嵌套与递归
1、函数的嵌套
2、函数的递归调用
定义:函数直接或者间接地调用自身。
例题1:计算n的阶乘
#include <iostream>
using namespace std;
unsigned factorial(unsigned n) {
if (n == 0)
return 1;
return n * factorial(n - 1);
}
int main() {
cout << factorial(5)<<endl;
}
例题2:用递归法计算从n个人中选k个人组成一个委员会的不同组合数。
分析
- 由n个人里选k个人的组合数= 由n-1个人里选k个人的组合数+由n-1个人里选k-1个人的组合数;
- 当n = k或k = 0时,组合数为1。
C n k = C n − 1 k + C n − 1 k − 1 C_n^k=C_{n-1}^k+C_{n-1}^{k-1} Cnk=Cn−1k+Cn−1k−1
#include <iostream>
using namespace std;
unsigned combination(unsigned n, unsigned k) {
unsigned results;
if (k > n)
results = 0;
else if (n == k || k == 0)
results = 1;
else
results = combination(n - 1, k) + combination(n - 1, k - 1);
return results;
}
int main() {
int n, k;
cout << "please enter n and k: " << endl;
cin >> n >> k;
cout << "C(n,k)="<<combination(n,k) << endl;
return 0;
}
例题3 汉诺塔
#include <iostream>
using namespace std;
void move(char src, char dest) {
cout << src << " ----> " << dest << endl;
}
void hanoi(int m, char src, char medium, char dest) {
//m: 盘子个数 src:源 dest: 目的地 medium: 中介
if (m == 1) {
move(src, dest);
}
else {
hanoi(m - 1, src, dest, medium);
move(src, dest);
hanoi(m - 1, medium, src, dest);
}
}
int main() {
hanoi(10, 'A', 'B', 'C');
}
二、函数的参数传递
1、引用类型
例题1: 值传递,交换
#include <iostream>
using namespace std;
void swap(int a, int b) {
int t;
t = a;
a = b;
b = t;
}
int main() {
int x = 4, y = 5;
cout << "x = " << x << " y = " << y << endl;
swap(x, y);
cout << "x = " << x << " y = " << y << endl;
return 0;
}
例题2:引用传递,交换
#include <iostream>
using namespace std;
void swap(int &a, int &b) {
int t;
t = a;
a = b;
b = t;
}
int main() {
int x = 4, y = 5;
cout << "x = " << x << " y = " << y << endl;
swap(x, y);
cout << "x = " << x << " y = " << y << endl;
return 0;
}
三、含有可变参数的函数
含有可变参数的函数
initializer_list提供的操作
initializer_list使用方法
四、内联函数
例题:内联函数应用举例
#include <iostream>
using namespace std;
const double PI = 3.1415926;
inline double calArea(double r) {
return PI * r*r;
}
int main() {
double r = 3.0;
double area = calArea(r);
cout << area << endl;
return 0;
}
五、constexpr函数
六、带默认参数值的函数
默认参数值
可以预先设置默认的参数值,调用时如给出实参,则采用实参值,否则采用预先设置的默认参数值。
例:
int add(int x = 5,int y = 6) {
return x + y;
}
int main() {
add(10,20); //10+20
add(10); //10+6
add(); //5+6
}
默认参数值的说明次序
- 有默认参数的形参必须列在形参列表的最右,即默认参数值的右面不能有无默认值的参数;
- 调用时实参与形参的结合次序是从左向右。
例:
int add(int x, int y = 5, int z = 6);//正确
int add(int x = 1, int y = 5, int z);//错误
int add(int x = 1, int y, int z = 6);//错误
默认参数值与函数的调用位置
- 如果一个函数有原型声明,且原型声明在定义之前,则默认参数值应在函数原型声明中给出;如果只有函数的定义,或函数定义在前,则默认参数值可以函数定义中给出。
例:
例题:计算长方体体积
函数getVolume计算体积
有三个形参:length(长)、width(宽)、height(高),其中width和height带有默认值2和3。
主函数中以不同形式调用getVolume函数。
#include <iostream>
#include <iomanip>
using namespace std;
int getVolume(int length, int width = 2, int height = 3);
int main() {
const int X = 10, Y = 12, Z = 15;
cout << "Some box data is " ;
cout << getVolume(X, Y, Z) << endl;
cout << "Some box data is " ;
cout << getVolume(X, Y) << endl;
cout << "Some box data is " ;
cout << getVolume(X) << endl;
return 0;
}
int getVolume(int length, int width, int height) {
cout << setw(5) << length << setw(5) << width << setw(5)
<< height << '\t';
return length * width * height;
}
七、函数重载
函数重载的概念
例题:重载函数应用举例
#include <iostream>
using namespace std;
int sumOfSquare(int a, int b) {
return a * a + b * b;
}
double sumOfSquare(double a, double b) {
return a * a + b * b;
}
int main() {
int m, n;
cout << "Enter two integer: ";
cin >> m >> n;
cout << "Their sum of square: " << sumOfSquare(m, n) << endl;
double x, y;
cout << "Enter two real number: ";
cin >> x >> y;
cout << "Their sum of square: " << sumOfSquare(x, y) << endl;
return 0;
}
八、C++系统函数
#include <iostream>
#include <cmath>
using namespace std;
const double PI = 3.14159265358979;
int main() {
double angle;
cout << "Please enter an angle: ";
cin >> angle; //输入角度值
double radian = angle * PI / 180; //转化为弧度值
cout << "sin(" << angle << ") = " << sin(radian) <<endl;
cout << "cos(" << angle << ") = " << cos(radian) <<endl;
cout << "tan(" << angle << ") = " << tan(radian) <<endl;
return 0;
}