资源描述:
《C程序设计课后答案.doc》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、第九章预处理命令9-1#include#defineexchange(x,y)t=x;x=y;y=tvoidmain(){intx,y,t;printf("输入x,y");scanf("%d%d",&x,&y);exchange(x,y);printf("交换后x=%d,y=%d",x,y);}9-2#include#defineacr(x,y)((x)%(y))voidmain(){intx,y;printf("输入x,y");scanf("%d%d",&x,&y);pri
2、ntf("余数为%d",acr(x,y));}9-3#include#include#defines(a,b,c)((a+b+c)/2)#definearea(a,b,c,s)sqrt(s*(s-a)*(s-b)*(s-c))voidmain(){floata,b,c,s,area;charm;intflag=1;while(flag==1){printf("输入a,b,c");scanf("%f%f%f",&a,&b,&c);if(a>0&&b>0&&c>0&&(a+b>c
3、
4、a+c>
5、b
6、
7、b+c>a)&&(fabs(a-b)8、
9、m=='y')flag=1;elseflag=0;}}}9-4#include#defineLEAP_YEAR(y)((y%4==0)
10、&&(y%100)!=0
11、
12、(y%400)==0)voidmain(){inty;printf("输入年份");scanf("%d",&y);if(LEAP_YEAR(y))printf("%d年是闰年",y);elseprintf("%d年不是闰年",y);}9.5解:展开后:printf("&#118alue=%formatt",x);printf("&#118alue=%formatt",x);putchar('');printf("&#118alue=%formatt");printf("
13、&#118alue=%formatt",x2);putchar('');输出结果:&#118alue=5.ormat&#118alue=5.ormat&#118alue=3.ormat&#118alue=8.ormat9-6#include#definePRprintf#defineNL""#defineD"%f"#defineD1DNL#defineD2DDNL#defineD3DDDNLvoidmain(){floatx=1;PR(D1,x);PR(D2,x,x);PR(D3,x,x,x);}9.7#incl
14、ude#include"format.h"voidmain(){intx=1;floaty=2;charz[]="Iloveyou";PR(D1,x);PR(F1,y);PR(S1,z);}9-8#includevoidmain(){floatx,y,z,t;printf("输入三个数x,y,z");scanf("%f%f%f",&x,&y,&z);t=(x>y)?x:y;t=(t>z)?t:z;printf("三个数中最大的为:%f",t);}#include#
15、defineMAX(a,b)((a)>(b)?(a):(b))voidmain(){floatx,y,z;printf("输入三个数x,y,z:");scanf("%f%f%f",&x,&y,&z);printf("其中最大的数为:%f",MAX(MAX(x,y),z));}9-10#include#defineN80#defineCHANGE1voidmain(){charstr[N];inti;printf("输入一行字符串");gets(str);#ifCHANGEfor(i=0;i16、&((str[i])!=' ');i++){if(((str[i]>='a')&&(str[i]<='z'))
17、
18、((str[i]>=