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
|
// Date class definition
#include "stdafx.h"
#include "iostream"
using std::cout;
using std::cin;
using std::endl;
#include "Date.h"
Date::Date(int d, int m, int y)
: day(d),
month(m),
year(y)
{}
int Date::leapYear()
{
if(year%400==0 || (year%100!=0 && year%4 == 0))
return 1;
else
return 0;
}
int Date::valid()
{
if(day<0)
{
return 0;
}
else
{
switch(month)
{
case 2:
{
if(this->leapYear()==0)
if(day<29)
return 1;
else
return 0;
else
if(day<30)
return 1;
else
return 0;
break;
}
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
{
if(day<32)
return 1;
else
return 0;
break;
}
default:
{
if(day<31)
return 1;
else
return 0;
break;
}
}
}
}
void Date::input()
{
do
{
cout<<" Enter Day: ";
cin>>day;
cout<<"Enter Month: ";
cin>>month;
cout<<" Enter Year: ";
cin>>year;
cout<<endl;
if(this->valid()==0)
cout<<"Re-enter date to replace invalid date"<<endl;
}
while(this->valid()==0);
}
void Date::output()
{
cout<<"You have entered: "
<<day<<"/"
<<month<<"/"
<<year<<endl;
}
void Date::nextDay(Date &next)
{
next.day=this->day;
next.month=this->month;
next.year=this->year;
switch(day)
{
case 28:
{
if(this->leapYear()==0)
next.day=next.day+1;
else
{
next.day=1;
next.month=3;
}
break;
}
case 30:
{
if(month==4 || month==6 || month==9 || month==11)
{
next.day=1;
next.month=next.month+1;
}
else
next.month=31;
break;
}
case 31:
{
if(month=12)
{
next.day=1;
next.month=1;
next.year=next.year+1;
}
else
{
next.day=1;
next.month=next.month+1;
}
break;
}
}
}
Date Date::operator=(const Date &a)
{
Date b;
b.day=a.day;
b.month=a.month;
b.year=a.year;
return b;
}
Date &Date::operator+(int a)
{
int sum=0;
int i;
int m[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
m[0]=leapYear();
if(m[0]!=0)
m[2]=29;
for(i=1;i<month;i++)
sum=sum+m[i];
sum=sum+day;// The number of day in the year
sum=sum+a; // The number of day after plus operator
// Change back to Date
i=1;
while(sum>m[i])
{
sum=sum-m[i];
i++;
}
int d=sum;
int mn=i;
int y=this->year;
Date b(d,mn,y);
return b;
}
|