-
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
13 changed files
with
119 additions
and
114 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#include "../exercise.h" | ||
|
||
// READ: 枚举类型 <https://zh.cppreference.com/w/cpp/language/enum> | ||
|
||
// `enum` 是 C 的兼容类型,本质上其对应类型的常量。 | ||
// 在 `enum` 中定义标识符等价于定义 constexpr 常量, | ||
// 这些标识符不需要前缀,可以直接引用。 | ||
// 因此 `enum` 定义会污染命名空间。 | ||
enum ColorEnum : unsigned char { | ||
COLOR_RED = 31, | ||
COLOR_GREEN, | ||
COLOR_YELLOW, | ||
COLOR_BLUE, | ||
}; | ||
|
||
// 有作用域枚举型是 C++ 引入的类型安全枚举。 | ||
// 其内部标识符需要带前缀引用,如 `Color::Red`。 | ||
// 作用域枚举型可以避免命名空间污染,并提供类型安全保证。 | ||
enum class Color : int { | ||
Red = COLOR_RED, | ||
Green, | ||
Yellow, | ||
Blue, | ||
}; | ||
|
||
ColorEnum convert_by_pun(Color c) { | ||
// `union` 表示在同一内存位置存储的不同类型的值。 | ||
// 其常见用法是实现类型双关转换,即将一种类型的值转换为另一种无关类型的值。 | ||
// READ: <https://zh.cppreference.com/w/cpp/language/union> | ||
union TypePun { | ||
ColorEnum e; | ||
Color c; | ||
}; | ||
|
||
TypePun pun; | ||
// TODO: 补全类型双关转换 | ||
|
||
return pun.e; | ||
} | ||
|
||
int main(int argc, char **argv) { | ||
ASSERT(convert_by_pun(Color::Red) == COLOR_RED, "Type punning conversion"); | ||
ASSERT(convert_by_pun(Color::Green) == COLOR_GREEN, "Type punning conversion"); | ||
ASSERT(convert_by_pun(Color::Yellow) == COLOR_YELLOW, "Type punning conversion"); | ||
ASSERT(convert_by_pun(Color::Blue) == COLOR_BLUE, "Type punning conversion"); | ||
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,25 @@ | ||
#include "../exercise.h" | ||
|
||
// 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; | ||
}; | ||
|
||
// TODO: 实现正确的缓存优化斐波那契计算 | ||
static unsigned long long fibonacci(FibonacciCache &cache, int i) { | ||
for (;; ++next) { | ||
cache[next] = cache[next - 1] + cache[next - 2]; | ||
} | ||
return cache.cache[i]; | ||
} | ||
|
||
int main(int argc, char **argv) { | ||
// TODO: 初始化缓存结构体,使计算正确 | ||
// NOTICE: C/C++ 中,读取未初始化的变量(包括结构体变量)是未定义行为 | ||
// READ: 初始化的各种写法 <https://zh.cppreference.com/w/cpp/language/initialization> | ||
FibonacciCache fib; | ||
ASSERT(fibonacci(fib, 10) == 55, "fibonacci(10) should be 55"); | ||
std::cout << "fibonacci(10) = " << fibonacci(fib, 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,22 @@ | ||
#include "../exercise.h" | ||
|
||
struct Fibonacci { | ||
unsigned long long cache[128]; | ||
int next; | ||
|
||
// TODO: 实现正确的缓存优化斐波那契计算 | ||
unsigned long long get(int i) { | ||
for (; false; ++next) { | ||
cache[next] = cache[next - 1] + cache[next - 2]; | ||
} | ||
return cache[i]; | ||
} | ||
}; | ||
|
||
int main(int argc, char **argv) { | ||
// TODO: 初始化缓存结构体,使计算正确 | ||
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 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,15 @@ | ||
#include "../exercise.h" | ||
|
||
struct Fibonacci { | ||
int numbers[11]; | ||
// TODO: 修改方法签名和实现,使测试通过 | ||
int get(int i) { | ||
} | ||
}; | ||
|
||
int main(int argc, char **argv) { | ||
Fibonacci constexpr FIB{{0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55}}; | ||
ASSERT(FIB.get(10) == 55, "fibonacci(10) should be 55"); | ||
std::cout << "fibonacci(10) = " << FIB.get(10) << std::endl; | ||
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
#include <iostream> | ||
#include "../exercise.h" | ||
|
||
class Fibonacci { | ||
size_t cache[128]; | ||
|
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,4 +1,4 @@ | ||
#include <iostream> | ||
#include "../exercise.h" | ||
|
||
class BoxedFibonacci { | ||
size_t *cache; | ||
|
2 changes: 1 addition & 1 deletion
2
exercises/10_class_drop/main.cpp → exercises/11_class_drop/main.cpp
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,4 +1,4 @@ | ||
#include <iostream> | ||
#include "../exercise.h" | ||
|
||
class BoxedFibonacci { | ||
size_t *cache; | ||
|
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,5 +1,5 @@ | ||
#include "../exercise.h" | ||
#include <cassert> | ||
#include <iostream> | ||
|
||
class BoxedFibonacci { | ||
size_t *cache; | ||
|
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