文档库 最新最全的文档下载
当前位置:文档库 › C语言程序设计(第3版)第3章习题参考答案

C语言程序设计(第3版)第3章习题参考答案

C语言程序设计(第3版)第3章习题参考答案
C语言程序设计(第3版)第3章习题参考答案

习题三参考答案

(1)从键盘输入一个年份值,判断是否闰年。设iYear为某一年份,iYear为闰年的条件为:iYear可以被4整除且不可以被100整除,或者iYear可以被400整除。

#include "Stdio.h"

#include "Conio.h"

int main(void)

{

int iYear;

printf("please input the year:");

scanf("%d",&iYear);

if(iYear%400==0||(iYear%4==0&&iYear%100!=0))

printf("%d is leap",iYear);

else

printf("%d is not leap",iYear);

getch();

return 0;

}

(2)从键盘输入三个整数,按由小到大的顺序输出。

#include "stdio.h"

main()

{int i,j,k,max;

scanf("%d%d%d",&i,&j,&k);

max=i>j?i:j;

max=max>k?max:k;

printf("max=%d",max);

getch();

}

(3)假设星期一至星期五每工作一小时的工资是20元,星期六和星期日每工作一小时的工资是平时的3倍,其中工资的4.5%是税金。试编一程序从键盘输入星期序号(1,2,3,4,5,6,7,分别表示星期一至星期天)和工作小时数,计算该日的工资及应交税金。

#include "Stdio.h"

#include "Conio.h"

int main(void)

{

int iWeek,iHours ;

float fSalary,fTaxes;

printf("please input the week number(1-7):");

scanf("%d",&iWeek);

printf("please input the work hours(1-12):");

scanf("%d",&iHours);

switch(iWeek){

case 1:

case 2:

case 3:

case 4:

case 5:

fSalary=20*iHours;

fTaxes=fSalary*0.045;

break;

case 6:

case 7:

fSalary=3*20*iHours;

fTaxes=fSalary*0.045;

break;

}

printf("the salary is %f ,the taxes is %f",fSalary,fTaxes);

getch();

return 0;

}

(4)从键盘输入三角形的三条边长,判断是否构成三角形,如能则求出三角形的周长和面积并输出;如不能,输出不能构成三角形的信息。构成三角形的条件为:三角形任意两边的和大于第三边时,构成三角形。

面积计算公式为:fArea

其中,f1,f2,f3是三角形的三条边长,fTemp=(f1+f2+f3)/2。计算一个数的平方根可用函数float sqrt(float f),该函数是数学库函数,需要在程序的开头加上#include

#include "Conio.h"

#include "math.h"

int main(void)

{

float f1,f2,f3,fTemp,fC,fArea;

printf("please input triangular three sides:");

scanf("%f,%f,%f",&f1,&f2,&f3);

if(f1+f2>f3&&f2+f3>f1&&f1+f3>f2){

fTemp=(f1+f2+f3)/2;

fArea=sqrt(fTemp*(fTemp-f1)*(fTemp-f2)*(fTemp-f3));

fC=2*fTemp;

printf("The triangle area is %f\n" ,fArea);

printf("The circumference of the triangle is %f ",fC);

}else

printf("Don't make the triangle");

getch();

return 0;

}

(5)iX2+iY2=16是平面上的一个圆,编一程序判断点(2,4)是在圆内?圆外?还是圆上?#include "Conio.h"

#include "math.h"

int main(void)

{

int ix,iy;

printf("please input a point coordinates :");

scanf("%d,%d",&ix,&iy );

if(ix*ix+iy*iy<16)

printf("The point is in the circle inside\n" );

else if(ix*ix+iy*iy>16)

printf("The point is in the circle of the external\n");

else

printf("The point is in the circle round\n");

getch();

return 0;

}

(6) 输入一个5位正整数,判断它是不是回文数。所谓回文数是指12321、23732…这样的数。

#include "stdio.h"

main()

{long i,i0,i1,i2,i3,i4 ; /*i0个位,i1十位,i2百位,i3千位,i4万位*/

printf("please input a integer:");

scanf("%ld",&i);

i0=i%10;

i1=i%100/10;

i2=i%1000/100;

i3=i/1000%10;

i4=i/10000;

if(i0==i4&&i1==i3)

printf("%ld is huiwen",i);

else

printf("%ld is not huiwen",i);

getch();

}

(7) 输入一个日期的年、月、日,计算并输出这天是该年的第几天。比如:2011年1月

31日,是该年的第31天。

#include "stdio.h"

main()

{

int year,month,day,days; /*年,月,日,该年第几天*/

printf("please input the year month day:");

scanf("%d%d%d",&year,&month,&day);

days=0;

switch(month){

case 1:

days=day; break;

case 2:

days=day+31; break;

case 3:

days=day+31+28;break;

case 4:

days=day+31+28+31;break;

case 5:

days=day+31+28+31+30;break;

case 6:

days=day+31+28+31+30+31;break;

case 7:

days=day+31+28+31+30+31+30;break;

case 8:

days=day+31+28+31+30+31+30+31;break;

case 9:

days=day+31+28+31+30+31+30+31+31;break;

case 10:

days=day+31+28+31+30+31+30+31+31+30;break;

case 11:

days=day+31+28+31+30+31+30+31+31+30+31;break;

case 12:

days=day+31+28+31+30+31+30+31+31+30+31+30;break;

}

if((month>=3&&month<=12)&&(year%4==0&&year%100!=0)||year%400==0) days+=1;

printf("days=%d",days);

getch();

}

相关文档