From 817cdc27712d33a6d775e8aabb7455eeb470a61d Mon Sep 17 00:00:00 2001 From: YdrMaster Date: Mon, 8 Jul 2024 12:34:04 +0800 Subject: [PATCH] =?UTF-8?q?update:=20=E6=B7=BB=E5=8A=A0=E9=A2=98=E7=9B=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: YdrMaster --- exercises/00_hello_world/main.cpp | 3 ++- exercises/01_variable&add/main.cpp | 8 ++++++++ exercises/02_function/main.cpp | 15 +++++++++++++++ exercises/03_argument¶meter/main.cpp | 17 +++++++++++++++++ exercises/04_static/main.cpp | 17 +++++++++++++++++ exercises/05_constexpr/main.cpp | 24 ++++++++++++++++++++++++ xmake.lua | 23 ++++++++++++++++++++++- 7 files changed, 105 insertions(+), 2 deletions(-) create mode 100644 exercises/01_variable&add/main.cpp create mode 100644 exercises/02_function/main.cpp create mode 100644 exercises/03_argument¶meter/main.cpp create mode 100644 exercises/04_static/main.cpp create mode 100644 exercises/05_constexpr/main.cpp diff --git a/exercises/00_hello_world/main.cpp b/exercises/00_hello_world/main.cpp index 8429a827..2c8bda05 100644 --- a/exercises/00_hello_world/main.cpp +++ b/exercises/00_hello_world/main.cpp @@ -1,6 +1,7 @@ #include int main(int argc, char **argv) { - std::cout << "Hello world!" << std::endl; + // TODO: 在控制台输出 "Hello, InfiniTensor!" 并换行 + std::cout << "Hello, InfiniTensor!" << std::endl; return 0; } diff --git a/exercises/01_variable&add/main.cpp b/exercises/01_variable&add/main.cpp new file mode 100644 index 00000000..0e545009 --- /dev/null +++ b/exercises/01_variable&add/main.cpp @@ -0,0 +1,8 @@ +#include + +int main(int argc, char **argv) { + // TODO: 补全变量定义并打印加法运算 + auto x = 1; + std::cout << x << " + " << x << " = " << x + x << std::endl; + return 0; +} diff --git a/exercises/02_function/main.cpp b/exercises/02_function/main.cpp new file mode 100644 index 00000000..fd15c6b3 --- /dev/null +++ b/exercises/02_function/main.cpp @@ -0,0 +1,15 @@ +#include + +// NOTE: 这是函数声明 +int add(int, int); + +int main(int argc, char **argv) { + auto x = 1, y = 2; + std::cout << x << " + " << y << " = " << add(x, y) << std::endl; + return 0; +} + +// TODO: 补全函数定义 +int add(int a, int b) { + return a + b; +} diff --git a/exercises/03_argument¶meter/main.cpp b/exercises/03_argument¶meter/main.cpp new file mode 100644 index 00000000..d056c32d --- /dev/null +++ b/exercises/03_argument¶meter/main.cpp @@ -0,0 +1,17 @@ +// See . + +#include + +void func(int param) { + std::cout << "befor add: " << param << std::endl; + param += 1; + std::cout << "after add: " << param << std::endl; +} + +int main(int argc, char **argv) { + auto arg = 99; + std::cout << "befor func call: " << arg << std::endl; + func(arg); + std::cout << "after func call: " << arg << std::endl; + return 0; +} diff --git a/exercises/04_static/main.cpp b/exercises/04_static/main.cpp new file mode 100644 index 00000000..460a9a10 --- /dev/null +++ b/exercises/04_static/main.cpp @@ -0,0 +1,17 @@ +// See . + +#include + +void func(int param) { + static int static_ = param; + std::cout << "static_ = " << static_++ << std::endl; +} + +int main(int argc, char **argv) { + func(5); + func(4); + func(3); + func(2); + func(1); + return 0; +} diff --git a/exercises/05_constexpr/main.cpp b/exercises/05_constexpr/main.cpp new file mode 100644 index 00000000..ade4e542 --- /dev/null +++ b/exercises/05_constexpr/main.cpp @@ -0,0 +1,24 @@ +// See . + +#include + +constexpr int fibonacci(int i) { + switch (i) { + case 0: + return 0; + case 1: + return 1; + default: + return fibonacci(i - 1) + fibonacci(i - 2); + } +} + +int main(int argc, char **argv) { + constexpr auto FIB25 = fibonacci(25); + std::cout << "fibonacci(25) = " << FIB25 << std::endl; + + auto fib30 = fibonacci(30); + std::cout << "fibonacci(30) = " << fib30 << std::endl; + + return 0; +} diff --git a/xmake.lua b/xmake.lua index 1593f3e5..6d04ef8c 100644 --- a/xmake.lua +++ b/xmake.lua @@ -1,5 +1,26 @@ add_rules("mode.debug", "mode.release") +set_encodings("utf-8") -target("00_hello_world") +target("exercise00") set_kind("binary") add_files("exercises/00_hello_world/main.cpp") + +target("exercise01") + set_kind("binary") + add_files("exercises/01_variable&add/main.cpp") + +target("exercise02") + set_kind("binary") + add_files("exercises/02_function/main.cpp") + +target("exercise03") + set_kind("binary") + add_files("exercises/03_argument¶meter/main.cpp") + +target("exercise04") + set_kind("binary") + add_files("exercises/04_static/main.cpp") + +target("exercise05") + set_kind("binary") + add_files("exercises/05_constexpr/main.cpp")