Skip to content

Commit

Permalink
feat: 扩充到 34 题
Browse files Browse the repository at this point in the history
Signed-off-by: YdrMaster <[email protected]>
  • Loading branch information
YdrMaster committed Dec 6, 2024
1 parent 735a24c commit 326e8fa
Show file tree
Hide file tree
Showing 30 changed files with 245 additions and 54 deletions.
26 changes: 26 additions & 0 deletions exercises/06_array/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include "../exercise.h"

// READ: 数组 <https://zh.cppreference.com/w/cpp/language/array>

unsigned long long arr[90]{0, 1};
unsigned long long fibonacci(int i) {
switch (i) {
case 0:
return 0;
case 1:
return 1;
default:
// TODO: 补全三目表达式缺失的部分
return <condition> ? <cache> : (arr[i] = fibonacci(i - 1) + fibonacci(i - 2));
}
}

int main(int argc, char **argv) {
// TODO: 为此 ASSERT 填写正确的值
ASSERT(sizeof(arr) == ?, "sizeof array is size of all its elements");
// ---- 不要修改以下代码 ----
ASSERT(fibonacci(2) == 1, "fibonacci(2) should be 1");
ASSERT(fibonacci(20) == 6765, "fibonacci(20) should be 6765");
ASSERT(fibonacci(80) == 23416728348467685, "fibonacci(80) should be 23416728348467685");
return 0;
}
File renamed without changes.
28 changes: 28 additions & 0 deletions exercises/08_pointer/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include "../exercise.h"

// 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>
bool is_fibonacci(int *ptr, int len, int stride) {
ASSERT(len >= 3, "`len` should be at least 3");
// TODO: 编写代码判断从 ptr 开始,每 stride 个元素取 1 个元素,组成长度为 n 的数列是否满足
// arr[i + 2] = arr[i] + arr[i + 1]
return true;
}

// ---- 不要修改以下代码 ----
int main(int argc, char **argv) {
int arr0[]{0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55},
arr1[]{0, 1, 2, 3, 4, 5, 6},
arr2[]{99, 98, 4, 1, 7, 2, 11, 3, 18, 5, 29, 8, 47, 13, 76, 21, 123, 34, 199, 55, 322, 0, 0};
// clang-format off
ASSERT( is_fibonacci(arr0 , sizeof(arr0) / sizeof(*arr0) , 1), "arr0 is Fibonacci" );
ASSERT( is_fibonacci(arr0 + 2, sizeof(arr0) / sizeof(*arr0) - 4, 1), "part of arr0 is Fibonacci" );
ASSERT(!is_fibonacci(arr1 , sizeof(arr1) / sizeof(*arr1) , 1), "arr1 is not Fibonacci");
ASSERT( is_fibonacci(arr1 + 1, 3 , 1), "part of arr1 is Fibonacci" );
ASSERT(!is_fibonacci(arr2 , sizeof(arr2) / sizeof(*arr2) , 1), "arr2 is not Fibonacci");
ASSERT( is_fibonacci(arr2 + 2, 10 , 2), "part of arr2 is Fibonacci" );
ASSERT( is_fibonacci(arr2 + 3, 9 , 2), "part of arr2 is Fibonacci" );
ASSERT(!is_fibonacci(arr2 + 3, 10 , 2), "guard check" );
ASSERT(!is_fibonacci(arr2 + 1, 10 , 2), "guard check" );
// clang-format on
return 0;
}
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.
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.
74 changes: 74 additions & 0 deletions exercises/30_std_unique_ptr/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#include "../exercise.h"
#include <memory>
#include <string>
#include <vector>

// READ: `std::unique_ptr` <https://zh.cppreference.com/w/cpp/memory/unique_ptr>

std::vector<std::string> RECORDS;

class Resource {
std::string _records;

public:
void record(char record) {
_records.push_back(record);
}

~Resource() {
RECORDS.push_back(_records);
}
};

using Unique = std::unique_ptr<Resource>;
Unique reset(Unique ptr) {
if (ptr) {
ptr->record('r');
}
return std::make_unique<Resource>();
}
Unique drop(Unique ptr) {
if (ptr) {
ptr->record('d');
}
return nullptr;
}
Unique forward(Unique ptr) {
if (ptr) {
ptr->record('f');
}
return ptr;
}

int main(int argc, char **argv) {
std::vector<std::string> problems[3];

drop(forward(reset(nullptr)));
problems[0] = std::move(RECORDS);

forward(drop(reset(forward(forward(reset(nullptr))))));
problems[1] = std::move(RECORDS);

drop(drop(reset(drop(reset(reset(nullptr))))));
problems[2] = std::move(RECORDS);

// ---- 不要修改以上代码 ----

std::vector<const char *> answers[]{
{"fd"},
// TODO: 分析 problems[1] 中资源的生命周期,将记录填入 `std::vector`
{"", "", "", "", "", "", "", ""},
{"", "", "", "", "", "", "", ""},
};

// ---- 不要修改以下代码 ----

for (auto i = 0; i < 3; ++i) {
ASSERT(problems[i].size() == answers[i].size(), "wrong size");
for (auto j = 0; j < problems[i].size(); ++j) {
ASSERT(std::strcmp(problems[i][j].c_str(), answers[i][j]) == 0, "wrong location");
}
}

return 0;
}
45 changes: 45 additions & 0 deletions exercises/31_std_shared_ptr/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include "../exercise.h"
#include <memory>

// READ: `std::shared_ptr` <https://zh.cppreference.com/w/cpp/memory/shared_ptr>
// READ: `std::weak_ptr` <https://zh.cppreference.com/w/cpp/memory/weak_ptr>

// TODO: 将下列 `?` 替换为正确的值
int main(int argc, char **argv) {
auto shared = std::make_shared<int>(10);
std::shared_ptr<int> ptrs[]{shared, shared, shared};

std::weak_ptr<int> observer = shared;
ASSERT(observer.use_count() == ?, "");

ptrs[0].reset();
ASSERT(observer.use_count() == ?, "");

ptrs[1] = nullptr;
ASSERT(observer.use_count() == ?, "");

ptrs[2] = std::make_shared<int>(*shared);
ASSERT(observer.use_count() == ?, "");

ptrs[0] = shared;
ptrs[1] = shared;
ptrs[2] = std::move(shared);
ASSERT(observer.use_count() == ?, "");

std::ignore = std::move(ptrs[0]);
ptrs[1] = std::move(ptrs[1]);
ptrs[1] = std::move(ptrs[2]);
ASSERT(observer.use_count() == ?, "");

shared = observer.lock();
ASSERT(observer.use_count() == ?, "");

shared = nullptr;
for (auto &ptr : ptrs) ptr = nullptr;
ASSERT(observer.use_count() == ?, "");

shared = observer.lock();
ASSERT(observer.use_count() == ?, "");

return 0;
}
File renamed without changes.
File renamed without changes.
112 changes: 64 additions & 48 deletions exercises/xmake.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,100 +28,116 @@ target("exercise04")
target("exercise05")
add_files("05_constexpr/main.cpp")

-- 循环
-- 数组
target("exercise06")
add_files("06_loop/main.cpp")
add_files("06_array/main.cpp")

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

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

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

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

--
-- 方法
target("exercise11")
add_files("11_class/main.cpp")
add_files("11_method/main.cpp")

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

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

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

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

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

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

-- 函数模板
-- 虚函数
target("exercise18")
add_files("18_function_template/main.cpp")
add_files("18_class_virtual/main.cpp")

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

-- 类模板
-- 函数模板
target("exercise20")
add_files("20_class_template/main.cpp")
add_files("20_function_template/main.cpp")

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

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

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

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

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

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

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

-- std::transform
-- std::string
target("exercise28")
add_files("28_std_transform/main.cpp")
add_files("28_std_string/main.cpp")

-- std::accumulate
-- std::map
target("exercise29")
add_files("29_std_accumulate/main.cpp")
add_files("29_std_map/main.cpp")

-- std::transform
target("exercise30")
add_files("30_std_unique_ptr/main.cpp")

-- std::accumulate
target("exercise31")
add_files("31_std_shared_ptr/main.cpp")

-- std::transform
target("exercise32")
add_files("32_std_transform/main.cpp")

-- std::accumulate
target("exercise33")
add_files("33_std_accumulate/main.cpp")

-- TODO: lambda; deque; forward_list; fs; thread; mutex;
14 changes: 8 additions & 6 deletions learn/summary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include <thread>
#include <vector>

constexpr auto MAX_EXERCISE = 29;
constexpr auto MAX_EXERCISE = 33;

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

std::cout << "concurrency: " << concurrency << std::endl;
Log log{Null{}};
for (auto i = 0u; i <= concurrency; ++i) {
threads.emplace_back([&log, &k] {
int i = k.fetch_add(1);
while (i <= MAX_EXERCISE) {
log << i;
i = k.fetch_add(1);
threads.emplace_back([i, &log, &k] {
int j = k.fetch_add(1);
while (j <= MAX_EXERCISE) {
std::printf("run %d at %d\n", j, i);
log << j;
j = k.fetch_add(1);
}
});
}
Expand Down

0 comments on commit 326e8fa

Please sign in to comment.