c++primer 9.52答案

萌新业余时间学c++中,学过一点c之前,下面一个自己写的9.52答案,尽量用到了前面学到的知识,与大加分享交流一下,希望能互相讨论学习。

/**
****************************************************************************************
* @FilePath: MyDate.h
* @Author: YMM
* @Date: 2024-04-07 22:00:13
* @LastEditors:
* @LastEditTime: 2024-04-07 22:00:13
* @Copyright: 2024 xxxTech CO.,LTD. All Rights Reserved.
* @Descripttion:
****************************************************************************************
*/
#ifndef _MYDATE_
#define _MYDATE_
#include <string>
#include <vector>
class MyDate
{
public:
    MyDate(const std::string &Date)
    {
        int i = 1;
        Month = 0;
        for (const auto &it : MonthVector)
        {

            if (Date.find(it) != std::string::npos) // 匹配到月份单词
            {
                Month = i;
            }
            ++i;
        }
        std::string str(" /");
        auto be = Date.begin() + Date.find_first_of(str);
        auto en = Date.begin() + Date.find_last_of(str);
        std::string Y(en + 1, Date.end()),M(Date.begin(),be), D(be + 1, en);
        Year = stoi(Y);
        if(0 ==Month)
        {
            Month = stoi(M);
        }
        Day = stoi(D);
    }
    using u32 = unsigned int;
    u32 GetYear() const { return this->Year; }
    u32 GetMonth() const { return this->Month; }
    u32 GetDay() const { return this->Day; }
private:
    u32 Year;
    u32 Month;
    u32 Day;
    std::vector<std::string> MonthVector{
        "January", "February", "March", "April", "May", "June", "August",
        "September", "October", "November", "December"};
};

#endif

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值