月度归档:2016年08月

c++primer 9.51习题

设计一个类,它有三个unsigned成员,分别表示年月日。为其编写构造函数,接受一个表示日期的string参数。你的构造函数能够处理不同的数据格式,如January 1, 1900 1/1/1900 Jan 1 1900等

// January 1, 1900      1/1/1900        Jan 1 1900
#include <iostream>
#include <vector>
#include <string>
using std::cin;
using std::cout;
using std::endl;
using std::ends;
using std::vector;
using std::string;
using std::stoi;

class Date {
public:
        vector<string> full_month{
                "January", "February", "March", "April", "May", "June", "July",
                "August", "September", "October", "November", "December"
        };
        vector<string> abb_month{
                "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sept",
                "Sep", "Oct", "Nov", "Dec"
        };
public:
        Date(const string& info);
        ~Date(void) {}
        void print_info(void)
        {
                cout << "_" << month << "." << day << "."<< year << "_\n";
                return;
        }
private:
        void init(void)
        {
                year = 1900;
                month = 1;
                day = 1;

                return;
        }
private:
        unsigned year;
        unsigned month;
        unsigned day;
};

int main(void)
{

        cout << "2343" << endl;
        Date error("2343");
        error.print_info();
        cout << string(20, '*') << endl;

        cout << "October 21, 2002" << endl;
        Date date1("October 21, 2002");
        date1.print_info();
        cout << string(20, '*') << endl;

        cout << "2/8/1995" << endl;
        Date date2("2/8/1995");
        date2.print_info();
        cout << string(20, '*') << endl;

        cout << "May 12 2011" << endl;
        Date date3("May 12 2011");
        date3.print_info();

        return 0;
}

Date::Date(const string& info)
{
        std::string::size_type pos;
        pos = info.find_first_of(", /");

        // error info
        if (pos == std::string::npos) {
                init();
                cout << "error date format: init" << endl;
                return;
        }

        // 1/1/1900
        std::string::size_type pos1 = info.find_first_of('/'),
                pos11 = pos1;
        if (pos1 != std::string::npos) {
                day = stoi(info.substr(0, pos1));
                pos11 = info.find_first_of('/', pos1 + 1);
                month = stoi(info.substr(pos1 + 1, pos11));
                year = stoi(info.substr(pos11 + 1, info.size() - 1));
                return;
        }

        // January 1, 1990 and Jan 1 1900
        std::string::size_type pos2 = info.find_first_of(' '),
                pos3 = pos2, pos33 = pos3, pos22 = info.find_first_of(',');
        if (pos22 != std::string::npos) {
                // January 1, 1990
                string str_month = info.substr(0, pos2);
                vector<string>::size_type n = 0;
                for (; n != full_month.size(); ++n) {
                        if (!full_month[n].compare(str_month))
                                break;
                }
                if (n == full_month.size()) {
                        init();
                        cout << "format(January 1, 1990) error: init" << endl;
                        return;
                } else {
                        month = n + 1;
                }
                day = stoi(info.substr(pos2 + 1, pos22));
                year = stoi(info.substr(pos22 + 1, info.size() -1));

                return;
        } else if (info.find_first_of(' ', pos3 + 1) != std::string::npos) {
                // Jan 1 1990
                vector<string>::size_type n = 0;
                string str_month = info.substr(0, pos3);
                for (; n != abb_month.size(); ++n) {
                        if (!abb_month[n].compare(str_month))
                                break;
                }
                if (n == abb_month.size()) {
                        init();
                        cout << "format(Jan 1 1990) error: init" << endl;
                        return;
                } else if (n < 9) {
                        month = n + 1;
                } else if (n == 9) {
                        month = n;
                } else {
                        month = n - 1;
                }
                pos33 = info.find_first_of(' ', pos3 + 1);
                day = stoi(info.substr(pos3 + 1, pos33));
                year = stoi(info.substr(pos33 + 1, info.size() - 1));
        } else {
                init();
                cout << "other error format: init" << endl;

                return;
        }

        return;
}

测试运行结果:

$ ./my_date
2343
error date format: init
_1.1.1900_
********************
October 21, 2002
_10.21.2002_
********************
2/8/1995
_8.2.1995_
********************
May 12 2011
_5.12.2011_