File tree Expand file tree Collapse file tree 11 files changed +182
-103
lines changed Expand file tree Collapse file tree 11 files changed +182
-103
lines changed Original file line number Diff line number Diff line change 1
1
/build
2
- / . * /
2
+ . * /
3
3
! /.github /
Original file line number Diff line number Diff line change 2
2
3
3
## Commands
4
4
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> `
Original file line number Diff line number Diff line change
1
+ #include < cassert>
1
2
#include < iostream>
2
3
3
4
// NOTE: 这是函数声明
4
5
int add (int , int );
5
6
6
7
int main (int argc, char **argv) {
8
+ assert (add (123 , 456 ) == 123 + 456 );
9
+
7
10
auto x = 1 , y = 2 ;
8
11
std::cout << x << " + " << y << " = " << add (x, y) << std::endl;
9
12
return 0 ;
10
13
}
11
14
12
15
// TODO: 补全函数定义
13
- int add (int a, int b) {
16
+ // THINK: `static` 修饰函数有什么效果?
17
+ static int add (int a, int b) {
14
18
return a + b;
15
19
}
Original file line number Diff line number Diff line change 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>
2
2
3
3
#include < iostream>
4
4
5
- void func (int param) {
5
+ static void func (int param) {
6
6
std::cout << " befor add: " << param << std::endl;
7
7
param += 1 ;
8
8
std::cout << " after add: " << param << std::endl;
Original file line number Diff line number Diff line change @@ -20,8 +20,7 @@ class BoxedFibonacci {
20
20
};
21
21
22
22
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;
26
25
return 0 ;
27
26
}
Original file line number Diff line number Diff line change @@ -9,6 +9,7 @@ class BoxedFibonacci {
9
9
: cache(new size_t [128 ]{0 , 1 }), next(2 ) {}
10
10
BoxedFibonacci (BoxedFibonacci const &other)
11
11
: cache(new size_t [128 ]), next(other.next) {
12
+ std::cout << " Clone Fibonacci with next = " << next << std::endl;
12
13
std::memcpy (cache, other.cache , sizeof (size_t ) * next);
13
14
}
14
15
~BoxedFibonacci () {
Original file line number Diff line number Diff line change
1
+ #include < cassert>
1
2
#include < iostream>
2
3
3
4
class BoxedFibonacci {
@@ -9,7 +10,9 @@ class BoxedFibonacci {
9
10
: cache(new size_t [128 ]{0 , 1 }), next(2 ) {}
10
11
BoxedFibonacci (BoxedFibonacci &&other) noexcept
11
12
: 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
+ }
13
16
~BoxedFibonacci () {
14
17
std::cout << " Drop Fibonacci with next = " << next << std::endl;
15
18
delete[] cache;
@@ -21,11 +24,20 @@ class BoxedFibonacci {
21
24
}
22
25
return cache[i];
23
26
}
27
+
28
+ bool is_available () const {
29
+ return cache;
30
+ }
24
31
};
25
32
26
33
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
+
30
42
return 0 ;
31
43
}
File renamed without changes.
Original file line number Diff line number Diff line change
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¶meter/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" )
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments