-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
159 lines (118 loc) · 3.22 KB
/
main.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
//Roosevelt Bannerman#include <iostream>
using namespace std;
string
months [] = {"January","February","March","April","May","June","July","August","September","October","November","December"},
weekdays [] = {"Su","Mo","Tu","We","Th","Fr","Sa"},
// 0 1 2 3 4 5 6
nullString (28,'\0'),
dashString (28,'-');
int days [] = {31,28,31,30,31,30,31,31,30,31,30,31};
void displayResults(int,int);
int
calculateLeaps(int),
calculateDayOne(int,int),
obtainYear(),
yearValidation(int);
bool calculateLeap(int);
const int AMOUNTWEEKDAYS = 7;
const int AMOUNTMONTHS = 12;
int main(){ int chosenYear = obtainYear();
bool isLeap = calculateLeap(chosenYear);
if(isLeap){
days[1] = 29;
}
int totalLeaps = calculateLeaps(chosenYear);
int JanOne = calculateDayOne(chosenYear,totalLeaps);
displayResults(chosenYear,JanOne);
}
int obtainYear(){
int year;
cout << "\n\nPlease enter a year to display: ";
cin >> year;
year = yearValidation(year);
return year;
}
int yearValidation(int validateThis){
if( validateThis < 1900){
cout << "Error: Please enter a valid year.";
obtainYear();
}
return validateThis;
}
bool calculateLeap(int year){
if(year % 4 == 0){
if(year % 100 == 0){
if(year % 400 == 0){
return true;
}
return false;
}
return true;
}
return false;
}
int calculateLeaps(int year){
int totalLeaps = 0;
for(int i=1900; i<year; i++){
if(i % 4 == 0){
if(i % 100 == 0){
if(i % 400 == 0){
totalLeaps += 1;
}
continue;
}
totalLeaps += 1;
}
continue;
}
return totalLeaps;
}
int calculateDayOne(int year, int leaps){
int difference = year - 1900;
int days = (difference * 365) + leaps;
return (days % AMOUNTWEEKDAYS);
}
void displayResults(int year, int start){
int counter = start + 1;
if(counter == AMOUNTWEEKDAYS) counter = 0;
for (int i=0;i<AMOUNTMONTHS;i++){
string nullspaces((23 - months[i].length())/2,'\0');
cout << endl << nullspaces << months[i] << " " << year << endl;
for(int i=0;i<AMOUNTWEEKDAYS;i++){
cout << " " << weekdays[i];
}
cout << endl << dashString << endl;
string startPosition(0+(counter*4),' ');
cout << startPosition;
for(int ii=1;ii<=days[i];ii++){
if(ii>9){
cout << " " << ii;
counter++;
if(counter == AMOUNTWEEKDAYS){
if(ii!=days[i]) cout << endl;
counter = 0;
}
}
else{
cout << " " << ii;
counter++;
if(counter == AMOUNTWEEKDAYS){
if(ii!=days[i]) cout << endl;
counter = 0;
}
}
}
cout << endl;
}
cout << endl;
}
/*
January 1, 1920 was a Thursday
December 31, 1920 was a Friday
January 1, 1965 was a Friday
December 31, 1965 was a Friday
January 1, 1988 was a Friday
December 31, 1988 was a Saturday
*/