//////////////////////////////////////////////////////////
//Date.h
//////////////////////////////////////////////////////////
#ifndef DATE_H
#define DATE_H
class Date
{
protected:
static const int dys[];
long int ndays; //从公元1年1月1日至今的天数(1/1/1==1)
public:
//取默认值并初始化构造函数
Date(int mo = 0, int da = 0, int yr = 0)
{SetDate(mo, da, yr);}
//复制构造函数
Date(const Date& dt)
{ *this = dt;}
//析构函数
virtual ~Date(){}
//重载赋值运算符
Date& operator=(const Date& dt)
{ ndays = dt.ndays; return *this;}
//重载算术运算符
Date operator+(int n) const
{ Date dt(*this); dt += n; return dt;}
Date operator-(int n) const
{ Date dt(*this); dt -= n; return dt;}
Date operator+=(int n)
{ ndays += n; return *this;}
Date operator-=(int n)
{ ndays -= n; return *this;}
Date& operator++()//前缀
{ ++ndays; return *this;}
Date operator++(int)//后缀
{ Date dt(*this); dt.ndays++; return dt;}
Date& operator--()//前缀
{ --ndays; return *this;}
Date operator--(int)
{ Date dt(*this); dt.ndays--; return dt;}
long int operator-(const Date& dt) const
{ return ndays-dt.ndays;}
//---重载关系运算符
bool operator==(const Date& dt) const
{ return ndays == dt.ndays;}
bool operator!=(const Date& dt) const
{ return ndays != dt.ndays;}
bool operator < (const Date& dt) const
{ return ndays < dt.ndays;}
bool operator > (const Date& dt) const
{ return ndays > dt.ndays;}
bool operator<=(const Date& dt) const
{ return ndays <= dt.ndays;}
bool operator>=(const Date& dt) const
{ return ndays >= dt.ndays;}
//Getter和Setter函数
void SetDate(int mo, int da, int yr);
void SetMonth(int mo)
{ *this = Date(mo, GetDay(), GetYear());}
void SetDay(int da)
{ *this = Date(GetMonth(), da, GetYear());}
void SetYear(int yr)
{ *this = Date(GetMonth(), GetDay(), yr);}
void GetDate(int& mo, int& da, int& yr) const;
int GetMonth() const;
int GetDay() const;
int GetYear() const;
//显示方法
virtual void Display() const;
//检测闰年
bool IsLeapYear(int yr) const
{ return (( yr % 4) ==0 && (yr % 1000)!=0);}
bool IsLeapYear() const
{ return IsLeapYear(GetYear());}
};
//重载(int + Date)
inline Date operator+(int n, const Date& dt)
{
return dt + n;
}
#endif
/////////////////////////////////////////////
//Date.cpp
/////////////////////////////////////////////
#include <iostream>
#include "Date.h"
const int Date::dys[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
////////////////////////////////////////////////////////////////////////////
//SetDate函数实现
////////////////////////////////////////////////////////////////////////////
void Date::SetDate(int mo, int da, int yr)
{
if (mo < 1 || mo >12 || yr < 1)
{
ndays = 0; //无效的月或年或空日期
return;
}
//计算上一年的天数
ndays = (yr-1) * 365 + (yr -1) / 4 - (yr - 1) / 1000;
for ( int i = 0; i < mo; i++)
{
int dy = dys[i];
if (i == 1 && IsLeapYear(yr))
dy++; //如果是闰年,就认为二月的天数为29
if (i < mo-1) //加所有的天数但不包括本月的天数
ndays += dy;
else if (da > dy)
{
ndays = 0; //无效的日子
return;
}
}
ndays += da; //加本月天数
}
///////////////////////////////////////////////////////////////
//GetDate()函数实现
////////////////////////////////////////////////////////////////
void Date::GetDate(int& mo, int& da, int& yr) const
{
da = ndays;
if ( ndays == 0)
{
yr = mo = 0;
return;
}
for (yr = 1; ; yr++)
{
int daysthisyear = IsLeapYear(yr) ? 366 :365;
if (da <= daysthisyear)
break;
da -= daysthisyear;
}
for (mo = 1; mo < 13; mo++)
{
int dy = dys[mo-1];
if (mo == 2 && IsLeapYear(yr))
dy++; //如果是闰年,就认为二月的天数为29
if (da <= dy)
break;
da -= dy;
}
}
//////////////////////////////////////////////////////////////////
//GetMonth()函数实现
//////////////////////////////////////////////////////////////////
int Date::GetMonth() const
{
int mo, da, yr;
GetDate(mo, da, yr);
return mo;
}
//////////////////////////////////////////////////////////////////
//Getday()函数实现
//////////////////////////////////////////////////////////////////
int Date::GetDay() const
{
int mo, da, yr;
GetDate(mo, da, yr);
return da;
}
///////////////////////////////////////////////////////////////////
//GetYear()函数实现
///////////////////////////////////////////////////////////////////
int Date::GetYear() const
{
int mo, da, yr;
GetDate(mo, da, yr);
return yr;
}
/////////////////////////////////////////////////////////////////////
//Display()函数实现
/////////////////////////////////////////////////////////////////////
void Date::Display() const
{
int mo, da, yr;
GetDate(mo, da, yr);
std::cout << mo << '/' << da << '/' << yr;
}
////////////////////////////////////
//CustomDate.h
////////////////////////////////////
#ifndef CUSTOMDATE_H
#define CUSTOMDATE_H
#include "Date.h"
class CustomDate : public Date
{
public:
CustomDate(int yr, int da);
int GetDay() const;
virtual void Display() const;
};
#endif
////////////////////////////////////////////////////
//CustomDate.cpp
////////////////////////////////////////////////////
#include <iostream>
#include "CustomDate.h"
//////////////////////////////////////////////////////
//构造函数实现
//////////////////////////////////////////////////////
CustomDate::CustomDate(int yr, int da)
{
if (yr <1 || da < 1 || da > (IsLeapYear(yr) ? 366 : 365))
return;
int mo;
for (mo = 1; mo < 13; mo++)
{
int dy = dys[mo-1];
if (mo == 2 && IsLeapYear(yr))
dy++;
if (da <= dy)
break;
da -= dy;
}
SetDate(mo, da, yr);
}
//////////////////////////////////////////////////////
//GetDay()实现
//////////////////////////////////////////////////////
int CustomDate::GetDay() const
{
int mo, da, yr;
GetDate(mo, da, yr);
int day = 0;
for (int m = 1; m < mo; m++)
{
day += dys[m-1];
if (m == 2 && IsLeapYear(yr))
day++;
}
return day + da;
}
///////////////////////////////////////////////////////
//Display()实现
///////////////////////////////////////////////////////
void CustomDate::Display() const
{
std::cout << GetDay() << '-' << GetYear();
}
///////////////////////////////////////////
//SpecialDate.h
///////////////////////////////////////////
#ifndef SPECIALDATE_H
#define SPECIALDATE_H
#include "Date.h"
class SpecialDate:public Date
{
static char* mos[];
public:
SpecialDate(int da, int mo, int yr):Date(mo, da, yr){}
virtual void Display() const;
bool IsNullDate() const
{ return ndays == 0;}
};
#endif
/////////////////////////////////////////////////
//SpecialDate.cpp
/////////////////////////////////////////////////
#include <iostream>
#include "SpecialDate.h"
char* SpecialDate::mos[] =
{
"January", "February", "March", "April",
"May", "June", "July", "August",
"September", "October", "November", "December"
};
void SpecialDate::Display() const
{
if (!IsNullDate())
std::cout << mos[GetMonth()-1] << ' '
<< GetDay() << ", " << GetYear();
}
前面代码经过测试编译完全正确
/////////////////////////////////
//SpecialCustomDate.h
/////////////////////////////////
#ifndef SPECIALCUSTOMDATE_H
#define SPECIALCUSTOMDATE_H
#include "SpecialDate.h"
#include "CustomDate.h"
class SpecialCustomDate
{
public:
SpecialCustomDate(int yr, int da) : SpecialDate(0,0,0)
{
CustomDate cd(yr, da);
SetDate(cd.GetMonth(),cd.Date::GetDay(),cd.GetYear());
}
};
#endif
///////////////////////
//E1711
///////////////////////
#include <iostream>
#include "SpecialCustomDate.h"
int main()
{
SpecialCustomDate scdt(1941, 321);
std::cout << std::endl << "Judy: ";
scdt.Display();
return 0;
}
哪位大虾能帮我找出里面的错误吗?