Skip to content

Commit 342109b

Browse files
committed
fix ch1
1 parent 1efdd89 commit 342109b

File tree

20 files changed

+116
-51
lines changed

20 files changed

+116
-51
lines changed

README.md

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,27 @@ Algorithms, 4th edition textbook code (using c++)
88
1. based on STL Library
99
2. using C++14
1010
3. **Not support** drawing
11-
4. **Bug: ** ① const object (many member function forget to add const) ② memory leak ③ point or reference ?
11+
4. **Bug: ** ① const object (many member function forget to add const)
1212
4. Welcome to point out the error, and pull better code
1313

14-
> the code is writed and debug in CLion IDE,and not test in terminal(I will check it after finish more code)
14+
## How to run demo
15+
16+
### terminal
17+
18+
```shell
19+
# for example
20+
cd ch1
21+
g++ 1_BinarySearch/main.cpp -o binary
22+
./binary
23+
```
24+
25+
### IDE(clion)
26+
27+
You sholud Edit the Configurations:(Due to the relative file path)
28+
29+
```shell
30+
Working directory: youroot/algs4cplusplus/ch1
31+
```
1532

1633
## ch1. Fundamentals
1734

ch1/10_Date/main.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33

44
using namespace std;
55

6-
6+
/**
7+
* Unit tests the {@code Date} data type.
8+
*
9+
* @param args the command-line arguments
10+
*/
711
int main() {
812
Date today(2, 25, 2004);
913
cout << today << endl;

ch1/11_Transaction/main.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44

55
using namespace std;
66

7-
7+
/**
8+
* Unit tests the {@code Transaction} data type.
9+
*
10+
* @param args the command-line arguments
11+
*/
812
int main() {
913
vector<Transaction> a;
1014
a.push_back(Transaction("Turing 6/17/1990 644.08"));

ch1/12_Point2D/main.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@
55

66
using namespace std;
77

8-
8+
/**
9+
* Unit tests the point data type.
10+
*
11+
* @param args the command-line arguments
12+
*/
913
int main() {
1014
vector<Point2D> vec;
1115
vec.push_back(Point2D(1, 2));

ch1/13_RectHV/main.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22
#include <vector>
33
#include "../head/RectHV.h"
44

5-
// TODO: un-finish
65
using namespace std;
76

8-
7+
/**
8+
* simple test
9+
* @return
10+
*/
911
int main() {
1012
RectHV v1(1, 1, 4, 4);
1113
RectHV v2(2, 2, 3, 3);

ch1/14_Interval1D/main.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@
55

66
using namespace std;
77

8-
8+
/**
9+
* Unit tests the {@code Interval1D} data type.
10+
*
11+
* @param args the command-line arguments
12+
*/
913
int main() {
1014
vector<Interval1D> intervals;
1115
intervals.push_back(Interval1D(15.0, 33.0));

ch1/15_Interval2D/main.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@
55

66
using namespace std;
77

8-
8+
/**
9+
* Unit tests the {@code Interval2D} data type.
10+
*
11+
* @param args the command-line arguments
12+
*/
913
int main() {
1014
Interval1D v1(1, 3), v2(1, 3);
1115
Interval2D d1(v1, v2);

ch1/16_Accumulator/main.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44

55
using namespace std;
66

7-
7+
/**
8+
* Unit tests the {@code Interval2D} data type.
9+
*
10+
* @param args the command-line arguments
11+
*/
812
int main() {
913
Accumulator stats;
1014
vector<double> nums{10.0, 12.0, 14.0, 2.0};

ch1/17_ResizingArrayStack/main.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@
44

55
using namespace std;
66

7-
7+
/**
8+
* Unit tests the {@code Stack} data type.
9+
*
10+
* @param args the command-line arguments
11+
*/
812
int main() {
9-
ifstream file("/home/ace/AceDev/C++/algorithm/ch1/data/tobe.txt");
13+
ifstream file("./data/tobe.txt");
1014
ResizingArrayStack<string> stack;
1115
string tmp;
1216
while (file >> tmp) {

ch1/18_LinkedStack/main.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@
44

55
using namespace std;
66

7-
7+
/**
8+
* Unit tests the {@code LinkedStack} data type.
9+
*
10+
* @param args the command-line arguments
11+
*/
812
int main() {
9-
ifstream file("/home/ace/AceDev/C++/algorithm/ch1/data/tobe.txt");
13+
ifstream file("./data/tobe.txt");
1014
LinkedStack<string> stack;
1115
string tmp;
1216
while (file >> tmp) {

ch1/19_Stack/main.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@
44

55
using namespace std;
66

7-
7+
/**
8+
* Unit tests the {@code Stack} data type.
9+
*
10+
* @param args the command-line arguments
11+
*/
812
int main() {
9-
ifstream file("/home/ace/AceDev/C++/algorithm/ch1/data/tobe.txt");
13+
ifstream file("./data/tobe.txt");
1014
Stack<string> stack;
1115
string tmp;
1216
while (file >> tmp) {

ch1/1_BinarySearch/main.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,24 @@
66

77
using namespace std;
88

9+
// Note: use relative path (clion need to change working directory)
10+
11+
/**
12+
* Reads in a sequence of integers from the whitelist file, specified as
13+
* a command-line argument; reads in integers from standard input;
14+
* prints to standard output those integers that do <em>not</em> appear in the file.
15+
*
16+
* @param args the command-line arguments
17+
*/
918
int main() {
10-
// TODO: change to relative path
11-
ifstream input("/home/ace/AceDev/C++/algorithm/ch1/data/tinyW.txt");
19+
ifstream input("./data/tinyW.txt");
1220
vector<int> whitelist;
1321
int t;
1422
while (input >> t) {
1523
whitelist.push_back(t);
1624
}
1725
sort(whitelist.begin(), whitelist.end());
18-
ifstream check("/home/ace/AceDev/C++/algorithm/ch1/data/tinyT.txt");
26+
ifstream check("./data/tinyT.txt");
1927
while (check >> t) {
2028
int idx = BinarySearch::indexOf(whitelist, t);
2129
if (idx == -1)

ch1/2_RandomSeq/main.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
#include <iostream>
2-
#include <vector>
32
#include <iomanip>
4-
#include <algorithm>
3+
#include <random>
54

65
using namespace std;
76

8-
const int seed = 100;
7+
random_device rd;
8+
mt19937 g(rd());
99

1010
/**
1111
* Reads in two command-line arguments lo and hi and prints n uniformly
@@ -17,16 +17,15 @@ int main() {
1717
int n = 10;
1818
double lo = 0.0, hi = 1.0;
1919
// uniform[0.0, 1.0)
20-
default_random_engine e(seed);
2120
uniform_real_distribution<double> dis(0.0, 1.0);
2221
cout << fixed;
2322
for (int i = 0; i < n; ++i)
24-
cout << setprecision(4) << dis(e) << " ";
23+
cout << setprecision(4) << dis(g) << " ";
2524
cout << endl;
2625
// uniform[lo, high)
2726
lo = 2.5, hi = 3.0;
2827
uniform_real_distribution<double> dis2(lo, hi);
2928
for (int i = 0; i < n; ++i)
30-
cout << setprecision(4) << dis2(e) << " ";
29+
cout << setprecision(4) << dis2(g) << " ";
3130
cout << endl;
3231
}

ch1/3_Average/main.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
#include <iostream>
22
#include <vector>
3-
#include <iomanip>
4-
#include <algorithm>
53

64
using namespace std;
75

ch1/4_Cat/main.cpp

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
#include <iostream>
22
#include <vector>
33
#include <fstream>
4-
#include <algorithm>
54

65
using namespace std;
76

8-
const int seed = 100;
9-
107
/**
118
* Reads in a sequence of text files specified as the first command-line
129
* arguments, concatenates them, and writes the results to the file
@@ -15,10 +12,8 @@ const int seed = 100;
1512
* @param args the command-line arguments
1613
*/
1714
int main() {
18-
// TODO: change to relative path
19-
ofstream out("/home/ace/AceDev/C++/algorithm/ch1/data/out.txt");
20-
vector<string> files{"/home/ace/AceDev/C++/algorithm/ch1/data/in1.txt",
21-
"/home/ace/AceDev/C++/algorithm/ch1/data/in2.txt"};
15+
ofstream out("./data/out.txt");
16+
vector<string> files{"./data/in1.txt", "./data/in2.txt"};
2217
string tmp;
2318
for (int i = 0; i < files.size(); ++i) {
2419
ifstream in(files[i]);

ch1/5_Knuth/main.cpp

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,13 @@
77
using namespace std;
88

99
/**
10-
* Reads in a sequence of text files specified as the first command-line
11-
* arguments, concatenates them, and writes the results to the file
12-
* specified as the last command-line argument.
13-
*
14-
* @param args the command-line arguments
15-
*/
10+
* Reads in a sequence of strings from standard input, shuffles
11+
* them, and prints out the results.
12+
*
13+
* @param args the command-line arguments
14+
*/
1615
int main() {
17-
// TODO: change to relative path
18-
ifstream file("/home/ace/AceDev/C++/algorithm/ch1/data/cards.txt");
16+
ifstream file("./data/cards.txt");
1917
string tmp;
2018
vector<string> a;
2119
while (file >> tmp) {

ch1/6_Counter/main.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,30 @@
11
#include <iostream>
22
#include <vector>
3-
#include <algorithm>
3+
#include <random>
44
#include "../head/Counter.h"
55

66
using namespace std;
77

8+
random_device rd;
9+
mt19937 g(rd());
810

9-
const int seed = 100;
10-
11+
/**
12+
* Reads two command-line integers n and trials; creates n counters;
13+
* increments trials counters at random; and prints results.
14+
*
15+
* @param args the command-line arguments
16+
*/
1117
int main() {
1218
int n = 6;
1319
int trials = 60000;
14-
default_random_engine eng(seed);
1520
uniform_int_distribution<> dis(0, n - 1);
1621
// create n counters
1722
vector<Counter> hits;
1823
for (int i = 0; i < n; ++i)
1924
hits.push_back(Counter("counter" + to_string(i)));
2025
// increment trials counters at random
2126
for (int t = 0; t < trials; ++t) {
22-
hits[dis(eng)].increment();
27+
hits[dis(g)].increment();
2328
}
2429
// print results
2530
for (int i = 0; i < n; ++i)

ch1/7_StaticSETofInts/main.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33

44
using namespace std;
55

6-
6+
/**
7+
* simple test
8+
*
9+
*/
710
int main() {
811
StaticSETofInts *s = new StaticSETofInts({4, 2, 3, 6, 5});
912
cout << s->contains(5) << endl;

ch1/8_Whitelist/main.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ using namespace std;
1212
* @param args the command-line arguments
1313
*/
1414
int main() {
15-
ifstream in("/home/ace/AceDev/C++/algorithm/ch1/data/tinyW.txt");
15+
ifstream in("./data/tinyW.txt");
1616
int tmp;
1717
vector<int> white;
1818
while (in >> tmp)
1919
white.push_back(tmp);
2020
StaticSETofInts *p = new StaticSETofInts(white);
21-
ifstream in2("/home/ace/AceDev/C++/algorithm/ch1/data/tinyT.txt");
21+
ifstream in2("./data/tinyT.txt");
2222
while (in2 >> tmp) {
2323
if (!p->contains(tmp))
2424
cout << tmp << endl;

ch1/9_vector/main.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33

44
using namespace std;
55

6-
6+
/**
7+
* Unit tests the {@code Vector} data type.
8+
*
9+
* @param args the command-line arguments
10+
*/
711
int main() {
812
vector<double> xdata{1.0, 2.0, 3.0, 4.0};
913
vector<double> ydata{5.0, 2.0, 4.0, 1.0};

0 commit comments

Comments
 (0)