-
Notifications
You must be signed in to change notification settings - Fork 23
/
12.8 SpaceshipCompareDates.cpp
66 lines (58 loc) · 1.51 KB
/
12.8 SpaceshipCompareDates.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
#include <iostream>
#include <compare>
using namespace std;
class Date
{
private:
int day, month, year;
public:
Date(int inMonth, int inDay, int inYear)
: month(inMonth), day(inDay), year(inYear) {}
auto operator <=>(const Date& rhs) const
{
if (year < rhs.year)
return std::strong_ordering::less;
else if (year > rhs.year)
return std::strong_ordering::greater;
else
{
// years are identical, compare months
if (month < rhs.month)
return std::strong_ordering::less;
else if (month > rhs.month)
return std::strong_ordering::greater;
else
{
// months are identical, compare days
if (day < rhs.day)
return std::strong_ordering::less;
else if (day > rhs.day)
return std::strong_ordering::greater;
else
return std::strong_ordering::equal;
}
}
}
};
int main()
{
cout << "Enter a date: month, day & year" << endl;
int month, day, year;
cin >> month;
cin >> day;
cin >> year;
Date date1(month, day, year);
cout << "Enter another date: month, day & year" << endl;
cin >> month;
cin >> day;
cin >> year;
Date date2(month, day, year);
auto result = date1 <=> date2;
if (result < 0)
cout << "Date 1 occurs before Date 2" << endl;
else if (result > 0)
cout << "Date 1 occurs after Date 2" << endl;
else
cout << "Dates are equal" << endl;
return 0;
}