Skip to content

Commit

Permalink
update: 添加题目
Browse files Browse the repository at this point in the history
Signed-off-by: YdrMaster <[email protected]>
  • Loading branch information
YdrMaster committed Jul 8, 2024
1 parent f2dc555 commit 817cdc2
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 2 deletions.
3 changes: 2 additions & 1 deletion exercises/00_hello_world/main.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <iostream>

int main(int argc, char **argv) {
std::cout << "Hello world!" << std::endl;
// TODO: 在控制台输出 "Hello, InfiniTensor!" 并换行
std::cout << "Hello, InfiniTensor!" << std::endl;
return 0;
}
8 changes: 8 additions & 0 deletions exercises/01_variable&add/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include <iostream>

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

// 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;
}
17 changes: 17 additions & 0 deletions exercises/03_argument&parameter/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// See <https://stackoverflow.com/questions/156767/whats-the-difference-between-an-argument-and-a-parameter>.

#include <iostream>

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;
}
17 changes: 17 additions & 0 deletions exercises/04_static/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// See <https://stackoverflow.com/questions/156767/whats-the-difference-between-an-argument-and-a-parameter>.

#include <iostream>

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;
}
24 changes: 24 additions & 0 deletions exercises/05_constexpr/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// See <https://stackoverflow.com/questions/156767/whats-the-difference-between-an-argument-and-a-parameter>.

#include <iostream>

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;
}
23 changes: 22 additions & 1 deletion xmake.lua
Original file line number Diff line number Diff line change
@@ -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&parameter/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")

0 comments on commit 817cdc2

Please sign in to comment.