Skip to content

Commit 12f325d

Browse files
committed
add cmake
1 parent c7dfe0f commit 12f325d

File tree

3 files changed

+174
-0
lines changed

3 files changed

+174
-0
lines changed

ch1/10_Date/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
cmake_minimum_required(VERSION 3.8)
2+
project(10_Date)
3+
4+
set(CMAKE_CXX_STANDARD 14)
5+
6+
set(SOURCE_FILES main.cpp)
7+
add_executable(10_Date ${SOURCE_FILES})

ch1/10_Date/main.cpp

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <sstream>
4+
#include <algorithm>
5+
6+
using namespace std;
7+
8+
auto split = [](string s, char delim) {
9+
vector<string> tokens;
10+
stringstream ss(s);
11+
string tmp;
12+
while (getline(ss, tmp, delim))
13+
tokens.push_back(tmp);
14+
return tokens;
15+
};
16+
17+
class Date {
18+
public:
19+
/**
20+
* Initializes a new date from the month, day, and year.
21+
* @param month the month (between 1 and 12)
22+
* @param day the day (between 1 and 28-31, depending on the month)
23+
* @param year the year
24+
* @throws IllegalArgumentException if this date is invalid
25+
*/
26+
Date(int m, int d, int y) : month_(m), day_(d), year_(y) {
27+
if (!isValid(month_, day_, year_))
28+
throw runtime_error("Invalid date");
29+
}
30+
31+
/**
32+
* Initializes new date specified as a string in form MM/DD/YYYY.
33+
* @param date the string representation of this date
34+
* @throws IllegalArgumentException if this date is invalid
35+
*/
36+
Date(string date) {
37+
vector<string> fields = split(date, '/');
38+
if (fields.size() != 3)
39+
throw runtime_error("Invalid data");
40+
month_ = stoi(fields[0]);
41+
day_ = stoi(fields[1]);
42+
year_ = stoi(fields[2]);
43+
if (!isValid(month_, day_, year_))
44+
throw runtime_error("Invalid data");
45+
}
46+
47+
/**
48+
* Return the month.
49+
* @return the month (an integer between 1 and 12)
50+
*/
51+
int month() {
52+
return month_;
53+
}
54+
55+
/**
56+
* Returns the day.
57+
* @return the day (an integer between 1 and 31)
58+
*/
59+
int day() {
60+
return day_;
61+
}
62+
63+
/**
64+
* Returns the year.
65+
* @return the year
66+
*/
67+
int year() {
68+
return year_;
69+
}
70+
71+
/**
72+
* Returns the next date in the calendar.
73+
*
74+
* @return a date that represents the next day after this day
75+
*/
76+
Date next() {
77+
if (isValid(month_, day_ + 1, year_)) return Date(month_, day_ + 1, year_);
78+
else if (isValid(month_ + 1, 1, year_)) return Date(month_ + 1, 1, year_);
79+
else return Date(1, 1, year_ + 1);
80+
}
81+
82+
/**
83+
* Compares two dates chronologically.
84+
*
85+
* @param that the other date
86+
* @return {@code true} if this date is after that date; {@code false} otherwise
87+
*/
88+
bool isAfter(Date &that) {
89+
return compareTo(that) > 0;
90+
}
91+
92+
/**
93+
* Compares two dates chronologically.
94+
*
95+
* @param that the other date
96+
* @return {@code true} if this date is before that date; {@code false} otherwise
97+
*/
98+
bool isBefore(Date that) {
99+
return compareTo(that) < 0;
100+
}
101+
102+
103+
104+
private:
105+
// is the given date valid?
106+
static bool isValid(int m, int d, int y) {
107+
if (m < 1 || m > 12) return false;
108+
if (d < 1 || d > DAYS[m]) return false;
109+
if (m == 2 && d == 29 && !isLeapYear(y)) return false;
110+
return true;
111+
}
112+
113+
// is y a leap year?
114+
static bool isLeapYear(int y) {
115+
if (y % 400 == 0) return true;
116+
if (y % 100 == 0) return false;
117+
return y % 4 == 0;
118+
}
119+
120+
/**
121+
* Compares two dates chronologically.
122+
*
123+
* @return the value {@code 0} if the argument date is equal to this date;
124+
* a negative integer if this date is chronologically less than
125+
* the argument date; and a positive ineger if this date is chronologically
126+
* after the argument date
127+
*/
128+
int compareTo(Date &that) {
129+
if (year_ < that.year_) return -1;
130+
if (year_ > that.year_) return +1;
131+
if (month_ < that.month_) return -1;
132+
if (month_ > that.month_) return +1;
133+
if (day_ < that.day_) return -1;
134+
if (day_ > that.day_) return +1;
135+
return 0;
136+
}
137+
138+
private:
139+
const static vector<int> DAYS;
140+
int month_; // month (between 1 and 12)
141+
int day_; // day (between 1 and DAYS[month]
142+
int year_; // year
143+
};
144+
145+
const vector<int> Date::DAYS = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
146+
147+
int main() {
148+
auto v = split("10/20/30", '/');
149+
for (auto a: v)
150+
cout << a << " ";
151+
cout << endl;
152+
}

ch1/CMakeLists.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
cmake_minimum_required(VERSION 3.8)
2+
project(ch1)
3+
4+
set(CMAKE_CXX_STANDARD 14)
5+
6+
add_subdirectory(1_BinarySearch)
7+
add_subdirectory(2_RandomSeq)
8+
add_subdirectory(3_Average)
9+
add_subdirectory(4_Cat)
10+
add_subdirectory(5_Knuth)
11+
add_subdirectory(6_Counter)
12+
add_subdirectory(7_StaticSETofInts)
13+
add_subdirectory(8_Whitelist)
14+
add_subdirectory(9_vector)
15+
add_subdirectory(10_Date)

0 commit comments

Comments
 (0)