-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdate.cc
More file actions
155 lines (134 loc) · 3.57 KB
/
Copy pathdate.cc
File metadata and controls
155 lines (134 loc) · 3.57 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
/*************************************************************************
The implementation file for the date class including the
implementation of overloaded insertion and extraction and
comparison operators.
John Dolan Ohio University EECS September 2005
Patricia Lindner Ohio University EECS August 2021
*************************************************************************/
#include "date.h"
using namespace std;
// default constructor
Date::Date()
{
day = month = year = 1;
// permissable days for each month are loaded into an array
daysallowed[0] = 0;
daysallowed[1] = 31;
daysallowed[2] = 29;
daysallowed[3] = 31;
daysallowed[4] = 30;
daysallowed[5] = 31;
daysallowed[6] = 30;
daysallowed[7] = 31;
daysallowed[8] = 31;
daysallowed[9] = 30;
daysallowed[10] = 31;
daysallowed[11] = 30;
daysallowed[12] = 31;
}
// constructor that takes three arguments
Date::Date(int d, int m, int y) : day(d), month(m), year(y)
{
// permissable days for each month are loaded into an array
daysallowed[0] = 0;
daysallowed[1] = 31;
daysallowed[2] = 29;
daysallowed[3] = 31;
daysallowed[4] = 30;
daysallowed[5] = 31;
daysallowed[6] = 30;
daysallowed[7] = 31;
daysallowed[8] = 31;
daysallowed[9] = 30;
daysallowed[10] = 31;
daysallowed[11] = 30;
daysallowed[12] = 31;
}
// output operator, overloaded as a friend function
ostream &operator<<(ostream &outs, Date d)
{
outs << d.month << '/' << d.day << '/' << d.year;
return outs;
}
// input operator, overloaded as a friend
istream &operator>>(istream &ins, Date &d)
{
string junk;
ins >> d.month;
// if an invalid month is detected throw a bad_month
if (d.month < 1 || d.month > 12)
{
getline(ins, junk); // eat the rest of the line
throw(bad_month(d.month));
}
// if the read has failed because of eof exit funtion
if (ins.eof())
return ins;
if (ins.peek() == '/')
ins.ignore();
ins >> d.day;
// if an invalid day is detected throw a bad_day
if (d.day < 1 || d.day > d.daysallowed[d.month])
{
getline(ins, junk); // eat the rest of the line
throw(bad_day(d.month, d.day));
}
if (ins.eof())
return ins;
if (ins.peek() == '/')
ins.ignore();
ins >> d.year;
return ins;
}
// return whether or not d1 is greater than d2
bool operator>(const Date &d1, const Date &d2)
{
if (d1.year != d2.year)
{
return (d1.year > d2.year);
}
else if (d1.month != d2.month)
{
return (d1.month > d2.month);
}
else
{
return (d1.day > d2.day);
}
}
// return whether or not d1 is less than d2
bool operator<(const Date &d1, const Date &d2)
{
if (d1.year != d2.year)
{
return (d1.year < d2.year);
}
else if (d1.month != d2.month)
{
return (d1.month < d2.month);
}
else
{
return (d1.day < d2.day);
}
}
// return whether or not d1 equals d2
bool operator==(const Date &d1, const Date &d2)
{
return (d1.year == d2.year && d1.month == d2.month && d1.day == d2.day);
}
// return whether or not d1 is not equal to d2
bool operator!=(const Date &d1, const Date &d2)
{
return (!(d1.year == d2.year && d1.month == d2.month && d1.day == d2.day));
}
// return whether or not d1 is less than or equal to d2
bool operator<=(const Date &d1, const Date &d2)
{
return (d1 == d2 || d1 < d2);
}
// return whether or not d1 is greater than or equal to d2
bool operator>=(const Date &d1, const Date &d2)
{
return (d1 == d2 || d1 > d2);
}