[C++]不同格式年月日提取程序

class YMD {
private:
    unsigned int year;
    unsigned int month;
    unsigned int day;
public:
    YMD(string date);
    void show() {
        cout << "today is " << year << "-" << month << "-" << day << endl;
    }
};
YMD::YMD(string date) {
    int flag = 0;
    string num("1234567890/");
    string comma(",");
    string space(" ");
    string mon;
    int pos1, pos2, pos3;
    if (date.find_first_not_of(num) == string::npos) {
        flag = 1;
    }
    if (date.find_first_of(comma) != string::npos) {
        flag = 2;
    }
    switch (flag)
    {
    case 0:
        pos1 = date.find_first_of(num);
        mon = date.substr(0, pos1);
        if (mon == "Jan ") month = 1;
        if (mon == "Feb ") month = 2;
        if (mon == "Mar ") month = 3;
        if (mon == "Apr ") month = 4;
        if (mon == "May ") month = 5;
        if (mon == "Jun ") month = 6;
        if (mon == "Jul ") month = 7;
        if (mon == "Aug ") month = 8;
        if (mon == "Sep ") month = 9;
        if (mon == "Oct ") month = 10;
        if (mon == "Nov ") month = 11;
        if (mon == "Dec ") month = 12;
        pos2 = (date.substr(pos1)).find_first_of(space) + pos1;
        day = stoul(date.substr(pos1, pos2));
        year = stoul(date.substr(pos2 + 1, date.size() - 1));
        break;
    case 1:
        pos1 = date.find_first_of("/");
        day = stoul(date.substr(0, pos1));
        pos2 = (date.substr(pos1+1)).find_first_of("/") + pos1+1;
        month = stoul(date.substr(pos1 + 1, pos2));
        year = stoul(date.substr(pos2 + 1, date.size() - 1));
        break;
    case 2:
        pos1 = date.find_first_of(num);
        mon = date.substr(0, pos1);
        if (mon == "Jan ") month = 1;
        if (mon == "Feb ") month = 2;
        if (mon == "Mar ") month = 3;
        if (mon == "Apr ") month = 4;
        if (mon == "May ") month = 5;
        if (mon == "Jun ") month = 6;
        if (mon == "Jul ") month = 7;
        if (mon == "Aug ") month = 8;
        if (mon == "Sep ") month = 9;
        if (mon == "Oct ") month = 10;
        if (mon == "Nov ") month = 11;
        if (mon == "Dec ") month = 12;
        pos2 = date.substr(pos1).find(comma) + pos1;
        day = stoul(date.substr(pos1, pos2));
        year = stoul(date.substr(pos2 + 1, date.size() - 1));
        break;
    default:
        break;
    }
}
int main()
{
    YMD s("Jan 8,2019");
    s.show();
    return 0;
}

 

原文链接: https://www.cnblogs.com/lightmonster/p/11434531.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    [C++]不同格式年月日提取程序

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/301752

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月15日 下午10:48
下一篇 2023年2月15日 下午10:49

相关推荐