-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path7. Date.cpp
154 lines (147 loc) · 2.89 KB
/
7. 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
#include<iostream>
using namespace std;
class date
{
private:int mm,dd,yy;
public:void getdate();
int operator -(date);
date operator +(int);
};
void date::getdate()
{
cout<<"Enter a valid date(dd mm yy):";
START:
cin>>dd>>mm>>yy;
if((mm==2) && (dd>29)){
cout<<"Wrong input!!!\n";
cout<<"Enter the date again....\n";
goto START;
}
if((mm>12)||(dd>31)){
cout<<"Wrong input!!!\n";
cout<<"Enter the date again....\n";
goto START;
}
if((mm==4||mm==6||mm==9||mm==11) && (dd>30)){
cout<<"Wrong input!!!\n";
cout<<"Enter the date again....\n";
goto START;
}
if((yy%4)!=0 && (mm==2) && (dd>28)){
cout<<"wrong input!!!\n";
cout<<"Eenter the date again....\n";
goto START;
}
}
int date::operator -(date d2)
{
int i,nod1,nod2,nody,lc,no_of_days;
nod1=nod2=lc=0;
for(i=1;i<mm;i++){
if(i==1||i==3||i==5||i==7||i==8||i==10||i==12)
nod1+=31;
else if(i==2)
nod1+=28;
else
nod1+=30;
}
nod1+=dd;
for(i=1;i<d2.mm;i++){
if(i==1||i==3||i==5||i==7||i==8||i==10||i==12)
nod2+=31;
else if(i==2)
nod2+=28;
else
nod2+=30;
}
nod2+=d2.dd;
nody=(yy-d2.yy)*365;
for(i=d2.yy;i<yy;i++)
if((i%4)==0)
lc++;
int y4=yy-d2.yy;
while(y4>400){
lc++;
y4-=400;
}
if((mm>2) && (yy%4)==0)
lc++;
if((d2.mm>2) && (d2.yy%4)==0)
lc--;
no_of_days=nody+nod1-nod2+lc;
if(no_of_days>0){
cout<<"Total number of days between these dates is=";
return(no_of_days);
}
else
return(no_of_days);
}
date date::operator +(int nd)
{
date dd3;
while(nd>365){
yy++;
nd-=365;
}
while(nd>30){
if(mm==1||mm==3||mm==5||mm==7||mm==8||mm==10||mm==12){
nd-=31;
mm++;
}
else if(mm==2){
nd-=28;
mm++;
}
else{
nd-=30;
mm++;
}
if(mm>12) {
yy++;
mm=1;
}
}
dd=dd+nd;
if(dd>30){
if(mm==4||mm==6||mm==9||mm==11){
mm++;
dd-=30;
}
else if(mm==2){
mm++;
dd-=28;
}
else if(dd>31){
mm++;
dd-=31;
}
if(mm>12){
yy++;
mm=1;
}
}
dd3.mm=mm;
dd3.dd=dd;
dd3.yy=yy;
cout<<"New date is:";
cout<<dd<<"-"<<mm<<"-"<<yy<<endl;
return(dd3);
}
int main()
{
int res,num;
date dd1,dd2;
BEGIN:
dd1.getdate();
dd2.getdate();
res=dd1-dd2;
if(res<0){
cout<<"The first date should be greater than the second date\n";
cout<<"So enter the dates again\n";
goto BEGIN;
}
cout<<res;
cout<<"\nEnter the no. of days to be added to the FIRST date:";
cin>>num;
dd2=dd1+num;
}