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 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
|
#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
#include <string>
#include <sstream>
using namespace std;
int calcDaysSoFar(int year, int month);
bool isLeapYear(int year);
void printCalendar(int month, int year, int firstDay);
void printGridTop(int innerWidth);
void repeatChar(char ch, int numChar);
void drawDivider(int innerWidth);
void printGridBottom(int innerWidth);
const int firstYear = 1800;
const int dayOffset = 3;
const int firstLeapYear = 1804;
string dayNames[] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
string months[] = { "January", "February", "March", "April", "May", "June", "July", "August",
"September", "October", "November", "December" };
int daysInMonth[] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
int daysPassed[] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
int main()
{
int year, month;
int days;
cout << "Please enter the number of the month and year to access calendar\n" << endl;
cout << "Please leave a space in between the month and year when you enter them: \n";
cin >> month >> year;
days = calcDaysSoFar(year, month);
int firstDayOfWeek = days % 7;
printCalendar(month, year, firstDayOfWeek);
system("pause");
}
int calcDaysSoFar(int year, int month)
{
int days;
days = (year - firstYear) * 365;
days += dayOffset;
days += daysPassed[month - 1];
for (int count = firstLeapYear; count < year; count += 4)
{
if (isLeapYear(count))
{
days++;
}
}
if (month > 2 && isLeapYear(year))
{
days++;
}
return days;
}
bool isLeapYear(int year)
{
return (!(year % 4) && (year % 100)) || !(year % 400);
}
void printCalendar(int month, int year, int firstDay)
{
string dayHeader = string("Sun ") + char(186) + "Mon " + char(186) + "Tue " + char(186) +
"Wed " + char(186) + "Thu " + char(186) + "Fri " + char(186) + "Sat ";
int innerWidth = dayHeader.length();
stringstream sstream;
string tempStr;
cout << '\n';
printGridTop(innerWidth);
sstream << year;
tempStr = string(" ") + months[month - 1] + string(" ") + sstream.str();
cout << char(186);
cout << tempStr;
repeatChar(' ', innerWidth - tempStr.length());
cout << char(186);
cout << '\n';
cout << char(204);
for (int count = 0; count < 7; count++)
{
repeatChar(char(205), 4);
if (count != 6)
{
cout << char(203);
}
}
cout << char(185);
cout << '\n';
cout << char(186);
cout << dayHeader;
cout << char(186) << '\n';
drawDivider(innerWidth);
int count, offset;
offset = 1 - firstDay;
count = daysInMonth[month - 1];
if (isLeapYear(year) && (month == 2))
{
count++;
}
int dayNum;
for (int x = offset; x <= count; x += 7)
{
cout << char(186);
for (dayNum = x; (dayNum < x + 7); dayNum++)
{
if (dayNum <= 0 || dayNum > count)
{
cout << setw(4) << left << setprecision(3) << " " << char(186);;
}
else
{
cout << setw(4) << left << setprecision(3) << setfill(' ') << dayNum << char(186);
}
}
cout << '\n';
if (dayNum <= count)
{
drawDivider(innerWidth);
}
}
printGridBottom(innerWidth);
}
void printGridTop(int innerWidth)
{
cout << char(201);
repeatChar((char)205, innerWidth);
cout << char(187);
cout << '\n';
}
void repeatChar(char ch, int numChar)
{
string aStr(numChar, ch);
cout << aStr;
}
void drawDivider(int innerWidth)
{
cout << char(204);
for (int count = 0; count < 7; count++)
{
repeatChar(char(205), 4);
if (count != 6)
{
cout << char(206);
}
}
cout << char(185);
cout << '\n';
}
void printGridBottom(int innerWidth)
{
cout << char(200);
for (int count = 0; count < 7; count++)
{
repeatChar(char(205), 4);
if (count != 6)
{
cout << char(202);
}
}
cout << char(188);
cout << '\n';
}
|