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 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
|
#include <iostream>
#include <iomanip>
using namespace std;
bool isLeapYear(int month, int year);
bool isLeapYear(int month, int year)
{
if ( year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
return true;
else
return false;
}
void display(int month, int year, int numDays, int offset)
{
int days;
cout << " Su Mo Tu We Th Fr Sa\n";
switch (offset) {
case 0:
cout << setw(4 * (offset + 1) % 7) << " ";
break;
case 1:
cout << setw(4 * (offset + 1) % 7) << " ";
break;
case 2:
cout << setw(4 * (offset + 1) % 7) << " ";
break;
case 3:
cout << setw(4 * (offset + 1) % 7) << " ";
break;
case 4:
cout << setw(4 * (offset + 1) % 7) << " ";
break;
case 5:
cout << setw(4 * (offset + 1) % 7) << " ";
break;
case 6:
cout << setw(0) << " ";
break;
}
for (days = 1; days <= numDays; days++)
{
if ((days + offset) % 7 == 0 && days != 0 )
cout << "\n";
cout << " " << setw(2) << days;
}
cout << "\n";
}
void computeOffset(int month, int year, int numDays)
{
int offset = 0;
int count = year - 1753;
for (int iYear = 0; iYear < count; iYear++)
{
offset = (offset + 365 + isLeapYear(month, year)) % 7;
}
for (int iMonth = 1; iMonth < month; iMonth++)
{
offset = (offset + numDays) % 7;
}
display(month, year, numDays, offset);
}
void numDaysMonth(int month, int year)
{
int numDays = -1;
switch (month)
{
case 1:
numDays = 31;
break;
case 2:
{
if (isLeapYear(month, year) == true)
numDays = 29;
else
numDays = 28;
break;
}
case 3:
numDays = 31;
break;
case 4:
numDays = 30;
break;
case 5:
numDays = 31;
break;
case 6:
numDays = 30;
break;
case 7:
numDays = 31;
break;
case 8:
numDays = 31;
break;
case 9:
numDays = 30;
break;
case 10:
numDays = 31;
break;
case 11:
numDays = 30;
break;
case 12:
numDays = 31;
break;
}
computeOffset(month, year, numDays);
}
void displayHeader(int month, int year)
{
switch (month) {
case 1:
cout << "January, " << year << endl;
break;
case 2:
cout << "February, " << year << endl;
break;
case 3:
cout << "March, " << year << endl;
break;
case 4:
cout << "April, " << year << endl;
break;
case 5:
cout << "May, " << year << endl;
break;
case 6:
cout << "June, " << year << endl;
break;
case 7:
cout << "July, " << year << endl;
break;
case 8:
cout << "August, " << year << endl;
break;
case 9:
cout << "September, " << year << endl;
break;
case 10:
cout << "October, " << year << endl;
break;
case 11:
cout << "November, " << year << endl;
break;
case 12:
cout << "December, " << year << endl;
break;
default:
cout << "Value not read correctly.";
}
numDaysMonth(month, year);
}
void getInfo()
{
int month = -1;
int year = -2;
while (month > 12 || month < 1)
{
cout << "Enter a month number: ";
cin >> month;
if (month > 12 || month < 1)
{
cout << "Month must be between 1 and 12." << endl;
}
}
while (year < 1753)
{
cout << "Enter year: ";
cin >> year;
if (year < 1753)
{
cout << "Year must be 1753 or later." << endl;
}
}
cout << endl;
displayHeader(month, year);
}
int main()
{
getInfo();
return 0;
}
|