about summary refs log tree commit diff
path: root/lang/cpptour
diff options
context:
space:
mode:
authorNguyễn Gia Phong <mcsinyx@disroot.org>2021-02-28 17:25:57 +0700
committerNguyễn Gia Phong <mcsinyx@disroot.org>2021-02-28 17:25:57 +0700
commit67bb27e9f5beec4d8ca0a6c29f045d5f23d6f40f (patch)
treeb677228d71b638dd6e423b037311ef0913ba66e7 /lang/cpptour
parentee9b8fc921f48dc893808e1c9dbfbef321aa362c (diff)
downloadcp-67bb27e9f5beec4d8ca0a6c29f045d5f23d6f40f.tar.gz
[lang] Reorganize language learning archive
Diffstat (limited to 'lang/cpptour')
-rw-r--r--lang/cpptour/README.md10
-rw-r--r--lang/cpptour/Vector-test.cc29
-rw-r--r--lang/cpptour/Vector.cc38
-rw-r--r--lang/cpptour/Vector.h12
-rw-r--r--lang/cpptour/abstract.cc19
-rw-r--r--lang/cpptour/abstract.h18
-rw-r--r--lang/cpptour/count.cc11
-rw-r--r--lang/cpptour/enumcls.cc28
-rw-r--r--lang/cpptour/helloworld.cc7
-rw-r--r--lang/cpptour/mycomplex.cc67
-rw-r--r--lang/cpptour/mycomplex.h15
-rw-r--r--lang/cpptour/myvec.cc36
-rw-r--r--lang/cpptour/square.cc21
-rw-r--r--lang/cpptour/static-ass.cc8
-rw-r--r--lang/cpptour/veccls.cc32
-rw-r--r--lang/cpptour/vecuser.cc28
-rw-r--r--lang/cpptour/weirdo.cc83
17 files changed, 462 insertions, 0 deletions
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 <cassert>
+#include <iostream>
+#include <stdexcept>
+
+#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 <stdexcept>
+
+#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<double> lst)
+: elem {new double[lst.size()]}, sz {static_cast<int> (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<double>);
+  ~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 <iostream>
+
+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 <iostream>
+
+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 <iostream>
+
+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 <iostream>
+
+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 <iostream>
+
+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 <iostream>
+
+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 <cmath>
+#include <iostream>
+#include <stdexcept>
+
+#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 <iostream>
+#include <set>
+#include <stdexcept>
+#include <string>
+#include <unordered_map>
+
+using namespace std;
+
+typedef unordered_map<char, size_t> charmap;
+typedef set<char> 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;
+    }
+}