问题描述:
6-3 【CPP0005】定义日期类Date定义一个日期类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
代码:
#include<iomanip>
class Date
{
public:
Date(int a=1,int b=1,int c=1)
{
year=a;
month=b;
day=c;
cout<<"Constructor run"<<endl;
}
Date(Date &d)
{
year=d.year;
month=d.month;
day=d.day;
cout<<"CopyConstructor run"<<endl;
}
~Date()
{
cout<<"Destructor run"<<endl;
}
void setYear(int a)
{
year=a;
}
int getYear()
{
return year;
}
void setMonth(int b)
{
if(b<1)
b=1;
if(b>12)
b=12;
month=b;
}
int getMonth()
{
return month;
}
void setDay(int c)
{
{
if(c<1)
c=1;
if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)//根据不同的月份,来修正错误输入
{if(c>31) c=31;}
else
if(month==4||month==6||month==9||month==11)
{if(c>30) c=30;}
else
{if(isLeapYear(year)) {if(c>29) c=29;} else {if(c>28) c=28;}}
}
day=c;
}
int getDay()
{
return day;
}
void tomorrow( )
{
day++;
switch(month)
{
case 1:if(day>31) {month++,day=1;};break;
case 2:if(isLeapYear(year)){if(day>29){month++,day=1;}} else {if(day>28){month++,day=1;}}break;
case 3:if(day>31) {month++,day=1;};break;
case 4:if(day>30) {month++,day=1;};break;
case 5:if(day>31) {month++,day=1;};break;
case 6:if(day>30) {month++,day=1;};break;
case 7:if(day>31) {month++,day=1;};break;
case 8:if(day>31) {month++,day=1;};break;
case 9:if(day>30) {month++,day=1;};break;
case 10:if(day>31) {month++,day=1;};break;
case 11:if(day>30) {month++,day=1;};break;
case 12:if(day>31) {year++,month=1,day=1;};break;
}
}
void yesterday( )
{
day--;
switch(month)//如果day--后day小于1,则说明yesterday是上月的最后一天
{
case 1:if(day<1) {year--,month=12,day=31;};break;
case 2:if(day<1) {month--,day=31;};break;
case 3:if(isLeapYear(year)){if(day<1){month--,day=29;}} else {if(day<1){month--,day=28;}};break;
case 4:if(day<1) {month--,day=31;};break;
case 5:if(day<1) {month--,day=30;};break;
case 6:if(day<1) {month--,day=31;};break;
case 7:if(day<1) {month--,day=30;};break;
case 8:if(day<1) {month--,day=31;};break;
case 9:if(day<1) {month--,day=31;};break;
case 10:if(day<1) {month--,day=30;};break;
case 11:if(day<1) {month--,day=31;};break;
case 12:if(day<1) {month--,day=30;};break;
}
}
void printMonthCalendar( )
{
int i,j,k;
int const rili[31]={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};
k=weekDay();
j=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<<setw(3)<<rili[i];
}
if(month==14&&isLeapYear(year+1)==1)
{
if(i%7==7-j)
{cout<<endl;}
cout<<setw(3)<<rili[28];
}
if(month==13||month==3||month==5||month==7||month==8||month==10||month==12)//打印天数为31天的月份的结尾
{
for( ;i<31;i++)
{
if(i%7==7-j)
cout<<endl;
cout<<setw(3)<<rili[i];
}
}
if(month==4||month==6||month==9||month==11)
{
for( ;i<30;i++)
{
if(i%7==7-j)
cout<<endl;
cout<<setw(3)<<rili[i];
}
}
cout<<endl;
}
else
{
for(i=0;i<28;i++)
{
cout<<setw(3)<<rili[i];
if(i%7==6&&i!=27)
cout<<endl;
}
if(month==14&&isLeapYear(year+1)==1)
{
cout<<endl;
cout<<setw(3)<<rili[28];
}
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<<setw(3)<<rili[i];
}
}
if(month==4||month==6||month==9||month==11)
{
for( ;i<30;i++)
{
if(i%7==0)
cout<<endl;
cout<<setw(3)<<rili[i];
}
}
cout<<endl;
}
}
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 year;
int month;
int day;
int isLeapYear(int year)
{
if(((year%4)==0&&(year%100)!=0)||(year%400)==0)
return 1;
else
return 0;
}
};
标签:case,第一个,int,month,问题,year,第十七,day,cout
From: https://www.cnblogs.com/czfznb/p/17386339.html