资源描述:
《实验六 函数与编译预处理》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、实验六函数与编译预处理1.voidmain(){floata,b,c;scanf("%f%f",&a,&b);c=add(a,b);printf("sumis%f",c);}floatadd(floatx,floaty){floatz;z=x+y;return(z);}//错误的地方,(1)没有包含头文件(2)函数定义应该在函数调用的前面.更正后的程序:#includefloatadd(floatx,floaty){floatz;z=x+y;return(z);}voidmain(){floata,b,c;sc
2、anf("%f%f",&a,&b);c=add(a,b);printf("sumis%f",c);}2.voidmain(){inta=3,b=6;printf("a=%d,b=%d",a,b);exchange1(a,b);printf(:a=%d,b=%d",a,b);}voidexchange1(intx,inty){intt;t=x;x=y;y=t;printf("x=%d,y=%d",x,y);}//错误的地方,(1)没有包含头文件(2)函数定义应该在函数调用的前面.更正后的程序:#include3、o.h>voidexchange1(intx,inty){intt;t=x;x=y;y=t;printf("x=%d,y=%d",x,y);}voidmain(){inta=3,b=6;printf("a=%d,b=%d",a,b);exchange1(a,b);printf("a=%d,b=%d",a,b);}3.longintfac(intn){longintp;inti;p=1;for(i=1;i<=n;i++)p=p*i;return(p);}intcmn(intm,intn){intx;x=fac(m)/(fac(n)*fac(
4、m-n));return(x);}voidmain(){intm,n,c;scanf("%d%d",&m,&n);c=cmn(m,n);printf("c=%d",c);}//应该加个#include头文件4.inta=7,b=9;voidmain(){inti,a=5,x=80,y=60;for(i=1;i<4;i++){printf("a=%d,b=5d,max=%d",a,b,max(a,b));b+=6;}printf("result_sum=%d",sum(x,y));}intsum(intx,inty){ex
5、ternintm,n;inttemp;temp=x+y+m+n+a+b;return(temp);}intm=12,n=25;intmax(int1,intb){return(a>b?a:b);}程序更正:#includeinta=7,b=9;voidmain(){inti,a=5,x=80,y=60;intmax(inta,intb);intsum(intx,inty);for(i=1;i<4;i++){printf("a=%d,b=5d,max=%d",a,b,max(a,b));b+=6;}printf("result_s
6、um=%d",sum(x,y));}intsum(intx,inty){externintm,n;inttemp;temp=x+y+m+n+a+b;return(temp);}intm=12,n=25;intmax(inta,intb){return(a>b?a:b);}5.调试运行以下程序.#includeintfun(inta){intb=0;staticintc=2;b+=c;c+=1;return(a+b-c);}voidmain(){inta=3,i;for(i=0;i<3;i++)printf("%4d",fun(a
7、));}6.略7.略8.#include#defineDEBUGvoidmain(){inta=14,b=15,temp;temp=a/b;#ifdefDEBUGprintf("a=%d,b=%d",a,b);#endifprintf("temp=%d",temp);}程序结果为:a=14,b=15,temp=0(1)第二行中,宏定义是可以没有具体的值的,在这里只是定义了一个宏名.DEBUG没有值.(2)第7~9行为:#ifdefDEBUGprintf("a=%d,b=%d",a,b);#endif会被编译,因为种种原因DEBU
8、G已经被定义过.(3)如将第2行删除,程序运行结果为:temp=0因为没有定义DEBUG,所以原来第7~9行