已阅读5页,还剩10页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
第十章 函數的進階與應用10-1 函數的重載(overloading)何謂重載:相同的函數名稱被重複用於不同函數的宣告與定義,稱為overloading範例:i. 參數的資料型態不同int Max(int,int);float Max(float,float);ii. 參數的數目不一樣double Area(double x);double Area(double x, double y);範例程式:試撰寫程式,由鍵盤輸入選擇,比較兩個數或三個數,然後自動判斷找出最大值Step1.先撰寫一個具備比較兩個數的程式Step2.再增加可以比較三個數的函數注意:函數名稱相同,但變數不同程式碼:#include using std:cin;using std:cout;using std:endl;double min(double, double);double min(double, double, double);int main()int n;double x,y,z;couthow many number you want to compare:(2 or 3)n;if ( n =2) coutPlease input two integers, first integer:x; cout The second integer:y; coutThe minimum value is min(x,y)endl; else if (n =3) coutPlease input three integers, first integer:x; cout The second integer:y; cout The third integer:z; coutThe minimum value is min(x,y,z)endl; else coutYou input a wrong numberendl;return 0;double min(double a, double b)return (ab)temp = b;if (temp c); temp = c;return temp;10-2 參數的預設值函數的參數可以做預設值,例如declaration float Area(float width, float length=12.0)definition float Area(float width, float length) return width*length ;call A=Area(6.5) 代表width=6.5, length已預設為12.0 A=Area(6.5,15) width=6.5,length=15.0 練習:將上述宣告,定義及呼叫撰寫成程式10-4 亂數的取得(random numbers)一、 準亂數產生器(pseudo-random number generator)每次執行起始值都一樣declaration#include /C+ standard library .cout rand(); /產生一個亂數介於032767之間如果想要產生有範圍的亂數,必須做縮尺(scaling),例如 i = rand() % 6 ;練習1.建立一個45的二維陣列,陣列每個元素介於1到6之間i =( rand() % 6 + 1 );主要程式碼 (program:10-4-1.cpp)int main() for(int i = 0 ; i 4 ; i+) for(int j = 0; j 4 ; j+) arrayij=( rand() % 6 + 1 ); coutsetw(10)arrayij; coutendl;程式碼:#include using std:endl;using std:cout;using std:cin;#include using std:setw;#include int main()const int m = 4; const int n = 5;int Amn=0;int temp = 1; while (temp = 1) for (int i = 0; i m; i+)for (int j = 0; j n; j+)Aij=(1+rand()%6);for (int ii = 0; ii m; ii+)for (int jj = 0; jj n; jj+)coutsetw(10)Aiijj;coutendl;coutDo you want to try again(1 for yes)temp; return 0;練習2.擲一骰子遊戲,骰子之數值介於16之間,試統計擲6000次之後,16每一個數字出現的機率次數程式撰寫方法先定義16發生機率次數為0再用for迴圈配合switch指令進行統計程式主要架構 (program 10-4-2.cpp)int main() int f1=0, f2=0, f3=0, f4=0, f5=0, f6=0 ; for(int roll = 0 ; roll 4 ; roll+) face=1 + rand() % 6 ; switch(face)case 1: +f1; break;case 2: +f2; break;case 3: +f3; break;case 4: +f4; break;case 5: +f5; break;case 6: +f6; break;default: coutProgram never get here.;return 0 ;程式碼:#include using std:endl;using std:cout;using std:cin;#include int main()int f6 = 0;int face;for (int roll = 1; roll =6000; roll+) face = 1+rand()%6; switch(face) case 1:+f0;break;case 2:+f1;break;case 3:+f2;break;case 4:+f3;break;case 5:+f4;break;case 6:+f5;break; default:coutProgram never get here!endl; coutThe times of die is:endl;for (int j = 0; j 6; j+)coutface =j+1, the times is:fjendl;coutnThe probability of die is :endl;for (int k = 0; k 6; k+)coutface =k+1, the times is:double(fk)/6000endl;return 0;二、 將pseudo-random number改為randomize1. 採用srand(),程式輸入一個unsigned integer為seed,可使rand函數產生不同系列的亂數Example: (program 10-4-3.cpp)unsigned int seed;coutseed;srand(seed);for(int i= 1;i =10; i+)coutsetw(10)1+rand()%6;if(i%5=0)coutendl;練習:將上述片段程式寫成完整程式碼,然後輸入不同seed2. 取用系統時間為亂數,較為簡易,需加上下列指令#includeusing std:time;.srand( time(0) );.練習:將上題程式碼之seed改為time(0) (program 10-4-4.cpp)10-5遞迴函數(recursive function)何謂遞迴函數:函數不斷呼叫自己,直到termination condition產生才停止一、 階乘函數(factorial function)n!n!=n(n-1)(n-2).321函數寫法1. 遞迴(10-5-2.cpp)int Factorial(int N) if(N=1) return 1; else return N*Factorial(N-1);2. 迴圈(10-5-1.cpp)int Factorial(int N) int temp=1; for(int i= 1;i =N; i+) temp *= i; return temp;練習:寫一個遞迴函數計算n!程式碼:1. 採用迴圈方式寫#include using std:endl;using std:cout;using std:cin;unsigned long Factorial(int); int main()int N;coutPlease input a integer bigger than 1N; coutThe value of N! is Factorial(N)endl;return 0;unsigned long Factorial (int Num)int temp=1;for (int i = 1; i = Num; i+)temp *=i; return temp;2. 採用遞迴方式撰寫#include using std:endl;using std:cout;using std:cin;unsigned long Factorial(int); int main()int N;coutPlease input a integer bigger than 1N; coutThe value of N! is Factorial(N)endl;return 0;unsigned long Factorial (int Num)if (Num =1)return 1;/termination conditionelse return Num*Factorial(Num-1);/recursive condition二、 Gretest Common Divider(GCD)找兩個整數的最大公因數程式碼:1. 遞迴(10-5-3.cpp)int GCD(int M, int N) if(M%N)=0) return N; else return GCD( N, M%N );程式碼:#include using std:endl;using std:cout;using std:cin;int GCD(int,int); int main()int M,N;coutPlease input two integer bigger than 1, the first value isN; coutThe second value is M;cout The greatest common divider is :GCD(M,N)1) if(M%N)=0) temp=1; else temp=M%N; M=N; N=temp; return N;程式碼:#include using std:endl;using std:cout;using std:cin;int GCD(int,int); int main()int M,N;coutPlease input two integer bigger than 1, the first value isN; coutThe second value is M;cout The greatest common divider is :GCD(M,N)1)if (M%N)=0) temp = 1; else temp = M%N;M = N;N = temp; return N;練習:使用遞迴撰寫程式,求兩個整數之最大公因數*第十章 作業*Topic 1. 函數的重載(overloading):相同函數名稱被重複用於不同函數的宣告和定義A. 試寫出一程式,內含函數,以重載方式使程式能經由輸入一整數或浮點數值,回傳此數值的平方。B. 利用函數重載功能,寫出兩個同樣名為Max( )的函數,分別可以找出二個數中之最大值及三個數中之最大值。Topic 2. 亂數(random numbers)取得A. 寫一程式,能以亂數跑出一組樂透彩卷號碼。Topic 3. 遞迴(recursion )函數A. 寫一程式,能以遞迴方式計算,再以迴圈方式計算一次。B. 以遞迴方式計算兩數之最大公約數。C. 已知費式級數(Fibonacci number)之定義如下:故為0、1、1、2、3、5、8、13 ,試以遞迴演算法(recursive algorithm) ,計算 之值。D.已知binomial coefficient定義為,有如下特性:,且當,試寫一遞迴
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 危险化学品数字化管控基础知识安全培训
- 全生命周期急救教育普及实施方案
- 年产xx吨冻干食品项目可行性研究报告
- 校园欺凌事件工作总结
- 医药领域腐败集中整治自查自纠个人报告
- (新)骨科医师晋升副主任医师职称病例分析专题报告(奇异性骨旁骨软骨瘤样增生)2篇
- 辅导员心理健康教育宣传方案设计指南
- 创意产出与专业能力评估表
- 关于新品上市准备工作的进度更新函4篇
- 年度预算调整申请处理通知7篇范本
- 实验室安全培训案例课件
- 快消品特殊渠道开发课件
- 2025-2026学年北京市文汇中学教育集团九年级(上)期中语文试卷
- 上海市郊野乡村风貌规划设计和建设导则二
- 风机降噪方案
- 产品开发TR5评审要素表
- 多学科交叉融合驱动下机器人导论课程实践与思考
- 航天科工集团在线测评题
- GB/T 44211-2024消费品质量分级导则家具
- 求职OMG-大学生就业指导与技能开发智慧树知到期末考试答案章节答案2024年中国海洋大学
- 第10章-幼儿园组织文化建设课件
评论
0/150
提交评论