#include <ctime>
#include <cstdio>
const char *months[]={
"\nJanuary","\nFebruary","\nMarch","\nApril","\nMay","\nJune","\nJuly","\nAugust","\nSeptember","\nOctober","\nNovember","\nDecember"
};
int month_days[] = {0, 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[2] = 29; //agora [2] está certo devido ao primeiro ponto
return 1;
}
//else é desnecessário basta fazer o retorno diretamente
return 0;
}
int calendar(int month, int year, int first_day)
{
int i;
printf("%s %d\n\n", months[month-1], year); //month-1 para imprimir o correto
printf("Sun Mon Tue Wed Thu Fri Sat\n");
for(i = 1; i < month; i++) //agora começa em 1
{
//+month_days[i] e não +month_days[month] que era o problema maior
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;
}
I2luY2x1ZGUgPGN0aW1lPgojaW5jbHVkZSA8Y3N0ZGlvPgoKY29uc3QgY2hhciAqbW9udGhzW109ewogICAgICAgICAgICAgICAgICAgICAgIlxuSmFudWFyeSIsIlxuRmVicnVhcnkiLCJcbk1hcmNoIiwiXG5BcHJpbCIsIlxuTWF5IiwiXG5KdW5lIiwiXG5KdWx5IiwiXG5BdWd1c3QiLCJcblNlcHRlbWJlciIsIlxuT2N0b2JlciIs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gbW9udGg7IGkrKykgLy9hZ29yYSBjb21lw6dhIGVtIDEKICAgIHsKICAgICAgICAvLyttb250aF9kYXlzW2ldIGUgbsOjbyArbW9udGhfZGF5c1ttb250aF0gcXVlIGVyYSBvIHByb2JsZW1hIG1haW9yIAogICAgICAgIGZpcnN0X2RheSA9ICgoZmlyc3RfZGF5ICsgbW9udGhfZGF5c1tpXSklNyk7IAogICAgfQogICAgCiAgICBmb3IoaSA9IDE7IGkgPD0gMSArIGZpcnN0X2RheSAqIDU7IGkrKykKICAgIHsKICAgICAgICBwcmludGYoIiAiKTsKICAgIH0KCiAgICBmb3IoaSA9IDE7IGkgPD0gbW9udGhfZGF5c1ttb250aF07IGkrKykKICAgIHsKICAgICAgICBwcmludGYoIiUyZCIsIGkpOwoKICAgICAgICBpZigoaSArIGZpcnN0X2RheSklNyA+IDApCiAgICAgICAgICAgIHByaW50ZigiICAgIik7CiAgICAgICAgZWxzZQogICAgICAgICAgICBwcmludGYoIlxuICIpOwogICAgfQp9CgppbnQgbWFpbih2b2lkKQp7CiAgICBpbnQgeWVhcjsKICAgIGludCBtb250aDsKICAgIGludCBmaXJzdF9kYXk7CgogICAgZG97CiAgICAgICAgcHJpbnRmKCJFbnRlciBhIG1vbnRoICgxIC0gMTIpOiAiKTsKICAgICAgICBzY2FuZigiJWQiLCAmbW9udGgpOwogICAgfSB3aGlsZSAobW9udGggPCAxIHx8IG1vbnRoID4gMTIpOwoKICAgIGRvewogICAgICAgIHByaW50ZigiRW50ZXIgYSB5ZWFyICgxMDAwIC0gMzAwMCk6ICIpOwogICAgICAgIHNjYW5mKCIlZCIsICZ5ZWFyKTsKICAgIH0gd2hpbGUgKHllYXIgPCAxMDAwIHx8IHllYXIgPiAzMDAwKTsKCiAgICBmaXJzdF9kYXkgPSBmaXJzdF9kYXlfeWVhcih5ZWFyKTsKCiAgICBsZWFweWVhcih5ZWFyKTsKCiAgICBjYWxlbmRhcihtb250aCwgeWVhciwgZmlyc3RfZGF5KTsKCiAgICBwcmludGYoIlxuIik7CgogICAgcmV0dXJuIDA7IAp9