#include<Windows.h>
#include<iostream>
typedef unsigned int uint;
using namespace std;
class Date
{
private:
uint DaysInThisYear;
uint DaysInThisMonth;
uint Year;
uint Month;
uint Day;
~Date();//防止对象在栈中初始化,在栈中初始化会导致变量无法销毁。
void DataCopy(Date& CopySource);
public:
Date(uint year, uint month, uint day);
void OutPutDate()const;
friend int GetDaysOfAMonth(int year, int month);
friend int GetDaysOfAYear(int year);
uint GetDaysInThisMonth()const;
uint GetDaysInThisYear()const;
void ChangeDay(uint day);
void ChangeMonth(uint month);
void ChangeYear(uint year);
};
/*私有函数列表*/
void Date::DataCopy(Date& CopySource)
{
DaysInThisMonth = CopySource.DaysInThisMonth;
DaysInThisYear = CopySource.DaysInThisYear;
Year = CopySource.Year;
Month = CopySource.Month;
Day = CopySource.Day;
}
/*公有函数列表*/
Date::Date(uint year, uint month, uint day)
{
Year = year;
DaysInThisYear = GetDaysOfAYear(year);
if (month > 0 && month < 13)
{
Month = month;
}
DaysInThisMonth = GetDaysOfAMonth(year,month);
if (day > DaysInThisMonth)
{
abort();
}
Day = day;
}
void Date::OutPutDate()const
{
cout << Year << "年" << Month << "月" << Day << "日";
}
int GetDaysOfAMonth(int year,int month)
{
{
switch (month)
{
case 2:
{
if (year == 366)
{
return 29;
}
else
{
return 28;
}
break;
}
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:return 31; break;
default:
return 30;
}
}
}
int GetDaysOfAYear(int year)
{
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
{
return 366;
}
else
{
return 365;
}
}
uint Date::GetDaysInThisMonth()const
{
return DaysInThisMonth;
}
uint Date::GetDaysInThisYear()const
{
return DaysInThisYear;
}
void Date::ChangeDay(uint day)
{
if (day < DaysInThisMonth && day>0)
{
Day = day;
}
else
{
abort();
}
}
void Date::ChangeMonth(uint month)
{
if (month > 0 && month < 13)
{
Month = month;
}
else
{
abort();
}
}
void Date::ChangeYear(uint year)
{
if (year > 0)
{
Year = year;
}
else
{
abort();
}
}
日期类
最新推荐文章于 2022-11-30 11:02:19 发布