-
Notifications
You must be signed in to change notification settings - Fork 0
/
Date.cpp
89 lines (80 loc) · 1.4 KB
/
Date.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
#include "Date.h"
Date::Date(){}
Date::Date(string date)
{
parse(date);
}
Date::Date(int day, int month, int year)
{
this->day = day;
this->month = month;
this->year = year;
}
void Date::write(ostream &out) const
{
out << "[" << day << "/" << month << "/" << year << "]";
}
void Date::parse(string date)
{
string day, month, year;
int slashFound = 0;
for(int i = 0; i < date.length(); i++)
{
switch(slashFound)
{
case 0:
{
if(date[i] == '/')
{
slashFound++;
}
else
{
day += date[i];
}
break;
}
case 1:
{
if(date[i] == '/')
{
slashFound++;
}
else
{
month += date[i];
}
break;
}
case 2:
{
year += date[i];
break;
}
}
}
this->day = stoi(day);
this->month = stoi(month);
this->year = stoi(year);
}
bool Date::isEarlierThan(Date date1, Date date2)
{
if(date1.year != date2.year)
{
return date1.year < date2.year;
}
if(date1.month != date2.month)
{
return date1.month < date2.month;
}
return date1.day <= date2.day;
}
bool Date::equals(Date date1, Date date2)
{
return (date1.day == date2.day && date1.month == date2.month && date1.year == date2.year);
}
void Date::read(istream &in)
{
cout << "Enter date: ";
in >> day >> month >> year;
}