1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
|
#include <iostream>
#include <map>
#include <iomanip>
using namespace std;
map<string, int> months{{"January", 1}, {"February", 2}, {"March", 3}, {"April", 4}, {"May", 5}, {"June", 6}, {"July", 7},
{"August", 8}, {"September", 9}, {"Ocotber", 10}, {"November", 11}, {"December", 12}};
void skip (int i)
{
while (i > 0)
{
cout << " ";
i = i - 1;
}
}
void skipToDay (int d)
{
return skip(3*d);
}
void string_pad(const string& DaysOfWeek, int padding)
{
int s = DaysOfWeek.size();
int pad1 = (padding - s)/2;
int pad2 = pad1;
for (int f = 0; f < pad1; f++)
{
cout << " ";
++f;
}
cout << DaysOfWeek;
for (int e = 0; e < pad2; e++)
{
cout << " ";
++e;
}
cout<<"\n";
}
struct DaysOf
{
string m_month;
int m_year;
DaysOf(string month, int year) : m_month(month), m_year(year) {}
int firstDay ()//Sakamoto algorithm;
{
static int t[] {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
auto iter = months.find(m_month);
m_year -= iter->second < 3;//this statement prevents method from being declared const;
return (m_year + m_year/4 - m_year/100 + m_year/400 + t[iter->second-1] + 1) % 7;
}
int daysMonth ()const
{
auto iter = months.find(m_month);
int numberOfDays;
if (iter->second == 4 || iter->second == 6 || iter->second == 9 || iter->second == 11)
numberOfDays = 30;
else if (iter->second == 2)
{
bool isLeapYear = (m_year % 4 == 0 && m_year % 100 != 0) || (m_year % 400 == 0);
if (isLeapYear)
{
numberOfDays = 29;
}
else
{
numberOfDays = 28;
}
}
else
{
numberOfDays = 31;
}
return numberOfDays;
}
void Print()
{
int num_days = this-> daysMonth();
int day_week = this-> firstDay();//Print() can't also be const as firstDay() isn't const;
string_pad("S M T W T F S", 25);
int day = 1;
skipToDay(day_week);
while (day <= num_days)
{
cout<<setw(3) << day << "";
if (day_week == 6)
{
cout<<"\n";
day_week = 0;
}
else
{
day_week = day_week + 1;
}
day = day + 1;
}
cout<<"\n\n";
}
};
int main()
{
DaysOf test("February", 2000);
test.Print();
}
|