From 5065abcb926db9e2d93a28be704daadc7679c465 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fritz=20B=C3=B6kler?= Date: Wed, 13 Nov 2024 12:08:17 +0100 Subject: [PATCH] Exercise-Template pushed by Artemis --- .gitattributes | 38 +++++++ .gitignore | 11 ++ main.cpp | 6 + std_lib_inc.h | 297 +++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 352 insertions(+) create mode 100644 .gitattributes create mode 100644 .gitignore create mode 100644 main.cpp create mode 100644 std_lib_inc.h diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..35561a2 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,38 @@ +# Source: https://github.com/alexkaratarakis/gitattributes/blob/master/C%2B%2B.gitattributes (08.12.2020) + +# Sources +*.c text diff=c +*.cc text diff=cpp +*.cxx text diff=cpp +*.cpp text diff=cpp +*.c++ text diff=cpp +*.hpp text diff=cpp +*.h text diff=c +*.h++ text diff=cpp +*.hh text diff=cpp + +# Compiled Object files +*.slo binary +*.lo binary +*.o binary +*.obj binary + +# Precompiled Headers +*.gch binary +*.pch binary + +# Compiled Dynamic libraries +*.so binary +*.dylib binary +*.dll binary + +# Compiled Static libraries +*.lai binary +*.la binary +*.a binary +*.lib binary + +# Executables +*.exe binary +*.out binary +*.app binary diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d1f18bf --- /dev/null +++ b/.gitignore @@ -0,0 +1,11 @@ +* + +!*.h +!*.hpp +!*.cpp +!*.sh +!Makefile +!.gitignore +!.gitattributes + +!*/ diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..83a668a --- /dev/null +++ b/main.cpp @@ -0,0 +1,6 @@ +#include "std_lib_inc.h" + +int main() +{ + return 0; +} \ No newline at end of file diff --git a/std_lib_inc.h b/std_lib_inc.h new file mode 100644 index 0000000..490e53a --- /dev/null +++ b/std_lib_inc.h @@ -0,0 +1,297 @@ +/* +std_lib_inc.h + +Version 2024-11-07 + +simple "Programming: Principles and Practice using C++ (second edition)" course header to +be used for the first few weeks. +It provides the most common standard headers (in the global namespace) +and minimal exception/error support. + +Students: please don't try to understand the details of headers just yet. +All will be explained. This header is primarily used so that you don't have +to understand every concept all at once. + +By Chapter 10, you don't need this file and after Chapter 21, you'll understand it + +Revised April 25, 2010: simple_error() added + +Revised November 25 2013: remove support for pre-C++11 compilers, use C++11: +Revised November 28 2013: add a few container algorithms +Revised June 8 2014: added #ifndef to workaround Microsoft C++11 weakness +Revised Febrary 2 2015: randint() can now be seeded (see exercise 5.13). +Revised August 3, 2020: a cleanup removing support for ancient compilers +Revised November 7, 2024: assertions to avoid cassert and NDEBUG (Fritz) +*/ + +#ifndef H112 +#define H112 080315L + + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +//------------------------------------------------------------------------------ + + +typedef long Unicode; + +//------------------------------------------------------------------------------ + +using namespace std; + +bool ASSERTIONS_ACTIVE = false; + +template string to_string(const T& t) +{ + ostringstream os; + os << t; + return os.str(); +} + +struct Range_error : out_of_range { // enhanced vector range error reporting + int index; + Range_error(int i) : out_of_range("Range error: " + to_string(i)), index(i) { } +}; + + +namespace std +{ + +// trivially range-checked vector (no iterator checking): + template + struct Vector : public std::vector + { + using size_type = typename std::vector::size_type; + +/* #ifdef _MSC_VER + // microsoft doesn't yet support C++11 inheriting constructors + Vector() { } + explicit Vector(size_type n) :std::vector(n) {} + Vector(size_type n, const T& v) :std::vector(n, v) {} + template + Vector(I first, I last) : std::vector(first, last) {} + Vector(initializer_list list) : std::vector(list) {} +*/ + using std::vector::vector; // inheriting constructor + + T &operator[](unsigned int i) // rather than return at(i); + { + if(i < 0 || this->size() <= i) throw Range_error(i); + return std::vector::operator[](i); + } + + const T &operator[](unsigned int i) const + { + if(i < 0 || this->size() <= i) throw Range_error(i); + return std::vector::operator[](i); + } + }; +} + +// disgusting macro hack to get a range checked vector: +#define vector Vector + +// trivially range-checked string (no iterator checking): +struct String : std::string { + using size_type = std::string::size_type; + // using string::string; + + char& operator[](unsigned int i) // rather than return at(i); + { + if (i<0 || size() <= i) throw Range_error(i); + return std::string::operator[](i); + } + + const char& operator[](unsigned int i) const + { + if (i<0 || size() <= i) throw Range_error(i); + return std::string::operator[](i); + } +}; + +namespace std { + + template<> struct hash + { + size_t operator()(const String& s) const + { + return hash()(s); + } + }; + +} // of namespace std + + +struct Exit : runtime_error { + Exit() : runtime_error("Exit") {} +}; + +// error() simply disguises throws: +[[noreturn]] +inline void error(const string& s) +{ + throw runtime_error(s); +} + +[[noreturn]] +inline void error(const string& s, const string& s2) +{ + error(s + s2); +} + +inline void error(const string& s, int i) +{ + ostringstream os; + os << s << ": " << i; + error(os.str()); +} + + +template char* as_bytes(T& i) // needed for binary I/O +{ + void* addr = &i; // get the address of the first byte + // of memory used to store the object + return static_cast(addr); // treat that memory as bytes +} + + +inline void keep_window_open() +{ + cin.clear(); + cout << "Bitte gib ein Zeichen zum beenden ein:\n"; + char ch; + cin >> ch; + return; +} + +inline void keep_window_open(string s) +{ + if (s == "") return; + cin.clear(); + cin.ignore(120, '\n'); + for (;;) { + cout << "Bitte gib '" << s << "' ein, um zu beenden\n"; + string ss; + while (cin >> ss && ss != s) + cout << "Bitte gib '" << s << "' ein, um zu beenden\n"; + return; + } +} + + +// Assertion handling that avoids cassert and debug mode (Fritz) +inline void activateAssertions() +{ + ASSERTIONS_ACTIVE = true; +} + +inline void assert(bool condition, const string& message = "") +{ + if (!condition && ASSERTIONS_ACTIVE) + { + if(message == "") + { + cout << "Assertion fehlgeschlagen!\n"; + } + else + { + cout << "Assertion fehlgeschlagen: " << message << "\n"; + } + keep_window_open(); + exit(1); + } +} + + + +// error function to be used (only) until error() is introduced in Chapter 5: +inline void simple_error(string s) // write ``error: s and exit program +{ + cout << "fehler: " << s << '\n'; + keep_window_open(); // for some Windows environments + exit(1); +} + +// make std::min() and std::max() accessible on systems with antisocial macros: +#undef min +#undef max + + +// run-time checked narrowing cast (type conversion). See ???. +// Fritz: deprecated with {}-construction +template R narrow_cast(const A& a) +{ + R r = R(a); + if (A(r) != a) error(string("informationsverlust")); + return r; +} + +// random number generators. See 24.7. + +inline default_random_engine& get_rand() +{ + static default_random_engine ran; // note: not thread_local + return ran; +}; + +inline void seed_randint(int s) { get_rand().seed(s); } + +inline int randint(int min, int max) { return uniform_int_distribution<>{min, max}(get_rand()); } + +inline int randint(int max) { return randint(0, max); } + +//inline double sqrt(int x) { return sqrt(double(x)); } // to match C++0x + +// container algorithms. See 21.9. // C++ has better versions of this: + +template +using Value_type = typename C::value_type; + +template +using Iterator = typename C::iterator; + +template +// requires Container() +void sort(C& c) +{ + std::sort(c.begin(), c.end()); +} + +template +// requires Container() && Binary_Predicate>() +void sort(C& c, Pred p) +{ + std::sort(c.begin(), c.end(), p); +} + +template +// requires Container() && Equality_comparable() +Iterator find(C& c, Val v) +{ + return std::find(c.begin(), c.end(), v); +} + +template +// requires Container() && Predicate>() +Iterator find_if(C& c, Pred p) +{ + return std::find_if(c.begin(), c.end(), p); +} + +#endif //H112