您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 质量控制/管理 > C程序设计-课后答案09
第九章预处理命令9-1#includeiostream#defineexchange(x,y)t=x;x=y;y=tvoidmain(){intx,y,t;printf(输入x,y\n\n);scanf(%d%d,&x,&y);exchange(x,y);printf(\n\n交换后x=%d,y=%d\n\n,x,y);}9-2#includeiostream#defineacr(x,y)((x)%(y))voidmain(){intx,y;printf(输入x,y\n\n);scanf(%d%d,&x,&y);printf(\n\n余数为%d\n\n,acr(x,y));}9-3#includeiostream#includemath.h#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\n\n);scanf(%f%f%f,&a,&b,&c);if(a0&&b0&&c0&&(a+bc||a+cb||b+ca)&&(fabs(a-b)c&&fabs(a-c)b&&fabs(b-c)a)){s=s(a,b,c);area=area(a,b,c,s);flag=0;printf(\n\n面积area=%8f\n\n,area);}else{printf(\n\n输入有误!!!是否重新输入?Y/N?\n\n);getchar();m=getchar();if(m=='Y'||m=='y')flag=1;elseflag=0;}}}9-4#includeiostream#defineLEAP_YEAR(y)((y%4==0)&&(y%100)!=0||(y%400)==0)voidmain(){inty;printf(输入年份\n\n);scanf(%d,&y);if(LEAP_YEAR(y))printf(\n\n%d年是闰年\n\n,y);elseprintf(\n\n%d年不是闰年\n\n,y);}9.5解:展开后:printf(&#118alue=%format\t,x);printf(&#118alue=%format\t,x);putchar('\n');printf(&#118alue=%format\t);printf(&#118alue=%format\t,x2);putchar('\n');输出结果:&#118alue=5.000000ormat&#118alue=5.000000ormat&#118alue=3.000000ormat&#118alue=8.000000ormat9-6#includeiostream#definePRprintf#defineNL\n\n#defineD%f#defineD1DNL#defineD2DDNL#defineD3DDDNLvoidmain(){floatx=1;PR(D1,x);PR(D2,x,x);PR(D3,x,x,x);}9.7#includeiostream#includeformat.hvoidmain(){intx=1;floaty=2;charz[]=Iloveyou;PR(D1,x);PR(F1,y);PR(S1,z);}9-8#includeiostreamvoidmain(){floatx,y,z,t;printf(输入三个数x,y,z\n\n);scanf(%f%f%f,&x,&y,&z);t=(xy)?x:y;t=(tz)?t:z;printf(\n\n三个数中最大的为:%f\n\n,t);}#includeiostream#defineMAX(a,b)((a)(b)?(a):(b))voidmain(){floatx,y,z;printf(输入三个数x,y,z:\n\n);scanf(%f%f%f,&x,&y,&z);printf(\n\n其中最大的数为:%f\n\n,MAX(MAX(x,y),z));}9-10#includeiostream#defineN80#defineCHANGE1voidmain(){charstr[N];inti;printf(输入一行字符串\n\n);gets(str);#ifCHANGEfor(i=0;iN&&((str[i])!='\0');i++){if(((str[i]='a')&&(str[i]='z'))||((str[i]='A')&&(str[i]='Z'))){if((str[i]=='z')||(str[i]=='Z'))str[i]=str[i]-25;elsestr[i]=str[i]+1;}}str[i]='\0';puts(str);#elseputs(str);#endif}
本文标题:C程序设计-课后答案09
链接地址:https://www.777doc.com/doc-5236362 .html