




已阅读5页,还剩3页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
第三章 函数典型案例【案例3-1】编写输出专用函数无参函数#includeusing namespace std;void DispMessage(void) /定义无参函数 coutThis is a Message!endl; int main() DispMessage(); /调用无参函数DispMessage return 0;【案例3-2】编写求和函数有参函数#includeusing namespace std;double add(double x,double y) /定义有参函数 double z; z=x+y; return(z); int main() double a=0.5, b=1.0; coutadd(a,b)=add(a,b)endl; /调用有参函数add() return 0;【案例3-3】编写求和函数函数的不同调用形式#includeusing namespace std;double add(double x,double y) /函数的定义,其有返回值 double z; z=x+y; coutx+y=zendl; return(z);int main() double a=0.5,b=1.0;/以不同参数形式调用函数add() coutadd(1.5,2.5)=add(1.5,2.5)endl; coutadd(a,b)=add(a,b)endl; coutadd(2*a,a+b)=add(2*a,a+b)endl; double c=2*add(a,b); /以表达式方式调用函数add() coutc=cendl; add(2*a,b); /以语句方式调用函数add() cout add(a, add(a,b)=add(a, add(a,b)endl; /以函数参数形式调用函数add() return 0;【案例3-4】编写符号函数函数的返回值#includeusing namespace std;int sgn(double x) /定义符号函数sgn(),其返回值为int类型 if (x0) return(1); /返回出口1 if (x0) return(-1); /返回出口2 return(0); /返回出口3int main() double x; for (int i=0;i=2;i+) coutx; coutsgn(x)=sgn(x)endl; return 0;【案例3-5】编写最值函数函数原型声明#includeusing namespace std;/函数原型声明语句也可以在这里int main() float max(float,float); /max()函数原型声明语句 float a,b,Max; /变量声明语句 couta;/输入参数a coutb; /输入参数b Max=max(a,b); /调用max()函数 coutmax(a,b)=Maxy)?x:y; return(z); /返回值类型为浮点型 【案例3-6】值传递和引用传递的区别#include using namespace std;void fun(int,int&); /函数参数一个为值传递,一个引用传递int main() int a = 22, b = 44; cout Initial a = a , b = b endl; fun(a,b); cout After fun(a,b), a = a , b = b endl; fun(2*a-3,b); cout After fun(2*a-3,b), a = a , b = b endl; return 0;void fun(int x, int& y) x = 88; y = 99;【案例3-7】编写最值函数内联函数#includeusing namespace std;inline int max(int x,int y) /使用inline关键字声明max()为内联函数 return xy?x:y; int main() int a=3,b=5,c; c=max(a,b); coutmax(a,b)=cendl; coutmax(15,11)=max(15,11)endl; return 0;【案例3-8】计算圆的面积和周长函数通过引用返回多于1个的数值#include using namespace std;void ComCircle(double&, double&, double); /函数的原型声明int main() double r, a, c; cout r; ComCircle(a, c, r); cout The area = a , and the circumference = c endl; return 0;void ComCircle(double& area, double& circum, double r) /通过引用变量返回面积和周长 const double PI = 3.141592653589793; area = PI*r*r; circum = 2*PI*r; /计算面积和周长【案例3-9】最小公倍数函数函数的嵌套调用#include using namespace std;long int gcd(long int m,long int n) /求最大公约数 if (m 0) int r = m%n;m = n; n = r; return m;long int lcm(long int m,long int n) /求最小公倍数 return m*n/gcd(m,n); int main() int m, n; cout m n; cout lcm( m , n ) = lcm(m,n) endl; return 0;【案例3-10】显示函数的参数带默认参数的函数#include using namespace std;void disp(int x=1,int y=1,int z=1) /带有默认参数值的函数 coutParameter 1 is: xendl; coutParameter 2 is: yendl; coutParameter 3 is: zendl;int main() /main()函数中测试参数带有默认值的函数disp() coutNo actual parameterendl; disp(); coutOne actual parameterendl; disp(1); coutTwo actual parameterendl; disp(1,2); coutThree actual parameterendl; disp(1,2,3); return 0;【案例3-11】通用最值函数参数数目可变的函数#include #include using namespace std;int max(int,int.); /原型声明int main() int a,b,c,d,e; coutabcde; coutThe maxmum in a and b is:max(2,a,b)endl; coutThe maxmum in five integers is:max(5,a,b,c,d,e)endl; return 0;int max(int num,int integer.) /定义参数数目可变的函数 va_list ap; int n=integer; va_start(ap,integer); for(int i=1;in) n=t; va_end(ap); return n;【案例3-12】多变的最值函数参数数目不同的重载函数#include using namespace std;int main() int min (int a, int b, int c); /函数声明 int min (int a, int b); /函数声明 int i1 ,i2,i3,i; couti1 i2 i3; /输入3个整数 i = min(i1 ,i2) ; cout The min in two intergers= i endl; / 2个整数中最小者 i = min(i1 ,i2 ,i3) ; / 3个整数中最小者 cout The min in three intergers= i endl; return 0;int min(int a,int b,int c) /定义求3个整数中的最小者的函数 int k; k=(ab)?a:b; k=(kc)?k:c; return k;int min(int a,int b) /定义求2个整数中的最小者的函数 int k; k=(ab)?a:b; return k;【案例3-13】求绝对值使用系统函数#include #include #include using namespace std;void main( void ) int ix = -4, iy; long lx = -41567L, ly; double dx = -3.141593, dy; iy = abs( ix ); coutThe absolute value ofix is iyendl; ly = labs( lx ); coutThe absolute value oflx is lyendl; dy = fabs( dx ); coutThe absolute value ofdx is dyendl;【案例3-14】将整数和小数分离使用系统函数#include#includeusing namespace std;void main(void) double fraction, integer,number = 103.567; fraction = modf(number, &integer); coutnumber整数部分为:integer 小数部分为:fraction;【案例3-15】求平方根使用系统函数#include #include #include using namespace std;void main( void ) double question = 45.35, answer; answer = sqrt( question ); if( question 0 ) coutError: sqrt returns answerendl; else coutThe square root of question is answerendl;【案例3-16】求随机数使用系统函数#include #include #include using namespace std;void main(void) cout RAND_MAX= RAND_MAX endl; cout产生10个 0 到 99的随机数如下:n; for(int i=0; i10; i+) cout(rand() % 100) ; /求除以100的余数 cout n使用srand:n; srand( (unsigned)time( NULL ) ); for( i =
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
评论
0/150
提交评论