From 90a4e1b4f51e25bbc0356772f1b1ced5d12b2bba Mon Sep 17 00:00:00 2001 From: Lucano Zganec Date: Tue, 13 Nov 2018 16:10:32 +0100 Subject: [PATCH 1/4] Exercise 1 --- exercise/01/color.cpp | 85 ++++++++++++++++++++++++++ exercise/01/color.h | 26 ++++++++ exercise/01/color_test.sln | 31 ++++++++++ exercise/01/color_test.vcxproj.filters | 6 +- 4 files changed, 145 insertions(+), 3 deletions(-) create mode 100644 exercise/01/color.cpp create mode 100644 exercise/01/color.h create mode 100644 exercise/01/color_test.sln diff --git a/exercise/01/color.cpp b/exercise/01/color.cpp new file mode 100644 index 0000000..62deec1 --- /dev/null +++ b/exercise/01/color.cpp @@ -0,0 +1,85 @@ +#include "stdafx.h" +#include "color.h" + +color::color() +{ + red = 0; + green = 0; + blue = 0; +} + +color::color(const double red, const double green, const double blue) +{ + set_red(red); + set_green(green); + set_blue(blue); +} + +COLORREF 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; +} + + +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(const double red) +{ + this->red = get_valid_value(red); +} + +void color::set_green(const double green) +{ + this->green = get_valid_value(green); +} + +void color::set_blue(const double blue) +{ + this->blue = get_valid_value(blue); +} + +double color::get_valid_value(const double value) const +{ + if (value > 1) + { + return 1; + } + + if (value < 0) + { + return 0; + } + + return value; +} + + +bool operator==(const color& lhs, const color& rhs) +{ + return lhs.get_red() == rhs.get_red() + && lhs.get_green() == rhs.get_green() + && lhs.get_blue() == rhs.get_blue(); +} + +color::~color() +{ +} diff --git a/exercise/01/color.h b/exercise/01/color.h new file mode 100644 index 0000000..ea0b530 --- /dev/null +++ b/exercise/01/color.h @@ -0,0 +1,26 @@ +#pragma once +#include + +class color +{ +public: + color(); + color(double, double, double); + ~color(); + COLORREF get_color_ref() const; + double get_luminance() 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); +private: + double red; + double green; + double blue; + double get_valid_value(double value) const; +}; + +bool operator==(const color& lhs, const color& rhs); + diff --git a/exercise/01/color_test.sln b/exercise/01/color_test.sln new file mode 100644 index 0000000..9502ce4 --- /dev/null +++ b/exercise/01/color_test.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.28010.2036 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "color_test", "color_test.vcxproj", "{F6408CAA-4C86-4D98-8D13-9AF4DF77D23B}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {F6408CAA-4C86-4D98-8D13-9AF4DF77D23B}.Debug|x64.ActiveCfg = Debug|x64 + {F6408CAA-4C86-4D98-8D13-9AF4DF77D23B}.Debug|x64.Build.0 = Debug|x64 + {F6408CAA-4C86-4D98-8D13-9AF4DF77D23B}.Debug|x86.ActiveCfg = Debug|Win32 + {F6408CAA-4C86-4D98-8D13-9AF4DF77D23B}.Debug|x86.Build.0 = Debug|Win32 + {F6408CAA-4C86-4D98-8D13-9AF4DF77D23B}.Release|x64.ActiveCfg = Release|x64 + {F6408CAA-4C86-4D98-8D13-9AF4DF77D23B}.Release|x64.Build.0 = Release|x64 + {F6408CAA-4C86-4D98-8D13-9AF4DF77D23B}.Release|x86.ActiveCfg = Release|Win32 + {F6408CAA-4C86-4D98-8D13-9AF4DF77D23B}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {3223AE33-DEE3-41B0-A0F3-4E608BD3CE78} + EndGlobalSection +EndGlobal diff --git a/exercise/01/color_test.vcxproj.filters b/exercise/01/color_test.vcxproj.filters index 61bbd8e..fbcc217 100644 --- a/exercise/01/color_test.vcxproj.filters +++ b/exercise/01/color_test.vcxproj.filters @@ -5,15 +5,15 @@ {4FC737F1-C7A5-4376-A066-2A32D752A2FF} cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx - - {3f0b361d-e959-4712-bd7f-b211a50b8b89} - {a5701a66-1845-4643-b359-7bc01a47fe2d} {ec9c9ba0-fde3-42a6-ad15-1d2221ab37fb} + + {72c35eb3-28cb-4cc5-89c7-97c6912d0a04} + From bd0ab6b17c3f5d4b2bdc2250043868a280d47ca0 Mon Sep 17 00:00:00 2001 From: Lucano Zganec Date: Tue, 13 Nov 2018 17:31:30 +0100 Subject: [PATCH 2/4] Partially implementation for ex2 --- exercise/02/array.cpp | 58 +++++++++++++++++++++++++++++++++++++++++++ exercise/02/array.h | 8 ++++++ exercise/02/test.cpp | 14 ----------- 3 files changed, 66 insertions(+), 14 deletions(-) diff --git a/exercise/02/array.cpp b/exercise/02/array.cpp index 9247ffd..8e9af5a 100644 --- a/exercise/02/array.cpp +++ b/exercise/02/array.cpp @@ -1 +1,59 @@ #include "array.h" +#include + +array::array() +{ + n = 0; + p = nullptr; +} + + +array::array(const int size, const double value) +{ + n = size; + p = new double[n]; + + for (int i = 0; i < size; i++) + { + p[i] = value; + } +} + + +array::array(const array& obj) +{ + n = obj.n; + p = new double[obj.n]; + + for (int i = 0; i < n; i++) + { + p[i] = obj.p[i]; + } +} + +int array::size() const +{ + return n; +} + + +double array::at(const int i) const +{ + if (i > n || i < 0) + { + throw std::out_of_range("Array index out of range"); + } + + return p[i]; +} + +array array::operator=(const array& obj) +{ + //TODO finish +} + +array::~array() +{ + delete[] p; + p = nullptr; +} diff --git a/exercise/02/array.h b/exercise/02/array.h index 20489df..72773cd 100644 --- a/exercise/02/array.h +++ b/exercise/02/array.h @@ -4,4 +4,12 @@ class array double* p; int n; public: + array(); + array(int size, double value); + array(const array& obj); + array operator=(const array& obj); + ~array(); + int size() const; + double at(int i) 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 8959ef6d77fad42f6b05bcedf04ebb9083ebec4b Mon Sep 17 00:00:00 2001 From: Lucano Zganec Date: Wed, 14 Nov 2018 11:14:00 +0100 Subject: [PATCH 3/4] Exercise 3 implementation, adjustments to exercises 1 and 2 --- exercise/01/color.cpp | 5 ++++- exercise/01/color.h | 3 ++- exercise/02/array.cpp | 27 ++++++++++++++++++--------- exercise/02/array.h | 3 ++- exercise/02/array.sln | 25 +++++++++++++++++++++++++ exercise/03/point.cpp | 32 ++++++++++++++++++++++++++++++++ exercise/03/point.h | 12 ++++++++++++ exercise/03/test.cpp | 8 -------- 8 files changed, 95 insertions(+), 20 deletions(-) create mode 100644 exercise/02/array.sln diff --git a/exercise/01/color.cpp b/exercise/01/color.cpp index 62deec1..ad4456c 100644 --- a/exercise/01/color.cpp +++ b/exercise/01/color.cpp @@ -80,6 +80,9 @@ bool operator==(const color& lhs, const color& rhs) && lhs.get_blue() == rhs.get_blue(); } -color::~color() +bool operator!=(const color& lhs, const color& rhs) { + return !(lhs.get_red() == rhs.get_red() + && lhs.get_green() == rhs.get_green() + && lhs.get_blue() == rhs.get_blue()); } diff --git a/exercise/01/color.h b/exercise/01/color.h index ea0b530..8d2a84a 100644 --- a/exercise/01/color.h +++ b/exercise/01/color.h @@ -6,7 +6,7 @@ class color public: color(); color(double, double, double); - ~color(); + ~color() = default; COLORREF get_color_ref() const; double get_luminance() const; double get_red() const; @@ -23,4 +23,5 @@ class color }; bool operator==(const color& lhs, const color& rhs); +bool operator!=(const color& lhs, const color& rhs); diff --git a/exercise/02/array.cpp b/exercise/02/array.cpp index 8e9af5a..1974647 100644 --- a/exercise/02/array.cpp +++ b/exercise/02/array.cpp @@ -22,13 +22,12 @@ array::array(const int size, const double value) array::array(const array& obj) { - n = obj.n; - p = new double[obj.n]; + *this = obj; +} - for (int i = 0; i < n; i++) - { - p[i] = obj.p[i]; - } + +array::array(const array&& obj) +{ } int array::size() const @@ -36,7 +35,6 @@ int array::size() const return n; } - double array::at(const int i) const { if (i > n || i < 0) @@ -47,9 +45,20 @@ double array::at(const int i) const return p[i]; } -array array::operator=(const array& obj) +array& array::operator=(const array& obj) { - //TODO finish + if (this == &obj) return *this; + delete[] p; + + n = obj.n; + p = new double[obj.n]; + + for (int i = 0; i < n; i++) + { + p[i] = obj.p[i]; + } + + return *this; } array::~array() diff --git a/exercise/02/array.h b/exercise/02/array.h index 72773cd..edfa1d2 100644 --- a/exercise/02/array.h +++ b/exercise/02/array.h @@ -7,7 +7,8 @@ class array array(); array(int size, double value); array(const array& obj); - array operator=(const array& obj); + array(const array&& obj); + array& operator=(const array& obj); ~array(); int size() const; double at(int i) const; diff --git a/exercise/02/array.sln b/exercise/02/array.sln new file mode 100644 index 0000000..6b1d8c2 --- /dev/null +++ b/exercise/02/array.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.28010.2036 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "array", "array.vcxproj", "{BB15329E-10D8-4740-94BC-64C50BA4EE12}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x86 = Debug|x86 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {BB15329E-10D8-4740-94BC-64C50BA4EE12}.Debug|x86.ActiveCfg = Debug|Win32 + {BB15329E-10D8-4740-94BC-64C50BA4EE12}.Debug|x86.Build.0 = Debug|Win32 + {BB15329E-10D8-4740-94BC-64C50BA4EE12}.Release|x86.ActiveCfg = Release|Win32 + {BB15329E-10D8-4740-94BC-64C50BA4EE12}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {B655C418-5CF9-4743-824D-DF7A50AC49A7} + EndGlobalSection +EndGlobal diff --git a/exercise/03/point.cpp b/exercise/03/point.cpp index 5c7c45f..0949400 100644 --- a/exercise/03/point.cpp +++ b/exercise/03/point.cpp @@ -1,2 +1,34 @@ #include "point.h" + +point::point() +{ + x = 0; + y = 0; +} + +point::point(int x, int y) +{ + this->x = x; + this->y = y; +} + +bool point::operator==(const point& other) const +{ + return this->x == other.x && this->y == other.y; +} + +bool point::operator!=(const point& other) const +{ + return !(*this == other); +} + +bool point::operator<(const point& other) const +{ + if (x == other.x) + { + return y < other.y; + } + + return x < other.x; +} diff --git a/exercise/03/point.h b/exercise/03/point.h index e69de29..13e3140 100644 --- a/exercise/03/point.h +++ b/exercise/03/point.h @@ -0,0 +1,12 @@ +class point +{ +public: + point(); + point(int x, int y); + bool operator==(const point&) const; + bool operator!=(const point&) const; + bool operator<(const point&) const; +private: + int x; + int 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)); - */ } }; From 2ef4479d0c5d5a4ef37faf055368e967413616f2 Mon Sep 17 00:00:00 2001 From: Lucano Zganec Date: Wed, 14 Nov 2018 15:17:56 +0100 Subject: [PATCH 4/4] Finished exercise 2, adjustments in exercise 3 --- exercise/02/array.cpp | 11 +++++------ exercise/02/array.h | 4 ++-- exercise/03/point.cpp | 13 ------------- exercise/03/point.h | 6 ++++-- 4 files changed, 11 insertions(+), 23 deletions(-) diff --git a/exercise/02/array.cpp b/exercise/02/array.cpp index 1974647..2ab67fd 100644 --- a/exercise/02/array.cpp +++ b/exercise/02/array.cpp @@ -8,11 +8,8 @@ array::array() } -array::array(const int size, const double value) +array::array(const int size, const double value) : n(size), p(new double[n]) { - n = size; - p = new double[n]; - for (int i = 0; i < size; i++) { p[i] = value; @@ -20,14 +17,16 @@ array::array(const int size, const double value) } -array::array(const array& obj) +array::array(const array& obj) : n(0), p(nullptr) { *this = obj; } -array::array(const array&& obj) +array::array(array&& obj) : n(obj.n), p(obj.p) { + obj.n = 0; + obj.p = nullptr; } int array::size() const diff --git a/exercise/02/array.h b/exercise/02/array.h index edfa1d2..ce90fe6 100644 --- a/exercise/02/array.h +++ b/exercise/02/array.h @@ -1,13 +1,13 @@ // do not use standard container as member or base class class array { - double* p; int n; + double* p; public: array(); array(int size, double value); array(const array& obj); - array(const array&& obj); + array(array&& obj); array& operator=(const array& obj); ~array(); int size() const; diff --git a/exercise/03/point.cpp b/exercise/03/point.cpp index 0949400..e7349ab 100644 --- a/exercise/03/point.cpp +++ b/exercise/03/point.cpp @@ -1,18 +1,5 @@ #include "point.h" - -point::point() -{ - x = 0; - y = 0; -} - -point::point(int x, int y) -{ - this->x = x; - this->y = y; -} - bool point::operator==(const point& other) const { return this->x == other.x && this->y == other.y; diff --git a/exercise/03/point.h b/exercise/03/point.h index 13e3140..8bfea94 100644 --- a/exercise/03/point.h +++ b/exercise/03/point.h @@ -1,8 +1,10 @@ +#pragma once + class point { public: - point(); - point(int x, int y); + point() = default; + point(int x, int y) : x(x), y(y) {}; bool operator==(const point&) const; bool operator!=(const point&) const; bool operator<(const point&) const;