-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPrg 12.cpp
209 lines (190 loc) · 3.96 KB
/
Prg 12.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/**
Lab Program 7
Operator Overloading - date Class
Author SkyKOG
*/
#include <iostream>
#include <conio.h>
#include <windows.h>
#include <process.h>
using namespace std;
class date {
int dd,mm,yy;
public :
void getdata();
int operator -(date);
date operator +(int);
friend ostream & operator <<(ostream &,date &);
};
int main()
{
int no_of_days,ch;
date d1,d2;
while(true)
{
system("cls");
cout<<"1. Diffrence Between 2 dates : \n2. Get New date : \n3. Exit\n\nEnter Selection : ";
cin>>ch;
switch(ch)
{
case 1:cout<<"\n\nEnter date d1 (dd/mm/yy) : ";
d1.getdata();
cout<<"Enter date d2 (dd/mm/yy) : ";
d2.getdata();
no_of_days=d1-d2;
if(no_of_days<0)
{
cout<<"\nInvalid dates";
}
else
cout<<"\nThe Number of Days in Between Are : "<<no_of_days;
break;
case 2:cout<<"\n\nEnter date d1 (dd/mm/yy) : ";
d1.getdata();
cout<<"Enter Days To Add : ";
cin>>no_of_days;
d2=d1+no_of_days;
cout<<"\nThe Required date is : ";
cout<<d2;
break;
case 3:exit(0);
break;
default:cout<<"\n\nInvalid Selection";
break;
}
getch();
}
return 0;
}
void date::getdata()
{
cin>>dd>>mm>>yy;
if(dd>31||mm>12)
{
cout<<"\nInvalid date";
getdata();
}
if(((mm==4)||(mm==6)||(mm==9)||(mm==11))&&(dd>30))
{
cout<<"\nInvalid date";
getdata();
}
if((mm==2))
{
if((yy%4!=0)&&(dd>28))
{
cout<<"\nInvalid date";
getdata();
}
else if((dd>29))
{
cout<<"\nInvalid date";
getdata();
}
}
}
int date::operator -(date d2)
{
int i,n1,n2,ny,d;
n1=n2=ny=0;
for(i=1;i<=mm;i++)
{
if((i==4)||(i==6)||(i==9)||(i==11))
n1+=30;
else if(i==2)
{
if(yy%4==0)
n1+=29;
else
n1+=28;
}
else
n1+=31;
}
n1+=dd;
for(i=1;i<=d2.mm;i++)
{
if((i==4)||(i==6)||(i==9)||(i==11))
n2+=30;
else if(i==2)
{
if(d2.yy%4==0)
n2+=29;
else
n2+=28;
}
else
n2+=31;
}
n2+=d2.dd;
ny=(yy-d2.yy)*365;
d=n1-n2+ny;
return d;
}
date date::operator +(int n)
{
while(n>365)
{
yy++;
if(yy%4==0)
n-=366;
n-=365;
}
while(n>30)
{
if((mm==4)||(mm==6)||(mm==9)||(mm==11))
n-=30;
else if(mm==2)
{
if(yy%4==0)
n-=29;
else
n-=28;
}
else
n-=31;
mm++;
if(mm>12)
{
yy++;
mm=1;
}
}
dd+=n;
if(dd>31)
{
if((mm==4)||(mm==9)||(mm==6)||(mm==11))
dd-=30;
else if(mm==2)
{
if(yy%4==0)
dd-=29;
else
dd-=28;
}
else
dd-=31;
mm++;
}
return *this;
}
ostream & operator<<(ostream &out,date &d3)
{
out<<d3.dd<<" "<<d3.mm<<" "<<d3.yy;
return out;
}
/*
License
=======
Copyright © 2011 SkyKOG
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/