定义日期类Date

发布时间 2023-05-21 13:58:18作者: 不如喝点

定义一个日期类Date,main()函数完成对其的测试。

Date类结构说明:

 
Date类的数据成员包括:
①私有数据成员:年year(int型),月month(int型),日day(int型)。
Date类成员函数包括:
①定义有参构造函数Date(int ,int ,int )和拷贝构造函数Date(Date &),其中有参构造函数参数默认值为1,输出信息“Constructor run”,拷贝构造函数输出信息“CopyConstructor run”
②定义析构函数,析构函数输出信息“Destructor run”
③公有函数成员:void  setYear(int )和int  getYear()分别设置和返回year
④公有函数成员:void  setMonth(int )和int  getMonth()分别设置和返回month
⑤公有函数成员:void  setDay(int )和int  getday()分别设置和返回day
⑥公有函数成员:void  tomorrow( )和void  yesterday( )实现日期加/减一天操作
⑦公有函数成员:void  printMonthCalendar( )打印当月日历,格式如下(每列宽度为3位,右对齐)(以2016年3月为例)
SunMonTueWedThuFriSat
        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
⑧公有函数成员:void  chineseFormat( )和void  americaformat( )分别显示中式日期(例:2014年4月21日)和美式日期(例:Api 21, 2014),其中美式格式中1~12月分别用Jan、Feb、Mar、Api、May、Jun、Jul、Aug、Sep、Oct、Nov、Dec表示
⑨私有函数成员:int  isLeapYear( )确定当前年份是否为闰年,闰年返回1,否则返回0;
⑩公有函数成员:int  weekDay( )返回该天是星期几,0~6分别表示Sun、Mon、Tue、Wed、Thu、Fri、Sat
 

注:在以上成员函数执行过程中,若输入的日期参数值超界,则按照最接近输入参数的有效日期值对待,例如:

假定输入的month>12,则按照12对待,若输入的month<1,则按照1对待;

假定当前month为12月,则若输入的day>31,则按照31对待,若输入的day<1,则按照1对待;

提示:cout.width(int)设置输出项的宽度

裁判测试程序样例:

 
#include<iostream>
using namespace std;
int const monthDay[12]={31,28,31,30,31,30,31,31,30,31,30,31};
char* const weekName[7]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
char* const monthName[12]={"Jan","Feb","Mar","Api","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};

/*请在这里填写答案*/

int main(){
    int year,month,day;
    Date d1;
    Date d2(d1);
    cin>>year>>month>>day;
    d1.setYear(year);
    d1.setMonth(month);
    d1.setDay(day);
    d1.yesterday();
    d1.chineseFormat();
    cin>>year>>month>>day;
    d2.setYear(year);
    d2.setMonth(month);
    d2.setDay(day);
    d2.tomorrow();
    d2.americaformat();
    d2.printMonthCalendar();
    return 0;
}
 

输入样例:

2019 3 27
2016 2 31
 

输出样例:

Constructor run
CopyConstructor run
2019年3月26日
Mar 1,2016
SunMonTueWedThuFriSat
        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
Destructor run
Destructor run



class Date
{
public:
Date(int x=1,int y=1,int z=1)
{
year=x;
month=y;
day=z;
cout<<"Constructor run"<<endl;
}
Date(Date &p)
{
year=p.year;
month=p.month;
day=p.day;
cout<<"CopyConstructor run"<<endl;
}
~Date()
{
cout<<"Destructor run"<<endl;
}
void setYear(int x)
{
year=x;
}
int getYear()
{
return year;
}
void setMonth(int m)
{
if(m<=12&&m>=1)
{
month=m;
}
else if(m>12)
{
month=12;
}
else
{
month=1;
}
}
int getMonth()
{
return month;
}
void setDay(int n)
{
if(n<=monthDay[month-1]&&n>=1)
{
day=n;
}
else if(n>monthDay[month-1])
{
day=monthDay[month-1];
}
else
{
day=1;
}
}
int getday()
{
return day;
}
void tomorrow()
{
day++;
if(day>monthDay[month-1])
{
month++;
day=1;
}
}
void yesterday()
{
day--;
if(day<1)
{
month--;
day=monthDay[month-1];
}
}
void printMonthCalendar()
{
int i,j,k;
int q=1;
j=k=weekDay();
cout<<"SunMonTueWedThuFriSat"<<endl;
while(k>=1&&k<=6)
{
cout<<" ";
k--;
}
if(j!=0)
{
for(i=0;i<28;i++)
{
if(i%7==7-j)
cout<<endl;
cout.width(3);cout<<q++;
}
if(month==14&&isLeapYear(year+1)==1)
{
if(i%7==7-j)
{cout<<endl;}
cout.width(3);cout<<q++;
}
if(month==13||month==3||month==5||month==7||month==8||month==10||month==12)
{
for( ;i<31;i++)
{
if(i%7==7-j)
cout<<endl;
cout.width(3);cout<<q++;
}
}
if(month==4||month==6||month==9||month==11)
{
for( ;i<30;i++)
{
if(i%7==7-j)
cout<<endl;
cout.width(3);cout<<q++;
}
}
cout<<endl;
}
else
{
for(i=0;i<28;i++)
{
cout.width(3);cout<<q++;
if(i%7==6&&i!=27)
cout<<endl;
}
if(month==14&&isLeapYear(year+1)==1)
{
cout<<endl;
cout.width(3);cout<<q++;
}

if(month==13||month==3||month==5||month==7||month==8||month==10||month==12)
{
for( ;i<31;i++)
{
if(i%7==0)
cout<<endl;
cout.width(3);cout<<q++;
}
}
if(month==4||month==6||month==9||month==11)
{
for( ;i<30;i++)
{
if(i%7==0)
cout<<endl;
cout.width(3);cout<<q++;
}
}
}
}
void chineseFormat()
{
cout<<year<<"年"<<month<<"月"<<day<<"日"<<endl;
}
void americaformat()
{
cout<<monthName[month-1]<<" "<<day<<","<<year<<endl;
}
int weekDay( )
{
int i;
if(month==1)
{
year--;
month=13;
}
if(month==2)
{
year--;
month=14;
}
i=(day+2*month+3*(month+1)/5+year+year/4-year/100+year/400)%7;
switch(i)
{
case 0:return 1;break;
case 1:return 2;break;
case 2:return 3;break;
case 3:return 4;break;
case 4:return 5;break;
case 5:return 6;break;
case 6:return 0;break;
}
}
private:
int isLeapYear(int year)
{
if(((year%4)==0&&(year%100)!=0)||(year%400)==0)
{
return 1;
}
else{
return 0;
}
}
private:
int year;
int month;
int day;
};