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
|
// Calendar Issues.cpp : main project file.
#include <iostream>
#include <iomanip>
using namespace std;
int numDays = 0;
int offset = 0 ;
int getMonth()
{
int month = 0;
do
{
cout << "Enter a month number: ";
cin >> month;
if (month < 1 || month > 12)
cout << "Month must be between 1 and 12.\n";
}
while (month < 1 || month > 12);
return month;
}
int getYear()
{
int year = 0;
do
{
cout << "Enter year: ";
cin >> year;
if (year < 1753)
cout << "Year must be 1753 or later.\n";
} while (year < 1753);
return year;
}
bool isLeapYear(int year)
{
bool leapYear = false;
if (year % 4 == 0)
leapYear = true;
if (year % 100 == 0 && year % 400 != 0)
leapYear = false;
return leapYear;
}
int calcNumDays(int month, int year)
{
int numDays;
if (month == 1)
numDays = 31;
else if (month == 2)
{
if (isLeapYear(year))
numDays = 29;
else
numDays = 28;
}
else if (month == 3)
numDays = 31;
else if (month == 4)
numDays = 30;
else if (month == 5)
numDays = 31;
else if (month == 6)
numDays = 30;
else if (month == 7)
numDays = 31;
else if (month == 8)
numDays = 31;
else if (month == 9)
numDays = 30;
else if (month == 10)
numDays = 31;
else if (month == 11)
numDays = 30;
else if (month == 12)
numDays = 31;
return numDays;
}
int getOffset(int month, int year)
{
// Revised getOffset function
int t[12] = { 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4 };
int offset = year - ((month-1) < 2);
offset = (offset + offset / 4 - offset / 100 + offset / 400 + t[month-1] + 1) % 7;
// offset = 0 for Sunday, 1 for Monday.. etc.
return offset;
}
void displayHeader(int month, int year)
{
switch (month)
{
case 1:
cout << endl << "January, " << year << endl;
break;
case 2:
cout << endl << "February, " << year << endl;
break;
case 3:
cout << endl << "March, " << year << endl;
break;
case 4:
cout << endl << "April, " << year << endl;
break;
case 5:
cout << endl << "May, " << year << endl;
break;
case 6:
cout << endl << "June, " << year << endl;
break;
case 7:
cout << endl << "July, " << year << endl;
break;
case 8:
cout << endl << "August, " << year << endl;
break;
case 9:
cout << endl << "September, " << year << endl;
break;
case 10:
cout << endl << "October, " << year << endl;
break;
case 11:
cout << endl << "November, " << year << endl;
break;
case 12:
cout << endl << "December, " << year << endl;
break;
}
return;
}
void displayTable(int offset, int numDays)
{
cout << setw(4) << "Su"
<< setw(4) << "Mo"
<< setw(4) << "Tu"
<< setw(4) << "We"
<< setw(4) << "Th"
<< setw(4) << "Fr"
<< setw(4) << "Sa"
<< endl;
cout << setw(4) << "";// Setting starting number below Sunday
for(int spaceCount = 1; spaceCount < offset; spaceCount++)
{
cout << setw(4) << " ";// Move spacing over for each offset
}
for(int dayCount = 1; dayCount <= numDays; dayCount++)
{
cout << setw(4) << dayCount;
if ((dayCount + offset) % 7 == 0) // Don't need the '+1' or if dayCount > numDays,
//since loop CAN'T go beyond numDays
cout << endl;
}
cout << endl;
return;
}
int main()
{
int month = getMonth();
int year = getYear();
int numDays = calcNumDays(month, year);
int offset = getOffset(month, year);
displayHeader(month, year);
displayTable(offset, numDays);
}
|