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;
    }
  • 相关阅读:
    本地化中文示例代码需求调查
    微软一站式示例代码库下载过热导致下载计数器溢出bug
    中文版示例代码浏览器for Windows 8
    Tips of the Week for Microsoft Products
    [leetcode] Longest Substring Without Repeating Characters
    [leetcode] Add Two Numbers *
    树的直径 图的直径
    解题笔记(2)——部分面试题解题思路 [转]
    两个容积互质的水杯可倒出任意从1到容积和的水量
    latex中括号大小控制 [转]
  • 原文地址:https://www.cnblogs.com/lightmonster/p/11434531.html
Copyright © 2011-2022 走看看