diff --git a/exercise/01/color.cpp b/exercise/01/color.cpp new file mode 100644 index 0000000..f0eae58 --- /dev/null +++ b/exercise/01/color.cpp @@ -0,0 +1,65 @@ +#include "stdafx.h" +#include +#include "color.h" + +const double color::max_color_value = 1.; +const double color::min_color_value = 0.; + +color::color() +{ + set_red(0.); + set_green(0.); + set_blue(0.); +} + +color::color(double red, double green, double blue) +{ + set_red(red); + set_green(green); + set_blue(blue); +} + +bool color::operator==(const color& colorCompare) const +{ + return m_red == colorCompare.get_red() && m_green == colorCompare.get_green() && m_blue == colorCompare.get_blue(); +} + +void color::set_red(double red) +{ + m_red = std::clamp(red, min_color_value, max_color_value); +} + +void color::set_green(double input) +{ + m_green = std::clamp(input, min_color_value, max_color_value); +} + +void color::set_blue(double input) +{ + m_blue = std::clamp(input, min_color_value, max_color_value); +} + +double color::get_red() const +{ + return m_red; +} + +double color::get_green() const +{ + return m_green; +} + +double color::get_blue() const +{ + return m_blue; +} + +COLORREF color::get_color_ref() +{ + return RGB((int)(m_red*255),(int)(m_green*255), (int)(m_blue*255)); +} + +double color::get_luminance() +{ + return 0.2126 * m_red + 0.7152 * m_green + 0.0722 * m_blue; +} \ No newline at end of file diff --git a/exercise/01/color.h b/exercise/01/color.h new file mode 100644 index 0000000..6d119ef --- /dev/null +++ b/exercise/01/color.h @@ -0,0 +1,27 @@ +#pragma once +#include + +class color { +public: + color(); + color(double, double, double); + + bool operator==(const color & color) const; + + double get_red() const; + double get_green() const; + double get_blue() const; + + void set_red(double); + void set_green(double); + void set_blue(double); + + COLORREF get_color_ref(); + double get_luminance(); + +private: + double m_red, m_green, m_blue; + + static const double color::max_color_value; + static const double color::min_color_value; +}; \ No newline at end of file diff --git a/exercise/02/array.cpp b/exercise/02/array.cpp index 9247ffd..38533ea 100644 --- a/exercise/02/array.cpp +++ b/exercise/02/array.cpp @@ -1 +1,72 @@ #include "array.h" +#include + +array::array() : + n(0), + p(nullptr) +{ +} + +array::array(array&& array) : + p(nullptr), n(0) +{ + std::swap(p, array.p); + std::swap(n, array.n); +} + +array::array(const array &other) : + n(0), + p(nullptr) +{ + if (other.n > 0) + { + n = other.n; + p = new double[n]; + for (int i = 0; i < other.n; ++i) + { + p[i] = other.p[i]; + } + } +} + +array::array(const int size, const double value) : + n(0), + p(nullptr) +{ + if (size > 0) + { + n = size; + p = new double[n]; + for (int i = 0; i < size; ++i) + { + p[i] = value; + } + } +} + +array array::operator=(array ©Array) +{ + n = copyArray.n; + p = copyArray.p; + copyArray.n = 0; + copyArray.p = nullptr; + + return *this; +} + +double array::at(const int index) const +{ + if (index >= 0 && index < n) + { + return p[index]; + } + else + { + throw std::out_of_range("Index is out of range."); + } +} + +int array::size() +{ + return n; +} \ No newline at end of file diff --git a/exercise/02/array.h b/exercise/02/array.h index 20489df..c00581c 100644 --- a/exercise/02/array.h +++ b/exercise/02/array.h @@ -4,4 +4,12 @@ class array double* p; int n; public: + array(); + array(const int size, const double value = 0); + array(const array &array); + array(array&&); + array operator=(array&); + + double at(const int index) const; + int size(); }; diff --git a/exercise/02/test.cpp b/exercise/02/test.cpp index 1adae67..8a8a96e 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..e8203bd 100644 --- a/exercise/03/point.cpp +++ b/exercise/03/point.cpp @@ -1,2 +1,22 @@ #include "point.h" +point::point(int x, int y) +{ + m_x = x; + m_y = y; +} + +bool point::operator==(point other) +{ + return (m_x == other.m_x) && (m_y == other.m_y); +} + +bool point::operator!=(point other) +{ + return (m_x != other.m_x) || (m_y != other.m_y); +} + +bool point::operator<(point other) +{ + return (m_x < other.m_x) || (m_y < other.m_y); +} \ No newline at end of file diff --git a/exercise/03/point.h b/exercise/03/point.h index e69de29..9a141fe 100644 --- a/exercise/03/point.h +++ b/exercise/03/point.h @@ -0,0 +1,10 @@ +class point { +public: + point(int x = 0, int y = 0); + bool operator==(point other); + bool operator!=(point other); + bool operator<(point other); +private: + int m_x; + int m_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)); - */ } };