diff --git a/exercise/01/color.cpp b/exercise/01/color.cpp new file mode 100644 index 0000000..3b5f2e2 --- /dev/null +++ b/exercise/01/color.cpp @@ -0,0 +1,74 @@ +#include "stdafx.h" +#include "color.h" + +color::color(double red, double green, double blue) +{ + set_red(red); + set_green(green); + set_blue(blue); +} + +color & color::operator=(const color & other) +{ + if (this != &other) { + red = other.red; + green = other.green; + blue = other.blue; + } + return *this; +} + +bool color::operator==(const color & other) const +{ + return this->red == other.red && this->green == other.green && this->blue == other.blue; +} + +double color::get_red() const +{ + return red; +} + +double color::get_green() const +{ + return green; +} + +double color::get_blue() const +{ + return blue; +} + +namespace { + + double convert(double col) + { + if (col < 0) return 0; + if (col > 1) return 1; + return col; + } +} + +void color::set_red(double red) +{ + this->red = convert(red); +} + +void color::set_green(double green) +{ + this->green = convert(green); +} + +void color::set_blue(double blue) +{ + this->blue = convert(blue); +} + +COLORREF color::get_color_ref() const +{ + return RGB(red * maxColorRange, green * maxColorRange, blue * maxColorRange); +} + +double color::get_luminance() const +{ + return red * redIntensity + green * greenIntensity + blue * blueIntensity; +} diff --git a/exercise/01/color.h b/exercise/01/color.h new file mode 100644 index 0000000..6b90764 --- /dev/null +++ b/exercise/01/color.h @@ -0,0 +1,27 @@ +#pragma once +#include + +class color { +public: + color() : red{0}, green{0}, blue{0} {}; + color(double red, double green, double blue); + color& operator=(const color& other); + bool operator==(const color& other) const; + 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); + COLORREF get_color_ref() const; + double get_luminance() const; + +private: + double red; + double green; + double blue; + const int maxColorRange = 255; + const double redIntensity = 0.2126; + const double greenIntensity = 0.7152; + const double blueIntensity = 0.0722; +}; \ No newline at end of file diff --git a/exercise/02/array.cpp b/exercise/02/array.cpp index 9247ffd..46aeb84 100644 --- a/exercise/02/array.cpp +++ b/exercise/02/array.cpp @@ -1 +1,54 @@ #include "array.h" +#include +#include + +array::array(int size, double value) +{ + if (size < 0) throw std::out_of_range("Size is too small!"); + n = size; + p = new double[size]; + std::fill(p, p + size, value); +} + +array::array(const array & other) +{ + n = other.n; + p = new double[n]; + std::copy(other.p, other.p + n, p); +} + +array::array(array && other) noexcept : n(std::exchange(other.n, 0)), p(std::exchange(other.p, nullptr)) {} + +array & array::operator=(array other) +{ + std::swap(n, other.n); + std::swap(p, other.p); + return *this; +} + +array & array::operator=(array && other) +{ + n = std::move(other.n); + p = std::move(other.p); + return *this; +} + +array::~array() +{ + if (p == nullptr) return; + delete[] p; + p = nullptr; + n = 0; + +} + +int array::size() const +{ + return n; +} + +double array::at(int index) const +{ + if (index > n || index < 0) throw std::out_of_range("Index out of range!"); + return *(p + index); +} diff --git a/exercise/02/array.h b/exercise/02/array.h index 20489df..5f7b1c6 100644 --- a/exercise/02/array.h +++ b/exercise/02/array.h @@ -4,4 +4,14 @@ class array double* p; int n; public: + array() : n(0), p(nullptr) {}; + array(int size, double value); + array(const array& other); + array(array&& other) noexcept; + array& operator=(array other); + array& operator=(array&& other); + ~array(); + + int size() const; + double at(int index) const; }; diff --git a/exercise/02/test.cpp b/exercise/02/test.cpp index 1adae67..55ed1b2 100644 --- a/exercise/02/test.cpp +++ b/exercise/02/test.cpp @@ -12,73 +12,59 @@ 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..01ecbf3 100644 --- a/exercise/03/point.cpp +++ b/exercise/03/point.cpp @@ -1,2 +1,16 @@ #include "point.h" +bool point::operator==(const point & other) const +{ + return x == other.x && y == other.y; +} + +bool point::operator!=(const point & other) const +{ + return !((*this)==other); +} + +bool point::operator<(const point & other) const +{ + return x <= other.x && y < other.y || x < other.x && y <= other.y; +} diff --git a/exercise/03/point.h b/exercise/03/point.h index e69de29..bfbfd99 100644 --- a/exercise/03/point.h +++ b/exercise/03/point.h @@ -0,0 +1,19 @@ +#pragma once + +class point +{ +public: + point() : x(0), y(0) {}; + point(int x, int y) : x(x), y(y) {}; + point(const point& other) = default; + point(point&& other) = default; + point& operator=(const point& other) = default; + point& operator=(point&& other) = default; + ~point() = default; + bool operator==(const point& other) const; + bool operator!=(const point& other) const; + bool operator<(const point& other) const; + +private: + int x, y; +}; \ No newline at end of file diff --git a/exercise/03/test.cpp b/exercise/03/test.cpp index d8e6669..abb842a 100644 --- a/exercise/03/test.cpp +++ b/exercise/03/test.cpp @@ -9,30 +9,22 @@ 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)); - */ } };