//给出年、月、日计算该日是该年的第几天
#include <stdio.h>
#include <stdlib.h>
int sum_day(int month, int day);
int leap(int year);
int main(int argc, char *argv[])
{
int year, month, day;
int days;
printf("请输入日期(年,月,日):");
scanf("%d, %d, %d", &year, &month, &day);
printf("%d年%d月%d日", year, month, day);
days = sum_day(month, day);
if(leap(year) && month >= 3)
days = days + 1;
printf("是该年的第%d天。\n",days);
system("pause");
return 0;
}
static int day_tab[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int sum_day(int month, int day)
{
int i;
for(i = 1; i < month; i++)
day = day + day_tab[i];
return day;
}
int leap(int year)
{
int leap;
leap = (year%4 == 0&&year%100 != 0)||(year%400 == 0);
return leap;
}
|