forked from jaege/Cpp-Primer-5th-Exercises
-
Notifications
You must be signed in to change notification settings - Fork 0
/
9.51.cpp
84 lines (74 loc) · 2.53 KB
/
9.51.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <string>
#include <vector>
#include <iostream>
// Use regular expression may be a better choice for this purpose.
// Also stringstream.
class Date {
friend std::ostream &print(std::ostream &os, const Date &d);
public:
explicit Date(const std::string &s);
explicit Date(unsigned y = 0, unsigned m = 1, unsigned d = 1)
: year(y), month(m), day(d) {}
private:
unsigned year;
unsigned month;
unsigned day;
static const std::vector<std::string> months;
static const std::string alphabets;
};
const std::vector<std::string> Date::months{"Jan", "Feb", "Mar", "Apr", "May",
"Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
const std::string Date::alphabets{
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"};
Date::Date(const std::string &s) : Date() {
if (s.find_first_of(alphabets) != std::string::npos) {
// MMM dd yyyy Dec 31, 1990 December 31, 1990
for (std::vector<std::string>::size_type pos = 0;
pos != months.size(); ++pos) {
if (s.find(months[pos]) != std::string::npos) {
month = pos + 1;
break;
}
}
std::string sub = s.substr(s.find_first_of("0123456789"));
day = std::stoi(sub);
sub = sub.substr(sub.find_first_not_of("0123456789"));
sub = sub.substr(sub.find_first_of("0123456789"));
year = std::stoi(sub);
} else if (s.find_first_of('/') != std::string::npos) {
// dd mm yyyy 31/12/1990
std::string sub = s.substr(s.find_first_of("0123456789"));
day = std::stoi(sub);
sub = sub.substr(sub.find_first_not_of("0123456789"));
sub = sub.substr(sub.find_first_of("0123456789"));
month = std::stoi(sub);
sub = sub.substr(sub.find_first_not_of("0123456789"));
sub = sub.substr(sub.find_first_of("0123456789"));
year = std::stoi(sub);
} else {
std::cerr << "Unrecognized date format." << std::endl;
}
}
std::ostream &print(std::ostream &os, const Date &d) {
os << "Year: " << d.year << " Month: " << d.month << " Day: " << d.day;
return os;
}
void testDate(const std::string &s) {
std::cout << s << "\n";
print(std::cout, Date(s)) << std::endl;
}
int main() {
std::cout << "Create from strings\n";
testDate("December 10, 1990");
testDate("10/12/1990");
testDate("10 / 12 / 1990");
testDate("10 12 1990");
testDate("10??12++1990");
testDate("Dec 10, 1990");
testDate(" Dec 10 1990 ");
std::cout << "\nCreate from numbers\n";
print(std::cout, Date(1990)) << std::endl;
print(std::cout, Date(1990, 12)) << std::endl;
print(std::cout, Date(1990, 12, 10)) << std::endl;
return 0;
}