diff --git a/exercise/01/color.cpp b/exercise/01/color.cpp new file mode 100644 index 0000000..b35e1c5 --- /dev/null +++ b/exercise/01/color.cpp @@ -0,0 +1,60 @@ +#include "stdafx.h" +#include "color.h" +#include +#include // RGB + + +color::color(double red, double green, double blue) : + red(get_valid_number(red)), + green(get_valid_number(green)), + blue(get_valid_number(blue)) { } + +double color::get_valid_number(double value) const +{ + return std::clamp(value, 0., 1.); +} + +double color::get_red() const +{ + return red; +} + +double color::get_green() const +{ + return green; +} + +double color::get_blue() const +{ + return blue; +} + +void color::set_red(double red) +{ + this->red = get_valid_number(red); +} + +void color::set_green(double green) +{ + this->green = get_valid_number(green); +} + +void color::set_blue(double blue) +{ + this->blue = get_valid_number(blue); +} + +unsigned long color::get_color_ref() const +{ + return RGB(red * 255, green * 255, blue * 255); +} + +double color::get_luminance() const +{ + return (0.2126 * red + 0.7152 * green + 0.0722 * blue); +} + +bool operator==(const color& c1, const color& c2) +{ + return c1.get_color_ref() == c2.get_color_ref(); +} \ No newline at end of file diff --git a/exercise/01/color.h b/exercise/01/color.h new file mode 100644 index 0000000..3d45fbf --- /dev/null +++ b/exercise/01/color.h @@ -0,0 +1,29 @@ + + +class color +{ + double red; + double green; + double blue; + + double get_valid_number(double value) const; + +public: + color(double red = 0, double green = 0, double blue = 0); + color(const color& other) = default; + ~color() = default; + + double get_red() const; + double get_green() const; + double get_blue() const; + + void set_red(double red); + void set_green(double green); + void set_blue(double blue); + + unsigned long get_color_ref() const; // COLORREF + double get_luminance() const; +}; + + +bool operator==(const color& c1, const color& c2); \ No newline at end of file diff --git a/exercise/02/array.cpp b/exercise/02/array.cpp index 9247ffd..bb00c2d 100644 --- a/exercise/02/array.cpp +++ b/exercise/02/array.cpp @@ -1 +1,50 @@ #include "array.h" +#include // std::out_of_range + +array::array(int size, double value) +{ + if (size < 0) return; + n = size; + p = new double[n]; + for (int i = 0; i < n; ++i) + p[i] = value; +} + +array::array(const array& other) : n(other.n), p(new double[other.n]) +{ + std::copy(other.p, other.p + other.n, p); +} + +array::~array() +{ + if (!p) + return; + delete[] p; +} + +array& array::operator=(const array & other) +{ + n = other.n; + if (p) + delete[] p; + p = new double[n]; + std::copy(other.p, other.p + n, p); + return *this; +} + +array::array(array&& other) noexcept : + n(std::exchange(other.n, 0)), + p(std::exchange(other.p, nullptr)) { } + +int array::size() +{ + return n; +} + +double array::at(int index) const +{ + if (index < 0 || index > n) + throw std::out_of_range("Index out of range."); + + return p[index]; +} \ No newline at end of file diff --git a/exercise/02/array.h b/exercise/02/array.h index 20489df..41b760d 100644 --- a/exercise/02/array.h +++ b/exercise/02/array.h @@ -4,4 +4,14 @@ class array double* p; int n; public: + array(int size = 0, double value = 0); + array(const array& other); + ~array(); + + array& operator=(const array& other); + + array(array&& other) noexcept; + + int size(); + double at(int index) const; }; diff --git a/exercise/02/test.cpp b/exercise/02/test.cpp index 1adae67..afe9366 100644 --- a/exercise/02/test.cpp +++ b/exercise/02/test.cpp @@ -12,73 +12,73 @@ TEST_CLASS(test_array) TEST_METHOD(array_default_constructor) { - /* + array a; Assert::AreEqual(0, a.size()); - */ + } TEST_METHOD(array_constructor) { - /* + array a(size, value); Assert::AreEqual(size, a.size()); for(int i=0; i(func); - */ + } TEST_METHOD(array_index_invalid_negative) { - /* + array a(size, value); auto func = [a]() { a.at(-2); }; Assert::ExpectException(func); - */ + } TEST_METHOD(array_assignment) { - /* + array a(size, value); array b; b = a; Assert::AreEqual(size, b.size()); for (int i = 0; i < b.size(); ++i) Assert::AreEqual(value, b.at(i)); - */ + } TEST_METHOD(array_copy_constructor) { - /* + array a(size, value); array b(a); Assert::AreEqual(size, b.size()); for (int i = 0; i < b.size(); ++i) Assert::AreEqual(value, b.at(i)); - */ + } TEST_METHOD(array_move_constructor) { - /* + array a(size, value); array b = std::move(a); Assert::AreEqual(0, a.size()); Assert::AreEqual(size, b.size()); for (int i = 0; i < b.size(); ++i) Assert::AreEqual(value, b.at(i)); - */ + } }; diff --git a/exercise/03/point.cpp b/exercise/03/point.cpp index 5c7c45f..4cc89e1 100644 --- a/exercise/03/point.cpp +++ b/exercise/03/point.cpp @@ -1,2 +1,17 @@ #include "point.h" +point::point(int x, int y) : x{x}, y{y} { } + +bool point::operator==(const point& other) +{ + return (this->x == other.x) && (this->y == other.y); +} + +bool point::operator!=(const point& other) +{ + return (this->x != other.x) || (this->y != other.y); +} +bool point::operator<(const point& other) +{ + return (this->x < other.x) || (this->x == other.x && this->y < other.y); +} \ No newline at end of file diff --git a/exercise/03/point.h b/exercise/03/point.h index e69de29..e9e94fb 100644 --- a/exercise/03/point.h +++ b/exercise/03/point.h @@ -0,0 +1,14 @@ + + +class point +{ + int x, y; + +public: + point(int x = 0, int y = 0); + ~point() = default; + + bool operator==(const point& other); + bool operator!=(const point& other); + bool operator<(const point& other); +}; \ No newline at end of file diff --git a/exercise/03/test.cpp b/exercise/03/test.cpp index d8e6669..b40cf5d 100644 --- a/exercise/03/test.cpp +++ b/exercise/03/test.cpp @@ -9,30 +9,30 @@ TEST_CLASS(test_1_operators) TEST_METHOD(point_default_constructor) { - /* + Assert::IsTrue(point(0, 0) == point()); - */ + } TEST_METHOD(points_equal) { - /* + Assert::IsTrue(point(1, 2) == point(1, 2)); Assert::IsTrue(point(-12, -34) == point(-12, -34)); - */ + } TEST_METHOD(points_not_equal) { - /* + Assert::IsTrue(point(1, 2) != point(2, 1)); Assert::IsTrue(point(-1, -2) != point(1, 2)); - */ + } TEST_METHOD(points_sorted_by_first_coordinate_then_second) { - /* + Assert::IsTrue(point(1, 2) < point(1, 3)); Assert::IsTrue(point(1, 2) < point(2, 2)); Assert::IsFalse(point(1, 2) < point(1, 2)); - */ + } };