Skip to content

Commit 1009749

Browse files
committed
update ch1
1 parent 3c850c7 commit 1009749

File tree

18 files changed

+1183
-146
lines changed

18 files changed

+1183
-146
lines changed

ch1/10_Date/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ project(10_Date)
33

44
set(CMAKE_CXX_STANDARD 14)
55

6-
set(SOURCE_FILES main.cpp)
6+
set(SOURCE_FILES main.cpp ../head/BinarySearch.h ../head/Knuth.h ../head/Counter.h ../head/StaticSetofInts.h ../head/Vector.h ../head/Date.h ../head/Transaction.h ../head/Point2D.h ../head/Interval1D.h ../head/Interval2D.h ../head/RectHV.h)
77
add_executable(10_Date ${SOURCE_FILES})

ch1/10_Date/main.cpp

Lines changed: 18 additions & 145 deletions
Original file line numberDiff line numberDiff line change
@@ -1,152 +1,25 @@
11
#include <iostream>
2-
#include <vector>
3-
#include <sstream>
4-
#include <algorithm>
2+
#include "../head/Date.h"
53

64
using namespace std;
75

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};
1466

1477
int main() {
148-
auto v = split("10/20/30", '/');
149-
for (auto a: v)
150-
cout << a << " ";
151-
cout << endl;
8+
Date today(2, 25, 2004);
9+
cout << today << endl;
10+
for (int i = 0; i < 10; ++i) {
11+
today = today.next();
12+
cout << today << endl;
13+
}
14+
cout << today.isAfter(today.next()) << endl;
15+
cout << today.isAfter(today) << endl;
16+
cout << today.next().isAfter(today) << endl;
17+
18+
Date birthday(10, 16, 1971);
19+
cout << birthday << endl;
20+
for (int i = 0; i < 10; ++i) {
21+
birthday = birthday.next();
22+
cout << birthday << endl;
23+
}
24+
cout << (today == birthday) << endl;
15225
}

ch1/11_Transaction/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(11_Transaction)
3+
4+
set(CMAKE_CXX_STANDARD 14)
5+
6+
set(SOURCE_FILES main.cpp)
7+
add_executable(11_Transaction ${SOURCE_FILES})

ch1/11_Transaction/main.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include <iostream>
2+
#include <algorithm>
3+
#include "../head/Transaction.h"
4+
5+
using namespace std;
6+
7+
8+
int main() {
9+
vector<Transaction> a;
10+
a.push_back(Transaction("Turing 6/17/1990 644.08"));
11+
a.push_back(Transaction("Tarjan 3/26/2002 4121.85"));
12+
a.push_back(Transaction("Dijkstra 8/22/2007 2678.40"));
13+
14+
cout << "Unsorted" << endl;
15+
for (int i = 0; i < a.size(); ++i)
16+
cout << a[i] << endl;
17+
18+
cout << "Sorted by date" << endl;
19+
sort(a.begin(), a.end(), Transaction::WhenOrder);
20+
for (int i = 0; i < a.size(); ++i)
21+
cout << a[i] << endl;
22+
23+
cout << "Sorted by customer" << endl;
24+
sort(a.begin(), a.end(), Transaction::WhoOrder);
25+
for (int i = 0; i < a.size(); ++i)
26+
cout << a[i] << endl;
27+
28+
cout << "Sorted by amount" << endl;
29+
sort(a.begin(), a.end(), Transaction::HowMuchOrder);
30+
for (int i = 0; i < a.size(); ++i)
31+
cout << a[i] << endl;
32+
}

ch1/12_Point2D/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(12_Point2D)
3+
4+
set(CMAKE_CXX_STANDARD 14)
5+
6+
set(SOURCE_FILES main.cpp)
7+
add_executable(12_Point2D ${SOURCE_FILES})

ch1/12_Point2D/main.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <algorithm>
4+
#include "../head/Point2D.h"
5+
6+
using namespace std;
7+
8+
9+
int main() {
10+
vector<Point2D> vec;
11+
vec.push_back(Point2D(1, 2));
12+
vec.push_back(Point2D(2.0, 3.0));
13+
vec.push_back(Point2D(1.1, 2.0));
14+
cout << "Unsorted: " << endl;
15+
for (auto a: vec)
16+
cout << a << endl;
17+
cout << "sorted with x: " << endl;
18+
sort(vec.begin(), vec.end(), Point2D::XOrder);
19+
for (auto a: vec)
20+
cout << a << endl;
21+
}

ch1/13_RectHV/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(13_RectHV)
3+
4+
set(CMAKE_CXX_STANDARD 14)
5+
6+
set(SOURCE_FILES main.cpp)
7+
add_executable(13_RectHV ${SOURCE_FILES})

ch1/13_RectHV/main.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include "../head/RectHV.h"
4+
5+
// TODO: un-finish
6+
using namespace std;
7+
8+
9+
int main() {
10+
RectHV v1(1, 1, 4, 4);
11+
RectHV v2(2, 2, 3, 3);
12+
cout << v1 << endl;
13+
cout << v2 << endl;
14+
// test intersects
15+
cout << "intersect(v1, v2): " << v1.intersects(v2) << endl;
16+
// test contains
17+
Point2D p(2, 3);
18+
cout << "v1 contain p?: " << v1.contains(p) << endl;
19+
// test ==
20+
cout << "v1 == v2?: " << (v1 == v2) << endl;
21+
}

ch1/14_Interval1D/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(14_Interval1D)
3+
4+
set(CMAKE_CXX_STANDARD 14)
5+
6+
set(SOURCE_FILES main.cpp)
7+
add_executable(14_Interval1D ${SOURCE_FILES})

ch1/14_Interval1D/main.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <algorithm>
4+
#include "../head/Interval1D.h"
5+
6+
using namespace std;
7+
8+
9+
int main() {
10+
vector<Interval1D> intervals;
11+
intervals.push_back(Interval1D(15.0, 33.0));
12+
intervals.push_back(Interval1D(45.0, 66.0));
13+
intervals.push_back(Interval1D(20.0, 70.0));
14+
intervals.push_back(Interval1D(46.0, 55.0));
15+
16+
cout << "Unsorted" << endl;
17+
for (auto inter: intervals)
18+
cout << inter << endl;
19+
20+
cout << "Sort by min endpoint" << endl;
21+
sort(intervals.begin(), intervals.end(), Interval1D::MinEndpointComparator);
22+
for (auto inter: intervals)
23+
cout << inter << endl;
24+
25+
cout << "Sort by max endpoint" << endl;
26+
sort(intervals.begin(), intervals.end(), Interval1D::MaxEndpointComparator);
27+
for (auto inter: intervals)
28+
cout << inter << endl;
29+
30+
cout << "Sort by length" << endl;
31+
sort(intervals.begin(), intervals.end(), Interval1D::LengthComparator);
32+
for (auto inter: intervals)
33+
cout << inter << endl;
34+
}

ch1/15_Interval2D/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(15_Interval2D)
3+
4+
set(CMAKE_CXX_STANDARD 14)
5+
6+
set(SOURCE_FILES main.cpp)
7+
add_executable(15_Interval2D ${SOURCE_FILES})

ch1/15_Interval2D/main.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <algorithm>
4+
#include "../head/Interval2D.h"
5+
6+
using namespace std;
7+
8+
9+
int main() {
10+
Interval1D v1(1, 3), v2(1, 3);
11+
Interval2D d1(v1, v2);
12+
13+
Interval1D v3(2, 4), v4(2, 4);
14+
Interval2D d2(v3, v4);
15+
16+
cout << "d1: " << d1 << endl;
17+
cout << "d2: " << d2 << endl;
18+
19+
// test intersects
20+
cout << "intersects(v1, v2)? : " << d1.intersects(d2) << endl;
21+
// test contains
22+
Point2D p(2, 4);
23+
cout << "d1 contains p? : " << d1.contains(p) << endl;
24+
// test area
25+
cout << "area of d1: " << d1.area() << endl;
26+
// test ==
27+
cout << "d1 equal d2? :" << (d1 == d2) << endl;
28+
}

0 commit comments

Comments
 (0)