diff --git a/day1/examples/fibonacci.cc b/day1/examples/fibonacci.cc new file mode 100644 index 0000000000000000000000000000000000000000..61f74e0399524b841184fa0042185cf7c4606740 --- /dev/null +++ b/day1/examples/fibonacci.cc @@ -0,0 +1,12 @@ +#include <iostream> +#include <limits> + +int main() +{ + size_t umax{ std::numeric_limits<size_t>::max() }; + unsigned nfib = 0; + size_t f0{ 0 }, f1{ 1 }; + // Insert your code here + std::cout << "The largest number in the Fibonacci sequence under " << umax << " is " << f1 << "\n"; + std::cout << "Number of entries in the Fibonacci sequence under " << umax << " is " << nfib << "\n"; +} diff --git a/day1/examples/gcd.cc b/day1/examples/gcd.cc new file mode 100644 index 0000000000000000000000000000000000000000..b355e66f122f6c5b99810caef43de9061f991d3b --- /dev/null +++ b/day1/examples/gcd.cc @@ -0,0 +1,21 @@ +#include <iostream> + +unsigned long euclid_gcd(unsigned long smaller, unsigned long larger) +{ + if (smaller > larger) + std::swap(smaller, larger); + // Fill in your code to implement Euclid's algorithm here. + + return ? ? ? ; +} + +int main(int argc, char* argv[]) +{ + 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 0000000000000000000000000000000000000000..3f457bd1957988c7100946328dcb74e5e53c97cc --- /dev/null +++ b/day1/examples/hanoi.cc @@ -0,0 +1,19 @@ +#include <iostream> +#include <string> +int the_other(int i, int j) { 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); +} + +int main() +{ + 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 0000000000000000000000000000000000000000..55fd5b28327407c534c69b9d29fa7713a248772f --- /dev/null +++ b/day1/examples/hello_qa.cc @@ -0,0 +1,12 @@ +// examples/hello_qa.cc +#include <string> +#include <iostream> + +int main() +{ + 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 0000000000000000000000000000000000000000..b75bb497765a0e4bb3416e3ce21170dc63544215 --- /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/namespaces.cc b/day1/examples/namespaces.cc new file mode 100644 index 0000000000000000000000000000000000000000..8d908bd2ad73747a24d1e46b165d1ff701bbfded --- /dev/null +++ b/day1/examples/namespaces.cc @@ -0,0 +1,17 @@ +#include <iostream> + +using namespace std; + +namespace UnitedKingdom { +string London = "Big city"; +} +namespace UnitedStates { +string London = "Small town in Kentucky"; +} +int main() +{ + using namespace UnitedKingdom; + cout << London << endl; + cout << UnitedStates::London << endl; + return 0; +} \ No newline at end of file diff --git a/day1/examples/namespaces2.cc b/day1/examples/namespaces2.cc new file mode 100644 index 0000000000000000000000000000000000000000..481dbf2d185b3835b66183eab1f82ba0ee377a7b --- /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 in C++17 +//namespace mylibrary::onefeature { +// double solve(int i); +//} +int main() +{ + namespace USOH = UnitedStates::OH; + std::cout << USOH::London << std::endl; + return 0; +} diff --git a/day1/examples/numsort.cc b/day1/examples/numsort.cc new file mode 100644 index 0000000000000000000000000000000000000000..90589b393a19c64bc64764a07a4fbc3a5dab7a91 --- /dev/null +++ b/day1/examples/numsort.cc @@ -0,0 +1,40 @@ +#include <iostream> +#include <string> +#include <fstream> +#include <filesystem> +#include <vector> +#include <sstream> + +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::sort(content.begin(), content.end(), + [](auto l1, auto l2) { + std::istringstream istr1{ l1 }; + std::istringstream istr2{ l2 }; + auto x1{0.}, x2{0.}; + istr1 >> x1; + istr2 >> x2; + return x1 < x2; + } + ); + 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 0000000000000000000000000000000000000000..8b1037a9b6ea80f37411ae0ec4146aa917b80ae9 --- /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/raw1.cc b/day1/examples/raw1.cc new file mode 100644 index 0000000000000000000000000000000000000000..0781800a602f8463ce7eba4483e5bf99ccb6738a --- /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 0000000000000000000000000000000000000000..2c57d2dce8aa6781bc8fd2e48e75663bc6b1e1f7 --- /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'; +} diff --git a/day1/solutions/fibonacci.cc b/day1/solutions/fibonacci.cc new file mode 100644 index 0000000000000000000000000000000000000000..fc9cf9685fbaa2754eb2a1bae15540c32771583f --- /dev/null +++ b/day1/solutions/fibonacci.cc @@ -0,0 +1,17 @@ +#include <iostream> +#include <limits> + +auto main() -> int +{ + size_t umax{ std::numeric_limits<size_t>::max() }; + unsigned nfib = 0; + size_t f0{ 0 }, f1{ 1 }; + do { + size_t f2 = f0 + f1; + f0 = f1; + f1 = f2; + ++nfib; + } while (f1 < umax - f0); + std::cout << "The largest number in the Fibonacci sequence under " << umax << " is " << f1 << "\n"; + std::cout << "Number of entries in the Fibonacci sequence under " << umax << " is " << nfib << "\n"; +} diff --git a/day1/solutions/gcd.cc b/day1/solutions/gcd.cc new file mode 100644 index 0000000000000000000000000000000000000000..f545c4eb5e609e30241a8d930780b8178a5b212e --- /dev/null +++ b/day1/solutions/gcd.cc @@ -0,0 +1,24 @@ +#include <iostream> + +auto euclid_gcd(unsigned long smaller, unsigned long larger) -> unsigned long +{ + if (smaller > larger) + std::swap(smaller, larger); + while (smaller != 0) { + auto rem = larger % smaller; + larger = smaller; + smaller = rem; + } + return larger; +} + +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/solutions/raw1.cc b/day1/solutions/raw1.cc new file mode 100644 index 0000000000000000000000000000000000000000..abe15584291f9d73737da98823bfe7fdf184dc56 --- /dev/null +++ b/day1/solutions/raw1.cc @@ -0,0 +1,12 @@ +#include <iostream> +#include <string> + +auto main() -> int +{ + using namespace std; + cout << "Use a single backslash character at the end of the line to continue input to the next line, as shown ...\n" + << R"(OBJ=a.o \ + b.o\ + c.o +)"; +}