diff --git a/Demo.cpp b/Demo.cpp index de960de..4465c4f 100644 --- a/Demo.cpp +++ b/Demo.cpp @@ -3,7 +3,7 @@ * * @author Evgeny Hershkovitch Neiterman * @since 2023-03 - */ + */ #include #include @@ -29,7 +29,7 @@ int main() { cout << c++ << endl; cout << --c << endl; - cout << "c >=b ? : " << c >= b << endl; + cout << "c >=b ? : " << (c >= b) << endl; if (a > 1.1) cout << " a is bigger than 1.1" << endl; else cout << " a is smaller than 1.1" << endl; diff --git a/Test.cpp b/Test.cpp new file mode 100644 index 0000000..b4cea61 --- /dev/null +++ b/Test.cpp @@ -0,0 +1,78 @@ +#include +#include +#include +#include +using namespace std; + +#include "doctest.h" +#include "sources/Fraction.hpp" +using namespace ariel; +TEST_CASE("Correct Init"){ + Fraction a(5,3); + CHECK(a.getNumerator() == 5); + CHECK(a.getDenominator() == 3); +} +TEST_CASE("Correct Init with negative"){ + Fraction a(-5,3); + CHECK(a.getNumerator() == -5); + CHECK(a.getDenominator() == 3); +} +TEST_CASE("Correct Reduce"){ + Fraction a(10,5); + CHECK(a.getNumerator() == 2); + CHECK(a.getDenominator() == 1); +} +TEST_CASE("Denominator can't be 0"){ + CHECK_THROWS(Fraction(5,0)); +} +TEST_CASE("Increment"){ + Fraction a(5,3); + a++; + CHECK(a.getNumerator() == 8); + CHECK(a.getDenominator() == 3); +} +TEST_CASE("Decrement"){ + Fraction a(5,3); + a--; + CHECK(a.getNumerator() == 2); + CHECK(a.getDenominator() == 3); +} +TEST_CASE("Addition"){ + Fraction a(5,3); + Fraction b(14,21); + Fraction c = a+b; + CHECK(c.getNumerator() == 7); + CHECK(c.getDenominator() == 3); +} +TEST_CASE("Addition with float"){ + Fraction a(5,3); + Fraction c = a + (float)2.5; + CHECK(c.getNumerator() == 25); + CHECK(c.getDenominator() == 6); +} +TEST_CASE("Subtraction"){ + Fraction a(5,3); + Fraction b(1, 7); + Fraction c = a-b; + CHECK(c.getNumerator() == 32); + CHECK(c.getDenominator() == 21); +} +TEST_CASE("Subtraction with float"){ + Fraction a(5,3); + Fraction c = a - (float)0.45; + CHECK(c.getNumerator() == 73); + CHECK(c.getDenominator() == 60); +} +TEST_CASE("Multiplication"){ + Fraction a(5,3); + Fraction b(1, 7); + Fraction c = a*b; + CHECK(c.getNumerator() == 5); + CHECK(c.getDenominator() == 21); +} +TEST_CASE("Multiplication with float"){ + Fraction a(5,3); + Fraction c = 0.45 * a; + CHECK(c.getNumerator() == 3); + CHECK(c.getDenominator() == 4); +} \ No newline at end of file diff --git a/sources/Fraction.cpp b/sources/Fraction.cpp new file mode 100644 index 0000000..60b55e2 --- /dev/null +++ b/sources/Fraction.cpp @@ -0,0 +1,125 @@ +#include "Fraction.hpp" +#include +#include +#include +#include +using namespace std; +namespace ariel{ + int Fraction::gcd(int first, int second){ + if (second == 0) + return first; + return gcd(second, first % second); + } + Fraction Fraction::operator+(const Fraction& other){ + int newNumerator = this->numerator * other.denominator + other.numerator * this->denominator; + int newDenominator = this->denominator * other.denominator; + int gcd = this->gcd(newNumerator, newDenominator); + return Fraction(newNumerator/gcd, newDenominator/gcd); + } + Fraction Fraction::operator+(double num){ + int newNumerator = this->numerator + num * this->denominator; + int newDenominator = this->denominator; + int gcd = this->gcd(newNumerator, newDenominator); + return Fraction(newNumerator/gcd, newDenominator/gcd); + } + Fraction Fraction::operator-(const Fraction& other){ + int newNumerator = this->numerator * other.denominator - other.numerator * this->denominator; + int newDenominator = this->denominator * other.denominator; + int gcd = this->gcd(newNumerator, newDenominator); + return Fraction(newNumerator/gcd, newDenominator/gcd); + } + Fraction Fraction::operator-(double num){ + int newNumerator = this->numerator - num * this->denominator; + int newDenominator = this->denominator; + int gcd = this->gcd(newNumerator, newDenominator); + return Fraction(newNumerator/gcd, newDenominator/gcd); + } + Fraction Fraction::operator*(const Fraction& other){ + int newNumerator = this->numerator * other.numerator; + int newDenominator = this->denominator * other.denominator; + int gcd = this->gcd(newNumerator, newDenominator); + return Fraction(newNumerator/gcd, newDenominator/gcd); + } + Fraction operator*(double num, const Fraction& other){ + // int newNumerator = this->numerator * num; + // int newDenominator = this->denominator; + // int gcd = this->gcd(newNumerator, newDenominator); + // return Fraction(newNumerator/gcd, newDenominator/gcd); + + return other; + } + Fraction Fraction::operator/(const Fraction& other){ + // int newNumerator = this->numerator * other.denominator; + // int newDenominator = this->denominator * other.numerator; + // int gcd = this->gcd(newNumerator, newDenominator); + // return Fraction(newNumerator/gcd, newDenominator/gcd); + return *this; + } + Fraction Fraction::operator/(double num){ + int newNumerator = this->numerator; + int newDenominator = this->denominator * num; + int gcd = this->gcd(newNumerator, newDenominator); + return Fraction(newNumerator/gcd, newDenominator/gcd); + } + bool Fraction::operator==(const Fraction& other){ + return (this->numerator == other.numerator && this->denominator == other.denominator); + } + bool Fraction::operator==(double num){ + return (this->numerator == num * this->denominator); + } + bool Fraction::operator>(const Fraction& other){ + return (this->numerator * other.denominator > other.numerator * this->denominator); + } + bool Fraction::operator>(double num){ + return (this->numerator > num * this->denominator); + } + bool Fraction::operator<(const Fraction& other){ + return (this->numerator * other.denominator < other.numerator * this->denominator); + } + bool Fraction::operator<(double num){ + return (this->numerator < num * this->denominator); + } + bool Fraction::operator>=(const Fraction& other) const{ + // return (this->numerator * other.denominator >= other.numerator * this->denominator); + return true; + } + bool Fraction::operator>=(double num){ + // return (this->numerator >= num * this->denominator); + return true; + } + bool Fraction::operator<=(const Fraction& other){ + // return (this->numerator * other.denominator <= other.numerator * this->denominator); + return true; + } + bool Fraction::operator<=(double num){ + // return (this->numerator <= num * this->denominator); + return true; + } + Fraction Fraction::operator++(int){ + Fraction temp = *this; + this->numerator += this->denominator; + return temp; + } + Fraction& Fraction::operator++(){ + this->numerator += this->denominator; + return *this; + } + Fraction Fraction::operator--(int){ + Fraction temp = *this; + this->numerator -= this->denominator; + return temp; + } + Fraction& Fraction::operator--(){ + this->numerator -= this->denominator; + return *this; + } + std::ostream& operator<<(std::ostream& os, const Fraction& other){ + // os << other.numerator << "/" << other.denominator; + return os; + } + std::istream& operator>>(std::istream& is, Fraction& other){ + // char c; + // is >> other.numerator >> c >> other.denominator; + return is; + } +} \ No newline at end of file diff --git a/sources/Fraction.hpp b/sources/Fraction.hpp new file mode 100644 index 0000000..4edefaa --- /dev/null +++ b/sources/Fraction.hpp @@ -0,0 +1,62 @@ +#include +#include +#include +#include +#pragma once +using namespace std; +namespace ariel{ + + + class Fraction{ + private: + int numerator; + int denominator; + public: + int gcd(int first, int second); + Fraction(int _numerator, const int _denominator): numerator(_numerator), denominator(_denominator){ + if(_denominator == 0){ + throw std::invalid_argument("Math: can't devide by 0"); + } + } + Fraction operator+(const Fraction& other); + Fraction operator+(double num); + Fraction operator-(const Fraction& other); + Fraction operator-(double num); + Fraction operator*(const Fraction& other); + friend Fraction operator*(double num, const Fraction& other); + Fraction operator/(const Fraction& other); + Fraction operator/(double num); + + bool operator==(const Fraction& other); + bool operator==(double num); + bool operator>(const Fraction& other); + bool operator>(double num); + bool operator<(const Fraction& other); + bool operator<(double num); + bool operator>=(const Fraction& other) const; + bool operator>=(double num); + bool operator<=(const Fraction& other); + bool operator<=(double num); + + Fraction operator++(int); // postfix + Fraction operator--(int); // postfix + Fraction &operator++(); // prefix + Fraction &operator--(); // prefix + + friend std::ostream& operator<<(std::ostream& ost, const Fraction& frc); + friend std::istream& operator>>(std::istream& ist, Fraction& frc); + + int getNumerator() const + { + return this->numerator; + } + int getDenominator() const{ + return this->denominator; + } + }; + + + + + +} \ No newline at end of file