-
Notifications
You must be signed in to change notification settings - Fork 0
/
lab07.cpp
217 lines (191 loc) · 6.5 KB
/
lab07.cpp
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
//Description: This program will output the month and year of the user input. Much like a calendar application.
#include <iostream>
#include <string>
using namespace std;
//This constant represent the week size
const int WEEKSIZE = 7;
// Function Initialization
void displayMonth(string monthName, int year);
void displayYear(int year);
void printWeekHeader(string monthName,int year,bool printYear);
void printMonthDays(int day, int offsetDays);
void inputChecking(string inp);
int getDayOfMonth(int monthNum, bool leapYear);
int getMonthNum(string monthName);
int getStartDayOfYear(int year);
int getStartDayOfMonth(int offsets, int monthDays);
string getMonthName(int monthNum);
bool isLeapYear(int year);
int main(){
string inp;
cout<< "Enter the month you want" << endl;
getline(cin,inp);
//Checks what the user inputs
inputChecking(inp);
return 0;
}
// This function will display the appropriare days in that month
void displayMonth(string monthName, int year){
int offsetsDay = getStartDayOfYear(year);
int targetMonth = getMonthNum(monthName);
cout << targetMonth << endl;
//This loop will figure out the off sets in the month
for(int m = 1; m < targetMonth;m++){
offsetsDay = getStartDayOfMonth(offsetsDay,getDayOfMonth(m,isLeapYear(year)));
}
//Prints the month with the appropriate off sets
printWeekHeader(monthName, year, true);
printMonthDays(getDayOfMonth(getMonthNum(monthName), isLeapYear(year)),offsetsDay);
}
//This function will display the whole calendar of that given year
void displayYear(int year){
cout << "********" << year << "********" << endl;
int offsetsDay = getStartDayOfYear(year);
//This loop will go through all the month and displaying them with their appropriate off sets
for(int m = 1; m < 13; m++){
cout << endl;
printWeekHeader(getMonthName(m),year, false);
printMonthDays(getDayOfMonth(getMonthNum(getMonthName(m)), isLeapYear(year)),offsetsDay);
offsetsDay = getStartDayOfMonth(offsetsDay,getDayOfMonth(m,isLeapYear(year)));
}
}
//This function will display the Weekheader of any month.
void printWeekHeader(string monthName, int year, bool printYear){
string weekDays[WEEKSIZE] = {"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
cout << endl;
if(printYear){
cout << "********" << monthName << "********" << endl;
cout << "********" << year << "********" << endl;
}
else{
cout << "********" << monthName << "********" << endl;
}
cout << endl;
for(int i = 0; i < WEEKSIZE; i++){
cout<< weekDays[i] << "\t";
}
cout << endl;
for(int i = 0; i < WEEKSIZE; i++){
for(int j =0; j<4; j++){
cout << "__";
}
}
cout << endl;
}
//This function will print the month days
void printMonthDays(int dayM, int offsetDays){
int count = 0;
for(int j = 0; j<offsetDays;j++){
cout <<" "<<"\t";
count+=1;
}
for(int i =1; i <dayM+1; i++){
cout << i << "\t";
count+=1;
if(count % 7 == 0){
cout<<endl;
count = 0;
}
}
cout << endl;
}
//Checks if its leap year
bool isLeapYear(int year){
return ((year%4==0) && (year%100 !=0))||(year%400==0);
}
//This function when called, takes in a string and checks what the user have inputed.
//Then this function will call the appropriate function toward what the user have inputed.
void inputChecking(string inp){
string tmp;
string month;
int year =0;
int spaceN;
//checks if the user inputed year
if(isdigit(inp[0])){
for(int i = 0; i <4; i++){
tmp += inp[i];
}
year = stoi(tmp);
//call print year
displayYear(year);
}
//Checks if the user inputed month and year
else{
spaceN = inp.find(" ");
for(int i = 0; i < (spaceN);i++){
month+=inp[i];
}
for(int i = 0; i < 5 ;i++){
tmp += inp[i+spaceN];
}
year = stoi(tmp);
// call print month
displayMonth(month, year);
}
}
//This function will return the off sets of beggining of the month for that year
int getStartDayOfYear(int year){
int x1 = (year-1)/4;
int x2 = (year-1)/100;
int x3 = (year-1)/400;
return (year + x1 -x2 +x3)%7;
}
//Will determine the offsets of any month.
int getStartDayOfMonth(int offsets, int monthDays){
int count = 0;
for(int j = 0; j<offsets;j++){
count+=1;
}
for(int i =1; i <monthDays+1; i++){
count+=1;
if(count % 7 == 0){
count = 0;
}
}
return count;
}
//This function will check the month number and will return the month name
string getMonthName(int monthNum){
if(monthNum == 1){return "January";}
else if(monthNum == 2){return "Febuary";}
else if(monthNum == 3){return "March";}
else if(monthNum == 4){return "April";}
else if(monthNum == 5){return "May";}
else if(monthNum == 6){return "June";}
else if(monthNum == 7){return "July";}
else if(monthNum == 8){return "August";}
else if(monthNum == 9){return "September";}
else if(monthNum == 10){return "October";}
else if(monthNum == 11){return "November";}
else if(monthNum == 12){return "December";}
}
//This function will check the month name and return the month number
int getMonthNum(string targetMonth){
if(targetMonth == "January"){return 1;}
else if(targetMonth == "February"){return 2;}
else if(targetMonth == "March"){return 3;}
else if(targetMonth == "April"){return 4;}
else if(targetMonth == "May"){return 5;}
else if(targetMonth == "June"){return 6;}
else if(targetMonth == "July"){return 7;}
else if(targetMonth == "August"){return 8;}
else if(targetMonth == "September"){return 9;}
else if(targetMonth == "October"){return 10;}
else if(targetMonth == "November"){return 11;}
else if(targetMonth == "December"){return 12;}
}
//This function will return the number of days in that specific month.
int getDayOfMonth(int monthNum,bool leapYear){
if(monthNum == 1){return 31;}
else if(monthNum == 2){if(leapYear){return 29;}else {return 28;}}
else if(monthNum == 3){return 31;}
else if(monthNum == 4){return 30;}
else if(monthNum == 5){return 31;}
else if(monthNum == 6){return 30;}
else if(monthNum == 7){return 31;}
else if(monthNum == 8){return 31;}
else if(monthNum == 9){return 30;}
else if(monthNum == 10){return 31;}
else if(monthNum == 11){return 30;}
else if(monthNum == 12){return 31;}
}