From 95bfddc4329119535cf2fb47bef3c1b87f61e411 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20H=C3=B6rnquist?= Date: Tue, 6 Sep 2022 12:24:03 +0200 Subject: tis 6 sep 2022 12:24:03 CEST --- C++.wiki | 83 ++------------------------------------------ C++/Friend Operator.wiki | 30 ++++++++++++++++ C++/Threeway Comparison.wiki | 43 +++++++++++++++++++++++ C++/Time Header.wiki | 54 ++++++++++++++++++++++++++++ 4 files changed, 130 insertions(+), 80 deletions(-) create mode 100644 C++/Friend Operator.wiki create mode 100644 C++/Threeway Comparison.wiki create mode 100644 C++/Time Header.wiki diff --git a/C++.wiki b/C++.wiki index c12d045..911fd1c 100644 --- a/C++.wiki +++ b/C++.wiki @@ -1,90 +1,13 @@ = Contents = - [[#C++ Examples|C++ Examples]] - - [[#C++ Examples#Friend operator|Friend operator]] - [[#C++ Examples#Time Header|Time Header]] = C++ Examples = -== Friend operator == +- [[C++/Friend Operator]] +- [[C++/Time Header]] +- [[C++/Threeway Comparison]] -{{{c++ -#include - -class T { - int x; - -public: - T (int x); - - friend std::ostream& operator<<(std::ostream&, const T&); -}; - -T::T (int x) { - this->x = x; -} - - -std::ostream& operator<<(std::ostream& out, const T& t) { - out << "T {" << t.x << "}"; - return out; -} - -int main () { - T t(10); - - std::cout << t << std::endl; -} -}}} == Time Header == -{{{c++ -#pragma once - -#include - -enum timetype { - _day, _hour, _minute, _second -}; - -class Time { -private: - - int _days = 0; - int _hours = 0; - int _minutes = 0; - int _seconds = 0; - -public: - Time() { }; - Time(int n, timetype type); - - int to_seconds() const; - - friend std::ostream& operator<<(std::ostream&, const Time&); - friend Time operator * (int, Time); - - Time operator + (const Time&); - Time operator , (const Time& t) { return *this + t; } - Time operator && (const Time& t) { return *this + t; } -}; - -#define days * Time(1, _day) -#define day days -#define hours * Time(1, _hour) -#define hour hours -#define minutes * Time(1, _minute) -#define minute minutes -#define seconds * Time(1, _second) -#define second seconds - -int main() { - std::cout - << 5 days << std::endl - << 7 minutes << std::endl - << (5 days and 3 seconds) << std::endl - << (7 days, 7 minutes and 3 seconds) << std::endl - << (9 hours and 1 second) << std::endl - ; -} -}}} diff --git a/C++/Friend Operator.wiki b/C++/Friend Operator.wiki new file mode 100644 index 0000000..a5e6730 --- /dev/null +++ b/C++/Friend Operator.wiki @@ -0,0 +1,30 @@ += Friend Operator = + +{{{c++ +#include + +class T { + int x; + +public: + T (int x); + + friend std::ostream& operator<<(std::ostream&, const T&); +}; + +T::T (int x) { + this->x = x; +} + + +std::ostream& operator<<(std::ostream& out, const T& t) { + out << "T {" << t.x << "}"; + return out; +} + +int main () { + T t(10); + + std::cout << t << std::endl; +} +}}} diff --git a/C++/Threeway Comparison.wiki b/C++/Threeway Comparison.wiki new file mode 100644 index 0000000..58edd84 --- /dev/null +++ b/C++/Threeway Comparison.wiki @@ -0,0 +1,43 @@ +Compile with `c++ -std=c++=20 main.cpp`. + +Tests the spaceship operator. + +{{{c++ +#include +#include +#include + +int str_to_int(const char* s) { + int ret; + std::basic_istringstream is(s); + is >> ret; + return ret; +} + +int main(int argc, char* argv[]) { + if (argc < 3) { + std::cerr << "Give at least 2 arguments" << std::endl; + return 1; + } + + int a, b; + + a = str_to_int(argv[1]); + b = str_to_int(argv[2]); + + std::cout << a << " <=> " << b << std::endl; + std::strong_ordering result = a <=> b; + if (std::is_lt(result)) { + std::cout << "LT"; + } else if (std::is_gt(result)) { + std::cout << "GT"; + } else if (std::is_eq(result)) { + std::cout << "EQ"; + } else { + std::cout << "error"; + } + + std::cout << std::endl; + return 0; +} +}}} diff --git a/C++/Time Header.wiki b/C++/Time Header.wiki new file mode 100644 index 0000000..abc1b12 --- /dev/null +++ b/C++/Time Header.wiki @@ -0,0 +1,54 @@ += Time Header = + +A silly header which allows timestamps to be written in english. + +{{{c++ +#pragma once + +#include + +enum timetype { + _day, _hour, _minute, _second +}; + +class Time { +private: + + int _days = 0; + int _hours = 0; + int _minutes = 0; + int _seconds = 0; + +public: + Time() { }; + Time(int n, timetype type); + + int to_seconds() const; + + friend std::ostream& operator<<(std::ostream&, const Time&); + friend Time operator * (int, Time); + + Time operator + (const Time&); + Time operator , (const Time& t) { return *this + t; } + Time operator && (const Time& t) { return *this + t; } +}; + +#define days * Time(1, _day) +#define day days +#define hours * Time(1, _hour) +#define hour hours +#define minutes * Time(1, _minute) +#define minute minutes +#define seconds * Time(1, _second) +#define second seconds + +int main() { + std::cout + << 5 days << std::endl + << 7 minutes << std::endl + << (5 days and 3 seconds) << std::endl + << (7 days, 7 minutes and 3 seconds) << std::endl + << (9 hours and 1 second) << std::endl + ; +} +}}} -- cgit v1.2.3