-
Notifications
You must be signed in to change notification settings - Fork 67
/
ex_5-8.c
66 lines (53 loc) · 1.46 KB
/
ex_5-8.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <stdio.h>
static char daytab[2][13] =
{
{ 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
{ 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
};
/* set day of year based on month and day */
int day_of_year(int year, int month, int day)
{
int i, leap;
if(year < 1752 || month > 12 || month < 1 || day < 1) { return -1; }
leap = year % 4 == 0 && year % 100 != 0 || year % 400 == 0;
if(day > daytab[leap][month]) { return -1; } // day month pair OK?
for( i = 1; i < month; i++)
day += daytab[leap][i];
return day;
}
/* get month, day from day of year */
int month_day(int year, int yearday, int *pmonth, int *pday)
{
int i, leap;
if(year < 1752) { return -1; }
leap = year % 4 == 0 && year % 100 != 0 || year % 400 == 0;
if((leap && yearday > 366) || (!leap && yearday > 365)) { return -1; }
for(i = 1; yearday > daytab[leap][i]; i++)
yearday -= daytab[leap][i];
*pmonth = i;
*pday = yearday;
return 1;
}
int main()
{
int ans, month, day;
ans = day_of_year(2009, 2, 1);
printf("%d\n", ans);
ans = month_day(2009, 34, &month, &day);
if(ans)
printf("Day: %d\tMonth:%d\n", day, month);
int yearday, year = 2009;
int leap = year % 4 == 0 && year % 100 != 0 || year % 400 == 0;
for(month = 1; month <= 12; ++month)
{
printf("\n");
day = 0;
while(++day <= daytab[leap][month])
{
yearday = day_of_year(year, month, day);
printf("Y: %d\tM: %d\tD: %d\t->\tYD:%d\n", year, month, day, yearday);
}
printf("\n");
}
return 1;
}