/*************************************************
**
**Description: 寻找并输出11-999之间的数m,m 自身、平方、立方值均为回文;数理解函数的调用
**
** Author:慕灵阁-wpke
** Time:2021-11-05
** Versions :2_1.cpp
**
*
***************************************************/#include<iostream>usingnamespace std;//判断n是否为回文数boolsymm(unsigned n){
unsigned i = n;unsigned m =0;while(i >0){
m = m *10+ i %10;
i /=10;}return m == n;}intmain(){
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");return0;}
示例2:
/*************************************************
**
**Description: 实现两数的平方和 ,理解函数的嵌套调用
**
** Author:慕灵阁-wpke
** Time:2021-11-05
** Versions :2_2.cpp
**
*
***************************************************///2_2.cpp #include<iostream>usingnamespace std;intfun2(int m){
return m * m;}intfun1(int x,int y){
returnfun2(x)+fun2(y);}intmain(){
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;return0;}
示例3:
/*************************************************
**
**Description: 理解函数的递归调用
**
** Author:慕灵阁-wpke
** Time:2021-11-05
** Versions :2_3.cpp
**
*
***************************************************///2_3.1 实现阶乘#include<iostream>usingnamespace std;//计算n的阶乘unsignedfac(unsigned n){
unsigned f;if(n ==0)
f =1;else
f =fac(n -1)* n;return f;}intmain(){
unsigned n;
cout <<"Enter a positive integer: ";
cin >> n;unsigned y =fac(n);
cout << n <<"! = "<< y << endl;system("pause");return0;}//2_3.2 汉诺塔问题#include<iostream>usingnamespace std;//把src针的最上面一个盘子移动到dest针上voidmove(char src,char dest){
cout << src <<" --> "<< dest << endl;}//把n个盘子从src针移动到dest针,以medium针作为中介voidhanoi(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);}}intmain(){
int m;
cout <<"Enter the number of diskes: ";
cin >> m;
cout <<"the steps to moving "<< m <<" diskes:"<< endl;hanoi(m,'A','B','C');return0;}
示例4:
/*************************************************
**
**Description: 理解函数参数传递的值传递与引用传递
**
** Author:慕灵阁-wpke
** Time:2021-11-05
** Versions :2_4.cpp
**
*
***************************************************///值传递#include<iostream>usingnamespace std;voidswap1(int a,int b){
int t = a;
a = b;
b = t;}intmain(){
int x =5, y =10;
cout <<"x = "<< x <<" y = "<< y << endl;swap1(x, y);
cout <<"x = "<< x <<" y = "<< y << endl;system("pause");return0;}// 引用传递#include<iostream>usingnamespace std;voidswap2(int&a,int&b){
int t = a;
a = b;
b = t;}intmain(){
int x =5, y =10;
cout <<"x = "<< x <<" y = "<< y << endl;swap2(x, y);
cout <<"x = "<< x <<" y = "<< y << endl;system("pause");return0;}
示例5:
/*************************************************
**
**Description: 理解内联函数:
**
** Author:慕灵阁-wpke
** Time:2021-11-05
** Versions :2_1.cpp
**
*
***************************************************/#include<iostream>usingnamespace std;constdouble PI =3.14159265358979;//内联函数,根据圆的半径计算其面积inlinedoublecalArea(double radius){
return PI * radius * radius;}intmain(){
double r ;//r是圆的半径
cout<<"please input r : "<<endl;
cin >> r;//调用内联函数求圆的面积,编译时此处被替换为CalArea函数体语句double area =calArea(r);
cout << area << endl;system("pause");return0;}
示例6:
/*************************************************
**
**Description: 理解带默认参数的函数:
定义函数getVolume有三个形参:length(长)、width(宽)、height(高),
其中width和height带有默认值。主函数中以不同形式调用getVolume函数。
** Author:慕灵阁-wpke
** Time:2021-11-05
** Versions :2_6.cpp
**
*
***************************************************/#include<iostream>#include<iomanip>usingnamespace std;intgetVolume(int length,int width =2,int height =3);intmain(){
constint 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");return0;}intgetVolume(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>usingnamespace std;intsumOfSquare(int a,int b){
return a * a + b * b;}doublesumOfSquare(double a,double b){
return a * a + b * b;}intmain(){
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;return0;}