Anonymous user
Talk:Last Friday of each month: Difference between revisions
→C version: Copy the program from an old revision of Last Fridays by year.
(→C version: Copy the program from an old revision of Last Fridays by year.) |
|||
Line 6:
== C version ==
<blockquote>''This discussion is about the program shown here:''
<lang c>#define _XOPEN_SOURCE
#include <stdio.h>
#include <time.h>
int main(int c, char *v[])
{
int days[] = {31,29,31,30,31,30,31,31,30,31,30,31};
int m, y, w;
struct tm tm;
char buf[32];
if (c < 2 || !sscanf(v[1], "%d", &y)) return 1;
days[1] -= y % 4 || (y % 100 && ! (y % 400));
sprintf(buf, "%d-1-1", y);
strptime(buf, "%Y-%m-%d", &tm);
w = tm.tm_wday - 1; /* day of week for zeroth of Jan */
for(m = 0; m < 12; m++) {
w = (w + days[m]) % 7;
printf("%d-%02d-%d\n", y, m + 1,
days[m] + (w < 5 ? -2 : 5) - w);
}
return 0;
}</lang></blockquote>
Building the C version under cygwin (gcc 3.4.4), I get:
|