代码如下:
头文件Date.h
#pragma once
#include <string>
#include<sstream>
using std::string;
using std::stringstream;
class Date
{
public:
enum Month{ jan = 1, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec };
// constructed function
Date();
Date(int d,Month m,int y);
Date(const Date&d);
Date& operator=(const Date&d);
// get the date
int get_day()const;
Month get_month()const;
int get_year()const;
string string_rep() const;
void char_rep(char s[])const;
// relation calc
bool operator==(const Date &d)const;
bool operator!=(const Date &d)const;
bool operator>(const Date &d)const;
bool operator<(const Date &d)const;
bool operator<=(const Date &d)const;
bool operator>=(const Date &d)const;
// increase
Date &operator++(int);
Date &operator++();
Date &operator--(int);
Date &operator--();
// assignment
Date& operator+=( int n);
Date& operator-=( int n);
Date& operator+( int n);
Date& operator-( int n);
protected:
static void set_default(int d, Month m, int y);
void IncDay();
bool IsEndOfMonth()const;
void IncMonth();
void DecMonth();
void DecDay();
int GetDayOfMonth()const;
// exception class
class Bad_date{};
private:
int day_, year_;
Month month_;
static Date default_date;
};
bool IsLeapYear(int y);
#include "stdafx.h"
#include "Date.h"
#include <iostream>
using namespace std;
Date Date::default_date(1, jan, 1970);
void Date::set_default(int d, Month m, int y)
{
default_date = Date(d, m, y);
}
// constucted function ///
Date::Date()
{
*this = default_date;
}
Date::Date(int d,Month m,int y)
{
if (d == 0) d = default_date.get_day();
if (m == 0) m = default_date.get_month();
if (y == 0) y = default_date.get_year();
int max;
switch (m)
{
case feb:
max = 29 + IsLeapYear(y);
break;
case apr:case jun:case sep:case nov:
max = 30;
break;
case jan:case may:case jul:case aug:case oct:case dec:
max = 31;
break;
default:
throw Bad_date();
}
if (d < 1 || max < d)throw Bad_date();
day_ = d;
month_ = m;
year_ = y;
}
// 复制构造函数
Date::Date(const Date&d)
{
day_ = d.get_day();
month_ = d.get_month();
year_ = d.get_year();
}
// 赋值构造函数
Date& Date::operator=(const Date&d)
{
if (*this != d)
{
year_ = d.get_year();
month_ = d.get_month();
day_ = d.get_day();
}
return *this;
}
// get the date /
int Date::get_day() const
{
return day_;
}
Date::Month Date::get_month() const
{
return month_;
}
int Date::get_year() const
{
return year_;
}
string Date::string_rep() const
{
string s,st;
stringstream ss;
ss << this->year_;
ss>>st;
s += st;
s += '/';
ss.clear();
ss << this->month_;
ss >> st;
s += st;
s += '/';
ss.clear();
ss << this->day_;
ss >> st;
s += st;
return s;
}
void Date::char_rep(char s[]) const
{
string sdate = string_rep();
strcpy_s(s, sdate.length(), sdate.c_str());
}
int Date::GetDayOfMonth() const
{
int day = 0;
switch (this->month_)
{
case jan: day = 31; break;
case feb:
if (month_ == feb && IsLeapYear(year_))
day = 29;
else
day = 28;
break;
case mar: day = 31; break;
case apr: day = 30; break;
case may: day = 31; break;
case jun: day = 30; break;
case jul: day = 31; break;
case aug: day = 31; break;
case sep: day = 30; break;
case oct: day = 31; break;
case nov: day = 30; break;
case dec: day = 31; break;
default:
throw Bad_date();
break;
}
return day;
}
// relation calc
bool Date::operator==(const Date &d) const
{
return year_ == d.year_ &&
month_ == d.month_ &&
day_ == d.day_;
}
bool Date::operator!=(const Date& d) const
{
return !(*this == d);
}
bool Date::operator>(const Date& d) const
{
if (year_ > d.year_)
{
return true;
}
if (year_ == d.year_)
{
if (month_ > d.month_)
{
return true;
}
if (month_ == d.month_)
{
if (day_ > d.day_)
return true;
}
}
return false;
}
bool Date::operator>=(const Date&d)const
{
return (*this > d) || (*this == d);
}
bool Date::operator<(const Date& d) const
{
return !(*this>=d);
}
bool Date::operator<=(const Date& d) const
{
return !(*this>d);
}
// increase calc //
bool Date::IsEndOfMonth() const
{
if (month_ == feb && IsLeapYear(year_))
return day_ == 29;
else
return day_ == GetDayOfMonth();
}
void Date::IncMonth()
{
switch (this->month_)
{
case jan:
this->month_ = feb;
break;
case feb:
this->month_ = mar;
break;
case mar:
this->month_ = apr;
break;
case apr:
this->month_ = may;
break;
case may:
this->month_ = jun;
break;
case jun:
this->month_ = jul;
break;
case jul:
this->month_ = aug;
break;
case aug:
this->month_ = sep;
break;
case sep:
this->month_ = oct;
break;
case oct:
this->month_ = nov;
break;
case nov:
this->month_ = dec;
break;
case dec:
this->month_ = jan;
break;
default:
throw Bad_date();
break;
}
}
void Date::IncDay()
{
if (IsEndOfMonth())
{
if (month_ == dec) // 如果是12月最后一天,则加1年
{
day_ = 1; month_ = jan; year_++;
}
else // 如果是本月最后一天,则加1月
{
day_ = 1; IncMonth();
}
}
else
day_++;
}
void Date::DecMonth()
{
switch (this->month_)
{
case feb:
this->month_ = jan;
break;
case mar:
this->month_ = feb;
break;
case apr:
this->month_ = mar;
break;
case may:
this->month_ = apr;
break;
case jun:
this->month_ = may;
break;
case jul:
this->month_ = jun;
break;
case aug:
this->month_ = jul;
break;
case sep:
this->month_ = aug;
break;
case oct:
this->month_ = sep;
break;
case nov:
this->month_ = oct;
break;
case dec:
this->month_ = nov;
break;
case jan:
this->month_ = dec;
break;
default:
throw Bad_date();
break;
}
}
void Date::DecDay() // dec one day
{
if (day_ == 1)
{
if (month_ == jan) // 如果是1月,则变为12月,并且减1年
{
month_ = dec; year_--; day_ = GetDayOfMonth();
}
else // 否则减1月
{
DecMonth(); day_ = GetDayOfMonth();
}
}
else
day_--;
}
Date& Date::operator++(int)
{
IncDay();
return *this;
}
Date& Date::operator++()
{
IncDay();
return *this;
}
Date& Date::operator--(int)
{
DecDay();
return *this;
}
Date& Date::operator--()
{
DecDay();
return *this;
}
Date& Date::operator+=( int n)
{
for (int i = 0; i < n; i++)
IncDay();
return *this;
}
Date& Date::operator-=( int n)
{
for (int i = n; i > 0; i--)
DecDay();
return *this;
}
Date& Date::operator+( int n)
{
for (int i = 0; i < n; i++)
IncDay();
return *this;
}
Date& Date::operator-( int n)
{
for (int i = n; i < 0; i--)
DecDay();
return *this;
}
// frend fun //
bool IsLeapYear(int y) // y == 0 ???
{
if (y % 400 == 0 || y % 100 != 0 && y % 4 == 0)
{
return true;
}
return false;
}