Skip to content

Commit

Permalink
update: 完成 10 题
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 32cd96c commit 7ba4f73
Show file tree
Hide file tree
Showing 13 changed files with 119 additions and 114 deletions.
2 changes: 1 addition & 1 deletion exercises/06_loop/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ static unsigned long long fibonacci(int i) {
// TODO: 为缓存设置正确的初始值
static unsigned long long cache[128], next = 2;
// TODO: 设置正确的循环条件
for (;; ++next) {
for (; false; ++next) {
cache[next] = cache[next - 1] + cache[next - 2];
}
return cache[i];
Expand Down
47 changes: 47 additions & 0 deletions exercises/07_enum&union/main.cpp
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;
}
25 changes: 0 additions & 25 deletions exercises/07_struct/main.cpp

This file was deleted.

25 changes: 25 additions & 0 deletions exercises/08_trivial/main.cpp
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;
}
31 changes: 0 additions & 31 deletions exercises/09_class_method_const/main.cpp

This file was deleted.

22 changes: 22 additions & 0 deletions exercises/09_method/main.cpp
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;
}
15 changes: 15 additions & 0 deletions exercises/10_method_const/main.cpp
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;
}
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];
Expand Down
2 changes: 1 addition & 1 deletion exercises/11_class_clone/main.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include <iostream>
#include "../exercise.h"

class BoxedFibonacci {
size_t *cache;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include <iostream>
#include "../exercise.h"

class BoxedFibonacci {
size_t *cache;
Expand Down
2 changes: 1 addition & 1 deletion exercises/12_class_move/main.cpp
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;
Expand Down
56 changes: 4 additions & 52 deletions exercises/xmake.lua
Original file line number Diff line number Diff line change
Expand Up @@ -31,64 +31,16 @@ target("exercise06")

target("exercise07")
set_kind("binary")
add_files("07_struct/main.cpp")
add_files("07_enum&union/main.cpp")

target("exercise08")
set_kind("binary")
add_files("08_class/main.cpp")
add_files("08_trivial/main.cpp")

target("exercise09")
set_kind("binary")
add_files("09_class_method_const/main.cpp")
add_files("09_method/main.cpp")

target("exercise10")
set_kind("binary")
add_files("10_class_drop/main.cpp")

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

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

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

target("exercise14")
set_kind("binary")
add_files("14_std_vector/main.cpp")

target("exercise15")
set_kind("binary")
add_files("15_std_deque/main.cpp")

target("exercise16")
set_kind("binary")
add_files("16_std_map/main.cpp")

target("exercise17")
set_kind("binary")
add_files("17_std_accumulate/main.cpp")

target("exercise18")
set_kind("binary")
add_files("18_std_transform/main.cpp")

target("exercise19")
set_kind("binary")
add_files("19_std_fs/main.cpp")

target("exercise20")
set_kind("binary")
add_files("20_std_thread/main.cpp")

target("exercise21")
set_kind("binary")
add_files("21_std_mutex/main.cpp")

target("exercise22")
set_kind("binary")
add_files("22_template/main.cpp")
add_files("10_method_const/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 = 22;
constexpr auto MAX_EXERCISE = 10;

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

0 comments on commit 7ba4f73

Please sign in to comment.