diff options
author | Nguyễn Gia Phong <vn.mcsinyx@gmail.com> | 2019-07-21 03:21:00 +0700 |
---|---|---|
committer | Nguyễn Gia Phong <vn.mcsinyx@gmail.com> | 2019-07-21 03:21:00 +0700 |
commit | 029688e143109344989b1529259e391822abb0aa (patch) | |
tree | 556f96273a5caed7df2e70a6ba253bcc7627bd33 | |
parent | be6678fbca007e73d69c9a9c5cddb8241a987149 (diff) | |
download | cp-029688e143109344989b1529259e391822abb0aa.tar.gz |
[cpptour] Learn the basis
-rw-r--r-- | README.md | 13 | ||||
-rw-r--r-- | cpptour/README.md | 10 | ||||
-rw-r--r-- | cpptour/Vector-test.cc | 20 | ||||
-rw-r--r-- | cpptour/Vector.cc | 25 | ||||
-rw-r--r-- | cpptour/Vector.h | 9 | ||||
-rw-r--r-- | cpptour/count.cc | 11 | ||||
-rw-r--r-- | cpptour/enumcls.cc | 28 | ||||
-rw-r--r-- | cpptour/helloworld.cc | 7 | ||||
-rw-r--r-- | cpptour/myvec.cc | 36 | ||||
-rw-r--r-- | cpptour/square.cc | 21 | ||||
-rw-r--r-- | cpptour/static-ass.cc | 8 | ||||
-rw-r--r-- | cpptour/veccls.cc | 32 | ||||
-rw-r--r-- | cpptour/vecuser.cc | 28 |
13 files changed, 244 insertions, 4 deletions
diff --git a/README.md b/README.md index 096082e..393c47a 100644 --- a/README.md +++ b/README.md @@ -8,8 +8,10 @@ Bài tập luyện tập thi Olympic, học sinh giỏi Tin học, trong đó: | `COCI` | [Giải Tin học Croatia mở rộng][0] | | `NTU` | [Đại học Nha Trang][1] | | `THT` | Hội thi Tin học trẻ | -| `codeforces` | [Codeforces][2] | -| `daily` | [/r/dailyprogrammer][3] | +| `codechef` | [Codechef][2] | +| `codeforces` | [Codeforces][3] | +| `cpptour` | A Tour of C++ | +| `daily` | [/r/dailyprogrammer][4] | | `others` | Các đề bài không rõ nguồn | | `paip` | Paradigms of Artificial Intelligence Programming | | `sicp` | Structure and Interpretation of Computer Programs | @@ -18,8 +20,9 @@ Bài tập luyện tập thi Olympic, học sinh giỏi Tin học, trong đó: [0]: http://www.hsin.hr/coci/ [1]: http://laptrinh.ntu.edu.vn/ -[2]: http://codeforces.com/ -[3]: https://www.reddit.com/r/dailyprogrammer +[2]: http://codechef.com/ +[3]: http://codeforces.com/ +[4]: https://www.reddit.com/r/dailyprogrammer Ở mỗi thư mục con sẽ có tệp `README.md` ghi lại đề bài. Riêng `COCI`, `NTU` và `codeforces` sẽ chỉ có danh sách đường dẫn tới các đề bài. Đề bài sẽ được cập @@ -31,7 +34,9 @@ Phiên bản các trình dịch sử dụng test: | ----------- | ------------------ | | C | GNU GCC 4.9+ | | Common Lisp | SBCL 1.4.8+ | +| Lua | Lua 5.1+ | | Pascal | Free Pascal 2.6.4+ | +| Perl 6 | Rakudo 2018.12+ | | Python | Python 3.5+ | | Scheme | GNU Guile 2.0.11+ | diff --git a/cpptour/README.md b/cpptour/README.md new file mode 100644 index 0000000..b60cb65 --- /dev/null +++ b/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/cpptour/Vector-test.cc b/cpptour/Vector-test.cc new file mode 100644 index 0000000..b49ff85 --- /dev/null +++ b/cpptour/Vector-test.cc @@ -0,0 +1,20 @@ +#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; } +} + +int +main () +{ + neg_length (); +} diff --git a/cpptour/Vector.cc b/cpptour/Vector.cc new file mode 100644 index 0000000..aa09eef --- /dev/null +++ b/cpptour/Vector.cc @@ -0,0 +1,25 @@ +#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; +} + +double& Vector::operator[] (int i) +{ + if (i < 0 || size() <= i) + throw out_of_range{"Vector::operator[]"}; + return elem[i]; +} + +int Vector::size () noexcept +{ + return sz; +} diff --git a/cpptour/Vector.h b/cpptour/Vector.h new file mode 100644 index 0000000..c7bbc68 --- /dev/null +++ b/cpptour/Vector.h @@ -0,0 +1,9 @@ +class Vector { +public: + Vector (int s); + double& operator[] (int i); + int size () noexcept; +private: + double* elem; + int sz; +}; diff --git a/cpptour/count.cc b/cpptour/count.cc new file mode 100644 index 0000000..a047f04 --- /dev/null +++ b/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/cpptour/enumcls.cc b/cpptour/enumcls.cc new file mode 100644 index 0000000..2340f65 --- /dev/null +++ b/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/cpptour/helloworld.cc b/cpptour/helloworld.cc new file mode 100644 index 0000000..89a7f8a --- /dev/null +++ b/cpptour/helloworld.cc @@ -0,0 +1,7 @@ +#include <iostream> + +int +main () +{ + std::cout << "Hello, World!" << std::endl; +} diff --git a/cpptour/myvec.cc b/cpptour/myvec.cc new file mode 100644 index 0000000..1314730 --- /dev/null +++ b/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/cpptour/square.cc b/cpptour/square.cc new file mode 100644 index 0000000..fb14456 --- /dev/null +++ b/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/cpptour/static-ass.cc b/cpptour/static-ass.cc new file mode 100644 index 0000000..837a965 --- /dev/null +++ b/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/cpptour/veccls.cc b/cpptour/veccls.cc new file mode 100644 index 0000000..0162d23 --- /dev/null +++ b/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/cpptour/vecuser.cc b/cpptour/vecuser.cc new file mode 100644 index 0000000..0eefb0e --- /dev/null +++ b/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; +} |