-
Notifications
You must be signed in to change notification settings - Fork 0
/
date.cpp
190 lines (167 loc) · 2.57 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
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
/* C++ program to demonstrate date addition and subtraction
* Written By: Ramchandra Shahi Thakuri
* Date;
*/
#include <iostream>
#include <stdlib.h>
using namespace std;
class date
{
private:
int d;
int m;
int y;
public:
int leap(int);
int days(int);
void getDate(void);
int operator -(date);
date operator +(int);
friend ostream &operator <<(ostream &, date);
friend int checkDate(date, date);
};
int date :: leap(int y)
{
if(y % 4 == 0)
return 1;
else
return 0;
}
int date :: days(int m)
{
if( m == 2)
return 28;
else if(m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12)
return 31;
else
return 30;
}
void date :: getDate(void)
{
cout << "\nEnter days: ";
cin >> d;
cout << "\nEnter Month: ";
cin >> m;
cout << "\nEnter Year: ";
cin >> y;
}
int date :: operator -(date d2)
{
int leapyear;
int count = 0;
while(m != d2.m || y != d2.y)
{
d2.d++;
count++;
leapyear = 0;
if(d2.m == 2)
leapyear = d2.leap(d2.y);
if(d2.d > (d2.days(d2.m) + leapyear))
{
d2.d = 1;
d2.m++;
}
if(d2.m > 12)
{
d2.m = 1;
d2.y++;
}
}
count += (d - d2.d);
return count;
}
date date :: operator +(int ndays)
{
int leapyear;
int i;
for(i = 1; i <= ndays; i++)
{
d++;
leapyear = 0;
if(m == 2)
leapyear = leap(y);
if(d > ((days(m) + leapyear)))
{
d = 1;
m++;
}
if(m > 12)
{
m = 1;
y++;
}
}
return *this;
}
ostream &operator <<(ostream &out, date d)
{
out << d.d << '/' << d.m << '/' << d.y << endl;
return out;
}
int checkDate(date d1, date d2)
{
if(d1.y < d2.y)
return 0;
else if( d1.y == d2.y)
{
if(d1.m < d2.m)
return 0;
else if(d1.m == d2. m)
{
if(d1.d < d2.d)
return 0;
else
return 1;
}
else
return 1;
}
else
return 1;
}
int main()
{
date d1;
date d2;
date d3;
int ch;
int ndays;
int i;
while(1)
{
cout << "\n1. Addition of two date\n2. Subtraction of two date\n3. Exit\nEnter your choice: ";
cin >> ch;
switch(ch)
{
case 1:
cout << "Enter first date: \n\n";
d1.getDate();
cout << "\nEnter no of days to add: ";
cin >> ndays;
d3 = d1 + ndays;
cout << "Result: \n";
cout << d3;
break;
case 2:
cout << "\nnEnter first date greater than second one: \n";
d1.getDate();
cout << "\nEnter second date: \n";
d2.getDate();
i = checkDate(d1, d2);
if(i == 1)
{
ndays = d1 - d2;
cout << "\nDiff in days is: " << ndays;
}
else
{
ndays = d2 - d1;
cout << "\nDiff in days is: " << ndays;
}
break;
defaut:
return 0;
}
}
return 0;
}