Skip to content

Commit

Permalink
feat: 初步实现学习系统
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 9828255 commit b4257b7
Show file tree
Hide file tree
Showing 11 changed files with 182 additions and 103 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
/build
/.*/
.*/
!/.github/
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,5 @@

## Commands

- configure: `xmake f -v`
- build: `xmake build`
- run: `xmake run <exerciseXX>`
- clean: `xmake clean -a`
- build: `xmake`
- run: `xmake run learn <exercise number>`
6 changes: 5 additions & 1 deletion exercises/02_function/main.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
#include <cassert>
#include <iostream>

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

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

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

// TODO: 补全函数定义
int add(int a, int b) {
// THINK: `static` 修饰函数有什么效果?
static int add(int a, int b) {
return a + b;
}
4 changes: 2 additions & 2 deletions exercises/03_argument&parameter/main.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// See <https://stackoverflow.com/questions/156767/whats-the-difference-between-an-argument-and-a-parameter>.
// READ: <https://stackoverflow.com/questions/156767/whats-the-difference-between-an-argument-and-a-parameter>

#include <iostream>

void func(int param) {
static void func(int param) {
std::cout << "befor add: " << param << std::endl;
param += 1;
std::cout << "after add: " << param << std::endl;
Expand Down
5 changes: 2 additions & 3 deletions exercises/10_class_drop/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ class BoxedFibonacci {
};

int main(int argc, char **argv) {
BoxedFibonacci fib0, fib1;
std::cout << "fibonacci(10) = " << fib0[10] << std::endl;
std::cout << "fibonacci(100) = " << fib1[100] << std::endl;
BoxedFibonacci fib;
std::cout << "fibonacci(10) = " << fib[10] << std::endl;
return 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class BoxedFibonacci {
: cache(new size_t[128]{0, 1}), next(2) {}
BoxedFibonacci(BoxedFibonacci const &other)
: cache(new size_t[128]), next(other.next) {
std::cout << "Clone Fibonacci with next = " << next << std::endl;
std::memcpy(cache, other.cache, sizeof(size_t) * next);
}
~BoxedFibonacci() {
Expand Down
20 changes: 16 additions & 4 deletions exercises/12_class_move/main.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <cassert>
#include <iostream>

class BoxedFibonacci {
Expand All @@ -9,7 +10,9 @@ class BoxedFibonacci {
: cache(new size_t[128]{0, 1}), next(2) {}
BoxedFibonacci(BoxedFibonacci &&other) noexcept
: cache(std::exchange(other.cache, nullptr)),
next(std::exchange(other.next, 0)) {}
next(std::exchange(other.next, 0)) {
std::cout << "Move Fibonacci with next = " << next << std::endl;
}
~BoxedFibonacci() {
std::cout << "Drop Fibonacci with next = " << next << std::endl;
delete[] cache;
Expand All @@ -21,11 +24,20 @@ class BoxedFibonacci {
}
return cache[i];
}

bool is_available() const {
return cache;
}
};

int main(int argc, char **argv) {
BoxedFibonacci fib0, fib1;
std::cout << "fibonacci(10) = " << fib0[10] << std::endl;
std::cout << "fibonacci(100) = " << fib1[100] << std::endl;
BoxedFibonacci fib;
std::cout << "fibonacci(10) = " << fib[10] << std::endl;

auto fib_ = std::move(fib);
assert(!fib.is_available());
assert(fib_.is_available());
std::cout << "fibonacci(12) = " << fib_[12] << std::endl;

return 0;
}
File renamed without changes.
94 changes: 94 additions & 0 deletions exercises/xmake.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
add_rules("mode.debug", "mode.release")
set_encodings("utf-8")

target("exercise00")
set_kind("binary")
add_files("00_hello_world/main.cpp")

target("exercise01")
set_kind("binary")
add_files("01_variable&add/main.cpp")

target("exercise02")
set_kind("binary")
add_files("02_function/main.cpp")

target("exercise03")
set_kind("binary")
add_files("03_argument&parameter/main.cpp")

target("exercise04")
set_kind("binary")
add_files("04_static/main.cpp")

target("exercise05")
set_kind("binary")
add_files("05_constexpr/main.cpp")

target("exercise06")
set_kind("binary")
add_files("06_loop/main.cpp")

target("exercise07")
set_kind("binary")
add_files("07_struct/main.cpp")

target("exercise08")
set_kind("binary")
add_files("08_class/main.cpp")

target("exercise09")
set_kind("binary")
add_files("09_class_method_const/main.cpp")

target("exercise10")
set_kind("binary")
add_files("10_class_drop/main.cpp")

target("exercise11")
set_kind("binary")
add_files("11_class_clone/main.cpp")

target("exercise12")
set_kind("binary")
add_files("12_class_move/main.cpp")

target("exercise13")
set_kind("binary")
add_files("13_std_array/main.cpp")

target("exercise14")
set_kind("binary")
add_files("14_std_vector/main.cpp")

target("exercise15")
set_kind("binary")
add_files("15_std_deque/main.cpp")

target("exercise16")
set_kind("binary")
add_files("16_std_map/main.cpp")

target("exercise17")
set_kind("binary")
add_files("17_std_accumulate/main.cpp")

target("exercise18")
set_kind("binary")
add_files("18_std_transform/main.cpp")

target("exercise19")
set_kind("binary")
add_files("19_std_fs/main.cpp")

target("exercise20")
set_kind("binary")
add_files("20_std_thread/main.cpp")

target("exercise21")
set_kind("binary")
add_files("21_std_mutex/main.cpp")

target("exercise22")
set_kind("binary")
add_files("22_template/main.cpp")
55 changes: 55 additions & 0 deletions learn/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include <cstdlib>
#include <filesystem>
#include <iostream>
#include <string>

#ifndef __XMAKE__
#define __XMAKE__ "XMAKE is not defined"
#endif

namespace fs = std::filesystem;
constexpr static auto XMAKE = __XMAKE__;

int process_run(const char *cmd, const char *proj) {
static auto exercises = fs::absolute(fs::path(XMAKE) / "exercises");
auto prefix = std::string("xmake ") + cmd + " -P " + exercises.string() + ' ' + proj;
return std::system(prefix.c_str());
}

bool test_exercise(int n) {
char str[] = "exerciseXX";
std::sprintf(str, "exercise%02d", n);
return process_run("", str) == 0 && process_run("run", str) == 0;
}

int main(int argc, char **argv) {
switch (argc) {
case 2: {
int num;
if (1 != std::sscanf(argv[1], "%d", &num)) {
std::cerr << "Invalid exercise number: " << argv[1] << std::endl;
return 1;
};
if (!test_exercise(num)) {
return 1;
}
break;
}
case 1: {
constexpr auto MAX_EXERCISE = 22;
auto success = 0;
for (auto i = 0; i <= MAX_EXERCISE; ++i) {
if (test_exercise(i)) {
++success;
}
}
std::cout << success << "/" << MAX_EXERCISE + 1 << std::endl;
break;
}
default:
std::cerr << "Usage: xmake run learn <exercice number>" << std::endl;
return 1;
}

return 0;
}
92 changes: 4 additions & 88 deletions xmake.lua
Original file line number Diff line number Diff line change
@@ -1,94 +1,10 @@
add_rules("mode.debug", "mode.release")
set_encodings("utf-8")

target("exercise00")
target("learn")
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")

target("exercise06")
set_kind("binary")
add_files("exercises/06_loop/main.cpp")

target("exercise07")
set_kind("binary")
add_files("exercises/07_struct/main.cpp")

target("exercise08")
set_kind("binary")
add_files("exercises/08_class/main.cpp")

target("exercise09")
set_kind("binary")
add_files("exercises/09_class_method_const/main.cpp")

target("exercise10")
set_kind("binary")
add_files("exercises/10_class_drop/main.cpp")

target("exercise11")
set_kind("binary")
add_files("exercises/11_class_copy/main.cpp")
add_defines(string.format("__XMAKE__=\"%s\"", os.scriptdir():gsub("\\", "/")))

target("exercise12")
set_kind("binary")
add_files("exercises/12_class_move/main.cpp")

target("exercise13")
set_kind("binary")
add_files("exercises/13_std_move/main.cpp")

target("exercise14")
set_kind("binary")
add_files("exercises/14_std_vector/main.cpp")

target("exercise15")
set_kind("binary")
add_files("exercises/15_std_deque/main.cpp")

target("exercise16")
set_kind("binary")
add_files("exercises/16_std_map/main.cpp")

target("exercise17")
set_kind("binary")
add_files("exercises/17_std_accumulate/main.cpp")

target("exercise18")
set_kind("binary")
add_files("exercises/18_std_transform/main.cpp")

target("exercise19")
set_kind("binary")
add_files("exercises/19_std_fs/main.cpp")

target("exercise20")
set_kind("binary")
add_files("exercises/20_std_thread/main.cpp")

target("exercise21")
set_kind("binary")
add_files("exercises/21_std_mutex/main.cpp")

target("exercise22")
set_kind("binary")
add_files("exercises/22_template/main.cpp")
set_languages("cxx17")
add_files("learn/main.cpp")

0 comments on commit b4257b7

Please sign in to comment.