6-3 【CPP0005】定义日期类Date (10 分)

本文介绍了一个C++实现的Date类,包含了构造函数、拷贝构造函数、析构函数、日期设置和获取、日期加减、打印日历、日期格式转换等方法。在测试程序中,展示了类的使用,如日期的前一日和后一日操作,以及中英文日期格式输出和打印指定月份的日历。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

定义一个日期类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<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"};

#include<iomanip>//setw函数所需要的头文件
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)//如果day++后day大于本月月底,则说明tomorrow是下月的第一天
        {
            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++)//每月至少有28天,先打印完成
                {
                    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)//打印天数为30天的月份的结尾
                {
                    for( ;i<30;i++)
                {
                    if(i%7==7-j)
                        cout<<endl;
                    cout<<setw(3)<<rili[i];
                }
                }
                        cout<<endl;
        }
        else//第一行没有空格情况,换行需要通过i%7==6控制
        {
            for(i=0;i<28;i++)//每行七个,输出28天
            {
                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)//打印天数31天的月份的结尾
                {
                    for( ;i<31;i++)
                {
                    if(i%7==0)
                        cout<<endl;
                    cout<<setw(3)<<rili[i];
                }
                }
                if(month==4||month==6||month==9||month==11)//打印天数30天的月份的结尾
                {
                    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)//基姆拉尔森公式规定,每年1月为去年13月
        {
            year--;
            month=13;
        }
        if(month==2)//基姆拉尔森公式规定,每年2月为去年14月
        {
            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;
    }
};

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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值