Skip to content
Draft
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
3 changes: 2 additions & 1 deletion exercises/00_hello_world/main.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
#include "../exercise.h"
#include <iostream>

// READ: std streams <https://zh.cppreference.com/w/cpp/io/c/std_streams>
// READ: 流修饰符 <https://zh.cppreference.com/w/cpp/io/manip>
// READ: format in cxx20 <https://zh.cppreference.com/w/cpp/utility/format/format>

int main(int argc, char **argv) {
// TODO: 在控制台输出 "Hello, InfiniTensor!" 并换行
std::cout : "Hello, InfiniTensor!" + std::endl;
std::cout << "Hello, InfiniTensor!" << std::endl;
return 0;
}
4 changes: 3 additions & 1 deletion exercises/01_variable&add/main.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#include "../exercise.h"
#include <iostream>

// READ: 运算符 <https://zh.cppreference.com/w/cpp/language/expressions#.E8.BF.90.E7.AE.97.E7.AC.A6>

int main(int argc, char **argv) {
// TODO: 补全变量定义并打印加法运算
// x ?
int x = 5;
// std::cin >> x;
std::cout << x << " + " << x << " = " << x + x << std::endl;
return 0;
}
3 changes: 3 additions & 0 deletions exercises/02_function/main.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#include "../exercise.h"
#include <iostream>

// READ: 声明 <https://zh.cppreference.com/w/cpp/language/declarations>
// NOTICE: cppreference 中的示例中指出了复杂声明的解读法,建议认真阅读。
// NOTICE: 补充由内而外读法的机翻解释 <https://learn.microsoft.com/zh-cn/cpp/c-language/interpreting-more-complex-declarators?view=msvc-170>

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

int main(int argc, char **argv) {
ASSERT(add(123, 456) == 123 + 456, "add(123, 456) should be 123 + 456");
Expand All @@ -16,4 +18,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 ?");
std::cout << "befor func call: " << arg << std::endl;
func(arg);
ASSERT(arg == ?, "arg should be ?");
ASSERT(arg == 99, "arg should be ?");
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 ?");
std::cout << "befor add: " << param << std::endl;
param += 1;
ASSERT(param == ?, "param should be ?");
ASSERT(param == 100, "param should be ?");
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;
}
26 changes: 24 additions & 2 deletions exercises/05_constexpr/main.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,23 @@
#include "../exercise.h"
#include <iostream>

/*
// Performance improved version: Reduce time complexity from O(2^n) to O(n)
constexpr unsigned long long fibonacci2(int n) {
int i = 2;
int f_1 = 1;
int f_2 = 0;
int f = 0;
do {
f = f_1 + f_2;
f_2 = f_1;
f_1 = f;
i++;
} while (i <= n);
unsigned long long result = f;
return result;
}
*/

constexpr unsigned long long fibonacci(int i) {
switch (i) {
Expand All @@ -15,12 +34,15 @@ int main(int argc, char **argv) {
constexpr auto FIB20 = fibonacci(20);
ASSERT(FIB20 == 6765, "fibonacci(20) should be 6765");
std::cout << "fibonacci(20) = " << FIB20 << std::endl;
// std::cout << "fibonacci(20) = " << fibonacci(20) << std::endl;
// std::cout << "fibonacci2(20) = " << fibonacci2(20) << std::endl;

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

return 0;
}
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]) != 0 ? 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) == 90 * sizeof(unsigned long long), "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
10 changes: 9 additions & 1 deletion exercises/07_loop/main.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
#include "../exercise.h"
#include <iostream>

// TODO: 改正函数实现,实现正确的缓存优化斐波那契计算
// THINk: 这个函数是一个纯函数(pure function)吗?
// 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;

if (cached == 0) {
cache[0] = 0ULL;
cache[1] = 1ULL;
cached = 2;
}
// TODO: 设置正确的循环条件
for (; false; ++cached) {
// static_cast<unsigned>(i) is to resolve the warning different signedness: ‘long long unsigned int’ and ‘int’
for (; cached <= static_cast<unsigned>(i); ++cached) {
cache[cached] = cache[cached - 1] + cache[cached - 2];
}
return cache[i];
Expand Down
7 changes: 7 additions & 0 deletions exercises/08_pointer/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,15 @@
// READ: 数组向指针退化 <https://zh.cppreference.com/w/cpp/language/array#%E6%95%B0%E7%BB%84%E5%88%B0%E6%8C%87%E9%92%88%E7%9A%84%E9%80%80%E5%8C%96>
bool is_fibonacci(int *ptr, int len, int stride) {
ASSERT(len >= 3, "`len` should be at least 3");
if (stride <= 0) return false; // basic guard
// TODO: 编写代码判断从 ptr 开始,每 stride 个元素取 1 个元素,组成长度为 n 的数列是否满足
// arr[i + 2] = arr[i] + arr[i + 1]
for (int k = 0; k <= len - 3; ++k) {
int a0 = ptr[k *stride];
int a1 = ptr[(k + 1) * stride];
int a2 = ptr[(k + 2) * stride];
if (a2 != a0 + a1) return false;
}
return true;
}

Expand Down
5 changes: 5 additions & 0 deletions exercises/09_enum&union/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ ColorEnum convert_by_pun(Color c) {

TypePun pun;
// TODO: 补全类型双关转换
pun.c = c;
// Well-defined conversion: Color(enum class,int) -> int -> unsigned char -> ColorEnum
pun.e = static_cast<ColorEnum>(
static_cast<unsigned char>(
static_cast<std::underlying_type_t<Color>>(pun.c)));

return pun.e;
}
Expand Down
19 changes: 14 additions & 5 deletions exercises/10_trivial/main.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "../exercise.h"
#include <iostream>

// READ: Trivial type <https://learn.microsoft.com/zh-cn/cpp/cpp/trivial-standard-layout-and-pod-types?view=msvc-170>

Expand All @@ -8,18 +9,26 @@ struct FibonacciCache {
};

// TODO: 实现正确的缓存优化斐波那契计算
static unsigned long long fibonacci(FibonacciCache &cache, int i) {
for (; false; ++cached) {
cache[cached] = cache[cached - 1] + cache[cached - 2];
static unsigned long long fibonacci(FibonacciCache &fc, int i) {
ASSERT(i >= 0 && i < 16, "i out of range for 16-entry cache")
// Guard: initialize F(0)=0, F(1)=1, and cached
std::cout << fc.cached << std::endl;
ASSERT(fc.cached >= 0 && fc.cached < 16, "fc.cached is out of range for 16 entry cache")
if (fc.cached < 2) fc.cached = 2;
if (fc.cache[0] != 0) fc.cache[0] = 0ULL;
if (fc.cache[1] != 1) fc.cache[1] = 1ULL;
// Fill in the Fibonacci variables
for (; fc.cached <= i; ++fc.cached) {
fc.cache[fc.cached] = fc.cache[fc.cached - 1] + fc.cache[fc.cached - 2];
}
return cache.cache[i];
return fc.cache[i];
}

int main(int argc, char **argv) {
// TODO: 初始化缓存结构体,使计算正确
// NOTICE: C/C++ 中,读取未初始化的变量(包括结构体变量)是未定义行为
// READ: 初始化的各种写法 <https://zh.cppreference.com/w/cpp/language/initialization>
FibonacciCache fib;
FibonacciCache fib{{0ULL, 1ULL}, 2};
ASSERT(fibonacci(fib, 10) == 55, "fibonacci(10) should be 55");
std::cout << "fibonacci(10) = " << fibonacci(fib, 10) << std::endl;
return 0;
Expand Down
13 changes: 11 additions & 2 deletions exercises/11_method/main.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
#include "../exercise.h"
#include <iostream>

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

// TODO: 实现正确的缓存优化斐波那契计算
unsigned long long get(int i) {
for (; false; ++cached) {
ASSERT(i >= 0 && i < 128, "i out of range for 128-entry cache");

// Guard: initialize F(0), F(1) and minimum index:
if (cached < 2 || cached >= 128) cached = 2;
if (cache[0] != 0) cache[0] = 0ULL;
if (cache[1] != 1) cache[1] = 1ULL;

// calculate Fibonacci
for (; cached <= i; ++cached) {
cache[cached] = cache[cached - 1] + cache[cached - 2];
}
return cache[i];
Expand All @@ -15,7 +24,7 @@ struct Fibonacci {

int main(int argc, char **argv) {
// TODO: 初始化缓存结构体,使计算正确
Fibonacci fib;
Fibonacci fib{{0, 1}, 2};
ASSERT(fib.get(10) == 55, "fibonacci(10) should be 55");
std::cout << "fibonacci(10) = " << fib.get(10) << std::endl;
return 0;
Expand Down
4 changes: 3 additions & 1 deletion exercises/12_method_const/main.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
#include "../exercise.h"
#include <iostream>

// READ: 有 cv 限定符的成员函数 <https://zh.cppreference.com/w/cpp/language/member_functions>

struct Fibonacci {
int numbers[11];
// TODO: 修改方法签名和实现,使测试通过
int get(int i) {
constexpr int get(int i) const{
return numbers[i];
}
};

Expand Down
10 changes: 7 additions & 3 deletions exercises/13_class/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,15 @@ class Fibonacci {

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

// TODO: 实现正确的缓存优化斐波那契计算
size_t get(int i) {
for (; false; ++cached) {
// Guard: initialize cache[0] + cache[1] and cached >= 2
cache[0] = 0ULL;
cache[1] = 1ULL;
if (cached < 2) cached = 2;
for (; cached <= i; ++cached) {
cache[cached] = cache[cached - 1] + cache[cached - 2];
}
return cache[i];
Expand All @@ -28,7 +32,7 @@ class Fibonacci {
int main(int argc, char **argv) {
// 现在类型拥有无参构造器,声明时会直接调用。
// 这个写法不再是未定义行为了。
Fibonacci fib;
Fibonacci fib{};
ASSERT(fib.get(10) == 55, "fibonacci(10) should be 55");
std::cout << "fibonacci(10) = " << fib.get(10) << std::endl;
return 0;
Expand Down
13 changes: 10 additions & 3 deletions exercises/14_class_destruct/main.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "../exercise.h"
#include <iostream>

// READ: 析构函数 <https://zh.cppreference.com/w/cpp/language/destructor>
// READ: RAII <https://learn.microsoft.com/zh-cn/cpp/cpp/object-lifetime-and-resource-management-modern-cpp?view=msvc-170>
Expand All @@ -11,14 +12,20 @@ class DynFibonacci {

public:
// TODO: 实现动态设置容量的构造器
DynFibonacci(int capacity): cache(new ?), cached(?) {}
DynFibonacci(int capacity)
: cache(new size_t[capacity]{}), cached(2) {
cache[0] = 0;
cache[1] = 1;
}

// TODO: 实现析构器,释放缓存空间
~DynFibonacci();
~DynFibonacci() {
delete[] cache;
};

// TODO: 实现正确的缓存优化斐波那契计算
size_t get(int i) {
for (; false; ++cached) {
for (; cached <= i; ++cached) {
cache[cached] = cache[cached - 1] + cache[cached - 2];
}
return cache[i];
Expand Down
20 changes: 16 additions & 4 deletions exercises/15_class_clone/main.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#include "../exercise.h"
#include <iostream>
#include <algorithm>

// READ: 复制构造函数 <https://zh.cppreference.com/w/cpp/language/copy_constructor>
// READ: 函数定义(显式弃置)<https://zh.cppreference.com/w/cpp/language/function>
Expand All @@ -10,17 +12,26 @@ class DynFibonacci {

public:
// TODO: 实现动态设置容量的构造器
DynFibonacci(int capacity): cache(new ?), cached(?) {}
DynFibonacci(int capacity): cache(new size_t[capacity]{}), cached(2) {
ASSERT(capacity >= 2, "capacity must be at least 2");
cache[0] = 0; cache[1] = 1; cached = 2;
}

// TODO: 实现复制构造器
DynFibonacci(DynFibonacci const &) = delete;
DynFibonacci(DynFibonacci const &other)
: cache(new size_t[other.cached + 1]{}), cached(other.cached) {
std::copy(other.cache, other.cache + other.cached + 1, cache);
}


// TODO: 实现析构器,释放缓存空间
~DynFibonacci();
~DynFibonacci() {
delete[] cache;
}

// TODO: 实现正确的缓存优化斐波那契计算
size_t get(int i) {
for (; false; ++cached) {
for (; cached <= i; ++cached) {
cache[cached] = cache[cached - 1] + cache[cached - 2];
}
return cache[i];
Expand All @@ -40,6 +51,7 @@ class DynFibonacci {

int main(int argc, char **argv) {
DynFibonacci fib(12);
// std::cout << fib.get(10) << std::endl;
ASSERT(fib.get(10) == 55, "fibonacci(10) should be 55");
DynFibonacci const fib_ = fib;
ASSERT(fib_.get(10) == fib.get(10), "Object cloned");
Expand Down
Loading