Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
959c7bd
习题00已完成
Xiaobanli-new Jan 4, 2026
ae82754
习题1已完成
Xiaobanli-new Jan 4, 2026
5980cc1
习题1和2已完成
Xiaobanli-new Jan 5, 2026
15bdc96
习题3已完成
Xiaobanli-new Jan 5, 2026
9dbbeee
习题4已完成
Xiaobanli-new Jan 5, 2026
3c81a50
习题5已完成
Xiaobanli-new Jan 6, 2026
6538576
习题6已完成
Xiaobanli-new Jan 6, 2026
753031a
习题7已完成
Xiaobanli-new Jan 6, 2026
481bf99
习题8已完成
Xiaobanli-new Jan 6, 2026
a83eae9
习题9已完成
Xiaobanli-new Jan 7, 2026
56ed9b8
习题10已完成
Xiaobanli-new Jan 7, 2026
a1ee98a
习题11已完成
Xiaobanli-new Jan 7, 2026
4ea4a74
习题12已提交
Xiaobanli-new Jan 7, 2026
7e742b5
习题13已完成
Xiaobanli-new Jan 9, 2026
919b83a
习题14已完成
Xiaobanli-new Jan 9, 2026
248544b
习题15已完成
Xiaobanli-new Jan 9, 2026
013405f
习题16已完成
Xiaobanli-new Jan 9, 2026
096882d
习题17已完成
Xiaobanli-new Jan 9, 2026
737fad3
习题18已完成
Xiaobanli-new Jan 9, 2026
131e93b
习题19已完成
Xiaobanli-new Jan 12, 2026
086903d
习题20已完成
Xiaobanli-new Jan 13, 2026
2e6d017
习题22已提交
Xiaobanli-new Jan 19, 2026
05a2a02
习题22已完成
Xiaobanli-new Jan 19, 2026
102c521
习题23已完成
Xiaobanli-new Jan 20, 2026
04e5393
习题24已提交
Xiaobanli-new Jan 21, 2026
629cb22
习题25已完成
Xiaobanli-new Jan 22, 2026
d802b0a
习题26已完成
Xiaobanli-new Jan 22, 2026
90e6d7a
习题27、28已完成
Xiaobanli-new Jan 27, 2026
9ac73a5
习题29已完成
Xiaobanli-new Jan 27, 2026
108fc2e
习题30已完成
Xiaobanli-new Jan 28, 2026
cf8a352
30修改
Xiaobanli-new Jan 28, 2026
629e1bc
习题31-33已完成
Xiaobanli-new Jan 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
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;
}
2 changes: 1 addition & 1 deletion exercises/01_variable&add/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

int main(int argc, char **argv) {
// TODO: 补全变量定义并打印加法运算
// x ?
auto x = 10.0;
std::cout << x << " + " << x << " = " << x + x << std::endl;
return 0;
}
2 changes: 2 additions & 0 deletions exercises/02_function/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// 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 +17,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;
}
4 changes: 2 additions & 2 deletions exercises/05_constexpr/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ int main(int argc, char **argv) {

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

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
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 = 2;
// TODO: 设置正确的循环条件
for (; false; ++cached) {
for (; cached <= i; ++cached) {
cache[cached] = cache[cached - 1] + cache[cached - 2];
}
return cache[i];
Expand Down
6 changes: 6 additions & 0 deletions exercises/08_pointer/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ 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 = 2; i < len; i++) {
if (ptr[(i) *stride] != ptr[(i - 1) * stride] + ptr[(i - 2) * 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 @@ -37,7 +37,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 <= 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 +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 = {.cache = {0, 1}, .cached = 2};
ASSERT(fibonacci(fib, 10) == 55, "fibonacci(10) should be 55");
std::cout << "fibonacci(10) = " << fibonacci(fib, 10) << std::endl;
return 0;
Expand Down
4 changes: 2 additions & 2 deletions exercises/11_method/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ 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 +15,7 @@ struct Fibonacci {

int main(int argc, char **argv) {
// TODO: 初始化缓存结构体,使计算正确
Fibonacci fib;
Fibonacci fib = {.cache = {0, 1}, .cached = 2};
ASSERT(fib.get(10) == 55, "fibonacci(10) should be 55");
std::cout << "fibonacci(10) = " << fib.get(10) << std::endl;
return 0;
Expand Down
5 changes: 3 additions & 2 deletions exercises/12_method_const/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
struct Fibonacci {
int numbers[11];
// TODO: 修改方法签名和实现,使测试通过
int get(int i) {
constexpr int get(int i) const {
return numbers[i];
}
};

int main(int argc, char **argv) {
Fibonacci constexpr FIB{{0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55}};
Fibonacci constexpr FIB = {.numbers = {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;
Expand Down
4 changes: 2 additions & 2 deletions exercises/13_class/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ class Fibonacci {
public:
// TODO: 实现构造器
// Fibonacci()

Fibonacci() : cache{0, 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
8 changes: 5 additions & 3 deletions exercises/14_class_destruct/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@ class DynFibonacci {

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

// 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
18 changes: 14 additions & 4 deletions exercises/15_class_clone/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,27 @@ class DynFibonacci {

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

// TODO: 实现复制构造器
DynFibonacci(DynFibonacci const &) = delete;
DynFibonacci(DynFibonacci const &other) : cached(other.cached) {
// 1. 根据老对象当前的大小,申请新内存
cache = new size_t[other.cached];

// 2. 把数据搬过来
for (int i = 0; i < cached; ++i) {
cache[i] = other.cache[i];
}
}
Comment on lines +16 to +24
Copy link

Copilot AI Jan 28, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The copy constructor allocates an array with size other.cached instead of the original capacity. This creates a subtle bug: if the copied object later needs to compute Fibonacci numbers beyond the cached index, it will cause a buffer overflow. For example, if the original object was constructed with capacity 12 but has only computed up to index 5 (cached=6), the copy would only allocate 6 elements. If the copy then tries to compute index 10, it would overflow. While the current test doesn't expose this bug (since the copy is const and doesn't compute new values), this is incorrect design. The capacity should be tracked as a member variable and preserved during copying.

Copilot uses AI. Check for mistakes.

// 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
26 changes: 21 additions & 5 deletions exercises/16_class_move/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,37 @@ class DynFibonacci {

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

// TODO: 实现移动构造器
DynFibonacci(DynFibonacci &&) noexcept = delete;
DynFibonacci(DynFibonacci &&other) noexcept : cache(other.cache), cached(other.cached) {
other.cache = nullptr;
other.cached = 0;
}

// TODO: 实现移动赋值
// NOTICE: ⚠ 注意移动到自身问题 ⚠
DynFibonacci &operator=(DynFibonacci &&) noexcept = delete;
DynFibonacci &operator=(DynFibonacci &&other) noexcept {
if (this != &other) {
delete[] cache;

cache = other.cache;
cached = other.cached;

other.cache = nullptr;
other.cached = 0;
}
return *this;
}

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

// TODO: 实现正确的缓存优化斐波那契计算
size_t operator[](int i) {
for (; false; ++cached) {
for (; cached <= i; ++cached) {
cache[cached] = cache[cached - 1] + cache[cached - 2];
}
return cache[i];
Expand Down
6 changes: 3 additions & 3 deletions exercises/17_class_derive/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ int main(int argc, char **argv) {
B b = B(3);

// TODO: 补全三个类型的大小
static_assert(sizeof(X) == ?, "There is an int in X");
static_assert(sizeof(A) == ?, "There is an int in A");
static_assert(sizeof(B) == ?, "B is an A with an X");
static_assert(sizeof(X) == 4, "There is an int in X");
static_assert(sizeof(A) == 4, "There is an int in A");
static_assert(sizeof(B) == 8, "B is an A with an X");

i = 0;
std::cout << std::endl
Expand Down
40 changes: 20 additions & 20 deletions exercises/18_class_virtual/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,38 +42,38 @@ int main(int argc, char **argv) {
C c;
D d;

ASSERT(a.virtual_name() == '?', MSG);
ASSERT(b.virtual_name() == '?', MSG);
ASSERT(c.virtual_name() == '?', MSG);
ASSERT(d.virtual_name() == '?', MSG);
ASSERT(a.direct_name() == '?', MSG);
ASSERT(b.direct_name() == '?', MSG);
ASSERT(c.direct_name() == '?', MSG);
ASSERT(d.direct_name() == '?', MSG);
ASSERT(a.virtual_name() == 'A', MSG);
ASSERT(b.virtual_name() == 'B', MSG);
ASSERT(c.virtual_name() == 'C', MSG);
ASSERT(d.virtual_name() == 'C', MSG);
ASSERT(a.direct_name() == 'A', MSG);
ASSERT(b.direct_name() == 'B', MSG);
ASSERT(c.direct_name() == 'C', MSG);
ASSERT(d.direct_name() == 'D', MSG);

A &rab = b;
B &rbc = c;
C &rcd = d;

ASSERT(rab.virtual_name() == '?', MSG);
ASSERT(rbc.virtual_name() == '?', MSG);
ASSERT(rcd.virtual_name() == '?', MSG);
ASSERT(rab.direct_name() == '?', MSG);
ASSERT(rbc.direct_name() == '?', MSG);
ASSERT(rcd.direct_name() == '?', MSG);
ASSERT(rab.virtual_name() == 'B', MSG);
ASSERT(rbc.virtual_name() == 'C', MSG);
ASSERT(rcd.virtual_name() == 'C', MSG);
ASSERT(rab.direct_name() == 'A', MSG);
ASSERT(rbc.direct_name() == 'B', MSG);
ASSERT(rcd.direct_name() == 'C', MSG);

A &rac = c;
B &rbd = d;

ASSERT(rac.virtual_name() == '?', MSG);
ASSERT(rbd.virtual_name() == '?', MSG);
ASSERT(rac.direct_name() == '?', MSG);
ASSERT(rbd.direct_name() == '?', MSG);
ASSERT(rac.virtual_name() == 'C', MSG);
ASSERT(rbd.virtual_name() == 'C', MSG);
ASSERT(rac.direct_name() == 'A', MSG);
ASSERT(rbd.direct_name() == 'B', MSG);

A &rad = d;

ASSERT(rad.virtual_name() == '?', MSG);
ASSERT(rad.direct_name() == '?', MSG);
ASSERT(rad.virtual_name() == 'C', MSG);
ASSERT(rad.direct_name() == 'A', MSG);

return 0;
}
Expand Down
Loading
Loading