Skip to content

add exercise 34~41 #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions exercises/34_lambda/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#include "../exercise.h"
#include <algorithm>
#include <vector>

// READ: `lambda` <https://zh.cppreference.com/w/cpp/language/lambda>

int main(int argc, char **argv) {
{
std::vector<int> numbers{1, 2, 3, 4, 5};
int sum = 0;

std::for_each(numbers.begin(), numbers.end(), [&sum](int n) {
sum += n;
});

// TODO: 为下列 ASSERT 填写正确的值
ASSERT(sum == ?, "Sum of numbers should be ?");

auto is_even = [](int n) { return n % 2 == 0; };
std::vector<int> evens;
std::copy_if(numbers.begin(), numbers.end(), std::back_inserter(evens), is_even);

ASSERT(evens.size() == ?, "There should be ? even numbers");
ASSERT(evens[0] == ? && evens[1] == ?, "Even numbers should be ?");

// TODO: 使用 Lambda 表达式计算奇数的平方和
int odd_square_sum = 0;
std::for_each(numbers.begin(), numbers.end(), ?);
ASSERT(odd_square_sum == 35, "Sum of squares of odd numbers should be 35");
}
{
// TODO: 为下面的 ? 填写正确的值
auto create_adder = [](int x) {
return [x](int y) { return x + y; };
};
auto add_five = create_adder(?);
ASSERT(add_five(?) == 8, "5 + 3 should be 8");

// 创建一个乘法器的 Lambda 表达式
// TODO: 返回一个 Lambda 表达式
auto create_multiplier = ?;
auto multiply_by_three = create_multiplier(3);
ASSERT(multiply_by_three(4) == 12, "3 * 4 should be 12");
}
{
// 使用 Lambda 表达式捕获外部变量
// TODO: 为下面的 ? 填写正确的表达式
int factor = 10;
auto multiply_by_factor = [?](int x) {
return x * factor;
};
ASSERT(multiply_by_factor(2) == 20, "2 * 10 should be 20");
}
{
// 递归 Lambda 表达式(计算阶乘)
std::function<int(int)> factorial;
// TODO: 替换下面的 ?,实现正确的 Lambda 表达式,计算阶乘
factorial = [?](int n) {
?
};

ASSERT(factorial(0) == 1, "Factorial of 0 should be 1");
ASSERT(factorial(5) == 120, "5! should be 120");
ASSERT(factorial(7) == 5040, "7! should be 5040");

// 递归 Lambda 表达式(fibonacci 数列)
// TODO: 替换下面的 ?,实现正确的 Lambda 表达式,计算 fibonacci 数列
std::function<int(int)> fib;
fib = [?](int n) {
?
};
ASSERT(fib(7) == 13, "7th Fibonacci number should be 13");
}
return 0;
}
62 changes: 62 additions & 0 deletions exercises/35_optional/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#include "../exercise.h"
#include <optional>
#include <vector>

// READ: `optional` <https://zh.cppreference.com/w/cpp/utility/optional>

std::optional<int> find_even(const std::vector<int> &nums) {
for (int n : nums) {
if (n % 2 == 0) {
return n;
}
}
return std::nullopt;
}

int main(int argc, char **argv) {
// TODO: 为下列 ASSERT 中填写正确的值
{
std::vector<int> nums{1, 3, 5, 7, 8};
auto result = find_even(nums);
ASSERT(?, "填入 result.has_value() 或 !result.has_value()");
ASSERT(result.value() == ?, "The even number should be ?");

std::vector<int> odd_nums{1, 3, 5, 7};
auto no_even = find_even(odd_nums);
// TODO: 为 ? 填写正确的值
int default_value = no_even.value_or(?);
ASSERT(default_value == -1, "Default value should be -1 when no even number is found");
}
{
std::optional<int> opt_value;
ASSERT(?, "填入 opt_value.has_value() 或 !opt_value.has_value()");
opt_value.emplace(42);
ASSERT(?, "填入 opt_value.has_value() 或 !opt_value.has_value()");
ASSERT(opt_value.value() == ?, "The value should be ?");

std::optional<int> empty_opt;
ASSERT(empty_opt ? std::nullopt, "填入 == 或 !=");
ASSERT(opt_value ? std::nullopt, "填入 == 或 !=");

opt_value.reset();
ASSERT(?, "填入 opt_value.has_value() 或 !opt_value.has_value()");
ASSERT(opt_value ? std::nullopt, "填入 == 或 !=y");
}
{
// 测试移动构造和赋值
std::optional<std::string> original("Hello World");
std::optional<std::string> moved = std::move(original);
original.reset();
// TODO: 为 ? 填写正确的值
ASSERT(original ? std::nullopt, "填入 == 或 !=");
ASSERT(?, "填入 moved.has_value() 或 !moved.has_value()");
}
{
// 测试 make_optional
auto str_opt = std::make_optional<std::string>(5, 'c');
ASSERT(str_opt.has_value(), "make_optional should create value");
// TODO: 为 ? 填写正确的值
ASSERT(*str_opt == ?, "填入正确的字符串值");
}
return 0;
}
49 changes: 49 additions & 0 deletions exercises/36_variant/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include "../exercise.h"
#include <iostream>
#include <variant>

// READ: `variant` <https://zh.cppreference.com/w/cpp/utility/variant>

int main(int argc, char **argv) {
// TODO: 为下列 ASSERT 填写正确的值
{
std::variant<int, std::string> var;

var = 42;
ASSERT(std::get<int>(var) == ?, "Variant should hold an int");

var = "Hello, variant!";
ASSERT(std::get<std::string>(var) == ?, "Variant should hold a string");
}
{
std::variant<int, std::string> var2 = 100;
std::visit([](auto &&value) {
using T = std::decay_t<decltype(value)>;
if constexpr (std::is_same_v<T, int>) {
ASSERT(value == ?, "Value should be ?");
} else if constexpr (std::is_same_v<T, std::string>) {
ASSERT(?, "填入 true 或 false");
}
},
var2);

ASSERT(std::holds_alternative<?>(var2), "填入正确的数据类型");
ASSERT(!std::holds_alternative<?>(var2), "填入正确的数据类型");

try {
std::get<std::string>(var2);
} catch (const std::bad_variant_access &e) {
std::cout << "Caught exception: " << e.what() << std::endl;
}
}
{
std::variant<int, double, std::string> var3 = 3.14;
ASSERT(std::holds_alternative<?>(var3), "填入正确的数据类型");
ASSERT(std::get<double>(var3) == ?, "Value should be ?");

var3 = "Variant supports multiple types!";
ASSERT(std::holds_alternative<?>(var3), "填入正确的数据类型g");
ASSERT(std::get<std::string>(var3) == ?, "String value mismatch");
}
return 0;
}
67 changes: 67 additions & 0 deletions exercises/37_std_deque/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#include "../exercise.h"
#include <deque>

// READ: `std::deque` <https://zh.cppreference.com/w/cpp/container/deque>

int main(int argc, char **argv) {
// TODO: 为下列 ASSERT 填写正确的值
{
std::deque<int> dq = {1, 2, 3};
// TODO: 使用 `push_front` 和 `push_back` 添加正确元素
dq.push_front(?);
dq.push_back(?);

ASSERT(dq.front() == 0, "Front element should be 0");
ASSERT(dq.back() == 4, "Back element should be 4");

dq.pop_front();
dq.pop_back();

ASSERT(dq.size() == ?, "Deque size should be ? after popping");
ASSERT(dq[0] == ? && dq[1] == ? && dq[2] == ?, "Deque elements should be ?");
}
{
std::deque<std::string> dq;
// TODO: 使用 `emplace_back` 和 `emplace_front` 添加正确元素
dq.emplace_back(?);
dq.emplace_front(?);

ASSERT(dq.front() == "World", "Front element should be 'World'");
ASSERT(dq.back() == "Hello", "Back element should be 'Hello'");

// TODO: 使用 `erase` 删除正确元素
dq.erase(?);
ASSERT(dq.size() == 1, "Deque size should be 1 after erase");
ASSERT(dq.front() == "Hello", "Remaining element should be 'Hello'");
}
{
std::deque<int> dq = {10, 20, 30};
int sum = 0;
for (const auto &val : dq) {
sum += val;
}
ASSERT(sum == ?, "Sum of elements should be ?");
}
{
// 比较运算符测试
std::deque<int> dq1 = {1, 2, 3};
std::deque<int> dq2 = {1, 2, 3};
std::deque<int> dq3 = {1, 2, 4};

ASSERT(dq1 ? dq2, "填入正确的比较运算符,== 或 != 或 < 或 >");
ASSERT(dq1 ? dq3, "填入正确的比较运算符,== 或 != 或 < 或 >");
ASSERT(dq1 ? dq3, "填入正确的比较运算符,== 或 != 或 < 或 >");
}
{
// swap测试
std::deque<int> dq1 = {1, 2, 3};
std::deque<int> dq2 = {4, 5};

dq1.swap(dq2);

// TODO: 为 ? 填写正确的值
ASSERT(dq1.size() == ? && dq1[0] == ? && dq1[1] == ?, "the contents of dq1 should be ?");
ASSERT(dq2.size() == ? && dq2[0] == ? && dq2[1] == ? && dq2[2] == ?, "the contents of dq2 should be ?");
}
return 0;
}
80 changes: 80 additions & 0 deletions exercises/38_std_forward_list/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#include "../exercise.h"
#include <forward_list>

// READ: `std::forward_list` <https://zh.cppreference.com/w/cpp/container/forward_list>

int main(int argc, char **argv) {
// TODO: 为下列 ASSERT 填写正确的值
{
std::forward_list<int> fl = {1, 2, 3};
fl.push_front(0);

// TODO: 为下面的 ASSERT 填写正确的表达式
ASSERT(? == 0, "Front element should be 0");

fl.pop_front();
ASSERT(fl.front() == ?, "Front element should now be ?");
}
{
std::forward_list<int> fl = {1, 3, 5};
auto it = fl.before_begin();
fl.insert_after(it, 0);

ASSERT(fl.front() == ?, "Front element should be ?");

// TODO: 为下面的 ASSERT 填写正确的表达式
fl.erase_after(?);
ASSERT(*std::next(fl.begin()) == 3, "Second element should be 3");
}
{
std::forward_list<int> fl = {10, 20, 30};
int sum = 0;
for (const auto &val : fl) {
sum += val;
}
ASSERT(sum == ?, "Sum of elements should be ?");
}
{
// 合并与排序
std::forward_list<int> fl1 = {3, 5, 7};
std::forward_list<int> fl2 = {2, 4, 6};

fl1.merge(fl2);

// TODO: 为 ? 填写正确的值
int expected[] = {?};
int index = 0;
for (int val : fl1) {
ASSERT(val == expected[index++], "Merged list should be sorted");
}
ASSERT(?, "填入 fl2.empty() 或者 !fl2.empty()");
}
{
// 拼接操作
std::forward_list<int> fl1 = {1, 2, 3, 4};
std::forward_list<int> fl2 = {10, 20};

auto it = fl1.begin();
++it;
fl1.splice_after(it, fl2, fl2.before_begin(), fl2.end());

// TODO: 为 ? 填写正确的值
int expected[] = {?};
int index = 0;
for (int val : fl1) {
ASSERT(val == expected[index++], "Splice_after should insert elements correctly");
}
ASSERT(?, "填入 fl2.empty() 或者 !fl2.empty()");
}
{
// 比较运算符测试
std::forward_list<int> fl1 = {1, 2, 3};
std::forward_list<int> fl2 = {1, 2, 3};
std::forward_list<int> fl3 = {1, 2, 4};

ASSERT(fl1 ? fl2, "填入正确的比较运算符,== 或 != 或 < 或 >");
ASSERT(fl1 ? fl3, "填入正确的比较运算符,== 或 != 或 < 或 >");
ASSERT(fl1 ? fl3, "填入正确的比较运算符,== 或 != 或 < 或 >");
}
return 0;
}
45 changes: 45 additions & 0 deletions exercises/39_std_fs/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include "../exercise.h"
#include <filesystem>
#include <fstream>

// READ: `std::filesystem` <https://zh.cppreference.com/w/cpp/filesystem>

int main(int argc, char **argv) {
namespace fs = std::filesystem;

// TODO: 为下列 ASSERT 填写正确的值
{
// 获取当前路径
fs::path current_path = fs::current_path();
ASSERT(?, "填入 current_path.empty() 或者 !current_path.empty()");
}
{
fs::path test_dir = "test_directory";
fs::create_directory(test_dir);
ASSERT(?, "填入 fs::exists(test_dir) 或者 !fs::exists(test_dir)");

fs::remove(test_dir);
ASSERT(?, "填入 fs::exists(test_dir) 或者 !fs::exists(test_dir)");
}
{
fs::path test_file = "test_file.txt";
std::ofstream ofs(test_file);
ofs << "Hello, filesystem!";
ofs.close();

ASSERT(?, "填入 fs::exists(test_file) 或者 !fs::exists(test_file)");

std::ifstream ifs(test_file);
std::string content;
std::getline(ifs, content);
ASSERT(content == ?, "File content should match the written content");
ifs.close();

auto file_size = fs::file_size(test_file);
ASSERT(file_size == ?, "File size should be ?");

fs::remove(test_file);
ASSERT(!fs::exists(test_file), "File should not exist after removal");
}
return 0;
}
Loading