#include <stdio.h>
#include <stdlib.h>
int leap_year(int year);
void show_calen(int day, int mon, int year, int week);
int week_df(int day, int mon, int year);
void show_year(int year);
int mon_d[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int mon_d_l[12] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int main(int argc, char **argv)
{
int year = 0;
int mon = 0;
//int mon_d[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
//int mon_d_l[12] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int day = 0;
int week;
if(1 == argc || 3 == argc || 4 < argc)
{
printf("reage: ./a.out year or ./a.out day mon year\n");
goto err0;
}
if(0 != atoi(argv[3]))
{
year = atoi(argv[3]);
mon = atoi(argv[2]);
day = atoi(argv[1]);
week = week_df(day, mon, year);
show_calen(day, mon, year, week);
}
else
{
year = atoi(argv[1]);
show_year(year);
}
/*else if(0 != atoi(argv[2]))
{
day = atoi(argv[2]);
mon = atoi(argv[1]);
}
else
{
year = atoi(argv[1]);
}*/
/*if(NULL != argv[2])
mon = atoi(argv[2]);
if(atoi(argv[1]) <= 31)
{
day = atoi(argv[1]);
}
else
{
printf("day 1-31");
goto err0;
}*/
return 0;
err0:
return 1;
}
int leap_year(int year)
{
if((0 == year % 4 && 0 != year % 100) || 0 == year % 400)
{
return 1;
}
return 0;
}
int week_df(int day, int mon, int year)
{
int i;
int leap_count = 0;
int year_cose;
int mon_cose = 0;
int day_cose;
int week;
for(i = 1900; i < year; i++)
{
if(leap_year(i))
{
leap_count++;
}
}
//printf("%d\n", leap_count);
year_cose = year - 1900;
if(leap_year(year))
{
for(i = 0; i < mon - 1; i++)
{
mon_cose += mon_d_l[i];
}
}
else
{
for(i = 0; i < mon - 1; i++)
{
mon_cose += mon_d[i];
}
}
day_cose = 365 * year_cose + leap_count + mon_cose + 1;
//printf("%d\n", day_cose);
week = day_cose % 7;
return week;
}
void show_calen(int day, int mon, int year, int week)
{
int i, j;
printf("\033[%dC%d年%d月%d日\n", 4, year, mon, day);
printf("日 一 二 三 四 五 六\n");
for(j = 0; j < week; j++)
{
printf(" ");
}
if(leap_year(year))
{
for(i = 1; i <= mon_d_l[mon - 1]; i++, j++)
{
if(j % 7 == 0)
putchar('\n');
if(i == day)
{
printf("\033[30;47m%2d\033[0m ", i);
}
else
{
printf("%2d ", i);
}
}
}
else
{
for(i = 1; i <= mon_d[mon - 1]; i++, j++)
{
if(j % 7 == 0)
putchar('\n');
if(i == day)
{
printf("\033[30;47m%2d\033[0m ", i);
}
else
{
printf("%2d ", i);
}
}
}
putchar('\n');
}
void show_year(int year)
{
int week;
int i, j, z;
int l = 1;
int l_start, l_end;
int w = 1;
system("clear");
for(z = 1; z <= 12; z++)
{
week = week_df(1, z, year);
l_start = l;
printf("\033[%d;%dH", l, w);
printf("\033[%dC%d年%d月\n", 6, year, z);
printf("日 一 二 三 四 五 六\n");
printf("\033[%d;%dH", ++l, w);
for(j = 0; j < week; j++)
{
printf(" ");
}
if(leap_year(year))
{
for(i = 1; i <= mon_d_l[z - 1]; i++, j++)
{
if(j % 7 == 0 && j != 0)
{
l++;
//putchar('\n');
printf("\033[%d;%dH", l, w);
}
printf("%2d ", i);
}
}
else
{
for(i = 1; i <= mon_d[z - 1]; i++, j++)
{
if(j % 7 == 0 && j != 0)
{
l++;
printf("\033[%d;%dH", l, w);
}
printf("%2d ", i);
}
}
putchar('\n');
l++;
if(l > l_end)
l_end = l;
if(z % 3 == 0)
{
w = 1;
l = l_end;
}
else
{
w = w + 25;
l = l_start;
}
}
}