Skip to content

Commit

Permalink
CodeyZ-first-pr (#315)
Browse files Browse the repository at this point in the history
* CodeyZ-first-pr

1~3题
  • Loading branch information
Codey-Z authored Apr 24, 2024
1 parent 491f2a8 commit ecd1dcd
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/群友提交/第01题/CodeyZ.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include<iostream>
#include<vector>
#include <functional>

template<typename V,typename T>
auto operator|(V&& v, T&& f) {
for (auto &i : v) {
f(i);
}
return v;
}


int main() {

std::vector v{ 1, 2, 3 };
std::function f{ [](const int& i) {std::cout << i << ' '; } };
auto f2 = [](int& i) {i *= i; };
v | f2 | f;
return 0;
}
32 changes: 32 additions & 0 deletions src/群友提交/第02题/CodeyZ.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include<iostream>
#include<format>
#include<string.h>
#include<numbers>

struct display{
private:
const char* m_s;
public:
display(const char* s) :m_s(s) {}

template<typename...Args>
auto operator()(Args...args) {
return std::vformat(m_s, std::make_format_args(std::forward<Args>(args)...));
}
};

auto operator""_f(const char* s,std::size_t n) {
return display{s};
}

int main() {
std::cout << "乐 :{} *\n"_f(5);
std::cout << "乐 :{0} {0} *\n"_f(5);
std::cout << "乐 :{:b} *\n"_f(0b01010101);
std::cout << "{:*<10}"_f("卢瑟");
std::cout << '\n';
int n{};
std::cin >> n;
std::cout << "π:{:.{}f}\n"_f(std::numbers::pi_v<double>, n);
return 0;
}
36 changes: 36 additions & 0 deletions src/群友提交/第03题/CodeyZ.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include<iostream>
#include<format>
#include<string.h>
#include<string_view>
struct Frac {
int a, b;
};

template<>
struct std::formatter<Frac, char> {

template<class ParseContext>
constexpr auto parse(ParseContext& ctx) {
auto it = ctx.begin();
if (it == ctx.end())
return it;
if (it != ctx.end() && *it != '}')
throw std::format_error("无效的 QuotableString 格式参数。");
return it;
}
template<class Fmtcontext>
auto format(::Frac f, Fmtcontext& ctx)const {
return format_to(ctx.out(), "{}/{}", f.a, f.b);
}
};

template<typename...Args>
auto print(std::string_view s,Args&&...args) {
std::cout << std::vformat(s,std::make_format_args(std::forward<Args>(args)...)) << '\n';
}

int main() {
Frac f{ 1,10 };
print("{}", f);// 结果为1/10
//std::cout << std::format("{}", f);
}

0 comments on commit ecd1dcd

Please sign in to comment.