Skip to content

Commit

Permalink
feat: 添加练习 00-06
Browse files Browse the repository at this point in the history
Signed-off-by: YdrMaster <[email protected]>
  • Loading branch information
YdrMaster committed Jul 10, 2024
1 parent b4257b7 commit 6301312
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 33 deletions.
4 changes: 2 additions & 2 deletions exercises/00_hello_world/main.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <iostream>
#include "../exercise.h"

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: 2 additions & 2 deletions exercises/01_variable&add/main.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#include <iostream>
#include "../exercise.h"

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

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

int main(int argc, char **argv) {
assert(add(123, 456) == 123 + 456);
ASSERT(add(123, 456) == 123 + 456, "add(123, 456) should be 123 + 456");

auto x = 1, y = 2;
std::cout << x << " + " << y << " = " << add(x, y) << std::endl;
return 0;
}

// TODO: 补全函数定义
// TODO: 补全函数定义,但不要移动代码行
// THINK: `static` 修饰函数有什么效果?
static int add(int a, int b) {
return a + b;
}
19 changes: 13 additions & 6 deletions exercises/03_argument&parameter/main.cpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
// READ: <https://stackoverflow.com/questions/156767/whats-the-difference-between-an-argument-and-a-parameter>
// TODO: 为下列 ASSERT 填写正确的值

#include <iostream>
#include "../exercise.h"

static void func(int param) {
std::cout << "befor add: " << param << std::endl;
param += 1;
std::cout << "after add: " << param << std::endl;
}
void func(int);

int main(int argc, char **argv) {
auto arg = 99;
ASSERT(arg == ?, "arg should be ?");
std::cout << "befor func call: " << arg << std::endl;
func(arg);
ASSERT(arg == ?, "arg should be ?");
std::cout << "after func call: " << arg << std::endl;
return 0;
}

static void func(int param) {
ASSERT(param == ?, "param should be ?");
std::cout << "befor add: " << param << std::endl;
param += 1;
ASSERT(param == ?, "param should be ?");
std::cout << "after add: " << param << std::endl;
}
15 changes: 8 additions & 7 deletions exercises/04_static/main.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
#include <iostream>
#include "../exercise.h"

void func(int param) {
int func(int param) {
static int static_ = param;
std::cout << "static_ = " << static_++ << std::endl;
return static_;
}

int main(int argc, char **argv) {
func(5);
func(4);
func(3);
func(2);
func(1);
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");
return 0;
}
12 changes: 7 additions & 5 deletions exercises/05_constexpr/main.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include <iostream>
#include "../exercise.h"

constexpr int fibonacci(int i) {
switch (i) {
Expand All @@ -12,11 +12,13 @@ constexpr int fibonacci(int i) {
}

int main(int argc, char **argv) {
constexpr auto FIB25 = fibonacci(25);
std::cout << "fibonacci(25) = " << FIB25 << std::endl;
constexpr auto FIB20 = fibonacci(20);
std::cout << "fibonacci(20) = " << FIB20 << std::endl;

auto fib30 = fibonacci(30);
std::cout << "fibonacci(30) = " << fib30 << std::endl;
// TODO: 观察错误信息,修改一处,使代码编译运行
constexpr auto ANS_N = 30;
constexpr auto ANS = fibonacci(ANS_N);
std::cout << "fibonacci(" << ANS_N << ") = " << ANS << std::endl;

return 0;
}
12 changes: 8 additions & 4 deletions exercises/06_loop/main.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
#include <iostream>
#include "../exercise.h"

unsigned long long fibonacci(int i) {
static unsigned long long cache[128]{0, 1}, next = 2;
for (; next <= i; ++next) {
// TODO: 改正函数实现,实现正确的缓存优化斐波那契计算
static unsigned long long fibonacci(int i) {
// TODO: 为缓存设置正确的初始值
static unsigned long long cache[128], next = 2;
// TODO: 设置正确的循环条件
for (;; ++next) {
cache[next] = cache[next - 1] + cache[next - 2];
}
return cache[i];
}

int main(int argc, char **argv) {
auto fib100 = fibonacci(100);
ASSERT(fib100 == 3736710778780434371, "fibonacci(100) should be 3736710778780434371");
std::cout << "fibonacci(100) = " << fib100 << std::endl;
return 0;
}
44 changes: 44 additions & 0 deletions exercises/exercise.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#ifndef __EXERCISE_H__
#define __EXERCISE_H__

#include <iostream>

template<class T>
struct Colorful {
int color;
T value;
};

template<class T>
inline std::ostream &operator<<(std::ostream &os, Colorful<T> const &cf) {
return os << "\x1b[" << cf.color << 'm' << cf.value << "\x1b[0m";
}

#define COLORFUL(VALUE, NAME) \
template<class T> \
inline Colorful<T> NAME(T &&val) { \
return {(VALUE), std::forward<T>(val)}; \
}

COLORFUL(31, red)
COLORFUL(32, green)
COLORFUL(33, yellow)
COLORFUL(34, blue)

#undef COLORFUL

#define ASSERT(COND, MSG) \
if (!(COND)) { \
std::cerr << red("Assertion failed: ") << std::endl \
<< std::endl \
<< #COND << std::endl \
<< std::endl \
<< green("Message:") << std::endl \
<< std::endl \
<< MSG << std::endl \
<< std::endl; \
exit(1); \
}


#endif// __EXERCISE_H__

0 comments on commit 6301312

Please sign in to comment.