From 67bb27e9f5beec4d8ca0a6c29f045d5f23d6f40f Mon Sep 17 00:00:00 2001 From: Nguyễn Gia Phong Date: Sun, 28 Feb 2021 17:25:57 +0700 Subject: [lang] Reorganize language learning archive --- lang/cpptour/README.md | 10 ++++++ lang/cpptour/Vector-test.cc | 29 ++++++++++++++++ lang/cpptour/Vector.cc | 38 +++++++++++++++++++++ lang/cpptour/Vector.h | 12 +++++++ lang/cpptour/abstract.cc | 19 +++++++++++ lang/cpptour/abstract.h | 18 ++++++++++ lang/cpptour/count.cc | 11 ++++++ lang/cpptour/enumcls.cc | 28 +++++++++++++++ lang/cpptour/helloworld.cc | 7 ++++ lang/cpptour/mycomplex.cc | 67 ++++++++++++++++++++++++++++++++++++ lang/cpptour/mycomplex.h | 15 ++++++++ lang/cpptour/myvec.cc | 36 ++++++++++++++++++++ lang/cpptour/square.cc | 21 ++++++++++++ lang/cpptour/static-ass.cc | 8 +++++ lang/cpptour/veccls.cc | 32 +++++++++++++++++ lang/cpptour/vecuser.cc | 28 +++++++++++++++ lang/cpptour/weirdo.cc | 83 +++++++++++++++++++++++++++++++++++++++++++++ 17 files changed, 462 insertions(+) create mode 100644 lang/cpptour/README.md create mode 100644 lang/cpptour/Vector-test.cc create mode 100644 lang/cpptour/Vector.cc create mode 100644 lang/cpptour/Vector.h create mode 100644 lang/cpptour/abstract.cc create mode 100644 lang/cpptour/abstract.h create mode 100644 lang/cpptour/count.cc create mode 100644 lang/cpptour/enumcls.cc create mode 100644 lang/cpptour/helloworld.cc create mode 100644 lang/cpptour/mycomplex.cc create mode 100644 lang/cpptour/mycomplex.h create mode 100644 lang/cpptour/myvec.cc create mode 100644 lang/cpptour/square.cc create mode 100644 lang/cpptour/static-ass.cc create mode 100644 lang/cpptour/veccls.cc create mode 100644 lang/cpptour/vecuser.cc create mode 100644 lang/cpptour/weirdo.cc (limited to 'lang/cpptour') diff --git a/lang/cpptour/README.md b/lang/cpptour/README.md new file mode 100644 index 0000000..b60cb65 --- /dev/null +++ b/lang/cpptour/README.md @@ -0,0 +1,10 @@ +A Tour of C++ +------------- + +These are my draft while following Bjarne Stroustrup tour to learn some C++11. +Not that I eventually fell for the language, but I did realize that I simply +cannot evite it forever. In fact, this is the preparation for a Python +extension. + +Since the book is non-free (in either sense), I may not upload it. However, +one could try to look for it in a *lib*rary. *Gen*erally it's available. diff --git a/lang/cpptour/Vector-test.cc b/lang/cpptour/Vector-test.cc new file mode 100644 index 0000000..fbdf129 --- /dev/null +++ b/lang/cpptour/Vector-test.cc @@ -0,0 +1,29 @@ +#include +#include +#include + +#include "Vector.h" + +using namespace std; + +void +neg_length () +{ + try { Vector v (-27); } + catch (length_error) { cout << "it's alright" << endl; } + catch (bad_alloc) { cout << "BIG OOF!" << endl; } +} + +void +init () +{ + Vector v {7.4, 3.2, 5.2, 6.9, 9.5, 4.2, 21.7}; + assert(v[5] == 4.2); +} + +int +main () +{ + neg_length (); + init (); +} diff --git a/lang/cpptour/Vector.cc b/lang/cpptour/Vector.cc new file mode 100644 index 0000000..8f94345 --- /dev/null +++ b/lang/cpptour/Vector.cc @@ -0,0 +1,38 @@ +#include + +#include "Vector.h" + +using namespace std; + +Vector::Vector (int s) +{ + if (s < 0) + throw length_error{"You're being negetive!"}; + elem = new double[s]; + sz = s; +} + +Vector::Vector (initializer_list lst) +: elem {new double[lst.size()]}, sz {static_cast (lst.size())} +{ + copy(lst.begin(), lst.end(), elem); +} + +Vector::~Vector () +{ + delete[] elem; +} + +int +Vector::size () noexcept +{ + return sz; +} + +double& +Vector::operator[] (int i) +{ + if (i < 0 || size() <= i) + throw out_of_range{"Vector::operator[]"}; + return elem[i]; +} diff --git a/lang/cpptour/Vector.h b/lang/cpptour/Vector.h new file mode 100644 index 0000000..8508503 --- /dev/null +++ b/lang/cpptour/Vector.h @@ -0,0 +1,12 @@ +class Vector { +public: + Vector (int s); + Vector (std::initializer_list); + ~Vector (); + double& operator[] (int i); + int size () noexcept; + void push_back (double); +private: + double* elem; + int sz; +}; diff --git a/lang/cpptour/abstract.cc b/lang/cpptour/abstract.cc new file mode 100644 index 0000000..f747a37 --- /dev/null +++ b/lang/cpptour/abstract.cc @@ -0,0 +1,19 @@ +Vector_container:Vector_container (int s) : v (s) +{ +} + +Vector_container:~Vector_container () +{ +} + +double& +Vector_container:operator[] (int i) +{ + return v[i]; +} + +int +size () const +{ + return v.size (); +} diff --git a/lang/cpptour/abstract.h b/lang/cpptour/abstract.h new file mode 100644 index 0000000..71191d9 --- /dev/null +++ b/lang/cpptour/abstract.h @@ -0,0 +1,18 @@ +#include "Vector.h" + +class Container { +public: + virtual double& operator[] (int) = 0; + virtual int size () const = 0; + virtual ~Container () {} +}; + +class Vector_container : public Container { + Vector v; +public: + Vector_container (int); + ~Vector_container (); + + double& operator[] (int); + int size () const; +}; diff --git a/lang/cpptour/count.cc b/lang/cpptour/count.cc new file mode 100644 index 0000000..a047f04 --- /dev/null +++ b/lang/cpptour/count.cc @@ -0,0 +1,11 @@ +#include + +using namespace std; + +int +main () +{ + int v[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; + for (auto& x : v) + cout << x << endl; +} diff --git a/lang/cpptour/enumcls.cc b/lang/cpptour/enumcls.cc new file mode 100644 index 0000000..2340f65 --- /dev/null +++ b/lang/cpptour/enumcls.cc @@ -0,0 +1,28 @@ +#include + +using namespace std; + +enum class Color { red, blue, green }; +enum class TraficLight { green, yellow, red }; + +TraficLight& operator++ (TraficLight& t) +{ + switch (t) + { + case TraficLight::green: + return t = TraficLight::yellow; + case TraficLight::yellow: + return t = TraficLight::red; + case TraficLight::red: + return t = TraficLight::green; + } +} + +int +main () +{ + Color col = Color::red; + TraficLight light = TraficLight::red; + TraficLight nxt = ++light; + // ugh now we test if it compiles? just wanna build some muscle memory +} diff --git a/lang/cpptour/helloworld.cc b/lang/cpptour/helloworld.cc new file mode 100644 index 0000000..89a7f8a --- /dev/null +++ b/lang/cpptour/helloworld.cc @@ -0,0 +1,7 @@ +#include + +int +main () +{ + std::cout << "Hello, World!" << std::endl; +} diff --git a/lang/cpptour/mycomplex.cc b/lang/cpptour/mycomplex.cc new file mode 100644 index 0000000..fade98f --- /dev/null +++ b/lang/cpptour/mycomplex.cc @@ -0,0 +1,67 @@ +#include "mycomplex.h" + +constexpr double +square (double x) +{ + return x * x; +} + +complex& +operator*= (complex& a, complex b) +{ + double r = a.real() * b.real() - a.imag() * b.imag(); + double i = a.real() * b.imag() + a.imag() * b.real(); + a.real(r); + a.imag(i); + return a; +} + +complex& +operator/= (complex& a, complex b) +{ + double d = square(b.real()) + square(b.imag()); + complex c {b.real() / d, -b.imag() / d}; + return a *= c; +} + +complex +operator+ (complex a, complex b) +{ + return a += b; +} + +complex +operator- (complex a, complex b) +{ + return a -= b; +} + +complex +operator- (complex a) +{ + return {-a.real(), -a.imag()}; +} + +complex +operator* (complex a, complex b) +{ + return a *= b; +} + +complex +operator/ (complex a, complex b) +{ + return a /= b; +} + +bool +operator== (complex a, complex b) +{ + return a.real() == b.real() && a.imag() == b.imag(); +} + +bool +operator!= (complex a, complex b) +{ + return !(a == b); +} diff --git a/lang/cpptour/mycomplex.h b/lang/cpptour/mycomplex.h new file mode 100644 index 0000000..b7440ee --- /dev/null +++ b/lang/cpptour/mycomplex.h @@ -0,0 +1,15 @@ +class complex { + double re, im; +public: + complex (double r, double i) : re {r}, im {i} {} + complex (double r) : re {r}, im {0.0} {} + complex () : re {0.0}, im {0.0} {} + + double real () const { return re; } + void real (double r) { re = r; } + double imag () const { return im; } + void imag (double i) { im = i; } + + complex& operator+= (complex z) { re += z.re, im += z.im; return *this; } + complex& operator-= (complex z) { re -= z.re, im -= z.im; return *this; } +}; diff --git a/lang/cpptour/myvec.cc b/lang/cpptour/myvec.cc new file mode 100644 index 0000000..1314730 --- /dev/null +++ b/lang/cpptour/myvec.cc @@ -0,0 +1,36 @@ +#include + +using namespace std; + +struct Vector +{ + int sz; // number of elements + double* elem; // pointer to elements +}; + +void +vector_init (Vector& v, int s) +{ + v.elem = new double[s]; + v.sz = s; +} + +double +read_and_sum (int s) +{ + Vector v; + vector_init (v, s); + for (int i = 0; i != s; ++i) + cin >> v.elem[i]; + + double sum = 0; + for (int i = 0; i != s; ++i) + sum += v.elem[i]; + return sum; +} + +int +main () +{ + cout << read_and_sum (5) << endl; +} diff --git a/lang/cpptour/square.cc b/lang/cpptour/square.cc new file mode 100644 index 0000000..fb14456 --- /dev/null +++ b/lang/cpptour/square.cc @@ -0,0 +1,21 @@ +#include + +using namespace std; + +double +square (double x) +{ + return x * x; +} + +void +print_square (double x) +{ + cout << "the quare of " << x << " is " << square (x) << endl; +} + +int +main () +{ + print_square (1.234); +} diff --git a/lang/cpptour/static-ass.cc b/lang/cpptour/static-ass.cc new file mode 100644 index 0000000..837a965 --- /dev/null +++ b/lang/cpptour/static-ass.cc @@ -0,0 +1,8 @@ +constexpr double C = 2999792.458; // km/s + +int +main () +{ + constexpr double local_max = 160.0 / (60 * 60); + static_assert (local_max < C, "can't go that fast"); +} diff --git a/lang/cpptour/veccls.cc b/lang/cpptour/veccls.cc new file mode 100644 index 0000000..0162d23 --- /dev/null +++ b/lang/cpptour/veccls.cc @@ -0,0 +1,32 @@ +#include + +using namespace std; + +class Vector { +public: + Vector (int s) : elem {new double[s]}, sz {s} {} // construct a Vector + double& operator[] (int i) { return elem[i]; } // random access + int size () { return sz; } +private: + double* elem; // pointer to the elements + int sz; // the number of elements +}; + +double +read_and_sum (int s) +{ + Vector v (s); + for (int i = 0; i != v.size (); ++i) + cin >> v[i]; + + double sum = 0; + for (int i = 0; i != v.size (); ++i) + sum += v[i]; + return sum; +} + +int +main () +{ + cout << read_and_sum (5) << endl; +} diff --git a/lang/cpptour/vecuser.cc b/lang/cpptour/vecuser.cc new file mode 100644 index 0000000..0eefb0e --- /dev/null +++ b/lang/cpptour/vecuser.cc @@ -0,0 +1,28 @@ +#include +#include +#include + +#include "Vector.h" + +using namespace std; + +double +sqrt_sum (Vector& v) +{ + double sum = 0; + for (int i = 0; i <= v.size(); ++i) + try { sum += sqrt(v[i]); } + catch (out_of_range) { cout << "Yeet!" << endl; } + return sum; +} + +int +main () +{ + int n; + cin >> n; + Vector v (n); + while (n--) + cin >> v[n]; + cout << sqrt_sum (v) << endl; +} diff --git a/lang/cpptour/weirdo.cc b/lang/cpptour/weirdo.cc new file mode 100644 index 0000000..f79336c --- /dev/null +++ b/lang/cpptour/weirdo.cc @@ -0,0 +1,83 @@ +#include +#include +#include +#include +#include + +using namespace std; + +typedef unordered_map charmap; +typedef set charset; + +constexpr double INF = 1e7; +const charset VOWELS {'a', 'e', 'i', 'o', 'u'}; + +inline size_t +sqr (size_t i) +{ + return i * i; +} + +bool +isvowel (const string& s, size_t i) +{ + try { return VOWELS.count (s.at (i)); } + catch (out_of_range const& e) { return true; } +} + +void +update (const string& s, charmap& x, charmap& f) +{ + charset b; + for (const auto& c : s) + { + f[c]++; + b.insert (c); + } + for (const auto& c : b) + x[c]++; +} + +int +main () +{ + size_t t, l; + string s; + + cin >> t; + while (t--) + { + charmap xa, fa, xb, fb; + cin >> l; + while (l--) + { + cin >> s; + size_t i = s.size (); + bool a = true; + + while (i--) + if (isvowel (s, i - 1) + isvowel (s, i) + isvowel (s, i + 1) < 2) + { + update (s, xb, fb); + a = false; + break; + } + if (a) + update (s, xa, fa); + } + + double sc = 1.0; + for (const auto& p : xa) + sc *= p.second; + for (const auto& p : fa) + sc /= sqr (p.second); + for (const auto& p : xb) + sc /= p.second; + for (const auto& p : fb) + sc *= sqr (p.second); + if (sc > INF) + cout << "Infinity" << endl; + else + cout << sc << endl; + } +} -- cgit 1.4.1