谭浩强C程序设计(第三版)习题11.1
题目:定义一个结构体变量(包括年,月,日)。计算改日在本年中是第几天?注意闰年问题。
我写的代码:
#include<stdio.h>
struct date
{
int year;
int month;
int day;
};
int count(int Y,int M,int D)
{
int k;
k=D;
switch (M)
{
case 12:k=k+30;
case 11:k=k+31;
case 10:k=k+30;
case 9:k=k+31;
case 8:k=k+31;
case 7:k=k+30;
case 6:k=k+31;
case 5:k=k+30;
case 4:k=k+31;
case 3:{
if((Y%4==0)&&(Y%100==0))k=k+29;
else k=k+28;
}
case 2:k=k+31;
case 1:k=k+0;break;
default:printf("输入的日期有误!\n");
break;
}
return(k);
}
void main()
{
int count(int Y,int M,int D);
int n;
struct date birthday;
scanf("%d%d%d",&birthday.year,&birthday.month,&birthday.day);
n=count(birthday.year,birthday.month,birthday.day);
printf("该日期为本年第%d天\n",n);
getch();
}