-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalendar.cpp
More file actions
166 lines (148 loc) · 4.17 KB
/
calendar.cpp
File metadata and controls
166 lines (148 loc) · 4.17 KB
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
#include <iostream>
#include <cmath>
#include <stdlib.h>
using namespace std;
int startDayofMonth;
int numDays;
//Gauss's algorithm, Disparate variation to calculate day of the week a month starts on
int startDay(int m,int y, int d=1)
{
int Y;
(m<3)? Y=y-1:Y=y;
y = Y%100;
int c = Y/100;
int shiftedMonth;
switch(m)
{
case 1: shiftedMonth = 11; break;
case 2: shiftedMonth = 12; break;
case 3: shiftedMonth = 1; break;
case 4: shiftedMonth = 2; break;
case 5: shiftedMonth = 3; break;
case 6: shiftedMonth = 4; break;
case 7: shiftedMonth = 5; break;
case 8: shiftedMonth = 6; break;
case 9: shiftedMonth = 7; break;
case 10: shiftedMonth = 8; break;
case 11: shiftedMonth = 9; break;
case 12: shiftedMonth = 10; break;
}
int weekDay = int((d+int(floor(2.6*shiftedMonth-.2))+y+y/4+c/4-2*c))%7;
if(weekDay<0)
weekDay += 7;
return weekDay;
}
//function to check is the year given is a leap year
bool isLeapYear(int year)
{
if(year % 400 == 0 || year % 4 == 0 && !(year % 100 == 0))
return true;
else
return false;
}
//prints begining of the calendar
void printHeader(int month, int year)
{
string monthTitle = "";
switch(month)
{
case 1: monthTitle = "January"; numDays= 31; break;
case 2: monthTitle = "February"; (isLeapYear(year))? numDays=29:numDays=28; break;
case 3: monthTitle = "March"; numDays= 31; break;
case 4: monthTitle = "April"; numDays= 30; break;
case 5: monthTitle = "May"; numDays= 31;break;
case 6: monthTitle = "June"; numDays= 30; break;
case 7: monthTitle = "July"; numDays= 31; break;
case 8: monthTitle = "August"; numDays= 31; break;
case 9: monthTitle = "September"; numDays= 30; break;
case 10: monthTitle = "October"; numDays= 31; break;
case 11: monthTitle = "November"; numDays= 30; break;
case 12: monthTitle = "December"; numDays= 31; break;
}
cout << "Calendar for " << monthTitle << " " << year << endl;
cout << "|---------------------------|" << endl;
cout << "|Sun|Mon|Tue|Wed|Thu|Fri|Sat|" << endl;
cout << "|---------------------------|" << endl;
}
//prints end of the calendar
void printFoot()
{
cout << "|---------------------------|" << endl;
}
//checks to see if given month and day is a holidays
bool isHoliday(int month, int day)
{
if(month==7 && day ==4) //4th of July
return true;
else if(month==10 && day==31) //Halloween
return true;
else if(month==12 && day==25) //Christmas
return true;
else
return false;
}
//fills in the calendar
void fillCalendar(int month, int year)
{
int day = 1;
int weeks = 5;
int daysSkipped = 0;
//adjusts the amount of weeks in a month
if(startDayofMonth==5 && numDays==31 || startDayofMonth==6 && numDays>29)
weeks = 6;
else if(startDayofMonth==0 && !isLeapYear(year) && month==2)
weeks = 4;
//iterates over each week in a month
for(int i = 0; i < weeks; i++)
{
//iterates over each day in a week
for(int j = 0; j < 7; j++)
{
//creates "empty" boxes on calendar after printing all days in a month
if(day > numDays)
{
cout << "| ";
continue;
}
cout << "| ";
//prints "empty" boxes on calendar before it prints day 1
if(i == 0 && j < startDayofMonth)
{
cout << " ";
continue;
}
//formats single digit
if(day<10) cout << " ";
cout << day++;
}
cout << "|" << endl;
//iterates through each day and identifies holidays
for(int j = 0; j < 7; j++)
{
//keeps track of what number day the loop is on
int currentDay = (i)*7+(j+1)-daysSkipped;
//skips over days before the first of the month
if(i == 0 && j < startDayofMonth)
{
daysSkipped++;
cout << "| ";
continue;
}
cout << "|";
//checks to see if the current day is a holiday and marks it accordingly
if(isHoliday(month, currentDay)) cout << " * ";
else cout << " ";
}
cout << "|" << endl;
printFoot();
}
}
int main(int argc, char ** argv)
{
//create needed variables
int month = atoi(argv[1]);
int year = atoi(argv[2]);
startDayofMonth = startDay(month,year);
printHeader(month, year);
fillCalendar(month, year);
}