Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions exercise/01/color.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#include "stdafx.h"
#include "color.h"

color::color()
{
r = 0;
g = 0;
b = 0;
}

color::color(double red, double green, double blue)
{
set_red(red);
set_green(green);
set_blue(blue);
}

double color::get_red() const
{
return r;
}

double color::get_green() const
{
return g;
}

double color::get_blue() const
{
return b;
}

double clamp(double color_channel)
{
return std::clamp(color_channel, 0.0, 1.0);
}

void color::set_red(double red)
{
r = clamp(red);
}

void color::set_green(double green)
{
g = clamp(green);
}

void color::set_blue(double blue)
{
b = clamp(blue);
}

COLORREF color::get_color_ref() const
{
return RGB(r * 255, g * 255, b * 255);
}

double color::get_luminance() const
{
return 0.2126*r + 0.7152*g + 0.0722*b;
}

bool color::operator==(const color& other) const
{
return r == other.r && g == other.g && b == other.b;
}
21 changes: 21 additions & 0 deletions exercise/01/color.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#pragma once

class color
{
private:
double r;
double g;
double b;
public:
color();
color(double red, double green, double blue);
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);
COLORREF get_color_ref() const;
double get_luminance() const;
bool operator==(const color& other) const;
};
31 changes: 31 additions & 0 deletions exercise/01/color_test.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.28010.2050
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 = {B6F18144-76F1-4C62-AEFC-5AF50AEDF509}
EndGlobalSection
EndGlobal
2 changes: 2 additions & 0 deletions exercise/01/stdafx.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@

#include <SDKDDKVer.h>
#include "CppUnitTest.h"
#include <Windows.h>
#include <algorithm>
81 changes: 81 additions & 0 deletions exercise/02/array.cpp
Original file line number Diff line number Diff line change
@@ -1 +1,82 @@
#include <stdexcept>
#include "array.h"

array::array()
{
p = nullptr;
n = 0;
}

array::array(int size, double value)
{
p = new double[size];
for (int i = 0; i < size; ++i)
{
p[i] = value;
}
n = size;
}

void array::copy_impl(const array& other)
{
p = new double[other.n];
for (int i = 0; i < other.n; i++)
{
p[i] = other.p[i];
}
n = other.n;
}

array::array(const array& other)
{
if (this != &other)
{
copy_impl(other);
}
}

array::array(array&& other)
{
p = other.p;
n = other.n;

other.p = nullptr;
other.n = 0;
}

array::~array()
{
delete[] p;
p = nullptr;
n = 0;
}

int array::size() const
{
return n;
}


void check_index_range(int index, int min, int max)
{
if (index < min || index > max)
{
throw std::out_of_range("index is out of range");
}
}

double array::at(int index) const
{
check_index_range(index, 0, n - 1);
return p[index];
}

array& array::operator=(const array& other)
{
if (this != &other)
{
delete[] p;
copy_impl(other);
}
return *this;
}
10 changes: 10 additions & 0 deletions exercise/02/array.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
// do not use standard container as member or base class
class array
{
private:
double* p;
int n;
void copy_impl(const array& other);
public:
array();
array(int size, double value);
array(const array& other);
array(array&& other);
~array();
int size() const;
double at(int index) const;
array& operator=(const array& other);
};
25 changes: 25 additions & 0 deletions exercise/02/array.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.28010.2050
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 = {4D6C71D3-37D1-47DA-A548-AF3841B529BC}
EndGlobalSection
EndGlobal
28 changes: 14 additions & 14 deletions exercise/02/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,73 +12,73 @@ 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<a.size(); ++i)
Assert::AreEqual(value, a.at(i));
*/

}

TEST_METHOD(array_index_invalid_large)
{
/*

array a(size, value);
auto func = [a]() { a.at(100); };
Assert::ExpectException<std::out_of_range>(func);
*/

}

TEST_METHOD(array_index_invalid_negative)
{
/*

array a(size, value);
auto func = [a]() { a.at(-2); };
Assert::ExpectException<std::out_of_range>(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));
*/

}

};
25 changes: 25 additions & 0 deletions exercise/03/operators.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.28010.2050
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "operators", "operators.vcxproj", "{86A75A8D-048F-4B18-A466-A62C44E92490}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x86 = Debug|x86
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{86A75A8D-048F-4B18-A466-A62C44E92490}.Debug|x86.ActiveCfg = Debug|Win32
{86A75A8D-048F-4B18-A466-A62C44E92490}.Debug|x86.Build.0 = Debug|Win32
{86A75A8D-048F-4B18-A466-A62C44E92490}.Release|x86.ActiveCfg = Release|Win32
{86A75A8D-048F-4B18-A466-A62C44E92490}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {4EC4BD2E-8BEF-4E10-935E-C5B239BA91E6}
EndGlobalSection
EndGlobal
26 changes: 26 additions & 0 deletions exercise/03/point.cpp
Original file line number Diff line number Diff line change
@@ -1,2 +1,28 @@
#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 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;
}
Loading