C語(yǔ)言程序設(shè)計(jì)中國(guó)水利水電AB函數(shù)學(xué)習(xí)教案_第1頁(yè)
C語(yǔ)言程序設(shè)計(jì)中國(guó)水利水電AB函數(shù)學(xué)習(xí)教案_第2頁(yè)
C語(yǔ)言程序設(shè)計(jì)中國(guó)水利水電AB函數(shù)學(xué)習(xí)教案_第3頁(yè)
C語(yǔ)言程序設(shè)計(jì)中國(guó)水利水電AB函數(shù)學(xué)習(xí)教案_第4頁(yè)
C語(yǔ)言程序設(shè)計(jì)中國(guó)水利水電AB函數(shù)學(xué)習(xí)教案_第5頁(yè)
已閱讀5頁(yè),還剩38頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、會(huì)計(jì)學(xué)1C語(yǔ)言程序設(shè)計(jì)中國(guó)水利水電語(yǔ)言程序設(shè)計(jì)中國(guó)水利水電AB函數(shù)函數(shù)第一頁(yè),編輯于星期六:四點(diǎn) 二十六分。第4章 函數(shù)函數(shù)頭函數(shù)頭函數(shù)體函數(shù)體第1頁(yè)/共43頁(yè)第二頁(yè),編輯于星期六:四點(diǎn) 二十六分。#include int max(int x, int y);void main() int a,b,c; cout a b; c = max(a,b); cout a,b中較大的數(shù)是: c y) m=x; else m=y; return m; 第4章 函數(shù)函數(shù)調(diào)用語(yǔ)句函數(shù)調(diào)用語(yǔ)句主調(diào)函數(shù)主調(diào)函數(shù)被調(diào)函數(shù)被調(diào)函數(shù)第2頁(yè)/共43頁(yè)第三頁(yè),編輯于星期六:四點(diǎn) 二十六分。第4章 函數(shù)執(zhí)行該語(yǔ)句時(shí),不帶回

2、返回值執(zhí)行該語(yǔ)句時(shí),不帶回返回值,只是返回主調(diào)函數(shù),只是返回主調(diào)函數(shù)第3頁(yè)/共43頁(yè)第四頁(yè),編輯于星期六:四點(diǎn) 二十六分。第4章 函數(shù)第4頁(yè)/共43頁(yè)第五頁(yè),編輯于星期六:四點(diǎn) 二十六分。第4章 函數(shù)第5頁(yè)/共43頁(yè)第六頁(yè),編輯于星期六:四點(diǎn) 二十六分。#include double power(double x, int n);void main() double x; int n; cout x n; cout x 的的 n 次方是:次方是: power(x,n) endl;double power(double x, int n) double a = 1.0; int i; for(i

3、=1; i=n; i+) a *= x; return a; 第4章 函數(shù) 返 回第6頁(yè)/共43頁(yè)第七頁(yè),編輯于星期六:四點(diǎn) 二十六分。第4章 函數(shù)第7頁(yè)/共43頁(yè)第八頁(yè),編輯于星期六:四點(diǎn) 二十六分。#include void swap(int x, int y);void main()int a, b;a = 10;b = 20;swap(a, b);cout a , b endl;10a10 x20b20y1020 xy10temp2020 xy10temp2010 xy10tempvoid swap(int x, int y)int temp;temp = x;x = y;y = te

4、mp;第4章 函數(shù)注意:值傳遞時(shí),函數(shù)的實(shí)參與形參在內(nèi)存中占用不同的存儲(chǔ)空間,值只能由實(shí)參傳遞注意:值傳遞時(shí),函數(shù)的實(shí)參與形參在內(nèi)存中占用不同的存儲(chǔ)空間,值只能由實(shí)參傳遞給形參,而形參的變化并不會(huì)影響實(shí)參。因此不能完成程序功能。給形參,而形參的變化并不會(huì)影響實(shí)參。因此不能完成程序功能。第8頁(yè)/共43頁(yè)第九頁(yè),編輯于星期六:四點(diǎn) 二十六分。第4章 函數(shù)第9頁(yè)/共43頁(yè)第十頁(yè),編輯于星期六:四點(diǎn) 二十六分。#include void main()int a=1;int c=10;int &b = a;cout a , b , c endl;b = c;cout a , b , c endl;b =

5、 20;cout a , b , c endl;定義引用定義引用b,并將其作為,并將其作為a的別名的別名第4章 函數(shù)將將c的值賦給的值賦給b,不是將,不是將b作為作為c的別名的別名第10頁(yè)/共43頁(yè)第十一頁(yè),編輯于星期六:四點(diǎn) 二十六分。第4章 函數(shù)#include void swap(int x, int y);void main()int a, b;a = 10;b = 20;swap(a, b);cout a , b endl;void swap(int &x, int &y)int temp;temp = x;x = y;y = temp;第11頁(yè)/共43頁(yè)第十二頁(yè),編輯于星期六:四點(diǎn)

6、 二十六分。第4章 函數(shù)10axby20axby10temp20axby10temp10axby10temp20202010注意:引用作參數(shù)時(shí),函數(shù)的實(shí)參與形參在內(nèi)存中共用存儲(chǔ)單元,因此形參的變化會(huì)使注意:引用作參數(shù)時(shí),函數(shù)的實(shí)參與形參在內(nèi)存中共用存儲(chǔ)單元,因此形參的變化會(huì)使實(shí)參同時(shí)變化。實(shí)參同時(shí)變化。 返 回第12頁(yè)/共43頁(yè)第十三頁(yè),編輯于星期六:四點(diǎn) 二十六分。第4章 函數(shù)第13頁(yè)/共43頁(yè)第十四頁(yè),編輯于星期六:四點(diǎn) 二十六分。#include long f1(int n);long f2(int m);void main()int n;long s;cout n;s = f1(n);

7、cout s endl;第4章 函數(shù)long f1(int n) / 求求1!+2!+n!int i;long sum = 0;for(i=1; i=n; i+)sum += f2(i);return sum;long f2(int m) / 求求m!int i;long s = 1;for(i=1; i=m; i+)s *= i;return s;第14頁(yè)/共43頁(yè)第十五頁(yè),編輯于星期六:四點(diǎn) 二十六分。#include int fun2(int m);int fun1(int x,int y);void main(void) int a,b; cout a b; cout a、b的平方和:

8、的平方和: fun1(a,b) endl;int fun1(int x, int y)return ( fun2(x) + fun2(y) );int fun2(int m)return ( m*m );第4章 函數(shù) 返 回第15頁(yè)/共43頁(yè)第十六頁(yè),編輯于星期六:四點(diǎn) 二十六分。第4章 函數(shù)f函數(shù)調(diào)用f函數(shù)f1函數(shù)調(diào)用f2函數(shù)f2函數(shù)調(diào)用f1函數(shù)直接調(diào)用間接調(diào)用第16頁(yè)/共43頁(yè)第十七頁(yè),編輯于星期六:四點(diǎn) 二十六分。#include long power(int n);void main() int n; long y; cout n; y=power(n); cout n != y 1)

9、 f=n*power(n-1); else f=1; return f;第17頁(yè)/共43頁(yè)第十八頁(yè),編輯于星期六:四點(diǎn) 二十六分。第4章 函數(shù)第18頁(yè)/共43頁(yè)第十九頁(yè),編輯于星期六:四點(diǎn) 二十六分。第4章 函數(shù)ABC第19頁(yè)/共43頁(yè)第二十頁(yè),編輯于星期六:四點(diǎn) 二十六分。#include void Move(char x, char y);void Hanoi(int n, char one, char two, char three);void main() int n; cout n; cout n 個(gè)盤(pán)子的移動(dòng)過(guò)程為: endl; Hanoi(n, A, B, C);/函數(shù)Move(

10、)將一個(gè)盤(pán)子從x針移到y(tǒng)針void Move(char x, char y) cout x y endl;第4章 函數(shù)第20頁(yè)/共43頁(yè)第二十一頁(yè),編輯于星期六:四點(diǎn) 二十六分。/函數(shù)Hanoi()將n-1個(gè)盤(pán)子從one針借助two針移到three針void Hanoi(int n, char one, char two, char three) if(n=1) Move(one, three); else Hanoi(n-1, one, three, two); Move(one, three); Hanoi(n-1, two, one, three); 第4章 函數(shù) 返 回運(yùn)行演示運(yùn)行演示

11、第21頁(yè)/共43頁(yè)第二十二頁(yè),編輯于星期六:四點(diǎn) 二十六分。第4章 函數(shù)第22頁(yè)/共43頁(yè)第二十三頁(yè),編輯于星期六:四點(diǎn) 二十六分。#include inline int Add(int a, int b)int x;x = a+b;return x;第4章 函數(shù) 返 回void main()int a, b, c;a = 10;b = 20;c = Add(a,b);cout a + b = c endl;c = Add(a,50);cout a + 50 = c endl;c = Add(50,50);cout 50 + 50 = c endl;第23頁(yè)/共43頁(yè)第二十四頁(yè),編輯于星期六:

12、四點(diǎn) 二十六分。第4章 函數(shù)第24頁(yè)/共43頁(yè)第二十五頁(yè),編輯于星期六:四點(diǎn) 二十六分。#include int max(int x, int y); double max(double x, double y); void main() int a=10, b=20 ,c; double x=200.3, y=400.6, z; c = max(a,b); z = max(x,y); cout c z endl; 第4章 函數(shù) 返 回int max(int x, int y) cout int function y) return x; else return y; double max(d

13、ouble x, double y) cout float function y) return x; else return y; 第25頁(yè)/共43頁(yè)第二十六頁(yè),編輯于星期六:四點(diǎn) 二十六分。第4章 函數(shù)#include double power(double x=10.0, int n=2); void main() cout power(3, 5) endl; cout power(3) endl; cout power() endl; double power(double x, int n) int i;double s=1.0;for(i=1; i=n; i+) s *= x;re

14、turn s; 第26頁(yè)/共43頁(yè)第二十七頁(yè),編輯于星期六:四點(diǎn) 二十六分。第4章 函數(shù)第27頁(yè)/共43頁(yè)第二十八頁(yè),編輯于星期六:四點(diǎn) 二十六分。第4章 函數(shù)#include int add(int x=5, int y=6); float add(int x=5, float y=10.0); void main() int a; float b; a= add(10,20); b= add(10); cout a= a endl; cout b= b endl; int add(int x, int y) return x+y; float add(int x, float y) ret

15、urn x+y; 返 回第28頁(yè)/共43頁(yè)第二十九頁(yè),編輯于星期六:四點(diǎn) 二十六分。第4章 函數(shù)void f1(int a) int b,c; void main() int m,n; int i,a;for(i=0; i10; i+) int b; 第29頁(yè)/共43頁(yè)第三十頁(yè),編輯于星期六:四點(diǎn) 二十六分。第4章 函數(shù)int a,b; void f1( ) int x,y;void main() 第30頁(yè)/共43頁(yè)第三十一頁(yè),編輯于星期六:四點(diǎn) 二十六分。#include int i=1; /全局變量,文件作用域 void main() cout全局變量 i=iendl; /輸出1 int

16、i=5; /函數(shù)局部變量,塊作用域 int i; /塊局部變量,塊作用域 i=7; cout塊局部變量 i=iendl; /輸出7 cout全局變量 i=:iendl; /輸出1,:使用全局變量 cout函數(shù)局部變量 i=iendl; /輸出5 cout全局變量 i=:iendl; /輸出1,:使用全局變量 第4章 函數(shù) 第31頁(yè)/共43頁(yè)第三十二頁(yè),編輯于星期六:四點(diǎn) 二十六分。第4章 函數(shù)第32頁(yè)/共43頁(yè)第三十三頁(yè),編輯于星期六:四點(diǎn) 二十六分。#include int fact( int n);void main() int i;for(i=1; i=4; i+) cout i ! =

17、 fact(i) endl;int fact(int n) static int f=1;/僅在第一次調(diào)用函數(shù)時(shí)執(zhí)行一次f *= n;return f;第4章 函數(shù) 第33頁(yè)/共43頁(yè)第三十四頁(yè),編輯于星期六:四點(diǎn) 二十六分。第4章 函數(shù)第34頁(yè)/共43頁(yè)第三十五頁(yè),編輯于星期六:四點(diǎn) 二十六分。#include void other(void); int i=1; / i 為全局變量,具有靜態(tài)生存期。 void main(void) static int a; / a為靜態(tài)局部變量,具有全局壽命,局部可見(jiàn)。 int b=-10; / b, c為動(dòng)態(tài)局部變量,具有局部生存期。 int c=0;

18、 cout-MAIN-n; cout i: i a: a b: b c: cendl; c=c+8; other(); cout-MAIN-n; cout i: i a: a b: b c: cendl; i=i+10; other(); 第4章 函數(shù)第35頁(yè)/共43頁(yè)第三十六頁(yè),編輯于星期六:四點(diǎn) 二十六分。void other(void) / a,b為靜態(tài)局部變量,具有全局壽命,局部可見(jiàn),/ 只第一次進(jìn)入函數(shù)時(shí)被初始化。 static int a=2; static int b; int c=10; / C為動(dòng)態(tài)局部變量,每次進(jìn)入函數(shù)時(shí)都初始化。 a=a+2; i=i+32; c=c+5; cout-OTHER-n;

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論