From 26eae8b67d3042fb3fdad66a291cd0bb41ab8b46 Mon Sep 17 00:00:00 2001 From: matanb1238 Date: Tue, 20 Apr 2021 18:18:17 +0300 Subject: [PATCH] finished --- NumberWithUnits.cpp | 38 +++++++++++++++++++ NumberWithUnits.hpp | 41 ++++++++++++++++++++ Test.cpp | 91 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 170 insertions(+) create mode 100644 NumberWithUnits.cpp create mode 100644 NumberWithUnits.hpp create mode 100644 Test.cpp diff --git a/NumberWithUnits.cpp b/NumberWithUnits.cpp new file mode 100644 index 0000000..e64834d --- /dev/null +++ b/NumberWithUnits.cpp @@ -0,0 +1,38 @@ + +#include +#include +#include +#include +#include "NumberWithUnits.hpp" +// #define BOOL true + +// using namespace std; + +namespace ariel{ + void NumberWithUnits::read_units(const ifstream& file){} + NumberWithUnits operator+(const NumberWithUnits& unit1, const NumberWithUnits& unit2){return NumberWithUnits();} + NumberWithUnits operator+=(const NumberWithUnits& unit1, const NumberWithUnits& unit2){return NumberWithUnits();} + NumberWithUnits operator+(const NumberWithUnits& unit){return NumberWithUnits();} + NumberWithUnits operator-(const NumberWithUnits& unit1, const NumberWithUnits& unit2){return NumberWithUnits();} + NumberWithUnits operator-=(const NumberWithUnits& unit1, const NumberWithUnits& unit2){return NumberWithUnits();} + NumberWithUnits operator-(const NumberWithUnits& unit){return NumberWithUnits();} + bool operator>(const NumberWithUnits& unit1, const NumberWithUnits& unit2){return unit1.BOOL;} + bool operator>=(const NumberWithUnits& unit1, const NumberWithUnits& unit2){return unit1.BOOL;} + bool operator<(const NumberWithUnits& unit1, const NumberWithUnits& unit2){return unit1.BOOL;} + bool operator<=(const NumberWithUnits& unit1, const NumberWithUnits& unit2){return unit1.BOOL;} + bool operator==(const NumberWithUnits& unit1, const NumberWithUnits& unit2){return unit1.BOOL;} + bool operator!=(const NumberWithUnits& unit1, const NumberWithUnits& unit2){return unit1.BOOL;} + NumberWithUnits operator++(const NumberWithUnits& unit){return NumberWithUnits();} //Prefix + NumberWithUnits operator++(const NumberWithUnits& unit, int){return NumberWithUnits();} //Postfix + NumberWithUnits operator--(const NumberWithUnits& unit){return NumberWithUnits();} //Prefix + NumberWithUnits operator--(const NumberWithUnits& unit, int){return NumberWithUnits();} //Postfix + NumberWithUnits operator*(const NumberWithUnits& unit, double num){return NumberWithUnits();} + NumberWithUnits operator*(double num, const NumberWithUnits& unit){return NumberWithUnits();} + ostream& operator<<(const ostream& os, const NumberWithUnits& unit){return cout << unit._value << "[" << unit._unit << "]" << endl;} + istream& operator>>(std::istream& in, NumberWithUnits& unit){ + string s; + in >> unit._value >> s >> unit._unit; + return in; + } +} + diff --git a/NumberWithUnits.hpp b/NumberWithUnits.hpp new file mode 100644 index 0000000..f3c64e9 --- /dev/null +++ b/NumberWithUnits.hpp @@ -0,0 +1,41 @@ +#include +#include +#include +using namespace std; + +namespace ariel{ + class NumberWithUnits{ + private: + double _value; + string _unit; + public: + const bool BOOL = true; + NumberWithUnits(double value, string unit){ + _value=value; + _unit=unit; + } + NumberWithUnits(){} + ~NumberWithUnits(){} + static void read_units(const ifstream& file); + friend NumberWithUnits operator+(const NumberWithUnits& unit1, const NumberWithUnits& unit2); + friend NumberWithUnits operator+=(const NumberWithUnits& unit1, const NumberWithUnits& unit2); + friend NumberWithUnits operator+(const NumberWithUnits& unit); + friend NumberWithUnits operator-(const NumberWithUnits& unit1, const NumberWithUnits& unit2); + friend NumberWithUnits operator-=(const NumberWithUnits& unit1, const NumberWithUnits& unit2); + friend NumberWithUnits operator-(const NumberWithUnits& unit); + friend bool operator>(const NumberWithUnits& unit1, const NumberWithUnits& unit2); + friend bool operator>=(const NumberWithUnits& unit1, const NumberWithUnits& unit2); + friend bool operator<(const NumberWithUnits& unit1, const NumberWithUnits& unit2); + friend bool operator<=(const NumberWithUnits& unit1, const NumberWithUnits& unit2); + friend bool operator==(const NumberWithUnits& unit1, const NumberWithUnits& unit2); + friend bool operator!=(const NumberWithUnits& unit1, const NumberWithUnits& unit2); + friend NumberWithUnits operator++(const NumberWithUnits& unit); //Prefix + friend NumberWithUnits operator++(const NumberWithUnits& unit, int); //Postfix + friend NumberWithUnits operator--(const NumberWithUnits& unit); //Prefix + friend NumberWithUnits operator--(const NumberWithUnits& unit, int); //Postfix + friend NumberWithUnits operator*(const NumberWithUnits& unit, double num); + friend NumberWithUnits operator*(double num, const NumberWithUnits& unit); + friend ostream& operator<<(const ostream& os, const NumberWithUnits& unit); + friend istream& operator>>(istream& in, NumberWithUnits& unit); + }; +} \ No newline at end of file diff --git a/Test.cpp b/Test.cpp new file mode 100644 index 0000000..3ab40a6 --- /dev/null +++ b/Test.cpp @@ -0,0 +1,91 @@ +#include "NumberWithUnits.hpp" +#include "doctest.h" +#include +#include +#include +#include +#include +#include +#include + +using namespace ariel; +static vector units = {"m"}; +ifstream units_file{"units.txt"}; + +TEST_CASE("Multi operators"){ + NumberWithUnits a{1, "m"}; + NumberWithUnits b{3, "m"}; + NumberWithUnits c{300, "cm"}; + double num = 3; + CHECK(a*num==b); + CHECK(a*num==c); + CHECK(num*a==b); + CHECK(num*a==c); + double number = 2; + CHECK_FALSE(a*number==b); + CHECK_FALSE(a*number==c); + CHECK(number*a!=b); + CHECK(number*a!=c); +} + +TEST_CASE("All bool operators"){ + NumberWithUnits a{110, "cm"}; + NumberWithUnits b{1, "m"}; + CHECK(a>b); + CHECK(a>=b); + CHECK(b<=a); + CHECK(bc); + CHECK(d>=c); + CHECK(c>=e); + CHECK(e>=c); + CHECK(a