资源描述:
《c语言程序设计-向艳-书上例题源代码.doc》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、第五章函数1.定义一个求两个整数和的函数intsum(x,y)intx,y;{intz;z=x+y;return(z);}2.编写函数求两个数的最大值#includefloatmax(floatx,floaty){floatz;if(x>y)z=x;elsez=y;return(z);}voidmain(){floata,b,c;scanf("%f%f",&a,&b);c=max(a,b);printf("max=&f",c);}3.计算并输出一个圆台两底面积之和#includefloatarea(floatx,f
2、loaty){floats;s=3.1415*(x*x+y*y);returns;}voidprintstar(){inti;for(i=0;i<30;i++)printf("*");printf("");}voidmain(){floatr1,r2,s;printstar();scanf("%f,%f",&r1,&r2);s=area(r1,r2);printf("s=%.2f",s);printstar();}1.实参求值顺序的例子#includeintfun(inta,intb){if(a>b)return1;elseif
3、(a==b)return0;elsereturn-1;}voidmain(){intk=3,s;s=fun(k,++k);printf("s=%d",s);}2.计算Σni=1i#includevoidmain(){voids(int);intn;printf("inputnumber");scanf("%d",&n);s(n);printf("n=%d",n);}voids(intn){inti;for(i=n-1;i>=1;i--)n=n+i;printf("n=%d",n);}3.全局变量被“屏蔽”#include
4、inta=3,b=5;max(inta,intb){intc;c=a>b?a:b;return(c);}voidmain(){inta=8;printf("%d",max(a,b));}1.输入正方体的长宽高,求体积及三个面的面积#includeints1,s2,s3;intv_s(inta,intb,intc){intv;v=a*b*c;s1=a*b;s2=b*c;s3=a*c;returnv;}voidmain(){intv,l,w,h;printf("inputlength,widthandheight
5、n");scanf("%d%d%d",&l,&w,&h);v=v_s(l,w,h);printf("v=%d,s1=%d,s2=%d,s3=%d",v,s1,s2,s3);}2.用register说明变量的程序#includeintfac(intn){registerinti,f=1;for(i=1;i<=n;i++);f=f*i;returnf;}voidmain(){inti;for(i=1;i<=5;i++)printf("%ds!=%d",i,fac(i));}1.用static说明变量的程序#include6、io.h>intfac(intn){staticinti,f=1;f=f*n;returnf;}voidmain(){inti;for(i=1;i<=5;i++)printf("%d!=%d",i,fac(i));}2.用extern说明变量的程序#includeexternintn;voidfun(){n-=20;}intn=100;voidmain(){for(;n>=60;){fun();printf("n=%d",n);}}3.计算s=2^2!+3^3!#includelongf1(intp){intk
7、;longr;longf2(int);k=p*p;r=f2(k);returnr;}longf2(intq){longc=1;inti;for(i=1;i<=q;i++)c=c*i;returnc;}voidmain(){inti;longs=0;for(i=2;i<=3;i++)s=s+f1(i);printf("s=%ld",s);}1.用梯形法编程求函数f(x)=x*x+2*x+1的定积分S0~2f(x)dx的值#includedoublefun(doublex){doubley;y=x*x+2*x+1;returny;}
8、doubledjf(doublen,doublea,doubleb){doubl