Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
add4a3d
Correct console output syntax in main.cpp
youjiaren Jul 28, 2026
27045ed
Define variable x and include iostream
youjiaren Jul 28, 2026
a00bf11
Declare and define add function in main.cpp
youjiaren Jul 28, 2026
48ca2a6
Update ASSERT statements with correct values
youjiaren Jul 28, 2026
83feece
Update assertions for static variable values
youjiaren Jul 28, 2026
7013625
Modify ANS declaration for runtime evaluation
youjiaren Jul 28, 2026
3d144bc
Fix Fibonacci function and update array size assertion
youjiaren Jul 28, 2026
64aa166
Fix Fibonacci function implementation
youjiaren Jul 28, 2026
dfae9a9
Add Fibonacci validation logic in is_fibonacci function
youjiaren Jul 28, 2026
ae4e7a1
Refactor comments and fix URLs in main.cpp
youjiaren Jul 28, 2026
6ba3f57
Correct Fibonacci calculation and cache initialization
youjiaren Jul 28, 2026
979ff0a
Correct Fibonacci caching implementation
youjiaren Jul 28, 2026
83d7aac
Change get method to be const qualified
youjiaren Jul 28, 2026
ba8367d
Update main.cpp
youjiaren Jul 28, 2026
b00ad37
Implement dynamic constructor and destructor for DynFibonacci
youjiaren Jul 28, 2026
dcd598d
Implement constructors and destructor for DynFibonacci
youjiaren Jul 28, 2026
d2eab7b
Implement dynamic constructor, move constructor, and assignment
youjiaren Jul 28, 2026
0d25c8c
Complete static_assert for class sizes
youjiaren Jul 28, 2026
bedcd8b
Update ASSERT statements for virtual and direct names
youjiaren Jul 28, 2026
11879a7
Fix static member initialization and virtual destructors
youjiaren Jul 28, 2026
8f74248
Update main.cpp
youjiaren Jul 28, 2026
4484297
Template sigmoid function for dynamic data types
youjiaren Jul 28, 2026
adf1639
Add broadcasting support in Tensor4D operator+=
youjiaren Jul 28, 2026
516f840
Update main.cpp
youjiaren Jul 28, 2026
5d520f1
Fill in correct values for std::array assertions
youjiaren Jul 28, 2026
b674ebf
Replace placeholders with correct vector assertions
youjiaren Jul 28, 2026
c8ec271
Fix vector<bool> initialization and assertions
youjiaren Jul 28, 2026
650d502
Implement strides function for tensor shape
youjiaren Jul 28, 2026
d3b37b8
Fix type assertions and expected output in main.cpp
youjiaren Jul 28, 2026
70ac4fd
Update main.cpp
youjiaren Jul 28, 2026
e856dd6
Modify answers vector with new resource data
youjiaren Jul 28, 2026
e397da8
Update use_count assertions for shared_ptr tests
youjiaren Jul 28, 2026
35405b5
Implement transformation of vector elements
youjiaren Jul 28, 2026
2733d34
Implement tensor size calculation in main.cpp
youjiaren Jul 28, 2026
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> // 新增IO头文件

// 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;
}
3 changes: 2 additions & 1 deletion exercises/01_variable&add/main.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#include "../exercise.h"
#include <iostream> // 补充标准IO头文件

// 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; // 定义整型变量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> // 补充IO头文件

// 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; // 计算两数之和并返回
}
11 changes: 6 additions & 5 deletions exercises/03_argument&parameter/main.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "../exercise.h"
#include <iostream>

// READ: <https://stackoverflow.com/questions/156767/whats-the-difference-between-an-argument-and-a-parameter>
// THINK: 参数都有哪些传递方式?如何选择传递方式?
Expand All @@ -8,19 +9,19 @@ void func(int);
// TODO: 为下列 ASSERT 填写正确的值
int main(int argc, char **argv) {
auto arg = 99;
ASSERT(arg == ?, "arg should be ?");
std::cout << "befor func call: " << arg << std::endl;
ASSERT(arg == 99, "arg should be ?");
std::cout << "befor func: " << 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;
}
11 changes: 6 additions & 5 deletions exercises/04_static/main.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "../exercise.h"
#include <iostream>

// READ: `static` 关键字 <https://zh.cppreference.com/w/cpp/language/storage_duration>
// THINK: 这个函数的两个 `static` 各自的作用是什么?
Expand All @@ -10,10 +11,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;
}
5 changes: 3 additions & 2 deletions exercises/05_constexpr/main.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "../exercise.h"
#include <iostream>

constexpr unsigned long long fibonacci(int i) {
switch (i) {
Expand All @@ -17,9 +18,9 @@ int main(int argc, char **argv) {
std::cout << "fibonacci(20) = " << FIB20 << std::endl;

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

return 0;
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] != 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) == 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
31 changes: 10 additions & 21 deletions exercises/07_loop/main.cpp
Original file line number Diff line number Diff line change
@@ -1,28 +1,17 @@
#include "../exercise.h"

// 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;
static unsigned long long cache[96], cached = 2;
// 初始化基底,仅首次调用执行
static bool init = true;
if (init) {
cache[0] = 0;
cache[1] = 1;
init = false;
}
// TODO: 设置正确的循环条件
for (; false; ++cached) {
for (; cached < i; ++cached) {
// 仅cached>=2才递推,避免下标越界
cache[cached] = cache[cached - 1] + cache[cached - 2];
}
return cache[i];
}

// ---- 不要修改以下代码 ----
int main(int argc, char **argv) {
ASSERT(fibonacci(0) == 0, "fibonacci(0) should be 0");
ASSERT(fibonacci(1) == 1, "fibonacci(1) should be 1");
ASSERT(fibonacci(2) == 1, "fibonacci(2) should be 1");
ASSERT(fibonacci(3) == 2, "fibonacci(3) should be 2");
ASSERT(fibonacci(10) == 55, "fibonacci(10) should be 55");

auto fib90 = fibonacci(90);
std::cout << "fibonacci(90) = " << fib90 << std::endl;
ASSERT(fib90 == 2880067194370816120, "fibonacci(90) should be 2880067194370816120");
return 0;
}
13 changes: 11 additions & 2 deletions exercises/08_pointer/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,17 @@
// 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");
// TODO: 编写代码判断从 ptr 开始,每 stride 个元素取 1 个元素,组成长度为 n 的数列是否满足
// arr[i + 2] = arr[i] + arr[i + 1]
// 遍历所有需要校验的三元组
for (int i = 0; i <= len - 3; ++i) {
// 取 seq[i], seq[i+1], seq[i+2]
int a = *(ptr + i * stride);
int b = *(ptr + (i + 1) * stride);
int c = *(ptr + (i + 2) * stride);
// 不满足斐波那契递推式,直接返回false
if (c != a + b) {
return false;
}
}
return true;
}

Expand Down
9 changes: 4 additions & 5 deletions exercises/09_enum&union/main.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include "../exercise.h"

// READ: 枚举类型 <https://zh.cppreference.com/w/cpp/language/enum>

// `enum` 是 C 的兼容类型,本质上其对应类型的常量。
// 在 `enum` 中定义标识符等价于定义 constexpr 常量,
// 这些标识符不需要前缀,可以直接引用。
Expand All @@ -14,7 +13,7 @@ enum ColorEnum : unsigned char {
};

// 有作用域枚举型是 C++ 引入的类型安全枚举。
// 其内部标识符需要带前缀引用,如 `Color::Red`
// 其内部标识符需要带前缀引用,如 `Color::Red`.
// 作用域枚举型可以避免命名空间污染,并提供类型安全保证。
enum class Color : int {
Red = COLOR_RED,
Expand All @@ -24,20 +23,20 @@ enum class Color : int {
};

ColorEnum convert_by_pun(Color c) {
// READ: <https://zh.cppreference.com/w/cpp/language/union>
// READ: https://zh.cppreference.com/w/cpp/language/union
// `union` 表示在同一内存位置存储的不同类型的值。
// 其常见用法是实现类型双关转换,即将一种类型的值转换为另一种无关类型的值。
// 但这种写法实际上仅在 C 语言良定义,在 C++ 中是未定义行为。
// 这是比较少见的 C++ 不与 C 保持兼容的特性。
// READ: 类型双关 <https://tttapa.github.io/Pages/Programming/Cpp/Practices/type-punning.html>
// READ: 类型双关 https://ttttapa.github.io/Pages/Programming/Cpp/Practices/type-punning.html
union TypePun {
ColorEnum e;
Color c;
};

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

pun.c = c;
return pun.e;
}

Expand Down
10 changes: 7 additions & 3 deletions exercises/10_trivial/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ 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 < i; ++cache.cached) {
cache.cache[cache.cached] = cache.cache[cache.cached - 1] + cache.cache[cache.cached - 2];
}
return cache.cache[i];
}
Expand All @@ -19,7 +20,10 @@ 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},
2
};
ASSERT(fibonacci(fib, 10) == 55, "fibonacci(10) should be 55");
std::cout << "fibonacci(10) = " << fibonacci(fib, 10) << std::endl;
return 0;
Expand Down
8 changes: 6 additions & 2 deletions exercises/11_method/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ struct Fibonacci {

// TODO: 实现正确的缓存优化斐波那契计算
unsigned long long get(int i) {
for (; false; ++cached) {
// 修正循环条件
for (; cached < i; ++cached) {
cache[cached] = cache[cached - 1] + cache[cached - 2];
}
return cache[i];
Expand All @@ -15,7 +16,10 @@ 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: 2 additions & 2 deletions exercises/12_method_const/main.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#include "../exercise.h"

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

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

Expand Down
13 changes: 9 additions & 4 deletions exercises/13_class/main.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#include "../exercise.h"
#include <iostream>

// C++ 中`class` 和 `struct` 之间的**唯一区别**是
// `class` 默认访问控制符是 `private`
// C++ 中, `class` 和 `struct` 之间的**唯一区别**是
// `class` 默认访问控制符是 `private`,
// `struct` 默认访问控制符是 `public`。
// READ: 访问说明符 <https://zh.cppreference.com/w/cpp/language/access>

Expand All @@ -14,11 +15,15 @@ class Fibonacci {

public:
// TODO: 实现构造器
// Fibonacci()
Fibonacci() {
cache[0] = 0;
cache[1] = 1;
cached = 2;
}

// 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
13 changes: 9 additions & 4 deletions exercises/14_class_destruct/main.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#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>

/// @brief 任意缓存容量的斐波那契类型。
/// @details 可以在构造时传入缓存容量,因此需要动态分配缓存空间。
class DynFibonacci {
Expand All @@ -11,14 +11,19 @@ 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
25 changes: 19 additions & 6 deletions exercises/15_class_clone/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,37 @@
// READ: 复制构造函数 <https://zh.cppreference.com/w/cpp/language/copy_constructor>
// READ: 函数定义(显式弃置)<https://zh.cppreference.com/w/cpp/language/function>


class DynFibonacci {
size_t *cache;
int cached;

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 const &) = delete;
// TODO: 实现复制构造器(不能delete,main需要拷贝对象)
DynFibonacci(DynFibonacci const &other) {
// 示例固定容量12,匹配main参数,通用可新增capacity成员
const int CAP = 12;
cache = new size_t[CAP];
cached = other.cached;
// 深拷贝缓存数据
for (int i = 0; i < cached; ++i) {
cache[i] = other.cache[i];
}
}

// 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
Loading