-
Notifications
You must be signed in to change notification settings - Fork 458
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: YdrMaster <[email protected]>
- Loading branch information
Showing
14 changed files
with
179 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters