zoukankan      html  css  js  c++  java
  • [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;
    }
  • 相关阅读:
    [Cocos2d-x]布局与定位
    [Cocos2d-x]创建项目
    nylg-154-king 选 太子
    nylg-153-king VS king
    hdoj-2053-Switch Game
    hdoj-2052-Picture
    hdoj-2051-Bitset
    hdoj-2050-折线分割平面
    大一c语言课程设计-学籍管理系统
    hdoj-2049-不容易系列之(4)——考新郎
  • 原文地址:https://www.cnblogs.com/lightmonster/p/11434531.html
Copyright © 2011-2022 走看看