/*************************************************
**
**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;elsef =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;}