日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

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

發布時間:2023/12/31 c/c++ 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【c++思维导图与代码示例】02 函数 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

【思維導圖與代碼示例】02 函數

    • 思維導圖
    • 代碼示例

  • 本系列 c++ 學習框架推薦:
  • 【零基礎一文學C++基礎與進階】.

思維導圖

代碼示例

  • 示例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;elsef = 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; }

總結

以上是生活随笔為你收集整理的【c++思维导图与代码示例】02 函数的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。