#include <bits/stdc++.h>
using namespace std;
int dayNumber(int day, int month, int year)
{
int t[] = {0, 3, 2, 5, 0, 3, 5, 1,
4, 6, 2, 4};
year -= month < 3;
return (year + year / 4 - year / 100 +
year / 400 + t[month - 1] + day) %
7;
}
string monthsName(int monthIndex)
{
string months[] = {"January", "February", "March",
"April", "May", "June",
"July", "August", "September",
"October", "November", "December"};
return (months[monthIndex]);
}
int dayNumber(int monthIndex, int year)
{
if (monthIndex == 0)
return (31);
if (monthIndex == 1)
{
if (year % 400 == 0 ||
(year % 4 == 0 && year % 100 != 0))
return (29);
else
return (28);
}
if (monthIndex == 2)
return (31);
if (monthIndex == 3)
return (30);
if (monthIndex == 4)
return (31);
if (monthIndex == 5)
return (30);
if (monthIndex == 6)
return (31);
if (monthIndex == 7)
return (31);
if (monthIndex == 8)
return (30);
if (monthIndex == 9)
return (31);
if (monthIndex == 10)
return (30);
if (monthIndex == 11)
return (31);
}
void getCalendar(int year)
{
printf(" Calendar - %d\n\n", year);
int days;
int current = dayNumber(1, 1, year);
for (int i = 0; i < 12; i++)
{
days = dayNumber(i, year);
printf("\n ------------%s-------------\n",
monthsName(i).c_str());
printf(" Sun Mon Tue Wed Thu Fri Sat\n");
int k;
for (k = 0; k < current; k++)
printf(" ");
for (int j = 1; j <= days; j++)
{
printf("%5d", j);
if (++k > 6)
{
k = 0;
printf("\n");
}
}
if (k)
printf("\n");
current = k;
}
return;
}
int main()
{
int year = 2021;
getCalendar(year);
return (0);
}
Comments
Leave a comment