【c++思维导图与代码示例】02 函数

【思维导图与代码示例】02 函数

思维导图

在这里插入图片描述

代码示例

  • 示例1:


/*************************************************
**
**Description: 寻找并输出11-999之间的数m,m 自身、平方、立方值均为回文;数理解函数的调用
**
** Author:慕灵阁-wpke
** Time:2021-11-05
** Versions :2_1.cpp
** 
*
***************************************************/
#include <iostream>
using namespace std;

//判断n是否为回文数
bool symm(unsigned n) {
    
    
	unsigned i = n;
	unsigned m = 0;
	while (i > 0) {
    
    
		m = m * 10 + i % 10;
		i /= 10;
	}
	return m == n;
}

int main() {
    
    
	for (unsigned m = 11; m < 1000; m++)
		if (symm(m) && symm(m * m) && symm(m * m * m)) {
    
    
			cout << "m = " << m;
			cout << "  m * m = " << m * m;
			cout << "  m * m * m = " << m * m * m << endl;
		}
    system("pause");
	return 0;
}


  • 示例2:



/*************************************************
**
**Description:  实现两数的平方和 ,理解函数的嵌套调用
**
** Author:慕灵阁-wpke
** Time:2021-11-05
** Versions :2_2.cpp
** 
*
***************************************************/
//2_2.cpp 
#include <iostream>
using namespace std;

int fun2(int m) {
    
    
	return m * m;
}

int fun1(int x,int y) {
    
    
	return fun2(x) + fun2(y);
}

int main() {
    
    
	int a, b;
	cout << "Please enter two integers(a and b): ";
	cin >> a >> b;
	cout << "The sum of square of a and b: " << fun1(a, b) << endl;
	return 0;
}

  • 示例3:



/*************************************************
**
**Description: 理解函数的递归调用
**
** Author:慕灵阁-wpke
** Time:2021-11-05
** Versions :2_3.cpp
** 
*
***************************************************/

//2_3.1  实现阶乘
#include <iostream>
using namespace std;

//计算n的阶乘
unsigned fac(unsigned n) {
    
    
	unsigned f;
	if (n == 0)
		f = 1;
	else
		f = fac(n - 1) * n;
	return f;
}

int main() {
    
    
	unsigned n;
	cout << "Enter a positive integer: ";
	cin >> n;
	unsigned y = fac(n);
	cout << n << "! = " << y << endl;
    system("pause");
	return 0;
}



//2_3.2 汉诺塔问题
#include <iostream>
using namespace std;

//把src针的最上面一个盘子移动到dest针上
void move(char src, char dest) {
    
     
	cout << src << " --> " << dest << endl;
}

//把n个盘子从src针移动到dest针,以medium针作为中介
void hanoi(int n, char src, char medium, char dest) {
    
    
	if (n == 1)
		move(src, dest);
	else {
    
    
		hanoi(n - 1, src, dest, medium);
		move(src, dest);
		hanoi(n - 1, medium, src, dest);
	}
}

int main() {
    
    
	int m;
	cout << "Enter the number of diskes: ";
	cin >> m;
	cout << "the steps to moving " << m << " diskes:" << endl;
	hanoi(m,'A','B','C');
    
	return 0;
}

  • 示例4:



/*************************************************
**
**Description: 理解函数参数传递的值传递与引用传递
**
** Author:慕灵阁-wpke
** Time:2021-11-05
** Versions :2_4.cpp
** 
*
***************************************************/
//值传递
#include <iostream>
using namespace std;

void swap1(int a, int b) {
    
    
	int t = a;
	a = b;
	b = t;
}

int main() {
    
    
	int x = 5, y = 10;
	cout << "x = " << x << "    y = " << y << endl;
	swap1(x, y);
	cout << "x = " << x << "    y = " << y << endl;
    system("pause");
	return 0;
}



// 引用传递
#include <iostream>
using namespace std;

void swap2(int &a, int &b) {
    
    
	int t = a;
	a = b;
	b = t;
}

int main() {
    
    
	int x = 5, y = 10;
	cout << "x = " << x << "    y = " << y << endl;
	swap2(x, y);
	cout << "x = " << x << "    y = " << y << endl;
system("pause");
	return 0;
}

  • 示例5:

/*************************************************
**
**Description: 理解内联函数:
**
** Author:慕灵阁-wpke
** Time:2021-11-05
** Versions :2_1.cpp
** 
*
***************************************************/

#include <iostream>
using namespace std;

const double PI = 3.14159265358979;

//内联函数,根据圆的半径计算其面积
inline double calArea(double radius) {
    
    
	return PI * radius * radius;
}

int main() {
    
    
	double r ;//r是圆的半径
    cout<< "please input  r : " <<endl;
    cin >> r;
	//调用内联函数求圆的面积,编译时此处被替换为CalArea函数体语句
	double area	= calArea(r);
	cout << area << endl;
    system("pause");
	return 0;
}


  • 示例6:


/*************************************************
**
**Description: 理解带默认参数的函数:	
定义函数getVolume有三个形参:length(长)、width(宽)、height(高),
其中width和height带有默认值。主函数中以不同形式调用getVolume函数。

** Author:慕灵阁-wpke
** Time:2021-11-05
** Versions :2_6.cpp
** 
*
***************************************************/

#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;
    system("pause");
	return 0;
}

int getVolume(int length, int width/* = 2*/ , int height/* = 3*/ ) {
    
    
	cout << setw(5) << length << setw(5) << width << setw(5) << height << '\t';
	return length * width * height;
}

  • 示例7:

/*************************************************
**
**Description: 理解函数重载
**
** Author:慕灵阁-wpke
** Time:2021-11-05
** Versions :2_7.cpp
** 
*
***************************************************/
#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;
}

Guess you like

Origin blog.csdn.net/Kefenggewu_/article/details/121914178