-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdate_problems.cpp
More file actions
171 lines (134 loc) · 4.9 KB
/
date_problems.cpp
File metadata and controls
171 lines (134 loc) · 4.9 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
167
168
169
170
171
// Your First C++ Program
#include <iostream>
#include <map>
#include <time.h>
#include <vector>
using namespace std;
string birthInput();
int yearsOld(int currentYear, int currentMonth, int currentDay,
int birthYear, int birthMonth, int birthDay);
int monthsOld(int currentYear, int currentMonth, int currentDay,
int birthYear, int birthMonth, int birthDay);
int dayOfTheWeek(int birthYear,int birthMonth,int birthDay);
int main() {
int birthDay, birthMonth, birthYear, YO, MO;
string birth;
birth = birthInput();
birthDay = stoi(birth.substr(0, 2));
birthMonth = stoi(birth.substr(3, 2)); // waarom werkt dit op deze manier?
birthYear = stoi(birth.substr(6, 10));
time_t currentTime;
time(¤tTime);
tm* timePtr = localtime(¤tTime);
int cDay, cMonth, cYear;
cYear =timePtr->tm_year+1900;
cMonth = timePtr->tm_mon+1;
cDay= timePtr->tm_mday;
YO = yearsOld(cYear, cMonth, cDay, birthYear, birthMonth, birthDay);
cout << "You are " << YO << " years old." << "\n";
MO = monthsOld(cYear, cMonth, cDay, birthYear, birthMonth, birthDay);
cout << "You are " << MO << " months old." << "\n";
cout << "Do you know what day you were born on? Select a number (e.g. 1) \n";
vector<string> dayInWeek = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
for (size_t i = 0; i < dayInWeek.size(); i++)
{
cout << i + 1 << ": " << dayInWeek[i] << "\n";
}
int guess;
cin >> guess;
int trueDay = dayOfTheWeek(birthYear, birthMonth, birthDay);
if (trueDay == guess) {
cout << "That is correct! \n";
}
else {
cout << "Nope you were born on " << dayInWeek[trueDay - 1] << "\n";
}
return 0;
}
string birthInput() {
string birth;
int birthDay, birthMonth, birthYear;
bool correctInput{false};
do {
cout << "Can you tell me your birth date (e.g. 01-01-2001)" << "\n";
cin >> birth;
try
{
birthDay = stoi(birth.substr(0, 2));
birthMonth = stoi(birth.substr(3, 2));
birthYear = stoi(birth.substr(6, 10));
correctInput = true;
if (birthYear < 1900 or birthYear > 2100 ) {
cout << "Incorrent age!" << "\n";
correctInput = false;
}
const int daysInMonth [12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if (birthDay > daysInMonth[birthMonth - 1]) {
if (birthMonth == 2 and birthYear % 4 == 0 and birthYear % 100 != 0 and birthDay == 29) {
cout << "Not many people were born on this day!" << "\n";
}
else {
cout << "Are there that many days in this month?" << "\n";
correctInput = false;
}
}
if (birthDay < 1 or birthMonth < 1) {
cout << "incorrect input" << "\n";
correctInput = false;
}
}
catch(const std::exception& e)
{
cout << "Your input is incorrect, make sure to get it right! \n";
}
}
while (!correctInput);
return birth;
}
int yearsOld(int currentYear, int currentMonth, int currentDay,
int birthYear, int birthMonth, int birthDay) {
int YearsOld;
// previous birthdays, ecept for (maybe) latest
YearsOld = currentYear - birthYear - 1;
// Check if birthday was this year
if (birthMonth < currentMonth) {
YearsOld += 1;
}
// Check if birthday was this month
if (birthMonth == currentMonth and birthDay < currentDay) {
YearsOld += 1;
}
return YearsOld;
}
int monthsOld(int currentYear, int currentMonth, int currentDay,
int birthYear, int birthMonth, int birthDay) {
return (currentYear - birthYear) * 12 + currentMonth - birthMonth - (currentDay <= birthDay);
}
int dayOfTheWeek(int birthYear,int birthMonth,int birthDay) {
// lowest birthday can be 01-01-1900, which is a monday
// calc leapyears
int leapYear = 0;
for (size_t i = 1900; i < birthYear; i++)
{
if (i % 4 == 0 and (i % 100 != 0 or i % 400 == 0)) {
leapYear++;
}
}
if (birthYear % 4 == 0 and (birthYear % 100 != 0 or birthYear % 400 == 0)) {
if (birthMonth > 2) {
leapYear++;
}
}
int years = birthYear - 1900;
int days_since_1900 = (years* 365 + leapYear);
// count days from 01-01-year born to 01-monthborn-yearborn
const int daysInMonth [12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
for (size_t i = 0; i < (birthMonth - 1); i++)
{
days_since_1900 += daysInMonth[i];
}
// count days from 01-monthborn-yearborn to birthday-monthborn-yearborn
days_since_1900 += birthDay - 1;
// We dont have to subtract a 1 since, monday is already day 1
return (days_since_1900) % 7 + 1;
}