Skip to content

Commit

Permalink
update: 完成 14 题
Browse files Browse the repository at this point in the history
Signed-off-by: YdrMaster <[email protected]>
  • Loading branch information
YdrMaster committed Jul 10, 2024
1 parent 7ba4f73 commit f941e99
Show file tree
Hide file tree
Showing 14 changed files with 179 additions and 121 deletions.
6 changes: 3 additions & 3 deletions exercises/06_loop/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
// TODO: 改正函数实现,实现正确的缓存优化斐波那契计算
static unsigned long long fibonacci(int i) {
// TODO: 为缓存设置正确的初始值
static unsigned long long cache[128], next = 2;
static unsigned long long cache[128], cached = 2;
// TODO: 设置正确的循环条件
for (; false; ++next) {
cache[next] = cache[next - 1] + cache[next - 2];
for (; false; ++cached) {
cache[cached] = cache[cached - 1] + cache[cached - 2];
}
return cache[i];
}
Expand Down
6 changes: 3 additions & 3 deletions exercises/08_trivial/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
// READ: Trivial type <https://learn.microsoft.com/en-us/cpp/cpp/trivial-standard-layout-and-pod-types?view=msvc-170>
struct FibonacciCache {
unsigned long long cache[16];
int next;
int cached;
};

// TODO: 实现正确的缓存优化斐波那契计算
static unsigned long long fibonacci(FibonacciCache &cache, int i) {
for (;; ++next) {
cache[next] = cache[next - 1] + cache[next - 2];
for (;; ++cached) {
cache[cached] = cache[cached - 1] + cache[cached - 2];
}
return cache.cache[i];
}
Expand Down
6 changes: 3 additions & 3 deletions exercises/09_method/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

struct Fibonacci {
unsigned long long cache[128];
int next;
int cached;

// TODO: 实现正确的缓存优化斐波那契计算
unsigned long long get(int i) {
for (; false; ++next) {
cache[next] = cache[next - 1] + cache[next - 2];
for (; false; ++cached) {
cache[cached] = cache[cached - 1] + cache[cached - 2];
}
return cache[i];
}
Expand Down
30 changes: 21 additions & 9 deletions exercises/11_class/main.cpp
Original file line number Diff line number Diff line change
@@ -1,23 +1,35 @@
#include "../exercise.h"

// C++ 中,`class` 和 `struct` 之间的**唯一区别**是
// `class` 默认访问控制符是 `private`,
// `struct` 默认访问控制符是 `public`。
// READ: 访问说明符 <https://zh.cppreference.com/w/cpp/language/access>

// 这个 class 中的字段被 private 修饰,只能在 class 内部访问。
// 因此必须提供构造器来初始化字段。
// READ: 构造器 <https://zh.cppreference.com/w/cpp/language/constructor>
class Fibonacci {
size_t cache[128];
int next = 2;
size_t cache[16];
int cached;

public:
Fibonacci() : cache{0, 1}, next(2) {}
// TODO: 实现构造器
// Fibonacci()

size_t operator[](int i) {
for (; next <= i; ++next) {
cache[next] = cache[next - 1] + cache[next - 2];
// TODO: 实现正确的缓存优化斐波那契计算
size_t get(int i) {
for (; false; ++cached) {
cache[cached] = cache[cached - 1] + cache[cached - 2];
}
return cache[i];
}
};

int main(int argc, char **argv) {
Fibonacci fib0, fib1;
std::cout << "fibonacci(10) = " << fib0[10] << std::endl;
std::cout << "fibonacci(100) = " << fib1[100] << std::endl;
// 现在类型拥有无参构造器,声明时会直接调用。
// 这个写法不再是未定义行为了。
Fibonacci fib;
ASSERT(fib.get(10) == 55, "fibonacci(10) should be 55");
std::cout << "fibonacci(10) = " << fib.get(10) << std::endl;
return 0;
}
33 changes: 0 additions & 33 deletions exercises/11_class_clone/main.cpp

This file was deleted.

26 changes: 0 additions & 26 deletions exercises/11_class_drop/main.cpp

This file was deleted.

32 changes: 32 additions & 0 deletions exercises/12_class_deconstruct/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include "../exercise.h"

// READ: 析构函数 <https://zh.cppreference.com/w/cpp/language/destructor>

/// @brief 任意缓存容量的斐波那契类型。
/// @details 可以在构造时传入缓存容量,因此需要动态分配缓存空间。
class DynFibonacci {
size_t *cache;
int cached;

public:
// TODO: 实现动态设置容量的构造器
DynFibonacci(int capacity): cache(new ?), cached(?) {}

// TODO: 实现析构器,释放缓存空间
~DynFibonacci();

// TODO: 实现正确的缓存优化斐波那契计算
size_t get(int i) {
for (; false; ++cached) {
cache[cached] = cache[cached - 1] + cache[cached - 2];
}
return cache[i];
}
};

int main(int argc, char **argv) {
DynFibonacci fib(12);
ASSERT(fib.get(10) == 55, "fibonacci(10) should be 55");
std::cout << "fibonacci(10) = " << fib.get(10) << std::endl;
return 0;
}
43 changes: 0 additions & 43 deletions exercises/12_class_move/main.cpp

This file was deleted.

45 changes: 45 additions & 0 deletions exercises/13_class_clone/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include "../exercise.h"

// READ: 复制构造函数 <https://zh.cppreference.com/w/cpp/language/copy_constructor>

class DynFibonacci {
size_t *cache;
int cached;

public:
// TODO: 实现动态设置容量的构造器
DynFibonacci(int capacity): cache(new ?), cached(?) {}

// TODO: 实现复制构造器
DynFibonacci(DynFibonacci const &other) = delete;

// TODO: 实现析构器,释放缓存空间
~DynFibonacci();

// TODO: 实现正确的缓存优化斐波那契计算
size_t get(int i) {
for (; false; ++cached) {
cache[cached] = cache[cached - 1] + cache[cached - 2];
}
return cache[i];
}

// NOTICE: 不要修改这个方法
// NOTICE: 名字相同参数也相同,但 const 修饰不同的方法是一对重载方法,可以同时存在
// 本质上,方法是隐藏了 this 参数的函数
// const 修饰作用在 this 上,因此它们实际上参数不同
size_t get(int i) const {
if (i <= cached) {
return cache[i];
}
ASSERT(false, "i out of range");
}
};

int main(int argc, char **argv) {
DynFibonacci fib(12);
ASSERT(fib.get(10) == 55, "fibonacci(10) should be 55");
DynFibonacci const fib_ = fib;
ASSERT(fib_.get(10) == fib.get(10), "Object cloned");
return 0;
}
55 changes: 55 additions & 0 deletions exercises/14_class_move/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include "../exercise.h"

// READ: 移动构造函数 <https://zh.cppreference.com/w/cpp/language/move_constructor>
// READ: 移动赋值 <https://zh.cppreference.com/w/cpp/language/move_assignment>
// READ: 运算符重载 <https://zh.cppreference.com/w/cpp/language/operators>

class DynFibonacci {
size_t *cache;
int cached;

public:
// TODO: 实现动态设置容量的构造器
DynFibonacci(int capacity): cache(new ?), cached(?) {}

// TODO: 实现移动构造器
DynFibonacci(DynFibonacci &&other) noexcept = delete;

// TODO: 实现移动赋值
// NOTICE: ⚠ 注意移动到自身问题 ⚠
DynFibonacci &operator=(DynFibonacci &&other) noexcept = delete;

// TODO: 实现析构器,释放缓存空间
~DynFibonacci();

// TODO: 实现正确的缓存优化斐波那契计算
size_t operator[](int i) {
for (; false; ++cached) {
cache[cached] = cache[cached - 1] + cache[cached - 2];
}
return cache[i];
}

// NOTICE: 不要修改这个方法
bool is_alive() const {
return cache;
}
};

int main(int argc, char **argv) {
DynFibonacci fib(12);
ASSERT(fib[10] == 55, "fibonacci(10) should be 55");

DynFibonacci const fib_ = std::move(fib);
ASSERT(!fib.is_alive(), "Object moved");
ASSERT(fib_[10] == 55, "fibonacci(10) should be 55");

DynFibonacci fib0(6);
DynFibonacci fib1(12);

fib0 = std::move(fib1);
fib0 = std::move(fib0);
ASSERT(fib0[10] == 55, "fibonacci(10) should be 55");

return 0;
}
File renamed without changes.
File renamed without changes.
16 changes: 16 additions & 0 deletions exercises/xmake.lua
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,19 @@ target("exercise09")
target("exercise10")
set_kind("binary")
add_files("10_method_const/main.cpp")

target("exercise11")
set_kind("binary")
add_files("11_class/main.cpp")

target("exercise12")
set_kind("binary")
add_files("12_class_deconstruct/main.cpp")

target("exercise13")
set_kind("binary")
add_files("13_class_clone/main.cpp")

target("exercise14")
set_kind("binary")
add_files("14_class_move/main.cpp")
2 changes: 1 addition & 1 deletion learn/summary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <sstream>
#include <vector>

constexpr auto MAX_EXERCISE = 10;
constexpr auto MAX_EXERCISE = 14;

int main(int argc, char **argv) {
if (argc == 1) {
Expand Down

0 comments on commit f941e99

Please sign in to comment.