Skip to content

Commit 7096e34

Browse files
committed
fix ch1
1 parent bca0adc commit 7096e34

File tree

14 files changed

+129
-118
lines changed

14 files changed

+129
-118
lines changed

ch1/10_Date/CMakeLists.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
cmake_minimum_required(VERSION 3.8)
1+
cmake_minimum_required(VERSION 3.12)
22
project(10_Date)
33

4-
set(CMAKE_CXX_STANDARD 14)
4+
set(CMAKE_CXX_STANDARD 17)
55

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 ../head/Accumulator.h ../head/ResizingArrayStack.h ../head/LinkedStack.h ../head/Stack.h ../head/ResizingArrayQueue.h ../head/LinkedQueue.h ../head/Queue.h ../head/ResizingArrayBag.h ../head/LinkedBag.h ../head/Bag.h ../head/Stopwatch.h ../head/ThreeSum.h ../head/ThreeSumFast.h ../head/DoublingTest.h ../head/QuickFindUF.h ../head/QuickUnionUF.h ../head/WeightedQuickUnionUF.h ../head/UF.h ../head/LinearRegression.h)
6+
set(SOURCE_FILES main.cpp ../head/Date.h)
77
add_executable(10_Date ${SOURCE_FILES})

ch1/11_Transaction/CMakeLists.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
cmake_minimum_required(VERSION 3.8)
1+
cmake_minimum_required(VERSION 3.12)
22
project(11_Transaction)
33

4-
set(CMAKE_CXX_STANDARD 14)
4+
set(CMAKE_CXX_STANDARD 17)
55

6-
set(SOURCE_FILES main.cpp)
6+
set(SOURCE_FILES main.cpp ../head/Transaction.h)
77
add_executable(11_Transaction ${SOURCE_FILES})

ch1/11_Transaction/main.cpp

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,27 +10,31 @@ using namespace std;
1010
* @param args the command-line arguments
1111
*/
1212
int main() {
13-
vector<Transaction> a;
14-
a.push_back(Transaction("Turing 6/17/1990 644.08"));
15-
a.push_back(Transaction("Tarjan 3/26/2002 4121.85"));
16-
a.push_back(Transaction("Dijkstra 8/22/2007 2678.40"));
13+
vector<Transaction *> a;
14+
a.push_back(new Transaction("Turing 6/17/1990 644.08"));
15+
a.push_back(new Transaction("Tarjan 3/26/2002 4121.85"));
16+
a.push_back(new Transaction("Knuth 6/14/1999 288.34"));
17+
a.push_back(new Transaction("Dijkstra 8/22/2007 2678.40"));
1718

1819
cout << "Unsorted" << endl;
1920
for (int i = 0; i < a.size(); ++i)
20-
cout << a[i] << endl;
21+
cout << *a[i] << endl;
22+
cout << endl;
2123

2224
cout << "Sorted by date" << endl;
2325
sort(a.begin(), a.end(), Transaction::WhenOrder);
2426
for (int i = 0; i < a.size(); ++i)
25-
cout << a[i] << endl;
27+
cout << *a[i] << endl;
28+
cout << endl;
2629

2730
cout << "Sorted by customer" << endl;
2831
sort(a.begin(), a.end(), Transaction::WhoOrder);
2932
for (int i = 0; i < a.size(); ++i)
30-
cout << a[i] << endl;
33+
cout << *a[i] << endl;
34+
cout << endl;
3135

3236
cout << "Sorted by amount" << endl;
3337
sort(a.begin(), a.end(), Transaction::HowMuchOrder);
3438
for (int i = 0; i < a.size(); ++i)
35-
cout << a[i] << endl;
39+
cout << *a[i] << endl;
3640
}

ch1/12_Point2D/CMakeLists.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
cmake_minimum_required(VERSION 3.8)
1+
cmake_minimum_required(VERSION 3.12)
22
project(12_Point2D)
33

4-
set(CMAKE_CXX_STANDARD 14)
4+
set(CMAKE_CXX_STANDARD 17)
55

6-
set(SOURCE_FILES main.cpp)
6+
set(SOURCE_FILES main.cpp ../head/Point2D.h)
77
add_executable(12_Point2D ${SOURCE_FILES})

ch1/12_Point2D/main.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ int main() {
1818
cout << "Unsorted: " << endl;
1919
for (auto a: vec)
2020
cout << a << endl;
21+
cout << endl;
22+
2123
cout << "sorted with x: " << endl;
2224
sort(vec.begin(), vec.end(), Point2D::XOrder);
2325
for (auto a: vec)

ch1/13_RectHV/CMakeLists.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
cmake_minimum_required(VERSION 3.8)
1+
cmake_minimum_required(VERSION 3.12)
22
project(13_RectHV)
33

4-
set(CMAKE_CXX_STANDARD 14)
4+
set(CMAKE_CXX_STANDARD 17)
55

6-
set(SOURCE_FILES main.cpp)
6+
set(SOURCE_FILES main.cpp ../head/RectHV.h)
77
add_executable(13_RectHV ${SOURCE_FILES})

ch1/head/BinarySearch.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ using std::vector;
1919
*/
2020
class BinarySearch {
2121
public:
22+
/**
23+
* This class should not be instantiated.
24+
*/
25+
BinarySearch() = delete;
2226
/**
2327
* Returns the index of the specified key in the specified array.
2428
*

ch1/head/Counter.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ class Counter {
6363
friend bool operator<(Counter &a1, Counter &a2);
6464

6565
private:
66-
const string name;
67-
int count;
66+
const string name; // counter name
67+
int count; // current value
6868
};
6969

7070
ostream &operator<<(ostream &stream, const Counter &item) {

ch1/head/Date.h

Lines changed: 40 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,19 @@ auto split = [](string s, char delim) {
3636
* @author Kevin Wayne
3737
*/
3838
class Date {
39+
private:
40+
const static vector<int> DAYS;
41+
int month; // month (between 1 and 12)
42+
int day; // day (between 1 and DAYS[month]
43+
int year; // year
44+
45+
public:
46+
// some friend function
47+
friend ostream &operator<<(ostream &stream, const Date &item);
48+
friend bool operator==(const Date &d1, const Date &d2);
49+
friend bool operator<(const Date &d1, const Date &d2);
50+
friend bool operator>(const Date &d1, const Date &d2);
51+
3952
public:
4053
/**
4154
* Initializes a new date from the month, day, and year.
@@ -54,7 +67,7 @@ class Date {
5467
* @param date the string representation of this date
5568
* @throws IllegalArgumentException if this date is invalid
5669
*/
57-
Date(string date) {
70+
explicit Date(string date) {
5871
vector<string> fields = split(date, '/');
5972
if (fields.size() != 3)
6073
throw runtime_error("Invalid data");
@@ -71,23 +84,23 @@ class Date {
7184
* Return the month.
7285
* @return the month (an integer between 1 and 12)
7386
*/
74-
int month_() const {
87+
int month_() const noexcept {
7588
return month;
7689
}
7790

7891
/**
7992
* Returns the day.
8093
* @return the day (an integer between 1 and 31)
8194
*/
82-
int day_() const {
95+
int day_() const noexcept {
8396
return day;
8497
}
8598

8699
/**
87100
* Returns the year.
88101
* @return the year
89102
*/
90-
int year_() {
103+
int year_() const noexcept {
91104
return year;
92105
}
93106

@@ -102,14 +115,15 @@ class Date {
102115
else return Date(1, 1, year + 1);
103116
}
104117

118+
105119
/**
106120
* Compares two dates chronologically.
107121
*
108122
* @param that the other date
109123
* @return {@code true} if this date is after that date; {@code false} otherwise
110124
*/
111-
bool isAfter(const Date &that) {
112-
return compareTo(that) > 0;
125+
bool isAfter(const Date &that) const {
126+
return (*this) > that;
113127
}
114128

115129
/**
@@ -118,18 +132,10 @@ class Date {
118132
* @param that the other date
119133
* @return {@code true} if this date is before that date; {@code false} otherwise
120134
*/
121-
bool isBefore(Date that) {
122-
return compareTo(that) < 0;
135+
bool isBefore(const Date &that) const {
136+
return (*this) < that;
123137
}
124138

125-
// 输入输出运算符为非成员函数
126-
friend ostream &operator<<(ostream &stream, const Date &item);
127-
128-
// 算术和关系运算符一般也定义成非成员函数
129-
friend bool operator==(const Date &d1, const Date &d2);
130-
131-
friend bool operator<(const Date &d1, const Date &d2);
132-
133139

134140
private:
135141
// is the given date valid?
@@ -147,29 +153,6 @@ class Date {
147153
return y % 4 == 0;
148154
}
149155

150-
/**
151-
* Compares two dates chronologically.
152-
*
153-
* @return the value {@code 0} if the argument date is equal to this date;
154-
* a negative integer if this date is chronologically less than
155-
* the argument date; and a positive ineger if this date is chronologically
156-
* after the argument date
157-
*/
158-
int compareTo(const Date &that) {
159-
if (year < that.year) return -1;
160-
if (year > that.year) return +1;
161-
if (month < that.month) return -1;
162-
if (month > that.month) return +1;
163-
if (day < that.day) return -1;
164-
if (day > that.day) return +1;
165-
return 0;
166-
}
167-
168-
private:
169-
const static vector<int> DAYS;
170-
int month; // month (between 1 and 12)
171-
int day; // day (between 1 and DAYS[month]
172-
int year; // year
173156
};
174157

175158
const vector<int> Date::DAYS = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
@@ -193,6 +176,14 @@ bool operator==(const Date &d1, const Date &d2) {
193176
return (d1.month == d2.month) && (d1.day == d2.day) && (d1.year == d2.year);
194177
}
195178

179+
/**
180+
* Compares two dates chronologically.
181+
*
182+
* @return the value {@code 0} if the argument date is equal to this date;
183+
* a negative integer if this date is chronologically less than
184+
* the argument date; and a positive ineger if this date is chronologically
185+
* after the argument date
186+
*/
196187
bool operator<(const Date &d1, const Date &d2) {
197188
if (d1.year < d2.year) return true;
198189
if (d1.year > d2.year) return false;
@@ -203,4 +194,14 @@ bool operator<(const Date &d1, const Date &d2) {
203194
return false;
204195
}
205196

197+
bool operator>(const Date &d1, const Date &d2) {
198+
if (d1.year > d2.year) return true;
199+
if (d1.year < d2.year) return false;
200+
if (d1.month > d2.month) return true;
201+
if (d1.month < d2.month) return false;
202+
if (d1.day > d2.day) return true;
203+
if (d1.day < d2.day) return false;
204+
return false;
205+
}
206+
206207
#endif //CH1_DATE_H

ch1/head/Knuth.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ std::mt19937 gen(rd());
3030
*/
3131
class Knuth {
3232
public:
33+
// this class should not be instantiated
3334
Knuth() = delete;
3435

3536
/**

0 commit comments

Comments
 (0)