C++——函數的調用
阿新 • • 發佈:2018-02-02
iostream 轉換 bit 個人 ber div end using 列表
調用前先聲明函數:
>>若函數定義在調用點之前,則無需另外聲明
>>若函數定義在調用點之後,則需要在調用函數前按如下形式聲明函數原型(原型聲明)
類型標識符 被調用函數名(含類型說明的形參表)
調用形式
函數名(實參列表)
嵌套調用
在一個函數的函數體中。可以調用另一函數,稱為嵌套調用
遞歸調用:
函數直接或間接調用自身
例一:編寫一個求x的n次方的函數
#include<iostream> using namespace std; double power(double x, int n) { double val = 1.0; while (n--) val *= x; return val; } int main() { cout << "5 to the power 2 is "<<power(5,2) << endl; system("pause"); return 0; }
輸出結果為25
例二:數制轉換:
輸入一個8位二進制數,將其轉化為十進制數輸出。
#include<iostream> using namespace std; double power(double x, int n);//計算x的n次方 int main() { int value = 0; cout << "Enter an 8 bit binary number "; for (int i = 7; i >= 0; i--) { char ch; cin >> ch; if (ch == ‘1‘) value += static_cast<int>(power(2, i)); } cout << "Decimal value is" << value << endl; system("pause"); return 0; } double power(double x, int n) { double val = 1.0; while (n--) val *= x; return val; }
輸出結果:
例三:編寫程序求π的值
π = 16arctan(1/5)-4arctan(1/239)
arctanx = x - x3/3 + x5/5 - x7/7直到級數某項絕對值不大於10-15為止,π和x均為double型
#include<iostream> using namespace std; double arctan(double x) { double sqr = x * x; double e = x; double r = 0; int i = 1; while (e / i > 1e-15) { double f = e / i; r = (i % 4 == 1) ? r + f : r - f; e = e * sqr; i += 2; } return r; } int main() { double a = 16.0 * arctan(1 / 5.0); double b = 4.0 * arctan(1 / 239.0); cout << "PI = " << a - b << endl; return 0; }#include<iostream> using namespace std; double arctan(double x) { double sqr = x * x; double e = x; double r = 0; int i = 1; while (e / i > 1e-15) { double f = e / i; r = (i % 4 == 1) ? r + f : r - f; e = e * sqr; i += 2; } return r; } int main() { double a = 16.0 * arctan(1 / 5.0); double b = 4.0 * arctan(1 / 239.0); cout << "PI = " << a - b << endl; return 0; }
輸出結果為3.14159
例四:
尋找並輸出11-999之間的數m,他滿足m,m2,m3都是回文數
#include<iostream> using namespace std; 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; }
輸出結果:
例五:
#include<iostream> #include<cmath> using namespace std; const double TINY_VALUE = 1e-10; double tsin(double x) { double g = 0; double t = x; int n = 1; do { g += t; n++; t = -t * x * x / (2 * n - 1) / (2 * n - 2); } while (fabs(t) >= TINY_VALUE); return g; } int main() { double k, r, s; cout << "r ="; cin >> r; cout << "s ="; cin >> s; if (r * r <= s * s) k = sqrt(tsin(r)*tsin(r) + tsin(s)*tsin(s)); else k = tsin(r * s) / 2; cout << k << endl; system("pause"); return 0; }
輸出結果:
嵌套調用
嵌套調用舉例:
輸入兩個整數,求平方和:
#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 nad b: " << fun1(a, b) << endl; system("pause"); return 0; }
遞歸調用:
函數直接或間接地調用自身,稱為遞歸調用、
遞歸過程的兩個階段:
遞歸調用舉例:
求n!
#include<iostream> using namespace std; unsigned fac(int 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; }
階乘問題與或圖
舉例如下:
用遞歸發計算從n個人中選擇k個人組成一個委員會的不同 組合數
分析:
>>由n個人裏選k個人的組合數
= 由n-1個人裏選k個人的組合數 + 由m-1個人裏選k-1個人的組合數
當n = k 或k = 0時,組合數為1
#include<iostream> using namespace std; int comm(int n, int k) { if (k > n) return 0; else if (n == k || k == 0) return 1; else return comm(n - 1, k) + comm(n - 1, k - 1); } int main() { int n, k; cout << "Please enter two integers n and k: "; cin >> n >> k; cout << "C(n,k) = " << comm(n,k) << endl; system("pause"); return 0; }
輸出結果:
C++——函數的調用