From d0c9254ae5434fad505efd0a2f6d10f035e616e4 Mon Sep 17 00:00:00 2001 From: Nicolette Date: Tue, 13 Nov 2018 18:07:03 +0100 Subject: [PATCH 1/6] Make exercise 01 compile --- exercise/01/color.cpp | 47 +++++++++++++++++++++++++++++++++++++++++++ exercise/01/color.h | 22 ++++++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 exercise/01/color.cpp create mode 100644 exercise/01/color.h diff --git a/exercise/01/color.cpp b/exercise/01/color.cpp new file mode 100644 index 0000000..4d5fad5 --- /dev/null +++ b/exercise/01/color.cpp @@ -0,0 +1,47 @@ +#include "stdafx.h" +#include "color.h" + +bool color::operator==(const color & other) const +{ + return false; +} + +double color::get_red() const +{ + return 0.0; +} + +double color::get_green() const +{ + return 0.0; +} + +double color::get_blue() const +{ + return 0.0; +} + +double color::set_red(double red) +{ + return 0.0; +} + +double color::set_green(double green) +{ + return 0.0; +} + +double color::set_blue(double blue) +{ + return 0.0; +} + +COLORREF color::get_color_ref() const +{ + return COLORREF(); +} + +double color::get_luminance() const +{ + return 0.0; +} diff --git a/exercise/01/color.h b/exercise/01/color.h new file mode 100644 index 0000000..85e7e69 --- /dev/null +++ b/exercise/01/color.h @@ -0,0 +1,22 @@ +#pragma once +#include + +class color { +public: + color() : red{0}, green{0}, blue{0} {}; + color(double red, double green, double blue) : red{red}, green{green}, blue{blue} {}; + bool operator==(const color& other) const; + double get_red() const; + double get_green() const; + double get_blue() const; + double set_red(double red); + double set_green(double green); + double set_blue(double blue); + COLORREF get_color_ref() const; + double get_luminance() const; + +private: + double red; + double green; + double blue; +}; \ No newline at end of file From a9fde5716bbae57976eea197f68b56e9d3033bd5 Mon Sep 17 00:00:00 2001 From: Nicolette Date: Tue, 13 Nov 2018 19:15:37 +0100 Subject: [PATCH 2/6] Make exercise 01 green --- exercise/01/color.cpp | 51 +++++++++++++++++++++++++++++++++---------- exercise/01/color.h | 13 +++++++---- 2 files changed, 48 insertions(+), 16 deletions(-) diff --git a/exercise/01/color.cpp b/exercise/01/color.cpp index 4d5fad5..3b5f2e2 100644 --- a/exercise/01/color.cpp +++ b/exercise/01/color.cpp @@ -1,47 +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 false; + return this->red == other.red && this->green == other.green && this->blue == other.blue; } double color::get_red() const { - return 0.0; + return red; } double color::get_green() const { - return 0.0; + return green; } double color::get_blue() const { - return 0.0; + return blue; +} + +namespace { + + double convert(double col) + { + if (col < 0) return 0; + if (col > 1) return 1; + return col; + } } -double color::set_red(double red) +void color::set_red(double red) { - return 0.0; + this->red = convert(red); } -double color::set_green(double green) +void color::set_green(double green) { - return 0.0; + this->green = convert(green); } -double color::set_blue(double blue) +void color::set_blue(double blue) { - return 0.0; + this->blue = convert(blue); } COLORREF color::get_color_ref() const { - return COLORREF(); + return RGB(red * maxColorRange, green * maxColorRange, blue * maxColorRange); } double color::get_luminance() const { - return 0.0; + return red * redIntensity + green * greenIntensity + blue * blueIntensity; } diff --git a/exercise/01/color.h b/exercise/01/color.h index 85e7e69..6b90764 100644 --- a/exercise/01/color.h +++ b/exercise/01/color.h @@ -4,14 +4,15 @@ class color { public: color() : red{0}, green{0}, blue{0} {}; - color(double red, double green, double blue) : red{red}, green{green}, blue{blue} {}; + 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; - double set_red(double red); - double set_green(double green); - double set_blue(double blue); + void set_red(double red); + void set_green(double green); + void set_blue(double blue); COLORREF get_color_ref() const; double get_luminance() const; @@ -19,4 +20,8 @@ class color { 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 From 8b48e7ef82c8fcdab6078c33e5efe0c65aeb5d50 Mon Sep 17 00:00:00 2001 From: Nicolette Date: Tue, 13 Nov 2018 19:28:21 +0100 Subject: [PATCH 3/6] Make exercise 02 compile --- exercise/02/array.cpp | 18 ++++++++++++++++++ exercise/02/array.h | 4 ++++ exercise/02/test.cpp | 14 -------------- 3 files changed, 22 insertions(+), 14 deletions(-) diff --git a/exercise/02/array.cpp b/exercise/02/array.cpp index 9247ffd..dfe6602 100644 --- a/exercise/02/array.cpp +++ b/exercise/02/array.cpp @@ -1 +1,19 @@ #include "array.h" + +array::array() +{ +} + +array::array(int size, double value) +{ +} + +int array::size() const +{ + return 0; +} + +double array::at(int index) const +{ + return 0.0; +} diff --git a/exercise/02/array.h b/exercise/02/array.h index 20489df..04f1a2d 100644 --- a/exercise/02/array.h +++ b/exercise/02/array.h @@ -4,4 +4,8 @@ class array double* p; int n; public: + array(); + array(int size, double value); + 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)); - */ } }; From 9cea040cbb703236ebcc43fa93edfcc0bbc38341 Mon Sep 17 00:00:00 2001 From: Nicolette Date: Wed, 14 Nov 2018 08:24:45 +0100 Subject: [PATCH 4/6] Make exercise 02 green --- exercise/02/array.cpp | 31 +++++++++++++++++++++++++++---- exercise/02/array.h | 10 ++++++++-- 2 files changed, 35 insertions(+), 6 deletions(-) diff --git a/exercise/02/array.cpp b/exercise/02/array.cpp index dfe6602..49ed1f7 100644 --- a/exercise/02/array.cpp +++ b/exercise/02/array.cpp @@ -1,19 +1,42 @@ #include "array.h" +#include +#include -array::array() +array::array(int size, double value) { + if (size < 0) return; + n = size; + p = new double[size]; + std::fill(p, p + size, value); } -array::array(int size, double value) +array::array(const array & other) : n(other.n), p (other.p) {} + +array::array(array && other) noexcept : n(std::exchange(other.n, 0)), p(std::move(other.p)) {} + +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() {} + int array::size() const { - return 0; + return n; } double array::at(int index) const { - return 0.0; + 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 04f1a2d..53f62e6 100644 --- a/exercise/02/array.h +++ b/exercise/02/array.h @@ -2,10 +2,16 @@ class array { double* p; - int n; + int n = 0; public: - array(); + array() {}; 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; }; From e0e7f0aaf00caf38b5028ab6034de2a4427eb0e0 Mon Sep 17 00:00:00 2001 From: Nicolette Date: Wed, 14 Nov 2018 09:47:29 +0100 Subject: [PATCH 5/6] Write destructor and fix constructors --- exercise/02/array.cpp | 20 ++++++++++++++++---- exercise/02/array.h | 4 ++-- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/exercise/02/array.cpp b/exercise/02/array.cpp index 49ed1f7..46aeb84 100644 --- a/exercise/02/array.cpp +++ b/exercise/02/array.cpp @@ -4,15 +4,20 @@ array::array(int size, double value) { - if (size < 0) return; + 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 (other.p) {} +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::move(other.p)) {} +array::array(array && other) noexcept : n(std::exchange(other.n, 0)), p(std::exchange(other.p, nullptr)) {} array & array::operator=(array other) { @@ -28,7 +33,14 @@ array & array::operator=(array && other) return *this; } -array::~array() {} +array::~array() +{ + if (p == nullptr) return; + delete[] p; + p = nullptr; + n = 0; + +} int array::size() const { diff --git a/exercise/02/array.h b/exercise/02/array.h index 53f62e6..5f7b1c6 100644 --- a/exercise/02/array.h +++ b/exercise/02/array.h @@ -2,9 +2,9 @@ class array { double* p; - int n = 0; + int n; public: - array() {}; + array() : n(0), p(nullptr) {}; array(int size, double value); array(const array& other); array(array&& other) noexcept; From 5c65243663bb51f98fd35434e8967aa5cab303a9 Mon Sep 17 00:00:00 2001 From: Nicolette Date: Wed, 14 Nov 2018 11:04:20 +0100 Subject: [PATCH 6/6] Make exercise 03 green --- exercise/03/point.cpp | 14 ++++++++++++++ exercise/03/point.h | 19 +++++++++++++++++++ exercise/03/test.cpp | 8 -------- 3 files changed, 33 insertions(+), 8 deletions(-) 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)); - */ } };