diff --git a/exercise/01/color.cpp b/exercise/01/color.cpp new file mode 100644 index 0000000..7271602 --- /dev/null +++ b/exercise/01/color.cpp @@ -0,0 +1,70 @@ +#include "stdafx.h" +#include "color.h" + +color::color() +{ + red_ = 0.; + blue_ = 0.; + green_ = 0.; +} + + +color::color(double red, double green, double blue) +{ + red_ = red; + blue_ = blue; + green_ = green; +} + +color::color(const color& color) +{ + red_ = color.red_; + blue_ = color.blue_; + green_ = color.green_; +} + +double color::get_red() const +{ + return std::clamp(red_, 0.0, 1.0); +} + +double color::get_green() const +{ + return std::clamp(green_, 0.0, 1.0); +} + +double color::get_blue() const +{ + return std::clamp(blue_, 0.0, 1.0); +} + +void color::set_red(double red) +{ + red_ = red; +} +void color::set_blue(double blue) +{ + blue_ = blue; +} +void color::set_green(double green) +{ + green_ = green; +} + +COLORREF color::get_color_ref() +{ + return RGB(red_ * 255, green_ * 255, blue_ * 255); +} +double color::get_luminance() +{ + return 0.2126 * red_ + 0.7152 * green_ + 0.0722* blue_; +} + +bool color::operator==(const color& second) const +{ + return red_ == second.red_ && green_ == second.green_ && blue_ == second.blue_; +} + + + + diff --git a/exercise/01/color.h b/exercise/01/color.h new file mode 100644 index 0000000..293fc17 --- /dev/null +++ b/exercise/01/color.h @@ -0,0 +1,20 @@ +#pragma once + + +class color +{ + double red_, blue_, green_ ; +public: + color(); + color(double red, double blue, double green); + color(const color& color); + double get_red() const; + double get_green() const; + double get_blue() const; + void set_red(double red); + void set_blue(double blue); + void set_green(double green); + COLORREF get_color_ref(); + bool operator==(const color& other) const; + double get_luminance(); +}; diff --git a/exercise/01/stdafx.h b/exercise/01/stdafx.h index 6027cdf..e96c844 100644 --- a/exercise/01/stdafx.h +++ b/exercise/01/stdafx.h @@ -2,3 +2,5 @@ #include #include "CppUnitTest.h" +#include +#include diff --git a/exercise/02/array.cpp b/exercise/02/array.cpp index 9247ffd..3e672c4 100644 --- a/exercise/02/array.cpp +++ b/exercise/02/array.cpp @@ -1 +1,61 @@ #include "array.h" +#include + +array::array(int size, double value) : n_(size), value_(value) +{ + p = new double[n_]; + for (int i=0; i= n_) + throw std::out_of_range("invalid index"); + return p[i]; +} + array& array::operator=(const array& secondArray) +{ + n_ = secondArray.n_; + value_ = secondArray.value_; + p = new double[n_]; + for (int i = 0; i < secondArray.n_; i++) + { + *(p + i) = secondArray.value_; + } + return *this; +} + +array::array(array&& second) noexcept +{ + p = second.p; + n_ = second.n_; + second.p = nullptr; + second.n_ = 0; +} + + diff --git a/exercise/02/array.h b/exercise/02/array.h index 20489df..ae16702 100644 --- a/exercise/02/array.h +++ b/exercise/02/array.h @@ -1,7 +1,18 @@ // do not use standard container as member or base class +#pragma once + class array { - double* p; - int n; + double* p, value_; + int n_; + public: + array(); + array(int size, double value); + array(const array& secondArray); + array(array && second) noexcept; + ~array(); + int size(); + double& at(int i) const; + array& operator=(const array& secondArray); }; diff --git a/exercise/02/test.cpp b/exercise/02/test.cpp index 1adae67..6b4ad4d 100644 --- a/exercise/02/test.cpp +++ b/exercise/02/test.cpp @@ -12,73 +12,61 @@ 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..772595f 100644 --- a/exercise/03/point.cpp +++ b/exercise/03/point.cpp @@ -1,2 +1,27 @@ #include "point.h" +point::point(int x, int y) +{ + x_ = x; + y_ = y; +} + + +bool point::operator==(const point& p) +{ + return x_ == p.x_ && y_ == p.y_; +} + +bool point::operator!=(const point& p) +{ + return x_ != p.x_ || y_ != p.y_; +} +bool point::operator < (const point &p) +{ + if (x_ < p.x_) return true; + if(p.x_==x_) + { + return y_ < p.y_; + } + return false; +} diff --git a/exercise/03/point.h b/exercise/03/point.h index e69de29..0b6c427 100644 --- a/exercise/03/point.h +++ b/exercise/03/point.h @@ -0,0 +1,10 @@ +class point +{ +private: + int x_, y_; +public: + point(int x = 0, int y = 0); + bool operator==(const point &p); + bool operator!=(const point &p); + bool operator < (const point &p); +}; \ 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)); - */ + } };