Skip to content

Commit b4257b7

Browse files
committed
feat: 初步实现学习系统
Signed-off-by: YdrMaster <[email protected]>
1 parent 9828255 commit b4257b7

File tree

11 files changed

+182
-103
lines changed

11 files changed

+182
-103
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
/build
2-
/.*/
2+
.*/
33
!/.github/

README.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,5 @@
22

33
## Commands
44

5-
- configure: `xmake f -v`
6-
- build: `xmake build`
7-
- run: `xmake run <exerciseXX>`
8-
- clean: `xmake clean -a`
5+
- build: `xmake`
6+
- run: `xmake run learn <exercise number>`

exercises/02_function/main.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
1+
#include <cassert>
12
#include <iostream>
23

34
// NOTE: 这是函数声明
45
int add(int, int);
56

67
int main(int argc, char **argv) {
8+
assert(add(123, 456) == 123 + 456);
9+
710
auto x = 1, y = 2;
811
std::cout << x << " + " << y << " = " << add(x, y) << std::endl;
912
return 0;
1013
}
1114

1215
// TODO: 补全函数定义
13-
int add(int a, int b) {
16+
// THINK: `static` 修饰函数有什么效果?
17+
static int add(int a, int b) {
1418
return a + b;
1519
}

exercises/03_argument&parameter/main.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
// See <https://stackoverflow.com/questions/156767/whats-the-difference-between-an-argument-and-a-parameter>.
1+
// READ: <https://stackoverflow.com/questions/156767/whats-the-difference-between-an-argument-and-a-parameter>
22

33
#include <iostream>
44

5-
void func(int param) {
5+
static void func(int param) {
66
std::cout << "befor add: " << param << std::endl;
77
param += 1;
88
std::cout << "after add: " << param << std::endl;

exercises/10_class_drop/main.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ class BoxedFibonacci {
2020
};
2121

2222
int main(int argc, char **argv) {
23-
BoxedFibonacci fib0, fib1;
24-
std::cout << "fibonacci(10) = " << fib0[10] << std::endl;
25-
std::cout << "fibonacci(100) = " << fib1[100] << std::endl;
23+
BoxedFibonacci fib;
24+
std::cout << "fibonacci(10) = " << fib[10] << std::endl;
2625
return 0;
2726
}

exercises/11_class_copy/main.cpp renamed to exercises/11_class_clone/main.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ class BoxedFibonacci {
99
: cache(new size_t[128]{0, 1}), next(2) {}
1010
BoxedFibonacci(BoxedFibonacci const &other)
1111
: cache(new size_t[128]), next(other.next) {
12+
std::cout << "Clone Fibonacci with next = " << next << std::endl;
1213
std::memcpy(cache, other.cache, sizeof(size_t) * next);
1314
}
1415
~BoxedFibonacci() {

exercises/12_class_move/main.cpp

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include <cassert>
12
#include <iostream>
23

34
class BoxedFibonacci {
@@ -9,7 +10,9 @@ class BoxedFibonacci {
910
: cache(new size_t[128]{0, 1}), next(2) {}
1011
BoxedFibonacci(BoxedFibonacci &&other) noexcept
1112
: cache(std::exchange(other.cache, nullptr)),
12-
next(std::exchange(other.next, 0)) {}
13+
next(std::exchange(other.next, 0)) {
14+
std::cout << "Move Fibonacci with next = " << next << std::endl;
15+
}
1316
~BoxedFibonacci() {
1417
std::cout << "Drop Fibonacci with next = " << next << std::endl;
1518
delete[] cache;
@@ -21,11 +24,20 @@ class BoxedFibonacci {
2124
}
2225
return cache[i];
2326
}
27+
28+
bool is_available() const {
29+
return cache;
30+
}
2431
};
2532

2633
int main(int argc, char **argv) {
27-
BoxedFibonacci fib0, fib1;
28-
std::cout << "fibonacci(10) = " << fib0[10] << std::endl;
29-
std::cout << "fibonacci(100) = " << fib1[100] << std::endl;
34+
BoxedFibonacci fib;
35+
std::cout << "fibonacci(10) = " << fib[10] << std::endl;
36+
37+
auto fib_ = std::move(fib);
38+
assert(!fib.is_available());
39+
assert(fib_.is_available());
40+
std::cout << "fibonacci(12) = " << fib_[12] << std::endl;
41+
3042
return 0;
3143
}
File renamed without changes.

exercises/xmake.lua

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
add_rules("mode.debug", "mode.release")
2+
set_encodings("utf-8")
3+
4+
target("exercise00")
5+
set_kind("binary")
6+
add_files("00_hello_world/main.cpp")
7+
8+
target("exercise01")
9+
set_kind("binary")
10+
add_files("01_variable&add/main.cpp")
11+
12+
target("exercise02")
13+
set_kind("binary")
14+
add_files("02_function/main.cpp")
15+
16+
target("exercise03")
17+
set_kind("binary")
18+
add_files("03_argument&parameter/main.cpp")
19+
20+
target("exercise04")
21+
set_kind("binary")
22+
add_files("04_static/main.cpp")
23+
24+
target("exercise05")
25+
set_kind("binary")
26+
add_files("05_constexpr/main.cpp")
27+
28+
target("exercise06")
29+
set_kind("binary")
30+
add_files("06_loop/main.cpp")
31+
32+
target("exercise07")
33+
set_kind("binary")
34+
add_files("07_struct/main.cpp")
35+
36+
target("exercise08")
37+
set_kind("binary")
38+
add_files("08_class/main.cpp")
39+
40+
target("exercise09")
41+
set_kind("binary")
42+
add_files("09_class_method_const/main.cpp")
43+
44+
target("exercise10")
45+
set_kind("binary")
46+
add_files("10_class_drop/main.cpp")
47+
48+
target("exercise11")
49+
set_kind("binary")
50+
add_files("11_class_clone/main.cpp")
51+
52+
target("exercise12")
53+
set_kind("binary")
54+
add_files("12_class_move/main.cpp")
55+
56+
target("exercise13")
57+
set_kind("binary")
58+
add_files("13_std_array/main.cpp")
59+
60+
target("exercise14")
61+
set_kind("binary")
62+
add_files("14_std_vector/main.cpp")
63+
64+
target("exercise15")
65+
set_kind("binary")
66+
add_files("15_std_deque/main.cpp")
67+
68+
target("exercise16")
69+
set_kind("binary")
70+
add_files("16_std_map/main.cpp")
71+
72+
target("exercise17")
73+
set_kind("binary")
74+
add_files("17_std_accumulate/main.cpp")
75+
76+
target("exercise18")
77+
set_kind("binary")
78+
add_files("18_std_transform/main.cpp")
79+
80+
target("exercise19")
81+
set_kind("binary")
82+
add_files("19_std_fs/main.cpp")
83+
84+
target("exercise20")
85+
set_kind("binary")
86+
add_files("20_std_thread/main.cpp")
87+
88+
target("exercise21")
89+
set_kind("binary")
90+
add_files("21_std_mutex/main.cpp")
91+
92+
target("exercise22")
93+
set_kind("binary")
94+
add_files("22_template/main.cpp")

learn/main.cpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#include <cstdlib>
2+
#include <filesystem>
3+
#include <iostream>
4+
#include <string>
5+
6+
#ifndef __XMAKE__
7+
#define __XMAKE__ "XMAKE is not defined"
8+
#endif
9+
10+
namespace fs = std::filesystem;
11+
constexpr static auto XMAKE = __XMAKE__;
12+
13+
int process_run(const char *cmd, const char *proj) {
14+
static auto exercises = fs::absolute(fs::path(XMAKE) / "exercises");
15+
auto prefix = std::string("xmake ") + cmd + " -P " + exercises.string() + ' ' + proj;
16+
return std::system(prefix.c_str());
17+
}
18+
19+
bool test_exercise(int n) {
20+
char str[] = "exerciseXX";
21+
std::sprintf(str, "exercise%02d", n);
22+
return process_run("", str) == 0 && process_run("run", str) == 0;
23+
}
24+
25+
int main(int argc, char **argv) {
26+
switch (argc) {
27+
case 2: {
28+
int num;
29+
if (1 != std::sscanf(argv[1], "%d", &num)) {
30+
std::cerr << "Invalid exercise number: " << argv[1] << std::endl;
31+
return 1;
32+
};
33+
if (!test_exercise(num)) {
34+
return 1;
35+
}
36+
break;
37+
}
38+
case 1: {
39+
constexpr auto MAX_EXERCISE = 22;
40+
auto success = 0;
41+
for (auto i = 0; i <= MAX_EXERCISE; ++i) {
42+
if (test_exercise(i)) {
43+
++success;
44+
}
45+
}
46+
std::cout << success << "/" << MAX_EXERCISE + 1 << std::endl;
47+
break;
48+
}
49+
default:
50+
std::cerr << "Usage: xmake run learn <exercice number>" << std::endl;
51+
return 1;
52+
}
53+
54+
return 0;
55+
}

0 commit comments

Comments
 (0)