Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
249 changes: 249 additions & 0 deletions exercise-summary.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,249 @@
toby@LAPTOP-GSIP3JR5:~/learning-cxx/exercises$ xmake run summary
exercise00 testing
==================
[100%]: build ok, spent 0.044s
Hello, InfiniTensor!
=================
exercise00 passed

exercise01 testing
==================
[100%]: build ok, spent 0.044s
0 + 0 = 0
=================
exercise01 passed

exercise02 testing
==================
[100%]: build ok, spent 0.045s
1 + 2 = 3
=================
exercise02 passed

exercise03 testing
==================
[100%]: build ok, spent 0.044s
befor func call: 99
befor add: 99
after add: 100
after func call: 99
=================
exercise03 passed

exercise04 testing
==================
[100%]: build ok, spent 0.045s
=================
exercise04 passed

exercise05 testing
==================
[100%]: build ok, spent 0.04s
fibonacci(20) = 6765
fibonacci(20) = 6765
=================
exercise05 passed

exercise06 testing
==================
[100%]: build ok, spent 0.045s
=================
exercise06 passed

exercise07 testing
==================
[100%]: build ok, spent 0.045s
fibonacci(90) = 2880067194370816120
=================
exercise07 passed

exercise08 testing
==================
[100%]: build ok, spent 0.043s
=================
exercise08 passed

exercise09 testing
==================
[100%]: build ok, spent 0.045s
=================
exercise09 passed

exercise10 testing
==================
[100%]: build ok, spent 0.044s
fibonacci(10) = 55
=================
exercise10 passed

exercise11 testing
==================
[100%]: build ok, spent 0.033s
fibonacci(10) = 55
=================
exercise11 passed

exercise12 testing
==================
[100%]: build ok, spent 0.045s
fibonacci(10) = 55
=================
exercise12 passed

exercise13 testing
==================
[100%]: build ok, spent 0.043s
fibonacci(10) = 55
=================
exercise13 passed

exercise14 testing
==================
[100%]: build ok, spent 0.045s
fibonacci(10) = 55
=================
exercise14 passed

exercise15 testing
==================
[100%]: build ok, spent 0.045s
=================
exercise15 passed

exercise16 testing
==================
[100%]: build ok, spent 0.046s
=================
exercise16 passed

exercise17 testing
==================
[100%]: build ok, spent 0.045s
1. X(1)
2. A(2)
3. A(1)
4. X(3)
5. B(1, X(3))

-------------------------

1. A(1)
2. X(5)
3. B(1, X(5))
4. A(A const &) : a(1)
5. ~B(1, X(5))
6. ~X(5)
7. ~A(1)

-------------------------

1. ~A(1)
2. ~B(1, X(3))
3. ~X(3)
4. ~A(1)
5. ~A(2)
6. ~X(1)
=================
exercise17 passed

exercise18 testing
==================
[100%]: build ok, spent 0.044s
=================
exercise18 passed

exercise19 testing
==================
[100%]: build ok, spent 0.045s
=================
exercise19 passed

exercise20 testing
==================
[100%]: build ok, spent 0.044s
=================
exercise20 passed

exercise21 testing
==================
[100%]: build ok, spent 0.044s
=================
exercise21 passed

exercise22 testing
==================
[100%]: build ok, spent 0.044s
=================
exercise22 passed

exercise23 testing
==================
[100%]: build ok, spent 0.045s
=================
exercise23 passed

exercise24 testing
==================
[100%]: build ok, spent 0.042s
=================
exercise24 passed

exercise25 testing
==================
[100%]: build ok, spent 0.044s
=================
exercise25 passed

exercise26 testing
==================
[100%]: build ok, spent 0.043s
sizeof(std::vector<bool>) = 40
=================
exercise26 passed

exercise27 testing
==================
[100%]: build ok, spent 0.044s
=================
exercise27 passed

exercise28 testing
==================
[100%]: build ok, spent 0.042s
=================
exercise28 passed

exercise29 testing
==================
[100%]: build ok, spent 0.041s
=================
exercise29 passed

exercise30 testing
==================
[100%]: build ok, spent 0.045s
=================
exercise30 passed

exercise31 testing
==================
[100%]: build ok, spent 0.043s
=================
exercise31 passed

exercise32 testing
==================
[100%]: build ok, spent 0.033s
=================
exercise32 passed

exercise33 testing
==================
[100%]: build ok, spent 0.044s
=================
exercise33 passed

34/34 [##################################]
warning: You are working in the project directory(/home/toby/learning-cxx) and you can also
force to build in current directory via run `xmake -P .`
toby@LAPTOP-GSIP3JR5:~/learning-cxx/exercises$
2 changes: 1 addition & 1 deletion exercises/00_hello_world/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@

int main(int argc, char **argv) {
// TODO: 在控制台输出 "Hello, InfiniTensor!" 并换行
std::cout : "Hello, InfiniTensor!" + std::endl;
std::cout << "Hello, InfiniTensor!" << std::endl;
return 0;
}
1 change: 1 addition & 0 deletions exercises/01_variable&add/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
int main(int argc, char **argv) {
// TODO: 补全变量定义并打印加法运算
// x ?
int x;
std::cout << x << " + " << x << " = " << x + x << std::endl;
return 0;
}
4 changes: 2 additions & 2 deletions exercises/02_function/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// NOTICE: cppreference 中的示例中指出了复杂声明的解读法,建议认真阅读。
// NOTICE: 补充由内而外读法的机翻解释 <https://learn.microsoft.com/zh-cn/cpp/c-language/interpreting-more-complex-declarators?view=msvc-170>

// TODO: 在这里声明函数
int add(int, int);

int main(int argc, char **argv) {
ASSERT(add(123, 456) == 123 + 456, "add(123, 456) should be 123 + 456");
Expand All @@ -15,5 +15,5 @@ int main(int argc, char **argv) {
}

int add(int a, int b) {
// TODO: 补全函数定义,但不要移动代码行
return a + b;
}
8 changes: 4 additions & 4 deletions exercises/03_argument&parameter/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@ void func(int);
// TODO: 为下列 ASSERT 填写正确的值
int main(int argc, char **argv) {
auto arg = 99;
ASSERT(arg == ?, "arg should be ?");
ASSERT(arg == 99, "arg should be 99");
std::cout << "befor func call: " << arg << std::endl;
func(arg);
ASSERT(arg == ?, "arg should be ?");
ASSERT(arg == 99, "arg should be 99");
std::cout << "after func call: " << arg << std::endl;
return 0;
}

// TODO: 为下列 ASSERT 填写正确的值
void func(int param) {
ASSERT(param == ?, "param should be ?");
ASSERT(param == 99, "param should be 99");
std::cout << "befor add: " << param << std::endl;
param += 1;
ASSERT(param == ?, "param should be ?");
ASSERT(param == 100, "param should be 100");
std::cout << "after add: " << param << std::endl;
}
10 changes: 5 additions & 5 deletions exercises/04_static/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ static int func(int param) {

int main(int argc, char **argv) {
// TODO: 将下列 `?` 替换为正确的数字
ASSERT(func(5) == ?, "static variable value incorrect");
ASSERT(func(4) == ?, "static variable value incorrect");
ASSERT(func(3) == ?, "static variable value incorrect");
ASSERT(func(2) == ?, "static variable value incorrect");
ASSERT(func(1) == ?, "static variable value incorrect");
ASSERT(func(5) == 5, "static variable value incorrect");
ASSERT(func(4) == 6, "static variable value incorrect");
ASSERT(func(3) == 7, "static variable value incorrect");
ASSERT(func(2) == 8, "static variable value incorrect");
ASSERT(func(1) == 9, "static variable value incorrect");
return 0;
}
2 changes: 1 addition & 1 deletion exercises/05_constexpr/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ int main(int argc, char **argv) {

// TODO: 观察错误信息,修改一处,使代码编译运行
// PS: 编译运行,但是不一定能算出结果……
constexpr auto ANS_N = 90;
constexpr auto ANS_N = 20;
constexpr auto ANS = fibonacci(ANS_N);
std::cout << "fibonacci(" << ANS_N << ") = " << ANS << std::endl;

Expand Down
4 changes: 2 additions & 2 deletions exercises/06_array/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ unsigned long long fibonacci(int i) {
return 1;
default:
// TODO: 补全三目表达式缺失的部分
return <condition> ? <cache> : (arr[i] = fibonacci(i - 1) + fibonacci(i - 2));
return arr[i] ? arr[i] : (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(sizeof(arr) == 720, "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");
Expand Down
4 changes: 2 additions & 2 deletions exercises/07_loop/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
// READ: 纯函数 <https://zh.wikipedia.org/wiki/%E7%BA%AF%E5%87%BD%E6%95%B0>
static unsigned long long fibonacci(int i) {
// TODO: 为缓存设置正确的初始值
static unsigned long long cache[96], cached;
static unsigned long long cache[96]{0, 1}, cached;
// TODO: 设置正确的循环条件
for (; false; ++cached) {
for (cached = 2; cached <= 90; ++cached) {
cache[cached] = cache[cached - 1] + cache[cached - 2];
}
return cache[i];
Expand Down
3 changes: 3 additions & 0 deletions exercises/08_pointer/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ 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]
for (int i = 0; i + 2 < len; ++i) {
if (ptr[(i + 2) * stride] != ptr[i * stride] + ptr[(i + 1) * stride]) return false;
}
return true;
}

Expand Down
2 changes: 1 addition & 1 deletion exercises/09_enum&union/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ ColorEnum convert_by_pun(Color c) {
};

TypePun pun;
// TODO: 补全类型双关转换
pun.c = c;

return pun.e;
}
Expand Down
6 changes: 3 additions & 3 deletions exercises/10_trivial/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ struct FibonacciCache {

// TODO: 实现正确的缓存优化斐波那契计算
static unsigned long long fibonacci(FibonacciCache &cache, int i) {
for (; false; ++cached) {
cache[cached] = cache[cached - 1] + cache[cached - 2];
for (cache.cached = 2; cache.cached <= 10; ++cache.cached) {
cache.cache[cache.cached] = cache.cache[cache.cached - 1] + cache.cache[cache.cached - 2];
}
return cache.cache[i];
}
Expand All @@ -19,7 +19,7 @@ int main(int argc, char **argv) {
// TODO: 初始化缓存结构体,使计算正确
// NOTICE: C/C++ 中,读取未初始化的变量(包括结构体变量)是未定义行为
// READ: 初始化的各种写法 <https://zh.cppreference.com/w/cpp/language/initialization>
FibonacciCache fib;
FibonacciCache fib{{0, 1}};
ASSERT(fibonacci(fib, 10) == 55, "fibonacci(10) should be 55");
std::cout << "fibonacci(10) = " << fibonacci(fib, 10) << std::endl;
return 0;
Expand Down
Loading