Skip to content

Commit 326e8fa

Browse files
committed
feat: 扩充到 34 题
Signed-off-by: YdrMaster <[email protected]>
1 parent 735a24c commit 326e8fa

File tree

30 files changed

+245
-54
lines changed

30 files changed

+245
-54
lines changed

exercises/06_array/main.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include "../exercise.h"
2+
3+
// READ: 数组 <https://zh.cppreference.com/w/cpp/language/array>
4+
5+
unsigned long long arr[90]{0, 1};
6+
unsigned long long fibonacci(int i) {
7+
switch (i) {
8+
case 0:
9+
return 0;
10+
case 1:
11+
return 1;
12+
default:
13+
// TODO: 补全三目表达式缺失的部分
14+
return <condition> ? <cache> : (arr[i] = fibonacci(i - 1) + fibonacci(i - 2));
15+
}
16+
}
17+
18+
int main(int argc, char **argv) {
19+
// TODO: 为此 ASSERT 填写正确的值
20+
ASSERT(sizeof(arr) == ?, "sizeof array is size of all its elements");
21+
// ---- 不要修改以下代码 ----
22+
ASSERT(fibonacci(2) == 1, "fibonacci(2) should be 1");
23+
ASSERT(fibonacci(20) == 6765, "fibonacci(20) should be 6765");
24+
ASSERT(fibonacci(80) == 23416728348467685, "fibonacci(80) should be 23416728348467685");
25+
return 0;
26+
}
File renamed without changes.

exercises/08_pointer/main.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include "../exercise.h"
2+
3+
// READ: 数组向指针退化 <https://zh.cppreference.com/w/cpp/language/array#%E6%95%B0%E7%BB%84%E5%88%B0%E6%8C%87%E9%92%88%E7%9A%84%E9%80%80%E5%8C%96>
4+
bool is_fibonacci(int *ptr, int len, int stride) {
5+
ASSERT(len >= 3, "`len` should be at least 3");
6+
// TODO: 编写代码判断从 ptr 开始,每 stride 个元素取 1 个元素,组成长度为 n 的数列是否满足
7+
// arr[i + 2] = arr[i] + arr[i + 1]
8+
return true;
9+
}
10+
11+
// ---- 不要修改以下代码 ----
12+
int main(int argc, char **argv) {
13+
int arr0[]{0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55},
14+
arr1[]{0, 1, 2, 3, 4, 5, 6},
15+
arr2[]{99, 98, 4, 1, 7, 2, 11, 3, 18, 5, 29, 8, 47, 13, 76, 21, 123, 34, 199, 55, 322, 0, 0};
16+
// clang-format off
17+
ASSERT( is_fibonacci(arr0 , sizeof(arr0) / sizeof(*arr0) , 1), "arr0 is Fibonacci" );
18+
ASSERT( is_fibonacci(arr0 + 2, sizeof(arr0) / sizeof(*arr0) - 4, 1), "part of arr0 is Fibonacci" );
19+
ASSERT(!is_fibonacci(arr1 , sizeof(arr1) / sizeof(*arr1) , 1), "arr1 is not Fibonacci");
20+
ASSERT( is_fibonacci(arr1 + 1, 3 , 1), "part of arr1 is Fibonacci" );
21+
ASSERT(!is_fibonacci(arr2 , sizeof(arr2) / sizeof(*arr2) , 1), "arr2 is not Fibonacci");
22+
ASSERT( is_fibonacci(arr2 + 2, 10 , 2), "part of arr2 is Fibonacci" );
23+
ASSERT( is_fibonacci(arr2 + 3, 9 , 2), "part of arr2 is Fibonacci" );
24+
ASSERT(!is_fibonacci(arr2 + 3, 10 , 2), "guard check" );
25+
ASSERT(!is_fibonacci(arr2 + 1, 10 , 2), "guard check" );
26+
// clang-format on
27+
return 0;
28+
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

exercises/30_std_unique_ptr/main.cpp

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#include "../exercise.h"
2+
#include <memory>
3+
#include <string>
4+
#include <vector>
5+
6+
// READ: `std::unique_ptr` <https://zh.cppreference.com/w/cpp/memory/unique_ptr>
7+
8+
std::vector<std::string> RECORDS;
9+
10+
class Resource {
11+
std::string _records;
12+
13+
public:
14+
void record(char record) {
15+
_records.push_back(record);
16+
}
17+
18+
~Resource() {
19+
RECORDS.push_back(_records);
20+
}
21+
};
22+
23+
using Unique = std::unique_ptr<Resource>;
24+
Unique reset(Unique ptr) {
25+
if (ptr) {
26+
ptr->record('r');
27+
}
28+
return std::make_unique<Resource>();
29+
}
30+
Unique drop(Unique ptr) {
31+
if (ptr) {
32+
ptr->record('d');
33+
}
34+
return nullptr;
35+
}
36+
Unique forward(Unique ptr) {
37+
if (ptr) {
38+
ptr->record('f');
39+
}
40+
return ptr;
41+
}
42+
43+
int main(int argc, char **argv) {
44+
std::vector<std::string> problems[3];
45+
46+
drop(forward(reset(nullptr)));
47+
problems[0] = std::move(RECORDS);
48+
49+
forward(drop(reset(forward(forward(reset(nullptr))))));
50+
problems[1] = std::move(RECORDS);
51+
52+
drop(drop(reset(drop(reset(reset(nullptr))))));
53+
problems[2] = std::move(RECORDS);
54+
55+
// ---- 不要修改以上代码 ----
56+
57+
std::vector<const char *> answers[]{
58+
{"fd"},
59+
// TODO: 分析 problems[1] 中资源的生命周期,将记录填入 `std::vector`
60+
{"", "", "", "", "", "", "", ""},
61+
{"", "", "", "", "", "", "", ""},
62+
};
63+
64+
// ---- 不要修改以下代码 ----
65+
66+
for (auto i = 0; i < 3; ++i) {
67+
ASSERT(problems[i].size() == answers[i].size(), "wrong size");
68+
for (auto j = 0; j < problems[i].size(); ++j) {
69+
ASSERT(std::strcmp(problems[i][j].c_str(), answers[i][j]) == 0, "wrong location");
70+
}
71+
}
72+
73+
return 0;
74+
}

exercises/31_std_shared_ptr/main.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#include "../exercise.h"
2+
#include <memory>
3+
4+
// READ: `std::shared_ptr` <https://zh.cppreference.com/w/cpp/memory/shared_ptr>
5+
// READ: `std::weak_ptr` <https://zh.cppreference.com/w/cpp/memory/weak_ptr>
6+
7+
// TODO: 将下列 `?` 替换为正确的值
8+
int main(int argc, char **argv) {
9+
auto shared = std::make_shared<int>(10);
10+
std::shared_ptr<int> ptrs[]{shared, shared, shared};
11+
12+
std::weak_ptr<int> observer = shared;
13+
ASSERT(observer.use_count() == ?, "");
14+
15+
ptrs[0].reset();
16+
ASSERT(observer.use_count() == ?, "");
17+
18+
ptrs[1] = nullptr;
19+
ASSERT(observer.use_count() == ?, "");
20+
21+
ptrs[2] = std::make_shared<int>(*shared);
22+
ASSERT(observer.use_count() == ?, "");
23+
24+
ptrs[0] = shared;
25+
ptrs[1] = shared;
26+
ptrs[2] = std::move(shared);
27+
ASSERT(observer.use_count() == ?, "");
28+
29+
std::ignore = std::move(ptrs[0]);
30+
ptrs[1] = std::move(ptrs[1]);
31+
ptrs[1] = std::move(ptrs[2]);
32+
ASSERT(observer.use_count() == ?, "");
33+
34+
shared = observer.lock();
35+
ASSERT(observer.use_count() == ?, "");
36+
37+
shared = nullptr;
38+
for (auto &ptr : ptrs) ptr = nullptr;
39+
ASSERT(observer.use_count() == ?, "");
40+
41+
shared = observer.lock();
42+
ASSERT(observer.use_count() == ?, "");
43+
44+
return 0;
45+
}

exercises/xmake.lua

Lines changed: 64 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -28,100 +28,116 @@ target("exercise04")
2828
target("exercise05")
2929
add_files("05_constexpr/main.cpp")
3030

31-
-- 循环
31+
-- 数组
3232
target("exercise06")
33-
add_files("06_loop/main.cpp")
33+
add_files("06_array/main.cpp")
3434

35-
-- 枚举/联合体
35+
-- 循环
3636
target("exercise07")
37-
add_files("07_enum&union/main.cpp")
37+
add_files("07_loop/main.cpp")
3838

39-
-- “普通”类型
39+
-- 指针
4040
target("exercise08")
41-
add_files("08_trivial/main.cpp")
41+
add_files("08_pointer/main.cpp")
4242

43-
-- 方法
43+
-- 枚举/联合体
4444
target("exercise09")
45-
add_files("09_method/main.cpp")
45+
add_files("09_enum&union/main.cpp")
4646

47-
-- const 修饰方法
47+
-- “普通”类型
4848
target("exercise10")
49-
add_files("10_method_const/main.cpp")
49+
add_files("10_trivial/main.cpp")
5050

51-
--
51+
-- 方法
5252
target("exercise11")
53-
add_files("11_class/main.cpp")
53+
add_files("11_method/main.cpp")
5454

55-
-- 析构器
55+
-- const 修饰方法
5656
target("exercise12")
57-
add_files("12_class_destruct/main.cpp")
57+
add_files("12_method_const/main.cpp")
5858

59-
-- 复制构造函数
59+
--
6060
target("exercise13")
61-
add_files("13_class_clone/main.cpp")
61+
add_files("13_class/main.cpp")
6262

63-
-- 移动语义
63+
-- 析构器
6464
target("exercise14")
65-
add_files("14_class_move/main.cpp")
65+
add_files("14_class_destruct/main.cpp")
6666

67-
-- 派生
67+
-- 复制构造函数
6868
target("exercise15")
69-
add_files("15_class_derive/main.cpp")
69+
add_files("15_class_clone/main.cpp")
7070

71-
-- 虚函数
71+
-- 移动语义
7272
target("exercise16")
73-
add_files("16_class_virtual/main.cpp")
73+
add_files("16_class_move/main.cpp")
7474

75-
-- 虚析构函数
75+
-- 派生
7676
target("exercise17")
77-
add_files("17_class_virtual_destruct/main.cpp")
77+
add_files("17_class_derive/main.cpp")
7878

79-
-- 函数模板
79+
-- 虚函数
8080
target("exercise18")
81-
add_files("18_function_template/main.cpp")
81+
add_files("18_class_virtual/main.cpp")
8282

83-
-- 习题:用于编译器的运行时类型
83+
-- 虚析构函数
8484
target("exercise19")
85-
add_files("19_runtime_datatype/main.cpp")
85+
add_files("19_class_virtual_destruct/main.cpp")
8686

87-
-- 类模板
87+
-- 函数模板
8888
target("exercise20")
89-
add_files("20_class_template/main.cpp")
89+
add_files("20_function_template/main.cpp")
9090

91-
-- 模板非类型实参
91+
-- 习题:用于编译器的运行时类型
9292
target("exercise21")
93-
add_files("21_template_const/main.cpp")
93+
add_files("21_runtime_datatype/main.cpp")
9494

95-
-- std::array
95+
-- 类模板
9696
target("exercise22")
97-
add_files("22_std_array/main.cpp")
97+
add_files("22_class_template/main.cpp")
9898

99-
-- std::vector
99+
-- 模板非类型实参
100100
target("exercise23")
101-
add_files("23_std_vector/main.cpp")
101+
add_files("23_template_const/main.cpp")
102102

103-
-- std::vector<bool>
103+
-- std::array
104104
target("exercise24")
105-
add_files("24_std_vector_bool/main.cpp")
105+
add_files("24_std_array/main.cpp")
106106

107-
-- 习题:步长计算
107+
-- std::vector
108108
target("exercise25")
109-
add_files("25_strides/main.cpp")
109+
add_files("25_std_vector/main.cpp")
110110

111-
-- std::string
111+
-- std::vector<bool>
112112
target("exercise26")
113-
add_files("26_std_string/main.cpp")
113+
add_files("26_std_vector_bool/main.cpp")
114114

115-
-- std::map
115+
-- 习题:步长计算
116116
target("exercise27")
117-
add_files("27_std_map/main.cpp")
117+
add_files("27_strides/main.cpp")
118118

119-
-- std::transform
119+
-- std::string
120120
target("exercise28")
121-
add_files("28_std_transform/main.cpp")
121+
add_files("28_std_string/main.cpp")
122122

123-
-- std::accumulate
123+
-- std::map
124124
target("exercise29")
125-
add_files("29_std_accumulate/main.cpp")
125+
add_files("29_std_map/main.cpp")
126+
127+
-- std::transform
128+
target("exercise30")
129+
add_files("30_std_unique_ptr/main.cpp")
130+
131+
-- std::accumulate
132+
target("exercise31")
133+
add_files("31_std_shared_ptr/main.cpp")
134+
135+
-- std::transform
136+
target("exercise32")
137+
add_files("32_std_transform/main.cpp")
138+
139+
-- std::accumulate
140+
target("exercise33")
141+
add_files("33_std_accumulate/main.cpp")
126142

127143
-- TODO: lambda; deque; forward_list; fs; thread; mutex;

learn/summary.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#include <thread>
1010
#include <vector>
1111

12-
constexpr auto MAX_EXERCISE = 29;
12+
constexpr auto MAX_EXERCISE = 33;
1313

1414
int main(int argc, char **argv) {
1515
if (argc == 1) {
@@ -34,13 +34,15 @@ int main(int argc, char **argv) {
3434
std::vector<std::thread> threads;
3535
threads.reserve(concurrency);
3636

37+
std::cout << "concurrency: " << concurrency << std::endl;
3738
Log log{Null{}};
3839
for (auto i = 0u; i <= concurrency; ++i) {
39-
threads.emplace_back([&log, &k] {
40-
int i = k.fetch_add(1);
41-
while (i <= MAX_EXERCISE) {
42-
log << i;
43-
i = k.fetch_add(1);
40+
threads.emplace_back([i, &log, &k] {
41+
int j = k.fetch_add(1);
42+
while (j <= MAX_EXERCISE) {
43+
std::printf("run %d at %d\n", j, i);
44+
log << j;
45+
j = k.fetch_add(1);
4446
}
4547
});
4648
}

0 commit comments

Comments
 (0)