From e02c63ecd513a85b471daf99237cf69f4993566d Mon Sep 17 00:00:00 2001
From: Sandipan Mohanty <s.mohanty@fz-juelich.de>
Date: Sat, 6 May 2023 11:27:49 +0200
Subject: [PATCH] Add day1 examples

---
 day1/examples/CMakeLists.txt  | 15 ++++++++++++++
 day1/examples/binforms.cc     | 38 +++++++++++++++++++++++++++++++++++
 day1/examples/check_prime.cc  | 20 ++++++++++++++++++
 day1/examples/fibonacci.cc    | 16 +++++++++++++++
 day1/examples/gcd.cc          | 21 +++++++++++++++++++
 day1/examples/hanoi.cc        | 20 ++++++++++++++++++
 day1/examples/hello_qa.cc     | 12 +++++++++++
 day1/examples/hello_xyz.cc    | 10 +++++++++
 day1/examples/midpt.cc        | 17 ++++++++++++++++
 day1/examples/min_of_three.cc | 16 +++++++++++++++
 day1/examples/namespaces.cc   | 16 +++++++++++++++
 day1/examples/namespaces2.cc  | 22 ++++++++++++++++++++
 day1/examples/numsort.cc      | 36 +++++++++++++++++++++++++++++++++
 day1/examples/onespace.cc     | 24 ++++++++++++++++++++++
 day1/examples/pyscope2.py     | 17 ++++++++++++++++
 day1/examples/raw1.cc         |  8 ++++++++
 day1/examples/rawstring.cc    | 14 +++++++++++++
 17 files changed, 322 insertions(+)
 create mode 100644 day1/examples/CMakeLists.txt
 create mode 100644 day1/examples/binforms.cc
 create mode 100644 day1/examples/check_prime.cc
 create mode 100644 day1/examples/fibonacci.cc
 create mode 100644 day1/examples/gcd.cc
 create mode 100644 day1/examples/hanoi.cc
 create mode 100644 day1/examples/hello_qa.cc
 create mode 100644 day1/examples/hello_xyz.cc
 create mode 100644 day1/examples/midpt.cc
 create mode 100644 day1/examples/min_of_three.cc
 create mode 100644 day1/examples/namespaces.cc
 create mode 100644 day1/examples/namespaces2.cc
 create mode 100644 day1/examples/numsort.cc
 create mode 100644 day1/examples/onespace.cc
 create mode 100644 day1/examples/pyscope2.py
 create mode 100644 day1/examples/raw1.cc
 create mode 100644 day1/examples/rawstring.cc

diff --git a/day1/examples/CMakeLists.txt b/day1/examples/CMakeLists.txt
new file mode 100644
index 0000000..997c5df
--- /dev/null
+++ b/day1/examples/CMakeLists.txt
@@ -0,0 +1,15 @@
+cmake_minimum_required(VERSION 3.16)
+set(CMAKE_CXX_EXTENSIONS OFF)
+set(CMAKE_CXX_STANDARD 20)
+set(CMAKE_CXX_STANDARD_REQUIRED ON)
+set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
+project(cxx2023_d1_examples CXX)
+
+
+FILE (GLOB sources ./ *.cc)
+foreach(source ${sources})
+    get_filename_component(withoutext "${source}" NAME_WE)
+    add_executable("${withoutext}" "${source}")
+endforeach()
+
+
diff --git a/day1/examples/binforms.cc b/day1/examples/binforms.cc
new file mode 100644
index 0000000..baeb5f8
--- /dev/null
+++ b/day1/examples/binforms.cc
@@ -0,0 +1,38 @@
+#include "binform17.hh"
+#include <iostream>
+
+template <class T>
+void handle()
+{
+    std::cout << "Enter value: ";
+    T v;
+    std::cin >> v;
+    cxx2023::showbits(v);
+}
+
+auto main() -> int
+{
+    bool keeplooping{true};
+    while (keeplooping) {
+        int ch{};
+        std::cout << "Select type (1: int, 2: unsigned int, 3: long, 4: unsigned long, 5: float, 6: double): ";
+        std::cin >> ch;
+        switch (ch) {
+        case 1: 
+            handle<int>(); break;
+        case 2: 
+            handle<unsigned int>(); break;
+        case 3: 
+            handle<long>(); break;
+        case 4: 
+            handle<unsigned long>(); break;
+        case 5: 
+            handle<float>(); break;
+        case 6: 
+            handle<double>(); break;
+        case 0: keeplooping = false; break;
+        default:;
+        };
+    }
+}
+
diff --git a/day1/examples/check_prime.cc b/day1/examples/check_prime.cc
new file mode 100644
index 0000000..ed41b4d
--- /dev/null
+++ b/day1/examples/check_prime.cc
@@ -0,0 +1,20 @@
+#include <iostream>
+
+auto is_prime(unsigned int n) -> bool
+{
+    // recipe to tell if n is a prime or not
+    return false;
+}
+
+auto main() -> int
+{
+    std::cout << "Number to check: ";
+    unsigned int inp{};
+    std::cin >> inp;
+    if (is_prime(n))
+        std::cout << "Yes, that's a prime.\n";
+    else
+        std::cout << "Not a prime.\n";
+}
+
+
diff --git a/day1/examples/fibonacci.cc b/day1/examples/fibonacci.cc
new file mode 100644
index 0000000..e771743
--- /dev/null
+++ b/day1/examples/fibonacci.cc
@@ -0,0 +1,16 @@
+#include <iostream>
+#include <limits>
+#include <format>
+
+auto main() -> int
+{
+    using namespace std;
+    size_t umax{ numeric_limits<size_t>::max() };
+    unsigned nfib = 0;
+    size_t f0{ 0 }, f1{ 1 };
+    // Insert your code here
+
+    cout << format(R"(The largest number in the Fibonacci sequence under {0:} is {1:},
+and there are {2:} entries under {0:} in that sequence.
+)"                 , umax, f1, nfib);
+}
diff --git a/day1/examples/gcd.cc b/day1/examples/gcd.cc
new file mode 100644
index 0000000..baf9a4e
--- /dev/null
+++ b/day1/examples/gcd.cc
@@ -0,0 +1,21 @@
+#include <iostream>
+
+auto euclid_gcd(unsigned long smaller, unsigned long larger) -> unsigned long 
+{
+    if (smaller > larger)
+        std::swap(smaller, larger);
+    // Fill in your code to implement Euclid's algorithm here.
+
+    return ? ? ? ;
+}
+
+auto main(int argc, char* argv[]) -> int
+{
+    if (argc != 3) {
+        std::cout << "Usage:\n"
+                  << argv[0] << " number1 number2\n";
+        return 1;
+    }
+    unsigned long n1 = std::stoul(argv[1]), n2 = std::stoul(argv[2]);
+    std::cout << euclid_gcd(n1, n2) << "\n";
+}
diff --git a/day1/examples/hanoi.cc b/day1/examples/hanoi.cc
new file mode 100644
index 0000000..c2f76cd
--- /dev/null
+++ b/day1/examples/hanoi.cc
@@ -0,0 +1,20 @@
+#include <iostream>
+#include <string>
+
+auto the_other(int i, int j) -> int { return 3 - i - j; }
+
+void transfer(unsigned long n, int from, int to)
+{
+    int oth = the_other(from, to);
+    if (n > 1)
+        transfer(n - 1, from, oth);
+    std::cout << n << ": " << from << "->" << to << "\n";
+    if (n > 1)
+        transfer(n - 1, oth, to);
+}
+
+auto main() -> int
+{
+    size_t N = 6;
+    transfer(N, 0, 1);
+}
diff --git a/day1/examples/hello_qa.cc b/day1/examples/hello_qa.cc
new file mode 100644
index 0000000..5991956
--- /dev/null
+++ b/day1/examples/hello_qa.cc
@@ -0,0 +1,12 @@
+// examples/hello_qa.cc
+#include <string>
+#include <iostream>
+
+auto main() -> int
+{
+    std::string name;
+    std::cout << "What's your name ? ";
+    std::cin >> name;
+    std::cout << "Hello, " << name << "\n";
+}
+
diff --git a/day1/examples/hello_xyz.cc b/day1/examples/hello_xyz.cc
new file mode 100644
index 0000000..b75bb49
--- /dev/null
+++ b/day1/examples/hello_xyz.cc
@@ -0,0 +1,10 @@
+// examples/hello_xyz.cc
+#include <iostream>
+auto main(int argc, char* argv[]) -> int
+{
+    std::cout << "Hello, ";
+    if (argc > 1)
+        std::cout << argv[1] << "!\n";
+    else
+        std::cout << "world!\n";
+}
diff --git a/day1/examples/midpt.cc b/day1/examples/midpt.cc
new file mode 100644
index 0000000..c132f0b
--- /dev/null
+++ b/day1/examples/midpt.cc
@@ -0,0 +1,17 @@
+// examples/midpt.cc
+#include <iostream>
+
+auto mid(int a, int b) -> int
+{
+    // recipe needed!
+    return a;
+}
+auto main() -> int
+{
+    int i = 0, j = 0;
+    std::cout << "Enter i, j: ";
+    std::cin >> i >> j;
+    std::cout << "A number half way between " << i << " and " << j
+              << " is " << mid(i, j) << "\n";
+}
+
diff --git a/day1/examples/min_of_three.cc b/day1/examples/min_of_three.cc
new file mode 100644
index 0000000..b43152f
--- /dev/null
+++ b/day1/examples/min_of_three.cc
@@ -0,0 +1,16 @@
+// examples/min_of_three.cc
+#include <iostream>
+
+auto min_of_three(int a, int b, int c) -> int
+{
+    // recipe needed!
+    return a;
+}
+auto main() -> int
+{
+    int i = 0, j = 0, k = 0;
+    std::cout << "Enter i, j and k: ";
+    std::cin >> i >> j >> k;
+    std::cout << "The smallest of the three is " << min_of_three(i, j, k) << "\n";
+}
+
diff --git a/day1/examples/namespaces.cc b/day1/examples/namespaces.cc
new file mode 100644
index 0000000..f942db0
--- /dev/null
+++ b/day1/examples/namespaces.cc
@@ -0,0 +1,16 @@
+#include <iostream>
+
+using namespace std;
+
+namespace UnitedKingdom {
+string London = "Big city";
+}
+namespace UnitedStates {
+string London = "Small town in Kentucky";
+}
+auto main() -> int
+{
+    using namespace UnitedKingdom;
+    cout << London << "\n";
+    cout << UnitedStates::London << "\n";
+}
diff --git a/day1/examples/namespaces2.cc b/day1/examples/namespaces2.cc
new file mode 100644
index 0000000..1a1d033
--- /dev/null
+++ b/day1/examples/namespaces2.cc
@@ -0,0 +1,22 @@
+#include <iostream>
+namespace UnitedKingdom {
+std::string London = "Big city";
+}
+namespace UnitedStates {
+namespace KY {
+    std::string London = "Small town in Kentucky";
+}
+namespace OH {
+    std::string London = "Small town in Ohio";
+}
+}
+// The following definition is ok since C++17
+//namespace mylibrary::onefeature {
+//    double solve(int i);
+//}
+auto main() -> int
+{
+    namespace USOH = UnitedStates::OH;
+    std::cout << USOH::London << "\n";
+}
+
diff --git a/day1/examples/numsort.cc b/day1/examples/numsort.cc
new file mode 100644
index 0000000..9ca16d0
--- /dev/null
+++ b/day1/examples/numsort.cc
@@ -0,0 +1,36 @@
+#include <iostream>
+#include <string>
+#include <fstream>
+#include <filesystem>
+#include <vector>
+#include <ranges>
+#include <algorithm>
+
+namespace fs = std::filesystem;
+auto as_lines(fs::path file) -> std::vector<std::string>
+{
+    std::ifstream fin{ file };
+    std::string line;
+    std::vector<std::string> lines;
+    while (getline(fin, line)) lines.push_back(line);
+    return lines;
+}
+
+auto main(int argc, char* argv[]) -> int
+{
+    if (argc != 2) {
+        std::cerr << "Usage:\n"
+                  << argv[0] << " filename\n";
+        return 1;
+    }
+    auto content = as_lines(argv[1]);
+    std::ranges::sort(content,
+                 [](auto l1, auto l2) {
+                     return std::stod(l1) < std::stod(l2);
+                 }
+             );
+    for (std::string_view line : content) {
+        std::cout << line << "\n";
+    }
+}
+
diff --git a/day1/examples/onespace.cc b/day1/examples/onespace.cc
new file mode 100644
index 0000000..8b1037a
--- /dev/null
+++ b/day1/examples/onespace.cc
@@ -0,0 +1,24 @@
+// examples/onespace.cc
+#include <iostream>
+
+auto main(int argc, char* argv[]) -> int
+{
+    std::string line;
+    while (getline(std::cin, line)) {
+        if (line.empty()) {
+            continue;
+        }
+        bool sp{true};
+        for (auto c : line) {
+            if (isspace(c)) {
+                if (not sp) std::cout << '\t';
+                sp = true;
+            } else {
+                sp = false;
+                std::cout << c;
+            }
+        }
+        std::cout << "\n";
+    }
+}
+
diff --git a/day1/examples/pyscope2.py b/day1/examples/pyscope2.py
new file mode 100644
index 0000000..c49fc39
--- /dev/null
+++ b/day1/examples/pyscope2.py
@@ -0,0 +1,17 @@
+import sys
+if __name__ == "__main__":
+    if len(sys.argv) > 1:
+        N = int(sys.argv[1])
+    else:
+        N = 5
+
+def fact(n):
+    if n > 1:
+        return n * fact(n-1)
+    return 1
+while N > 0:
+    print(fact(N))
+    if N % 4 == 0:
+        fact = N * (N - 1) / 2
+    N = N -1
+
diff --git a/day1/examples/raw1.cc b/day1/examples/raw1.cc
new file mode 100644
index 0000000..0781800
--- /dev/null
+++ b/day1/examples/raw1.cc
@@ -0,0 +1,8 @@
+#include <iostream>
+#include <string>
+
+int main()
+{
+    std::cout << "Use a single backslash character at the end of the line to continue input to the next line, as shown ...\n"
+              << "OBJ=a.o \\\n\tb.o\\\n\tc.o\n";
+}
diff --git a/day1/examples/rawstring.cc b/day1/examples/rawstring.cc
new file mode 100644
index 0000000..2c57d2d
--- /dev/null
+++ b/day1/examples/rawstring.cc
@@ -0,0 +1,14 @@
+#include <iostream>
+#include <string>
+
+int main()
+{
+    // clang-format off
+    std::string s = R"xyz(This is a " raw " C++11 string.)xyz";
+    std::cout << s << '\n';
+    s = R"(Inside a raw string \n does not create a new line.)";
+    std::cout << s << '\n';
+    s = R"(The regular expression to match a sequence of up to 3 digits is \d{3})";
+    // clang-format on
+    std::cout << s << '\n';
+}
-- 
GitLab