From 5480e2332d9340e31e294d785cc77884015d82a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivan=20Kvesi=C4=87?= Date: Tue, 13 Nov 2018 18:51:11 +0100 Subject: [PATCH 1/3] Solved first exercise --- exercise/01/color.cpp | 84 ++++++++++++++++++++++++++++++++++++++ exercise/01/color.h | 28 +++++++++++++ exercise/01/color_test.cpp | 1 - exercise/01/stdafx.h | 1 + 4 files changed, 113 insertions(+), 1 deletion(-) 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..7c8c7ac --- /dev/null +++ b/exercise/01/color.cpp @@ -0,0 +1,84 @@ +#include "stdafx.h" +#include "color.h" + +color::color(double r, double g, double b) +{ + set_rgb(r, g, b); +} + +color::color(const color& other) : color(other.r,other.g,other.b) +{ +} + +void color::set_rgb(double r, double g, double b) +{ + set_red(r); + set_green(g); + set_blue(b); +} + +double color::set_color(double v) const +{ + if (v < 0) + { + return 0.; + } + else if (v > 1) + { + return 1.; + } + + return v; +} + +const double color::get_red() const +{ + return r; +} + +const double color::get_green() const +{ + return g; +} + +const double color::get_blue() const +{ + return b; +} + +void color::set_red(const double r) +{ + this ->r = set_color(r); +} + +void color::set_green(const double g) +{ + this ->g = set_color(g); +} + +void color::set_blue(const double b) +{ + this ->b = set_color(b); +} + +unsigned long color::get_color_ref() const +{ + const double rgb_max_val = 255.0; + return RGB((int)(r*rgb_max_val), (int)(g*rgb_max_val), (int)(b * rgb_max_val)); +} + +double color::get_luminance() const +{ + return r * 0.2126 + g * 0.7152 + b * 0.0722; +} + +bool color::operator==(const color& other) const +{ + return (this->r == other.r && this->g == other.g && this->b == other.b); +} + +color color::operator=(const color& c) +{ + set_rgb(c.r, c.g, c.b); + return *this; +} \ No newline at end of file diff --git a/exercise/01/color.h b/exercise/01/color.h new file mode 100644 index 0000000..a9fd7aa --- /dev/null +++ b/exercise/01/color.h @@ -0,0 +1,28 @@ +#pragma once + +class color { +private: + double r; + double g; + double b; + +public: + color(double r = 0, double g = 0, double b = 0); + color(const color& other); + const double get_red() const; + const double get_green() const ; + const double get_blue() const; + void set_red(const double r); + void set_green(const double g); + void set_blue(const double b); + unsigned long get_color_ref() const; + double get_luminance() const; + + bool operator==(const color& color1) const; + color operator=(const color& c); + +private: + double set_color(double v) const; + void set_rgb (double r, double g, double b); + +}; \ No newline at end of file diff --git a/exercise/01/color_test.cpp b/exercise/01/color_test.cpp index 1af693e..8527729 100644 --- a/exercise/01/color_test.cpp +++ b/exercise/01/color_test.cpp @@ -1,6 +1,5 @@ #include "stdafx.h" #include "color.h" -#include // RGB using namespace Microsoft::VisualStudio::CppUnitTestFramework; diff --git a/exercise/01/stdafx.h b/exercise/01/stdafx.h index 6027cdf..60d521f 100644 --- a/exercise/01/stdafx.h +++ b/exercise/01/stdafx.h @@ -2,3 +2,4 @@ #include #include "CppUnitTest.h" +#include From 340a02623a80944a734e56ce17e51f31ebd0a1f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivan=20Kvesi=C4=87?= Date: Tue, 13 Nov 2018 20:57:20 +0100 Subject: [PATCH 2/3] Exercise 02 commit (almost all tests passed) --- exercise/02/array.cpp | 87 +++++++++++++++++++++++++++++++++++++++++++ exercise/02/array.h | 12 ++++++ exercise/02/test.cpp | 23 +++++------- 3 files changed, 108 insertions(+), 14 deletions(-) diff --git a/exercise/02/array.cpp b/exercise/02/array.cpp index 9247ffd..30acf77 100644 --- a/exercise/02/array.cpp +++ b/exercise/02/array.cpp @@ -1 +1,88 @@ #include "array.h" +#include + +array::array() +{ + this ->p = nullptr; + this ->n = 0; +} + +array::array(const array& other) +{ + alloc_array(other.size()); + for (int i = 0; i < this->n; ++i) + { + p[i] = other.at(i); + } +} + +array::array(const int size, const double value):array() +{ + alloc_array(size); + for (int i = 0; i < size; i++) + { + p[i] = value; + } + +} + +array::~array() +{ + if (this->p != nullptr) + { + delete[] this->p; this->p = nullptr; + } + this->n = 0; +} + +void array::alloc_array(const int size) +{ + this->p = new double[size]; + if (this->p == nullptr) + { + throw; + } + + this->n = size; +} + +int array::size() const +{ + return this->n; +} + +double array::at(const int i) const +{ + if (! (i >= 0 && i < n)) + { + throw std::out_of_range("Value of array out of range"); + } + return p[i]; +} + +bool array::operator== (const array& other) const +{ + if (this->size() != other.size()) + { + return 0; + } + + for (int i = 0; i < size(); i++) + { + if (at(i) != other.at(i)) + return 0; + } + + return 1; + +} + +const array array::operator= (const array& other) +{ + alloc_array(other.size()); + for (int i = 0; i < this->n; i++) + { + p[i] = other.at(i); + } + return (*this); +} \ No newline at end of file diff --git a/exercise/02/array.h b/exercise/02/array.h index 20489df..8431e23 100644 --- a/exercise/02/array.h +++ b/exercise/02/array.h @@ -3,5 +3,17 @@ class array { double* p; int n; + +private: + void alloc_array(const int size); public: + array(); + array(const array& other); + array(const int size, const double value); + ~array(); + int size() const; + double at(const int i) const; + + bool operator==(const array& other) const; + const array operator= (const array& other); }; diff --git a/exercise/02/test.cpp b/exercise/02/test.cpp index 1adae67..91a9d50 100644 --- a/exercise/02/test.cpp +++ b/exercise/02/test.cpp @@ -12,73 +12,68 @@ 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 15f6aef105597168341fe476a3b4aa141abe3fb3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivan=20Kvesi=C4=87?= Date: Wed, 14 Nov 2018 10:50:28 +0100 Subject: [PATCH 3/3] Exercise 3 solved and changes in exercise 1 --- exercise/01/color.cpp | 35 ++++++++++++++++++----------------- exercise/01/color.h | 2 -- exercise/03/point.cpp | 28 ++++++++++++++++++++++++++++ exercise/03/point.h | 11 +++++++++++ exercise/03/test.cpp | 14 ++++++-------- 5 files changed, 63 insertions(+), 27 deletions(-) diff --git a/exercise/01/color.cpp b/exercise/01/color.cpp index 7c8c7ac..77b185b 100644 --- a/exercise/01/color.cpp +++ b/exercise/01/color.cpp @@ -1,13 +1,28 @@ #include "stdafx.h" #include "color.h" -color::color(double r, double g, double b) + + +namespace { - set_rgb(r, g, b); + double set_color(double v) + { + if (v < 0) + { + return 0.; + } + else if (v > 1) + { + return 1.; + } + + return v; + } } -color::color(const color& other) : color(other.r,other.g,other.b) +color::color(double r, double g, double b) { + set_rgb(r, g, b); } void color::set_rgb(double r, double g, double b) @@ -17,20 +32,6 @@ void color::set_rgb(double r, double g, double b) set_blue(b); } -double color::set_color(double v) const -{ - if (v < 0) - { - return 0.; - } - else if (v > 1) - { - return 1.; - } - - return v; -} - const double color::get_red() const { return r; diff --git a/exercise/01/color.h b/exercise/01/color.h index a9fd7aa..0c05134 100644 --- a/exercise/01/color.h +++ b/exercise/01/color.h @@ -8,7 +8,6 @@ class color { public: color(double r = 0, double g = 0, double b = 0); - color(const color& other); const double get_red() const; const double get_green() const ; const double get_blue() const; @@ -22,7 +21,6 @@ class color { color operator=(const color& c); private: - double set_color(double v) const; void set_rgb (double r, double g, double b); }; \ No newline at end of file diff --git a/exercise/03/point.cpp b/exercise/03/point.cpp index 5c7c45f..f6baf8e 100644 --- a/exercise/03/point.cpp +++ b/exercise/03/point.cpp @@ -1,2 +1,30 @@ #include "point.h" +point::point(int x, int y) +{ + this->x = x; + this->y = y; +} + +bool point::operator==(const point& other) +{ + if (this->x == other.x && this->y == other.y) + return 1; + + return 0; +} + +bool point::operator!=(const point& other) +{ + return !(this == &other); +} + +bool point::operator<(const point& other) +{ + if (this->x < other.x) + return true; + else if (this->y < other.y) + return true; + + return false; +} \ No newline at end of file diff --git a/exercise/03/point.h b/exercise/03/point.h index e69de29..2aceee4 100644 --- a/exercise/03/point.h +++ b/exercise/03/point.h @@ -0,0 +1,11 @@ +class point +{ + int x; + int y; + +public: + point(int x = 0, int y = 0); + bool operator == (const point& other); + bool operator!= (const point& other); + bool operator< (const point& other); +}; \ No newline at end of file diff --git a/exercise/03/test.cpp b/exercise/03/test.cpp index d8e6669..bab580b 100644 --- a/exercise/03/test.cpp +++ b/exercise/03/test.cpp @@ -9,30 +9,28 @@ 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)); - */ + } };