-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
199 lines (160 loc) · 4.8 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
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
/*
---------------------------------------------------------------------------
Fichier : labo3-feratiHebert.cpp
Nom du labo : 02_taxi
Auteurs : Kevin Ferati, Elliott Hébert
Date : 26.10.2020
But : Ce programme demande à l'utilisateur une année,
en vérifie l'intégrité, puis affiche tous les mois de cette année
Remarques : Lancer l'exe si possible depuis le terminal windows
afin de profiter au maximum des fonctions de nettoyage d'écran.
Les entrées utilisateurs sont vérifiées.
Pour simplifier, les premiers janviers sont des lundis.
Compilateur : MinGW-W64
---------------------------------------------------------------------------
*/
#include <iostream>
#include <climits>
#include <iomanip>
#include <cmath>
using namespace std;
#define CLEAR_BUFFER cin.ignore(numeric_limits<streamsize>::max(), '\n')
int main() {
const int MIN_YEAR = 1900,
MAX_YEAR = 2100;
const char CHAR_TO_REPEAT = 'O'; // Char that has to be input by the user to repeat the operation
const int CELL_WIDTH = 3,
DAYS_IN_WEEK = 7;
enum class Months {
JAN = 1, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC
};
enum class Days {
MONDAY = 'L',
TUESDAY = 'M',
WEDNESDAY = 'M',
THURSDAY = 'J',
FRIDAY = 'V',
SATURDAY = 'S',
SUNDAY = 'D'
};
cout << "Bonjour" << endl
<< "Ce programme vous demande une annee et vous affiche les calendriers de cette annee" << endl;
char quit;
do {
system("cls"); // clear the screen
int chosenYear;
bool isIncorrect;
do {
cout << "entrer une valeur [" << MIN_YEAR << "-" << MAX_YEAR << "] : ";
cin >> chosenYear;
cin.clear();
CLEAR_BUFFER;
isIncorrect = cin.fail() || chosenYear < MIN_YEAR || chosenYear > MAX_YEAR;
if (isIncorrect) {
cout << endl << "/!\\ recommencer" << endl;
}
} while (isIncorrect);
bool isChosenYearLeap = chosenYear % 400 == 0 || (chosenYear % 4 == 0 && chosenYear % 100 != 0);
int daysInFebruary = isChosenYearLeap ? 29 : 28;
int offset = 0;
for (int month = (int) Months::JAN; month <= (int) Months::DEC; ++month) {
int numberOfDaysInMonth;
switch (Months(month)) {
case Months::JAN : {
numberOfDaysInMonth = 31;
cout << "JANVIER";
break;
}
case Months::FEB : {
numberOfDaysInMonth = daysInFebruary;
cout << "FEVRIER";
break;
}
case Months::MAR : {
numberOfDaysInMonth = 31;
cout << "MARS";
break;
}
case Months::APR : {
numberOfDaysInMonth = 30;
cout << "AVRIL";
break;
}
case Months::MAY : {
numberOfDaysInMonth = 31;
cout << "MAI";
break;
}
case Months::JUN : {
numberOfDaysInMonth = 30;
cout << "JUIN";
break;
}
case Months::JUL : {
numberOfDaysInMonth = 31;
cout << "JUILLET";
break;
}
case Months::AUG : {
numberOfDaysInMonth = 31;
cout << "AOUT";
break;
}
case Months::SEP : {
numberOfDaysInMonth = 30;
cout << "SEPTEMBRE";
break;
}
case Months::OCT : {
numberOfDaysInMonth = 31;
cout << "OCTOBRE";
break;
}
case Months::NOV : {
numberOfDaysInMonth = 30;
cout << "NOVEMBRE";
break;
}
case Months::DEC : {
numberOfDaysInMonth = 31;
cout << "DECEMBRE";
break;
}
}
cout << " " << chosenYear << endl;
cout << right << setw(CELL_WIDTH) << (char) Days::MONDAY
<< right << setw(CELL_WIDTH) << (char) Days::TUESDAY
<< right << setw(CELL_WIDTH) << (char) Days::WEDNESDAY
<< right << setw(CELL_WIDTH) << (char) Days::THURSDAY
<< right << setw(CELL_WIDTH) << (char) Days::FRIDAY
<< right << setw(CELL_WIDTH) << (char) Days::SATURDAY
<< right << setw(CELL_WIDTH) << (char) Days::SUNDAY
<< endl;
int numberOfCells = numberOfDaysInMonth + offset;
int numberOfWeeks = (int)ceil(numberOfCells / (double) DAYS_IN_WEEK);
int day = 1;
for (int week = 1; week <= numberOfWeeks; ++week) {
for (int dayInWeek = 1; dayInWeek <= DAYS_IN_WEEK; ++dayInWeek) {
cout << right << setw(CELL_WIDTH);
if (dayInWeek <= offset && week == 1) {
cout << "";
} else {
cout << day;
if (day == numberOfDaysInMonth) {
break;
}
++day;
}
}
cout << endl;
}
offset = numberOfCells % DAYS_IN_WEEK;
cout << endl;
}
cout << "Voulez-vous recommencer ? [" << CHAR_TO_REPEAT << "/N]";
cin >> quit;
cin.clear();
CLEAR_BUFFER;
} while (!cin.fail() && quit == CHAR_TO_REPEAT);
return EXIT_SUCCESS;
}