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
Binary file added exercises/00_hello_world/main
Binary file not shown.
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;
}
Binary file added exercises/01_variable&add/main
Binary file not shown.
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 ?
int x=3;
std::cout << x << " + " << x << " = " << x + x << std::endl;
return 0;
}
Binary file added exercises/02_function/main
Binary file not shown.
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;
}
Binary file added exercises/03_argument&parameter/main
Binary file not shown.
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;
}
Binary file added exercises/04_static/main
Binary file not shown.
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;
}
Binary file added exercises/05_constexpr/main
Binary file not shown.
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
Binary file added exercises/06_array/main
Binary file not shown.
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 i>89 ? (fibonacci(i - 1) + fibonacci(i - 2)) : (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
Binary file added exercises/07_loop/main
Binary file not shown.
4 changes: 3 additions & 1 deletion exercises/07_loop/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ static unsigned long long fibonacci(int i) {
// TODO: 为缓存设置正确的初始值
static unsigned long long cache[96], cached;
// TODO: 设置正确的循环条件
for (; false; ++cached) {
cache[0]=0;
cache[1]=1;
for (int cached=2; cached<96; ++cached) {
cache[cached] = cache[cached - 1] + cache[cached - 2];
}
return cache[i];
Expand Down
Binary file added exercises/08_pointer/main
Binary file not shown.
21 changes: 20 additions & 1 deletion exercises/08_pointer/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,26 @@ 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]
return true;
ASSERT(len >= 3, "`len` should be at least 3");
// 判断从 ptr 开始,每 stride 个元素取1个,组成的数列是否满足斐波那契规则(arr[i+2] = arr[i]+arr[i+1])
if (ptr == nullptr)
return false;
else
{
// 遍历数列:从第0个元素开始,验证每一组“前两个元素之和=第三个元素”
for (int i = 0; i < len - 2; ++i) {
// 按步长stride取对应位置的元素
int curr = ptr[i * stride]; // 当前元素
int next1 = ptr[(i + 1) * stride];// 下一个元素
int next2 = ptr[(i + 2) * stride];// 下下个元素
// 不满足斐波那契规则则直接返回false
if (next2 != curr + next1) {
return false;
}
}
// 所有组均满足规则,返回true
return true;
}
}

// ---- 不要修改以下代码 ----
Expand Down
Binary file added exercises/09_enum&union/main
Binary file not shown.
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
Binary file added exercises/10_trivial/main
Binary file not shown.
7 changes: 5 additions & 2 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 (int cached=2;cached<i ; ++cached) {
cache.cache[cached] = cache.cache[cached - 1] + cache.cache[cached - 2];
}
return cache.cache[i];
}
Expand All @@ -20,6 +21,8 @@ int main(int argc, char **argv) {
// NOTICE: C/C++ 中,读取未初始化的变量(包括结构体变量)是未定义行为
// READ: 初始化的各种写法 <https://zh.cppreference.com/w/cpp/language/initialization>
FibonacciCache fib;
fib.cache[0]=0;
fib.cache[1]=1;
ASSERT(fibonacci(fib, 10) == 55, "fibonacci(10) should be 55");
std::cout << "fibonacci(10) = " << fibonacci(fib, 10) << std::endl;
return 0;
Expand Down
Binary file added exercises/11_method/main
Binary file not shown.
18 changes: 17 additions & 1 deletion exercises/11_method/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,32 @@ struct Fibonacci {

// TODO: 实现正确的缓存优化斐波那契计算
unsigned long long get(int i) {
for (; false; ++cached) {
if (i < 0 || i >= 128) {
return false;
}

// 2. 缓存命中:目标值已计算,直接返回
if (i < cached) {
return cache[i];
}

// 3. 缓存未命中:从当前已缓存位置计算到目标i
for (; cached <=i; ++cached) {
cache[cached] = cache[cached - 1] + cache[cached - 2];
}
// 更新已缓存的数量
cached = i + 1;

return cache[i];
}
};

int main(int argc, char **argv) {
// TODO: 初始化缓存结构体,使计算正确
Fibonacci fib;
fib.cache[0]=0;
fib.cache[1]=1;
fib.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
Binary file added exercises/12_method_const/main
Binary file not shown.
5 changes: 3 additions & 2 deletions exercises/12_method_const/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@

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

Expand Down
Binary file added exercises/13_class/main
Binary file not shown.
7 changes: 6 additions & 1 deletion exercises/13_class/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,15 @@ class Fibonacci {
public:
// TODO: 实现构造器
// Fibonacci()
Fibonacci() {
cache[0] = 0; // 第0个斐波那契数
cache[1] = 1; // 第1个斐波那契数
cached = 2; // 标记已缓存前2个有效数据
}

// TODO: 实现正确的缓存优化斐波那契计算
size_t get(int i) {
for (; false; ++cached) {
for (int cached=2; cached<=i; ++cached) {
cache[cached] = cache[cached - 1] + cache[cached - 2];
}
return cache[i];
Expand Down
Binary file added exercises/14_class_destruct/main
Binary file not shown.
14 changes: 11 additions & 3 deletions exercises/14_class_destruct/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,22 @@ class DynFibonacci {

public:
// TODO: 实现动态设置容量的构造器
DynFibonacci(int capacity): cache(new ?), cached(?) {}
DynFibonacci(int capacity):cache(new size_t[capacity]), cached(2) {
if (capacity < 2) {
std::cout<<"capacity must be at least 2";
}
cache[0] = 0; // 第0个斐波那契数
cache[1] = 1; // 第1个斐波那契数
}

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

// TODO: 实现正确的缓存优化斐波那契计算
size_t get(int i) {
for (; false; ++cached) {
for (cached=2; cached<=i; ++cached) {
cache[cached] = cache[cached - 1] + cache[cached - 2];
}
return cache[i];
Expand Down
Binary file added exercises/15_class_clone/main
Binary file not shown.
62 changes: 46 additions & 16 deletions exercises/15_class_clone/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,67 @@


class DynFibonacci {
size_t *cache;
int cached;

size_t *cache;
int capacity; // 改为 capacity 更清晰
int cached; // 已计算的最大索引

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

// TODO: 实现复制构造器
DynFibonacci(DynFibonacci const &) = delete;
DynFibonacci(DynFibonacci const &other)
: capacity(other.capacity),
cached(other.cached),
cache(new size_t[other.capacity]) {
// 深拷贝已缓存的斐波那契数据
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) {
cache[cached] = cache[cached - 1] + cache[cached - 2];
}
// const 版本,不修改缓存
size_t get(int i) {
if (i < 0) {
return false;
}
if (i <= cached) {
return cache[i];
}

// 扩展缓存
for (int j = cached + 1; j <= i; ++j) {
cache[j] = cache[j - 1] + cache[j - 2];
}
cached = i; // 更新已缓存的最大索引
return cache[i];
}

// 保留一个 const 版本用于只读访问
size_t get(int i) const {
if (i < 0 || i > cached) {
return false;
}
return cache[i];
}
// NOTICE: 不要修改这个方法
// NOTICE: 名字相同参数也相同,但 const 修饰不同的方法是一对重载方法,可以同时存在
// 本质上,方法是隐藏了 this 参数的函数
// const 修饰作用在 this 上,因此它们实际上参数不同
size_t get(int i) const {
if (i <= cached) {
return cache[i];
}
ASSERT(false, "i out of range");
}

};

int main(int argc, char **argv) {
Expand Down
Binary file added exercises/16_class_move/main
Binary file not shown.
41 changes: 35 additions & 6 deletions exercises/16_class_move/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,50 @@ class DynFibonacci {

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

// TODO: 实现移动构造器
DynFibonacci(DynFibonacci &&) noexcept = delete;
}
}

// TODO: 实现移动构造器
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 (int cached=2; cached<=i; ++cached) {
cache[cached] = cache[cached - 1] + cache[cached - 2];
}
return cache[i];
Expand Down
Binary file added exercises/17_class_derive/main
Binary file not shown.
Loading