#include <ctime>
#include <cstdio>
const char *months[]={
"\nJanuary","\nFebruary","\nMarch","\nApril","\nMay","\nJune","\nJuly","\nAugust","\nSeptember","\nOctober","\nNovember","\nDecember"
};
int month_days[] = {31, 28, 31, 30, 31, 30, 31 ,31 ,30, 31, 30, 31};
int first_day_year(int year)
{
int first_day;
int x;
int y;
int z;
x = (year - 1.)/4.0;
y = (year - 1.)/100.;
z = (year - 1.)/400.;
first_day = (year + x - y + z) %7;
return first_day;
}
int leapyear(int year)
{
if((year% 4 == 0 && year%100 != 0) || year%400 == 0){
month_days[1] = 29; //agora com mês posição 1, que é o segundo
return 1;
}
return 0;
}
int calendar(int month, int year, int first_day)
{
int i;
month--;//agora reduz o próprio month, pois em todos os locais que é
//utilizado daqui para a frente tem de ter 1 a menos
printf("%s %d\n\n", months[month], year); //sem -1 aqui, pois já foi reduzido acima
printf("Sun Mon Tue Wed Thu Fri Sat\n");
//volta ao i=0 que era o que tinha. month também tem agora -1 que veio de cima
for(i = 0; i < month; i++)
{
first_day = ((first_day + month_days[i])%7);
}
for(i = 1; i <= 1 + first_day * 5; i++)
{
printf(" ");
}
for(i = 1; i <= month_days[month]; i++)
{
printf("%2d", i);
if((i + first_day)%7 > 0)
printf(" ");
else
printf("\n ");
}
}
int main(void)
{
int year;
int month;
int first_day;
do{
printf("Enter a month (1 - 12): ");
scanf("%d", &month);
} while (month < 1 || month > 12);
do{
printf("Enter a year (1000 - 3000): ");
scanf("%d", &year);
} while (year < 1000 || year > 3000);
first_day = first_day_year(year);
leapyear(year);
calendar(month, year, first_day);
printf("\n");
return 0;
}
I2luY2x1ZGUgPGN0aW1lPgojaW5jbHVkZSA8Y3N0ZGlvPgogCmNvbnN0IGNoYXIgKm1vbnRoc1tdPXsKICAgICAgICAgICAgICAgICAgICAgICJcbkphbnVhcnkiLCJcbkZlYnJ1YXJ5IiwiXG5NYXJjaCIsIlxuQXByaWwiLCJcbk1heSIsIlxuSnVuZSIsIlxuSnVseSIsIlxuQXVndXN0Ii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xdWksIHBvaXMgasOhIGZvaSByZWR1emlkbyBhY2ltYQogICAgcHJpbnRmKCJTdW4gIE1vbiAgVHVlICBXZWQgIFRodSAgRnJpICBTYXRcbiIpOwoKICAgIC8vdm9sdGEgYW8gaT0wIHF1ZSBlcmEgbyBxdWUgdGluaGEuIG1vbnRoIHRhbWLDqW0gdGVtIGFnb3JhIC0xIHF1ZSB2ZWlvIGRlIGNpbWEKICAgIGZvcihpID0gMDsgaSA8IG1vbnRoOyBpKyspIAogICAgewogICAgICAgIGZpcnN0X2RheSA9ICgoZmlyc3RfZGF5ICsgbW9udGhfZGF5c1tpXSklNyk7IAogICAgfQogCiAgICBmb3IoaSA9IDE7IGkgPD0gMSArIGZpcnN0X2RheSAqIDU7IGkrKykKICAgIHsKICAgICAgICBwcmludGYoIiAiKTsKICAgIH0KIAogICAgZm9yKGkgPSAxOyBpIDw9IG1vbnRoX2RheXNbbW9udGhdOyBpKyspCiAgICB7CiAgICAgICAgcHJpbnRmKCIlMmQiLCBpKTsKIAogICAgICAgIGlmKChpICsgZmlyc3RfZGF5KSU3ID4gMCkKICAgICAgICAgICAgcHJpbnRmKCIgICAiKTsKICAgICAgICBlbHNlCiAgICAgICAgICAgIHByaW50ZigiXG4gIik7CiAgICB9Cn0KIAppbnQgbWFpbih2b2lkKQp7CiAgICBpbnQgeWVhcjsKICAgIGludCBtb250aDsKICAgIGludCBmaXJzdF9kYXk7CiAKICAgIGRvewogICAgICAgIHByaW50ZigiRW50ZXIgYSBtb250aCAoMSAtIDEyKTogIik7CiAgICAgICAgc2NhbmYoIiVkIiwgJm1vbnRoKTsKICAgIH0gd2hpbGUgKG1vbnRoIDwgMSB8fCBtb250aCA+IDEyKTsKIAogICAgZG97CiAgICAgICAgcHJpbnRmKCJFbnRlciBhIHllYXIgKDEwMDAgLSAzMDAwKTogIik7CiAgICAgICAgc2NhbmYoIiVkIiwgJnllYXIpOwogICAgfSB3aGlsZSAoeWVhciA8IDEwMDAgfHwgeWVhciA+IDMwMDApOwogCiAgICBmaXJzdF9kYXkgPSBmaXJzdF9kYXlfeWVhcih5ZWFyKTsKIAogICAgbGVhcHllYXIoeWVhcik7CiAKICAgIGNhbGVuZGFyKG1vbnRoLCB5ZWFyLCBmaXJzdF9kYXkpOwogCiAgICBwcmludGYoIlxuIik7CiAKICAgIHJldHVybiAwOyAKfQ==