文档库 最新最全的文档下载
当前位置:文档库 › 日期类VC++

日期类VC++

#include
#include

class Date
{
private:
int year;
int month;
int date;

int day;
int week;
char string[15];
bool status;
bool check();

public:
Date(int y, int m, int d);
Date();
~Date();

bool isleapyear(); // 润年的判定
void setYear(int y);
void setMonth(int m);
void setDate(int d);
int getYear();
int getMonth();
int getDate();
char * getYMD();
Date & operator =( Date d);
const Date operator +( int d);
const Date operator -( int d);
const Date & operator ++(); // x = ++d;
const Date operator ++( int x ); // x = d++;
const Date & operator --(); // x = --d;
const Date operator --( int x ); // x = d--;
bool operator ==( Date x );
bool operator !=( Date x );
bool operator <( Date x );
bool operator <=( Date x );
bool operator >( Date x );
bool operator >=( Date x );
int GetNumDates(Date t);
friend ostream & operator <<(ostream &stream, Date &t);
};

bool Date::isleapyear()
{
return (((year % 4) == 0) && ((year % 100) != 0) || ((year % 400) == 0));
}

bool Date::check()
{
if (year < 0)
{
status = false;
return status;
}
if ((month < 1) || (month > 12))
{
status = false;
return status;
}
switch (month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
if ((date < 1) || (date > 31))
status = false;
break;
case 2:
if (isleapyear())
{
if ((date < 1) || (date > 29))
status = false;
}
else
{
if ((date < 1) || (date > 28))
status = false;
}
break;
case 4:
case 6:
case 9:
case 11:
if ((date < 1) || (date > 30))
status = false;
break;
default:
status = false;

}
if (status)
sprintf(string, "%04d年%02d月%02d日", year, month, date);
return status;
}

Date::Date(int y, int m, int d)
{
year = y;
month = m;
date = d;
status = check();
}

Date::Date()
{
year = 1900;
month = 1;
date = 1;
status = check();
}

Date::~Date()
{
}

void Date::setYear(int y)
{
year = y;
check();
}

void Date::setMonth(int m)
{
month = m;
check();
}

void Date::setDate(int d)
{
date = d;
check();
}

int Date::getYear()
{
return year;
}

int Date::getMonth()
{
return month;
}

int Date::getDate()
{
return date;
}

char * Date::getYMD()
{
return string;
}

const Date & Date::operator ++()
{
date++;
switch (month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
if (date > 31)
{
month++;
date = 1;
if (month > 12)
{
month = 1;
year++;
}
}
break;
case 2:
if (isleapyear())
{
if (date > 29)
{
month++;
date = 1;
}
}
else
{
if (date > 28)
{
month++;
date = 1;
}
}
break;
case 4:
case 6:
case 9:
case 11:
if (date > 30)
{
month++

;
date = 1;
}
break;
default:
status = false;

}
check();
return *this;
}

bool Date::operator ==(Date d)
{
return ((year == d.getYear()) && (month == d.getMonth()) && (date == d.getDate()));
}

bool Date::operator <(Date d)
{
if (year < d.getYear())
return true;
else if (year > d.getYear())
return false;
else
{
if (month < d.getMonth())
return true;
else if (month > d.getMonth())
return false;
else
{
if (date < d.getDate())
return true;
}
}
return false;
}

bool Date::operator <=(Date d)
{
if (year < d.getYear())
return true;
else if (year > d.getYear())
return false;
else
{
if (month < d.getMonth())
return true;
else if (month > d.getMonth())
return false;
else
{
if (date <= d.getDate())
return true;
}
}
return false;
}

bool Date::operator >(Date d)
{
if (year > d.getYear())
return true;
else if (year < d.getYear())
return false;
else
{
if (month > d.getMonth())
return true;
else if (month < d.getMonth())
return false;
else
{
if (date > d.getDate())
return true;
}
}
return false;
}

bool Date::operator >=(Date d)
{
if (year > d.getYear())
return true;
else if (year < d.getYear())
return false;
else
{
if (month > d.getMonth())
return true;
else if (month < d.getMonth())
return false;
else
{
if (date >= d.getDate())
return true;
}
}
return false;
}

bool Date::operator !=(Date d)
{
return (!(*this == d));
}

const Date & Date::operator --()
{
date--;
if (date<1)
{
month--;
switch (month)
{
case 0:
year--;
month = 12;
date = 31;
check();
break;
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
date = 31;
break;
case 2:
if (isleapyear())
date = 29;
else
date = 28;
break;
case 4:
case 6:
case 9:
case 11:
date = 30;
break;
default:
status = false;
}
}
check();
return *this;
}

const Date Date::operator +( int d )
{
Date temp;
int i;
temp = *this;
for(i=0;i++temp;
return temp;
}

const Date Date::operator -( int d )
{
Date temp;
int i;
temp = *this;
for(i=0;i--temp;
return temp;
}

const Date Date::operator ++( int x )
{
Date temp = *this;
++*this;
return temp;
}

const Date Date::operator --( int x )
{
Date temp = *this;
--*this;
return temp;
}

Date & Date::operator =( Date d )
{
year = d.getYear();
month = d.getMonth();
date = d.getDate();
check();
return *this;
}

int Date::GetNumDates(Date t)
{
int NumDate;
bool sgn;
NumDate = 0;
Date d1,d2;
d1 = *this;
d2 = t;
sgn = false;
if (d1 < d2)
{
sgn = true;
}
while (d1 != d2)
{
if (sgn)
{
d1++;
NumDate++;
}
else
{
d1--;
NumDate--;
}
}
return NumDate

;
}

ostream & operator <<(ostream &stream, Date &t)
{
stream << t.getYear() << "年" << t.getMonth() << "月" << t.getDate() << "日";
return stream;
}

void main()
{
Date a(1998, 9, 29);
Date b, d, e;

cout << a << " " << b << endl;

d = a++;
cout << d << " " << a << endl;

d = ++a;
cout << d << " " << a << endl;

Date c(2000,2,27);
e = c + 20;
d = e;
d++;

cout << "e = " << c << "+20 = " << e << endl;
cout << "d=" << d << endl;
cout << "N=" << c << "-" << e << "=" << e.GetNumDates(c) << endl;
cout << "N=" << e << "-" << c << "=" << c.GetNumDates(e) << endl;

cout << " Test c++ " << endl;

c++;
cout << c << endl;

c++;
cout << c << endl;

c++;
cout << c << endl;

c++;
cout << c << endl;

Date *c1;
c1 = new Date(1900,2,27);

(*c1)++;
cout << *c1 << endl;

(*c1)++;
cout << *c1 << endl;

(*c1)++;
cout << *c1 << endl;

(*c1)++;
cout << *c1 << endl;

cout << " Test c--" << endl;

c.setYear(1998);
c.setMonth(3);
c.setDate(2);
c--;
cout << c << endl;

c--;
cout << c << endl;

c--;
cout << c << endl;

c--;
cout << c << endl;

/*
cout << a.getYear() << "." << a.getMonth() << "." << a.getDate() << endl;
cout << b.getYear() << "." << b.getMonth() << "." << b.getDate() << endl;
cout << c->getYear() << "." << c->getMonth() << "." << c->getDate() << endl;
++a;
cout << a.getYear() << "." << a.getMonth() << "." << a.getDate() << endl;
++a;
cout << a.getYear() << "." << a.getMonth() << "." << a.getDate() << endl;

d = ++a;
cout << a.getYear() << "." << a.getMonth() << "." << a.getDate() << " ";
cout << d.getYear() << "." << d.getMonth() << "." << d.getDate() << endl;

d = a++;
cout << a.getYear() << "." << a.getMonth() << "." << a.getDate() << " ";
cout << d.getYear() << "." << d.getMonth() << "." << d.getDate() << endl;

e = a + d;
cout << e;

printf("%s\n",a.getYMD());
printf("%s\n",e.getYMD());
*/
return;
}

相关文档