Skip to content

Commit

Permalink
finished
Browse files Browse the repository at this point in the history
  • Loading branch information
matanb1238 committed Apr 20, 2021
1 parent 97aa06c commit 26eae8b
Show file tree
Hide file tree
Showing 3 changed files with 170 additions and 0 deletions.
38 changes: 38 additions & 0 deletions NumberWithUnits.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@

#include <iostream>
#include <fstream>
#include <sstream>
#include <stdexcept>
#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;
}
}

41 changes: 41 additions & 0 deletions NumberWithUnits.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include <iostream>
#include <fstream>
#include <sstream>
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);
};
}
91 changes: 91 additions & 0 deletions Test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#include "NumberWithUnits.hpp"
#include "doctest.h"
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <ctime>
#include <stdexcept>
#include <vector>

using namespace ariel;
static vector<string> 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(b<a);
NumberWithUnits c{1, "hour"};
NumberWithUnits d{3601, "sec"};
NumberWithUnits e{60, "min"};
CHECK(d>c);
CHECK(d>=c);
CHECK(c>=e);
CHECK(e>=c);
CHECK(a<d);
NumberWithUnits f{1, "kg"};
NumberWithUnits g{1000, "g"};
CHECK_FALSE(b!=a);
CHECK(g==f);
CHECK(g<=f);
NumberWithUnits h{0.0001, "ton"};
CHECK(f==h);
CHECK(f==g);
NumberWithUnits i{0.0002, "ton"};
CHECK(f!=i);
CHECK(f!=g);
}

TEST_CASE("+, -, +="){
NumberWithUnits::read_units(units_file);
NumberWithUnits a{2, "km"};
NumberWithUnits b{1000, "m"};
NumberWithUnits c{3000, "m"};
CHECK((b+a)==c);
NumberWithUnits d{5, "km"};
CHECK((a+b)==c);
CHECK((b+=a)==c);
CHECK((a+=c)==d);
CHECK_FALSE((b+=a)==d);
NumberWithUnits e{0.5, "km"};
NumberWithUnits a2{2, "km"};
NumberWithUnits b2{1000, "m"};
CHECK((a2-b2)==e);
NumberWithUnits f{500, "m"};
CHECK((b2-a2)==e);
CHECK((b2-a2)==f);
}

TEST_CASE("Postfix and Prefix operators (++/--)"){
NumberWithUnits a{1, "hour"};
NumberWithUnits b{2, "hour"};
NumberWithUnits c{180, "min"};
CHECK(++a==b);
CHECK(++a==c);
CHECK(a++==a);
NumberWithUnits d{3, "hour"};
CHECK(a==d);
CHECK(--a==++b);
CHECK(a--==b);
}

0 comments on commit 26eae8b

Please sign in to comment.