forked from pezy/CppPrimer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
57 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,74 +1,71 @@ | ||
//! @Alan | ||
//! | ||
//! Exercise 9.51: | ||
//! Write a class that has three unsigned members representing year, | ||
//! month, and day. Write a constructor that takes a string representing | ||
//! a date. Your constructor should handle a variety of date formats, | ||
//! such as January 1, 1900, 1/1/1900, Jan 1, 1900, and so on. | ||
//! | ||
// Exercise 9.51: | ||
// Write a class that has three unsigned members representing year, month, and day. | ||
// Write a constructor that takes a string representing a date. Your constructor should handle a | ||
// variety of date formats, such as January 1, 1900, 1/1/1900, Jan 1, 1900, and so on. | ||
|
||
#include <array> | ||
#include <iostream> | ||
#include <string> | ||
#include <vector> | ||
|
||
class wy_Date { | ||
class Date { | ||
public: | ||
wy_Date(const std::string& s); | ||
unsigned year; | ||
unsigned month; | ||
unsigned day; | ||
explicit Date(const std::string& str = ""); | ||
void Print(); | ||
unsigned year = 1970; | ||
unsigned month = 1; | ||
unsigned day = 1; | ||
|
||
private: | ||
std::array<std::string, 12> month_names{"Jan", "Feb", "Mar", "Apr", "May", "Jun", | ||
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; | ||
unsigned MonthFromName(const std::string& str); | ||
}; | ||
|
||
int main() | ||
Date::Date(const std::string& str) | ||
{ | ||
wy_Date d("99/21/3871"); | ||
|
||
std::cout << d.day << " " << d.month << " " << d.year << " "; | ||
|
||
return 0; | ||
if (str.empty()) return; | ||
std::string delimiters{" ,/"}; | ||
auto month_day_delim_pos = str.find_first_of(delimiters); | ||
if (month_day_delim_pos == std::string::npos) | ||
throw std::invalid_argument("This format is not supported now."); | ||
month = MonthFromName(str.substr(0, month_day_delim_pos)); | ||
auto day_year_delim_pos = str.find_first_of(delimiters, month_day_delim_pos + 1); | ||
auto day_len = day_year_delim_pos - month_day_delim_pos - 1; | ||
day = std::stoi(str.substr(month_day_delim_pos + 1, day_len)); | ||
year = std::stoi(str.substr(day_year_delim_pos + 1)); | ||
} | ||
|
||
wy_Date::wy_Date(const std::string& s) | ||
void Date::Print() | ||
{ | ||
unsigned format = 0; | ||
|
||
//! 1/1/1900 | ||
if (s.find_first_of("/") != std::string::npos) format = 0x10; | ||
|
||
//! Jan 1, 1900 | ||
if (s.find_first_of(",") >= 4 && s.find_first_of(",") != std::string::npos) | ||
format = 0x01; | ||
|
||
switch (format) { | ||
|
||
//! format = 1/1/1900 | ||
case 0x10: | ||
day = std::stoi(s.substr(0, s.find_first_of("/"))); | ||
month = std::stoi(s.substr(s.find_first_of("/") + 1, | ||
s.find_last_of("/") - s.find_first_of("/")-1)); | ||
year = std::stoi(s.substr(s.find_last_of("/") + 1, 4)); | ||
break; | ||
|
||
//! format = January 1, 1900 or Jan 1, 1900 | ||
case 0x01: | ||
day = std::stoi( | ||
s.substr(s.find_first_of("1234567890"), | ||
s.find_first_of(",") - s.find_first_of("1234567890"))); | ||
|
||
if (s.find("Jan") < s.size()) month = 1; | ||
if (s.find("Feb") < s.size()) month = 2; | ||
if (s.find("Mar") < s.size()) month = 3; | ||
if (s.find("Apr") < s.size()) month = 4; | ||
if (s.find("May") < s.size()) month = 5; | ||
if (s.find("Jun") < s.size()) month = 6; | ||
if (s.find("Jul") < s.size()) month = 7; | ||
if (s.find("Aug") < s.size()) month = 8; | ||
if (s.find("Sep") < s.size()) month = 9; | ||
if (s.find("Oct") < s.size()) month = 10; | ||
if (s.find("Nov") < s.size()) month = 11; | ||
if (s.find("Dec") < s.size()) month = 12; | ||
std::cout << year << "-" << month << "-" << day << "\n"; | ||
} | ||
|
||
year = std::stoi(s.substr(s.find_last_of(" ") + 1, 4)); | ||
break; | ||
unsigned Date::MonthFromName(const std::string& str) | ||
{ | ||
if (str.empty()) return 0; | ||
if (std::isdigit(str[0])) return std::stoi(str); | ||
for (size_t i = 0; i != 12; ++i) { | ||
if (str.find(month_names[i]) != std::string::npos) return i + 1; | ||
} | ||
return 0; // not found | ||
} | ||
|
||
int main() | ||
{ | ||
{ // default case | ||
auto date = Date(); | ||
date.Print(); | ||
} | ||
{ // case 0: January 1, 1900 | ||
auto date = Date("January 1, 1900"); | ||
date.Print(); | ||
} | ||
{ // case 1: 1/1/1900 | ||
auto date = Date("1/1/1900"); | ||
date.Print(); | ||
} | ||
{ // case 2: Jan 1, 1900 | ||
auto date = Date("Jan 1, 1900"); | ||
date.Print(); | ||
} | ||
} |