从CDate类派生一个新类CDateMultiRegion,使其不仅可以显示中国习惯的日期格式(年月日),还可以显示美国习惯的日期格式(月日年)和英国习惯的日期格式(日月年)。

输入格式

年、月、日、国家缩写(uk、cn、us三种之一),空格隔开

输出格式

日期增加一天后斜线隔开输出。依然请使用成员函数showDate()完成

输入样例 

2022 5 6 uk
2022 12 31 cn
2000 2 29 us

输出样例 

7/5/2022
2023/1/1
3/1/2000

示例:

 

# include<iostream>
using namespace std;
class  CDate
{
public:
    int Year, Month, Day;
    string country;
    CDate()
    {
        Year = 1;
        Month = 1;
        Day = 1;
    }
    void setDate(int Y, int M, int D);
    void increaseDate(int Y, int M, int D);
    void showDate();
};
void CDate::setDate(int Y, int M, int D) { Year = Y; Month = M; Day = D; }
void CDate::showDate() 
{
    if (country == "uk")
        cout <<Day<< "/" << Month << "/" << Year << endl;
    else if (country == "cn")
        cout << Year << "/" << Month << "/" << Day << endl;
    else if (country == "us")
        cout << Month << "/" << Day << "/" << Year  << endl;
}
//分析不同日期加一天的情况
void CDate::increaseDate(int Y, int M, int D)
{
    if (M == 2)//二月的最后一天比较特殊,单独分析
    {
        if (Y % 4 == 0 && Y % 100 != 0 || Y % 400 == 0)//闰年2月最后一天为29号
        {
            if (D == 29)//如果是29号,则后一天为三月一号
            {
                Day = 1;//月期加一
                Month += 1;//日期为一
            }
            else
            {
                Day += 1;//其他日期,天数加一
            }
        }
        else//非闰年
        {
            if (D == 28)//如果是28号,则后一天为四月一号
            {
                Day = 1;
                Month += 1;
            }
            else
            {
                Day += 1;//其他日期,天数加1
            }
        }
    }
    else if (M == 12 && D == 31)//12月31号后一天为一月一号
    {
        Year += 1;//年份加1
        Day = 1;//日期1
        Month = 1;//月期1

    }
    else if (D == 30)//日期为30的分两种情况
    {
        if (M == 1 || 3 || 5 || 7 || 8 || 10 || 12)//大月有31天
        {
            Day += 1;//日期加一天
        }
        else//除二月的小月
        {
            Month += 1;//月期加1
            Day = 1;//日期为1
        }
    }
    else if (M != 12 && D == 31)//非12月的第31天后一天是下个月1号
    {
        Day = 1;//日期1
        Month += 1;///月份加1
    }
    else
    {
        Day += 1;
    }
}

class CDateMultiRegion : public CDate
{
public:
    void getct(string Country);
    void setDate(int Y, int M, int D);
};
void CDateMultiRegion::getct(string Country) { country = Country; }
void CDateMultiRegion::setDate(int Y, int M, int D)  { Year = Y; Month = M; Day = D;}

int main()
{
    int y, m, d;
    string country;
    CDateMultiRegion mydate;
    while (cin >> y >> m >> d >> country)
    {
        mydate.getct(country);
        mydate.setDate(y, m, d);
        mydate.increaseDate(y,m,d);
        mydate.showDate();
    }
    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值