From 1c5a3b18f96c749a6bf25889920e035574180bf4 Mon Sep 17 00:00:00 2001 From: Lucano Zganec Date: Fri, 16 Nov 2018 15:27:24 +0100 Subject: [PATCH 1/5] Work on the exercise --- FinalExercise/FinalExercise.sln | 41 ++++ FinalExercise/FinalExercise/FinalExercise.cpp | Bin 0 -> 1720 bytes .../FinalExercise/FinalExercise.vcxproj | 170 +++++++++++++++++ .../FinalExercise.vcxproj.filters | 36 ++++ FinalExercise/FinalExercise/mathblocks.cpp | 118 ++++++++++++ FinalExercise/FinalExercise/mathblocks.h | 35 ++++ FinalExercise/FinalExercise/pch.cpp | Bin 0 -> 430 bytes FinalExercise/FinalExercise/pch.h | Bin 0 -> 1262 bytes FinalExercise/FinalExercise/public.h | 7 + .../FinalExerciseTests.vcxproj | 179 ++++++++++++++++++ .../FinalExerciseTests.vcxproj.filters | 39 ++++ .../FinalExerciseTests/mathblockstest.cpp | 82 ++++++++ FinalExercise/FinalExerciseTests/pch.h | 13 ++ FinalExercise/FinalExerciseTests/stdafx.cpp | 8 + FinalExercise/FinalExerciseTests/targetver.h | 8 + 15 files changed, 736 insertions(+) create mode 100644 FinalExercise/FinalExercise.sln create mode 100644 FinalExercise/FinalExercise/FinalExercise.cpp create mode 100644 FinalExercise/FinalExercise/FinalExercise.vcxproj create mode 100644 FinalExercise/FinalExercise/FinalExercise.vcxproj.filters create mode 100644 FinalExercise/FinalExercise/mathblocks.cpp create mode 100644 FinalExercise/FinalExercise/mathblocks.h create mode 100644 FinalExercise/FinalExercise/pch.cpp create mode 100644 FinalExercise/FinalExercise/pch.h create mode 100644 FinalExercise/FinalExercise/public.h create mode 100644 FinalExercise/FinalExerciseTests/FinalExerciseTests.vcxproj create mode 100644 FinalExercise/FinalExerciseTests/FinalExerciseTests.vcxproj.filters create mode 100644 FinalExercise/FinalExerciseTests/mathblockstest.cpp create mode 100644 FinalExercise/FinalExerciseTests/pch.h create mode 100644 FinalExercise/FinalExerciseTests/stdafx.cpp create mode 100644 FinalExercise/FinalExerciseTests/targetver.h diff --git a/FinalExercise/FinalExercise.sln b/FinalExercise/FinalExercise.sln new file mode 100644 index 0000000..96918f6 --- /dev/null +++ b/FinalExercise/FinalExercise.sln @@ -0,0 +1,41 @@ + +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}") = "FinalExercise", "FinalExercise\FinalExercise.vcxproj", "{49A8615B-EDC6-46F3-A8A2-F2D66BD85F9E}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "FinalExerciseTests", "FinalExerciseTests\FinalExerciseTests.vcxproj", "{D57382B1-FE9F-45E6-92E9-926D43932666}" +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 + {49A8615B-EDC6-46F3-A8A2-F2D66BD85F9E}.Debug|x64.ActiveCfg = Debug|x64 + {49A8615B-EDC6-46F3-A8A2-F2D66BD85F9E}.Debug|x64.Build.0 = Debug|x64 + {49A8615B-EDC6-46F3-A8A2-F2D66BD85F9E}.Debug|x86.ActiveCfg = Debug|Win32 + {49A8615B-EDC6-46F3-A8A2-F2D66BD85F9E}.Debug|x86.Build.0 = Debug|Win32 + {49A8615B-EDC6-46F3-A8A2-F2D66BD85F9E}.Release|x64.ActiveCfg = Release|x64 + {49A8615B-EDC6-46F3-A8A2-F2D66BD85F9E}.Release|x64.Build.0 = Release|x64 + {49A8615B-EDC6-46F3-A8A2-F2D66BD85F9E}.Release|x86.ActiveCfg = Release|Win32 + {49A8615B-EDC6-46F3-A8A2-F2D66BD85F9E}.Release|x86.Build.0 = Release|Win32 + {D57382B1-FE9F-45E6-92E9-926D43932666}.Debug|x64.ActiveCfg = Debug|x64 + {D57382B1-FE9F-45E6-92E9-926D43932666}.Debug|x64.Build.0 = Debug|x64 + {D57382B1-FE9F-45E6-92E9-926D43932666}.Debug|x86.ActiveCfg = Debug|Win32 + {D57382B1-FE9F-45E6-92E9-926D43932666}.Debug|x86.Build.0 = Debug|Win32 + {D57382B1-FE9F-45E6-92E9-926D43932666}.Release|x64.ActiveCfg = Release|x64 + {D57382B1-FE9F-45E6-92E9-926D43932666}.Release|x64.Build.0 = Release|x64 + {D57382B1-FE9F-45E6-92E9-926D43932666}.Release|x86.ActiveCfg = Release|Win32 + {D57382B1-FE9F-45E6-92E9-926D43932666}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {FAA11F85-3C1D-4CC6-BF9D-8D356B566A41} + EndGlobalSection +EndGlobal diff --git a/FinalExercise/FinalExercise/FinalExercise.cpp b/FinalExercise/FinalExercise/FinalExercise.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1ebb4092cc2ec82a7d272ab571941568c7fb9e05 GIT binary patch literal 1720 zcma)-TWi!%6ovPzg8$)AAF3!bc&QIn3ZiW(h!s2H^GqfatLbEDGS(vgb@f~O%$dX( zN(o6$&c3X*Zzq3#k8Nzhj_ll)Hnq|Y?T6)dX&E~;s}ZXeSFnBViGA_?U<=+Q@_@X; zw!vCHYIYj5B-yo#&F(H=3tJ=4ke<86h`kRWRaWqvq8qRhdWL4w3N~Y>*!rI0WA2)I zw`iAWjqIM?C7!XrwZ89m8BEt`i_;FVWbhkt@3`LoIL_1GcYTVzLB$QFmrpk(U9Lc+1y7eZvpQIH(31IV#iv2^gIT9c5|odym7`Q zt4G{Nw#TzVE7)7`XV?|2g>{DJ6p5IAhJ`9tdDgqSG3&CqzWX^5<}@ItLV;I!IHm`HS6>tkP6g@Y3unYwDSrE9R!3k>`V44zUz-*}nrz zvog%-JJQ_iA+%`I$Jld>WBjFD#q;JXmNA2xh1XumVDC5nPQc7zpKLO`t4`Vt^jbt! zp5uv + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 15.0 + {49A8615B-EDC6-46F3-A8A2-F2D66BD85F9E} + Win32Proj + FinalExercise + 10.0.17134.0 + + + + Application + true + v141 + Unicode + + + Application + false + v141 + true + Unicode + + + Application + true + v141 + Unicode + + + Application + false + v141 + true + Unicode + + + + + + + + + + + + + + + + + + + + + true + + + true + + + false + + + false + + + + Use + Level3 + Disabled + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + pch.h + + + Console + true + + + + + Use + Level3 + Disabled + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + pch.h + + + Console + true + + + + + Use + Level3 + MaxSpeed + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + pch.h + + + Console + true + true + true + + + + + Use + Level3 + MaxSpeed + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + pch.h + + + Console + true + true + true + + + + + + + Create + Create + Create + Create + + + + + + + + + + \ No newline at end of file diff --git a/FinalExercise/FinalExercise/FinalExercise.vcxproj.filters b/FinalExercise/FinalExercise/FinalExercise.vcxproj.filters new file mode 100644 index 0000000..8eac141 --- /dev/null +++ b/FinalExercise/FinalExercise/FinalExercise.vcxproj.filters @@ -0,0 +1,36 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Source Files + + + Source Files + + + Source Files + + + + + Header Files + + + Header Files + + + \ No newline at end of file diff --git a/FinalExercise/FinalExercise/mathblocks.cpp b/FinalExercise/FinalExercise/mathblocks.cpp new file mode 100644 index 0000000..147a8e0 --- /dev/null +++ b/FinalExercise/FinalExercise/mathblocks.cpp @@ -0,0 +1,118 @@ +#include "pch.h" +#include "mathblocks.h" +#include + +mathblocks::mathblocks(double value) +{ + inputValue = value; +} + +double mathblocks::identity() const +{ + return inputValue; +} + +double mathblocks::addition(const double value) +{ + return inputValue += value; +} + +double mathblocks::multiplication(double value) +{ + return inputValue *= value; +} + +double mathblocks::power(double value) +{ + return inputValue = pow(inputValue, value); +} + +double mathblocks::limit(double min, double max) const +{ + if (inputValue < min) + { + return min; + } + if (inputValue > max) + { + return max; + } + return inputValue; +} + +double mathblocks::condition(double value) const +{ + if (inputValue < value) + { + return -1; + } + if (inputValue > value) + { + return 1; + } + return 0; +} + +void mathblocks::add_block(block block) +{ + sequence.push_back(block); +} + +void mathblocks::delete_block(int blockIndex) +{ + sequence.erase(sequence.begin()+blockIndex); +} + +void mathblocks::move_block_to_beginning(int blockIndex) +{ + std::move(sequence.begin() + blockIndex, sequence.begin() + blockIndex, sequence.begin()); +} + +void mathblocks::print_sequence() +{ + for(auto block:sequence) + { + + } +} + +double mathblocks::run_sequence() +{ + for (int i = 0; i < sequence.size(); ++i) + { + switch (sequence[i].blockType) + { + case blocktype::identity: + { + identity(); + break; + } + case blocktype::addition: + { + addition(sequence[i].parameters[0]); + break; + } + case blocktype::multiplication: + { + multiplication(sequence[i].parameters[0]); + break; + } + case blocktype::power: + { + power(sequence[i].parameters[0]); + break; + } + case blocktype::limit: + { + limit(sequence[i].parameters[0], sequence[i].parameters[1]); + break; + } + case blocktype::condition: + { + condition(sequence[i].parameters[0]); + break; + } + default:; + } + } +} diff --git a/FinalExercise/FinalExercise/mathblocks.h b/FinalExercise/FinalExercise/mathblocks.h new file mode 100644 index 0000000..c050213 --- /dev/null +++ b/FinalExercise/FinalExercise/mathblocks.h @@ -0,0 +1,35 @@ +#pragma once +#include + +enum class blocktype +{ + identity = 1, addition = 2, multiplication = 3, power = 4, limit = 5, condition = 6 +}; + +struct block +{ + blocktype blockType; + std::vector parameters; +}; + +class mathblocks +{ +public: + mathblocks() = default; + mathblocks(double value); + double identity() const; + double addition(double value); + double multiplication(double value); + double power(double value); + double limit(double min, double max) const; + double condition(double value) const; + void add_block(block block); + void delete_block(int blockIndex); + void move_block_to_beginning(int blockIndex); + void print_sequence(); + double run_sequence(); +private: + double inputValue; + std::vector sequence; +}; + diff --git a/FinalExercise/FinalExercise/pch.cpp b/FinalExercise/FinalExercise/pch.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a38f8a37da858b0e7a4a577210e923c883ac97f3 GIT binary patch literal 430 zcma)(OAdlS5JYQj;vFWsRoUwaJO;ubLXfO} ziM!*RK?b#&)PqF4LX~Q_GVq=g$>^{f<-{7@$r|ykmqm|MwtC_%l{t|TJS9Jpalvn} ztpC7C!Dg_+`-h+El+KlNtsDc|GOvyF+iK(A-j!r1ot!0T2mdlziM{46Ghg8k=#H+V zM?tLvNCnoo3Y&aHb%nm~4lL&e!^r!5 zY>)Yt7?F2lcmBR1+QK#-XGNr7XZ8fr!cn3lZ5^Y)vUSfD^AwDN*h^Lw&)lPx++47# zd)SAA*b{C}iSS;`84M~$v>AMrgP7FJ8+gu~LAukhS6*EOJ`b`OdG>3c0~t!sZmv>H zn*HLA@+(*ivq2}#QbRtnCS6sZB|bGHY13EaXpqYpyv1DpS76m1eTdT|r_t-78YQ1T z#<62u5HD*r_L~oQCI!E8@(t$Ve*GNp63pmTN&3$4QJrKBuD-fJIu~wfYrcy;aeoU( z{gpDyo!duDy%mVwzd3}ObITpQ?&R3*fD>2bk}TRUp!6E5gQ6*el=X_*#+Q5I8?Z>{ zRy|dAN-mZA&0pz!j#b}5FeyBGiKSg%o7_9|!S#V|#ttsxpDyDUbdaS$838t + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 15.0 + {D57382B1-FE9F-45E6-92E9-926D43932666} + Win32Proj + FinalExerciseTests + 10.0.17134.0 + NativeUnitTestProject + + + + DynamicLibrary + true + v141 + Unicode + false + + + DynamicLibrary + false + v141 + true + Unicode + false + + + DynamicLibrary + true + v141 + Unicode + false + + + DynamicLibrary + false + v141 + true + Unicode + false + + + + + + + + + + + + + + + + + + + + + true + + + true + + + true + + + true + + + + Use + Level3 + Disabled + $(VCInstallDir)UnitTest\include;%(AdditionalIncludeDirectories) + WIN32;_DEBUG;%(PreprocessorDefinitions) + true + pch.h + + + Windows + $(VCInstallDir)UnitTest\lib;%(AdditionalLibraryDirectories) + %(AdditionalDependencies) + + + + + Use + Level3 + Disabled + $(VCInstallDir)UnitTest\include;%(AdditionalIncludeDirectories) + _DEBUG;%(PreprocessorDefinitions) + true + + + Windows + $(VCInstallDir)UnitTest\lib;%(AdditionalLibraryDirectories) + + + + + Level3 + Use + MaxSpeed + true + true + $(VCInstallDir)UnitTest\include;%(AdditionalIncludeDirectories) + WIN32;NDEBUG;%(PreprocessorDefinitions) + true + + + Windows + true + true + $(VCInstallDir)UnitTest\lib;%(AdditionalLibraryDirectories) + + + + + Level3 + Use + MaxSpeed + true + true + $(VCInstallDir)UnitTest\include;%(AdditionalIncludeDirectories) + NDEBUG;%(PreprocessorDefinitions) + true + + + Windows + true + true + $(VCInstallDir)UnitTest\lib;%(AdditionalLibraryDirectories) + + + + + + + + + + + Create + Create + Create + Create + + + + + + {49a8615b-edc6-46f3-a8a2-f2d66bd85f9e} + + + + + + \ No newline at end of file diff --git a/FinalExercise/FinalExerciseTests/FinalExerciseTests.vcxproj.filters b/FinalExercise/FinalExerciseTests/FinalExerciseTests.vcxproj.filters new file mode 100644 index 0000000..96bb64b --- /dev/null +++ b/FinalExercise/FinalExerciseTests/FinalExerciseTests.vcxproj.filters @@ -0,0 +1,39 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Header Files + + + Header Files + + + Header Files + + + + + Source Files + + + Source Files + + + Source Files + + + \ No newline at end of file diff --git a/FinalExercise/FinalExerciseTests/mathblockstest.cpp b/FinalExercise/FinalExerciseTests/mathblockstest.cpp new file mode 100644 index 0000000..acfd4e8 --- /dev/null +++ b/FinalExercise/FinalExerciseTests/mathblockstest.cpp @@ -0,0 +1,82 @@ +#include "pch.h" +#include "CppUnitTest.h" +#include "../FinalExercise/mathblocks.h" + +using namespace Microsoft::VisualStudio::CppUnitTestFramework; + +namespace FinalExerciseTests +{ + TEST_CLASS(UnitTest1) + { + public: + + TEST_METHOD(TestIdentity) + { + mathblocks mb(12.5); + Assert::AreEqual(12.5, mb.identity()); + mathblocks mb2(5.0); + Assert::AreEqual(5.0, mb2.identity()); + mathblocks mb3(-7.2); + Assert::AreEqual(-7.2, mb3.identity()); + mathblocks mb4(0.0); + Assert::AreEqual(0.0, mb4.identity()); + } + + TEST_METHOD(TestAddition) + { + mathblocks mb(15); + Assert::AreEqual(20.0, mb.addition(5)); + mathblocks mb2(11.25); + Assert::AreEqual(12.5, mb2.addition(1.25)); + mathblocks mb3(-1.25); + Assert::AreEqual(-2.5, mb3.addition(-1.25)); + } + + TEST_METHOD(TestMultiplication) + { + mathblocks mb(7); + Assert::AreEqual(49.0, mb.multiplication(7)); + mathblocks mb2(2.5); + Assert::AreEqual(-7.5, mb2.multiplication(-3)); + mathblocks mb3(12.5); + Assert::AreEqual(12.5, mb3.multiplication(1)); + mathblocks mb4(17.5); + Assert::AreEqual(0.0, mb4.multiplication(0)); + } + + TEST_METHOD(TestPower) + { + mathblocks mb(5); + Assert::AreEqual(125.0, mb.power(3)); + mathblocks mb2(5); + Assert::AreEqual(25.0, mb2.power(2)); + mathblocks mb3(15); + Assert::AreEqual(1.0, mb3.power(0)); + } + + TEST_METHOD(TestLimit) + { + mathblocks mb(3); + Assert::AreEqual(5.0, mb.limit(5, 10)); + mathblocks mb2(7); + Assert::AreEqual(7.0, mb2.limit(5, 10)); + mathblocks mb3(15); + Assert::AreEqual(-5.0, mb3.limit(-10, -5)); + } + + TEST_METHOD(TestCondition) + { + mathblocks mb(1); + Assert::AreEqual(-1.0, mb.condition(9)); + mathblocks mb2(10.5); + Assert::AreEqual(1.0, mb2.condition(5.5)); + mathblocks mb3(7.5); + Assert::AreEqual(0.0, mb3.condition(7.5)); + } + + TEST_METHOD(TestSequence) + { + + } + }; +} \ No newline at end of file diff --git a/FinalExercise/FinalExerciseTests/pch.h b/FinalExercise/FinalExerciseTests/pch.h new file mode 100644 index 0000000..5105641 --- /dev/null +++ b/FinalExercise/FinalExerciseTests/pch.h @@ -0,0 +1,13 @@ +// pch.h : include file for standard system include files, +// or project specific include files that are used frequently, but +// are changed infrequently +// + +#pragma once + +#include "targetver.h" + +// Headers for CppUnitTest +#include "CppUnitTest.h" + +// TODO: reference additional headers your program requires here diff --git a/FinalExercise/FinalExerciseTests/stdafx.cpp b/FinalExercise/FinalExerciseTests/stdafx.cpp new file mode 100644 index 0000000..5669aba --- /dev/null +++ b/FinalExercise/FinalExerciseTests/stdafx.cpp @@ -0,0 +1,8 @@ +// stdafx.cpp : source file that includes just the standard includes +// FinalExerciseTests.pch will be the pre-compiled header +// stdafx.obj will contain the pre-compiled type information + +#include "pch.h" + +// TODO: reference any additional headers you need in STDAFX.H +// and not in this file diff --git a/FinalExercise/FinalExerciseTests/targetver.h b/FinalExercise/FinalExerciseTests/targetver.h new file mode 100644 index 0000000..87c0086 --- /dev/null +++ b/FinalExercise/FinalExerciseTests/targetver.h @@ -0,0 +1,8 @@ +#pragma once + +// Including SDKDDKVer.h defines the highest available Windows platform. + +// If you wish to build your application for a previous Windows platform, include WinSDKVer.h and +// set the _WIN32_WINNT macro to the platform you wish to support before including SDKDDKVer.h. + +#include From b28f415a7f3563c4bf06a883725db443d7c9348d Mon Sep 17 00:00:00 2001 From: Lucano Zganec Date: Thu, 22 Nov 2018 16:53:02 +0100 Subject: [PATCH 2/5] Base tests and command line UI implemented TODO: - save/read sequence from a file --- FinalExercise/FinalExercise/FinalExercise.cpp | Bin 1720 -> 8362 bytes FinalExercise/FinalExercise/mathblocks.cpp | 192 +++++++++++------- FinalExercise/FinalExercise/mathblocks.h | 106 ++++++++-- .../FinalExerciseTests/mathblockstest.cpp | 77 ++++--- 4 files changed, 242 insertions(+), 133 deletions(-) diff --git a/FinalExercise/FinalExercise/FinalExercise.cpp b/FinalExercise/FinalExercise/FinalExercise.cpp index 1ebb4092cc2ec82a7d272ab571941568c7fb9e05..022b07cbdfb09a896c54ca616b673e8450af6be3 100644 GIT binary patch literal 8362 zcmd6tYi|=r6o%)kEA>BYQ>g^eCfq+jibNrxNNqtS6<;bvjsuQM9H+4ZREWRc_I>8; z@vPUo%b2)TWjS82XU^q4*FBTJ|Jn}Qp$JdHD4d3VI0?JqeRvhl!$5bYy4up!nO;S> zr}ut1j`xf3TAwHSz0mIy?VV{iG^V;U(<*+s62|4-EBf|2T_O{FTZQJs)>Ru32T(bbBopy}BgvBlNcvXyM^Ur0h{-^l zr+OVk4bCDWn~ik7kyc|-{#KvJG1KRTc4EaneYd?=!~Lj3JD!?}<;6=l%+L)WkL4dnAAN5`@7J&})aU4$Kd{;50A z!EdSQ|3|TF}*p3 zWv{XoaOp|fnf<UStF!7(90LcO$%%9w+g>VS+v30dB{-s^d@8Ux@FCDB{QXwr3Tn zkK_ zQ|zuz^JYo&am2x*<8l;ll@x!Hw!}!48d!nva=ffA`R$Sqnc6)mA+c<^)j8ZLIqXUg zD$vC|n(AJ$I_>Lo+V3Sj9=TX?x+u2$P1WmR?z6#%K-F4*=L*ipZ02JDD z8XByliowN^p2j$z2}g2BldP8Mdh;Cv`4(PPgd55Yb-UHkOBL~Ot}7@X#(dLP6*v9C z&1fb1H#6whYvt-(Ze?)EtD>v(zTLo^K4dF;q2-FE%IaQ|i{zaKK6OiV)$v@-u*Pfo z$AxmxOgzb-*8kP*oa67wcdW*xoH-5Nu`SkN&#uey>0Xv9SW|^1YAq5HFQS=ROdT!M zeQ;gq+ep4k6j+@wSuay7_e7tnO4m*Y)Rk8)QtDOLE2LFjsMu4&>XcGtG)s=;k7!J6 zem>Py2k9=z->XaJmF`DLGmc2DPUEhuMtAZ^wxo;6L#n-&)vZyjPgOtS*~BYUlRxHE zDC4#6tZg~Re3)m1)FG#Mq0_|U$w$Y!Ca#av_n}qQZ>#$ssG@ZJP9M$7(O6$ya9z6k z{~c*Tf12!HmmOSIl`f~AQk=@d6J$fk}RUvAy-bd%9D$nWm#e! za^20SqIq&rdloa#=gYU=nhUku*#o)gL)o>ZJh5C?+w&n9Ft73|TIKGU(lXv^l|3hu z^3+bJRpnW=LUS){nJTAy$?F+&JDc_3>oPgs%TKre73wZY74fba)pAGYc{#@xUA^tP zEpV!=jc3{Osdb0OAJlECTYoC2$D|4!&-;Ua&+EH9>4l`O&hUy@<)>vGGT-n%$fBnm z*(Qtm-XsH^19PN$T>YQtg3Wo42@;d}i7?xdPCO^jNxg~_2Qrd9yB}y3*&hs;#Lsl) z{ZDn5O`_ZO_>P2RJ@5W7@8W!B%3Lw^b_e3^^HrZQn!WLWQE7SRDQ>l9ip$y@M7rp2 zlk`O0s$=T6m{k?Rg>KShvANEsFu7XcoU~l@7mE^Rmdt`SMT40sbGwn=HetD6eq)OI zc^W|BnqDKxWE?+~pZRmDWgV-?BJDN3nVULVJWINj7x`4HTbEy@-Ce6VMZwGH>dD4^ z`RAM1x3_z&=U$)M7%G+#yaP&}hBbYz-S!l*Y#E*%mhbXB(JmKWlb$D1Wv~;vU!q6i zZ`Bhsvf3lbcOHEaD?AB2WtvYtR{SzYALp1#GrYoII6GGrd3(8Iw*t(Sb+WUFh2lc}Nj{6%Ctr zSPgq2y_q!oJfMi@p3DhA&Z?iCA&}LW{#Pd@+d_uy*OvrP^|R1vjrYw)-uv1^zR2Zi zIf_#VGPFxFjh5q_771((et=$!2;6Fv;#t(*GPG52YFSlR5QBC)iNUYH%y;=&>_qp# z75m^boLd?;bwqAA%Cf7SNX~v{BSc)YOmFq8oyNe;_SAXYi9DW)o@4bquKZm)4MAez zp|oWt_H#*ZPdquz^F0IY!A{&wXF%Y56z?RPb2eqNck$s|-86I0t#h>8V`t!tE1S5w zoZgjLUT_^3J>YIcWKSN;E{gz8<7axKrJb&U*F?4@=4~BPc$_=sB3jnY`q?+E;QT}y g+W!QA@UuBBzSXgD-@(h+Q(%qo+6xH$xq7;+f$8Il>Y8A2E;844IuC)W!rVv25_EG^E;!pp$L F002{{54HdR diff --git a/FinalExercise/FinalExercise/mathblocks.cpp b/FinalExercise/FinalExercise/mathblocks.cpp index 147a8e0..bcc2725 100644 --- a/FinalExercise/FinalExercise/mathblocks.cpp +++ b/FinalExercise/FinalExercise/mathblocks.cpp @@ -1,118 +1,166 @@ #include "pch.h" #include "mathblocks.h" -#include +#include +#include -mathblocks::mathblocks(double value) -{ - inputValue = value; -} +const std::string identity_block::block_name = "Identity"; +const std::string addition_block::block_name = "Addition"; +const std::string multiplication_block::block_name = "Multiplication"; +const std::string power_block::block_name = "Power"; +const std::string limit_block::block_name = "Limit"; +const std::string condition_block::block_name = "Condition"; -double mathblocks::identity() const +void mathblocks::add_block(std::shared_ptr block) { - return inputValue; + sequence.push_back(block); } -double mathblocks::addition(const double value) +void mathblocks::delete_block(int blockIndex) { - return inputValue += value; + sequence.erase(sequence.begin()+blockIndex); } -double mathblocks::multiplication(double value) +void mathblocks::move_block_to_beginning(int blockIndex) { - return inputValue *= value; + std::move(sequence.begin() + blockIndex, sequence.begin() + blockIndex, sequence.begin()); } -double mathblocks::power(double value) +void mathblocks::print_sequence() { - return inputValue = pow(inputValue, value); + for(auto& block : sequence) + { + std::cout << block->get_block_name() << "\t"; + } + + std::cout << std::endl; } -double mathblocks::limit(double min, double max) const +double mathblocks::run_sequence(double value) { - if (inputValue < min) - { - return min; - } - if (inputValue > max) + double lastValue = value; + + for (auto& block : sequence) { - return max; + lastValue = block->evaluate(lastValue); } - return inputValue; + + return lastValue; +} + +double identity_block::evaluate(double input) +{ + return input; } -double mathblocks::condition(double value) const +std::string identity_block::get_block_name() { - if (inputValue < value) + return block_name; +} + +addition_block::addition_block(std::vector evaluation_constants) +{ + std::swap(this->evaluation_constants, evaluation_constants); +} + +double addition_block::evaluate(double input) +{ + for(auto value : evaluation_constants) { - return -1; + input += value; } - if (inputValue > value) + + return input; +} + +std::string addition_block::get_block_name() +{ + return block_name; +} + +multiplication_block::multiplication_block(std::vector evaluation_constants) +{ + std::swap(this->evaluation_constants, evaluation_constants); +} + +double multiplication_block::evaluate(double input) +{ + for (auto value : evaluation_constants) { - return 1; + input *= value; } - return 0; + + return input; } -void mathblocks::add_block(block block) +std::string multiplication_block::get_block_name() { - sequence.push_back(block); + return block_name; } -void mathblocks::delete_block(int blockIndex) +power_block::power_block(std::vector evaluation_constants) { - sequence.erase(sequence.begin()+blockIndex); + std::swap(this->evaluation_constants, evaluation_constants); } -void mathblocks::move_block_to_beginning(int blockIndex) +double power_block::evaluate(double input) { - std::move(sequence.begin() + blockIndex, sequence.begin() + blockIndex, sequence.begin()); + for (auto value : evaluation_constants) + { + input = pow(input, value); + } + + return input; } -void mathblocks::print_sequence() +std::string power_block::get_block_name() +{ + return block_name; +} + +limit_block::limit_block(std::vector evaluation_constants) { - for(auto block:sequence) + std::swap(this->evaluation_constants, evaluation_constants); +} + +double limit_block::evaluate(double input) +{ + if (input < evaluation_constants[0]) { - + return evaluation_constants[0]; } + if (input > evaluation_constants[1]) + { + return evaluation_constants[1]; + } + + return input; +} + +std::string limit_block::get_block_name() +{ + return block_name; } -double mathblocks::run_sequence() +condition_block::condition_block(std::vector evaluation_constants) { - for (int i = 0; i < sequence.size(); ++i) + std::swap(this->evaluation_constants, evaluation_constants); +} + +double condition_block::evaluate(double input) +{ + if (input < evaluation_constants[0]) { - switch (sequence[i].blockType) - { - case blocktype::identity: - { - identity(); - break; - } - case blocktype::addition: - { - addition(sequence[i].parameters[0]); - break; - } - case blocktype::multiplication: - { - multiplication(sequence[i].parameters[0]); - break; - } - case blocktype::power: - { - power(sequence[i].parameters[0]); - break; - } - case blocktype::limit: - { - limit(sequence[i].parameters[0], sequence[i].parameters[1]); - break; - } - case blocktype::condition: - { - condition(sequence[i].parameters[0]); - break; - } - default:; - } + return -1; } + if (input > evaluation_constants[0]) + { + return 1; + } + + return 0; +} + +std::string condition_block::get_block_name() +{ + return block_name; } diff --git a/FinalExercise/FinalExercise/mathblocks.h b/FinalExercise/FinalExercise/mathblocks.h index c050213..cae5f49 100644 --- a/FinalExercise/FinalExercise/mathblocks.h +++ b/FinalExercise/FinalExercise/mathblocks.h @@ -1,35 +1,99 @@ #pragma once #include +#include -enum class blocktype +class base_block; + +class mathblocks { - identity = 1, addition = 2, multiplication = 3, power = 4, limit = 5, condition = 6 +public: + void add_block(std::shared_ptr block); + void delete_block(int blockIndex); + void move_block_to_beginning(int blockIndex); + void print_sequence(); + double run_sequence(double value); +private: + std::vector> sequence; }; -struct block +class base_block { - blocktype blockType; - std::vector parameters; +public: + virtual ~base_block() = default; + virtual double evaluate(double input) = 0; + virtual std::string get_block_name() = 0; }; -class mathblocks +class identity_block : public base_block { public: - mathblocks() = default; - mathblocks(double value); - double identity() const; - double addition(double value); - double multiplication(double value); - double power(double value); - double limit(double min, double max) const; - double condition(double value) const; - void add_block(block block); - void delete_block(int blockIndex); - void move_block_to_beginning(int blockIndex); - void print_sequence(); - double run_sequence(); + static const std::string block_name; + + double evaluate(double input) override; + std::string get_block_name() override; +}; + +class addition_block : public base_block +{ +public: + static const int constants_amount = 1; + static const std::string block_name; + + addition_block(std::vector evaluation_constants); + double evaluate(double input) override; + std::string get_block_name() override; private: - double inputValue; - std::vector sequence; + std::vector evaluation_constants; }; +class multiplication_block : public base_block +{ +public: + static const int constants_amount = 1; + static const std::string block_name; + + multiplication_block(std::vector evaluation_constants); + double evaluate(double input) override; + std::string get_block_name() override; +private: + std::vector evaluation_constants; +}; + +class power_block : public base_block +{ +public: + static const int constants_amount = 1; + static const std::string block_name; + + power_block(std::vector evaluation_constants); + double evaluate(double input) override; + std::string get_block_name() override; +private: + std::vector evaluation_constants; +}; + +class limit_block : public base_block +{ +public: + static const int constants_amount = 1; + static const std::string block_name; + + limit_block(std::vector evaluation_constants); + double evaluate(double input) override; + std::string get_block_name() override; +private: + std::vector evaluation_constants; +}; + +class condition_block : public base_block +{ +public: + static const int constants_amount = 1; + static const std::string block_name; + + condition_block(std::vector evaluation_constants); + double evaluate(double input) override; + std::string get_block_name() override; +private: + std::vector evaluation_constants; +}; diff --git a/FinalExercise/FinalExerciseTests/mathblockstest.cpp b/FinalExercise/FinalExerciseTests/mathblockstest.cpp index acfd4e8..18e168e 100644 --- a/FinalExercise/FinalExerciseTests/mathblockstest.cpp +++ b/FinalExercise/FinalExerciseTests/mathblockstest.cpp @@ -12,66 +12,63 @@ namespace FinalExerciseTests TEST_METHOD(TestIdentity) { - mathblocks mb(12.5); - Assert::AreEqual(12.5, mb.identity()); - mathblocks mb2(5.0); - Assert::AreEqual(5.0, mb2.identity()); - mathblocks mb3(-7.2); - Assert::AreEqual(-7.2, mb3.identity()); - mathblocks mb4(0.0); - Assert::AreEqual(0.0, mb4.identity()); + identity_block ib; + Assert::AreEqual(12.5, ib.evaluate(12.5)); + Assert::AreEqual(5.0, ib.evaluate(5.0)); + Assert::AreEqual(-7.2, ib.evaluate(-7.2)); + Assert::AreEqual(0.0, ib.evaluate(0.0)); } TEST_METHOD(TestAddition) { - mathblocks mb(15); - Assert::AreEqual(20.0, mb.addition(5)); - mathblocks mb2(11.25); - Assert::AreEqual(12.5, mb2.addition(1.25)); - mathblocks mb3(-1.25); - Assert::AreEqual(-2.5, mb3.addition(-1.25)); + addition_block ab(std::vector{15.0}); + Assert::AreEqual(20.0, ab.evaluate(5.0)); + addition_block ab2(std::vector{11.25}); + Assert::AreEqual(12.5, ab2.evaluate(1.25)); + addition_block ab3(std::vector{-1.25}); + Assert::AreEqual(-2.5, ab3.evaluate(-1.25)); } TEST_METHOD(TestMultiplication) { - mathblocks mb(7); - Assert::AreEqual(49.0, mb.multiplication(7)); - mathblocks mb2(2.5); - Assert::AreEqual(-7.5, mb2.multiplication(-3)); - mathblocks mb3(12.5); - Assert::AreEqual(12.5, mb3.multiplication(1)); - mathblocks mb4(17.5); - Assert::AreEqual(0.0, mb4.multiplication(0)); + multiplication_block mb(std::vector{7}); + Assert::AreEqual(49.0, mb.evaluate(7)); + multiplication_block mb2(std::vector{2.5}); + Assert::AreEqual(-7.5, mb2.evaluate(-3)); + multiplication_block mb3(std::vector{12.5}); + Assert::AreEqual(12.5, mb3.evaluate(1)); + multiplication_block mb4(std::vector{17.5}); + Assert::AreEqual(0.0, mb4.evaluate(0)); } TEST_METHOD(TestPower) { - mathblocks mb(5); - Assert::AreEqual(125.0, mb.power(3)); - mathblocks mb2(5); - Assert::AreEqual(25.0, mb2.power(2)); - mathblocks mb3(15); - Assert::AreEqual(1.0, mb3.power(0)); + power_block pb(std::vector{3}); + Assert::AreEqual(125.0, pb.evaluate(5)); + power_block pb2(std::vector{2}); + Assert::AreEqual(25.0, pb2.evaluate(5)); + power_block pb3(std::vector{0}); + Assert::AreEqual(1.0, pb3.evaluate(15)); } TEST_METHOD(TestLimit) { - mathblocks mb(3); - Assert::AreEqual(5.0, mb.limit(5, 10)); - mathblocks mb2(7); - Assert::AreEqual(7.0, mb2.limit(5, 10)); - mathblocks mb3(15); - Assert::AreEqual(-5.0, mb3.limit(-10, -5)); + limit_block lb(std::vector{5,10}); + Assert::AreEqual(5.0, lb.evaluate(3)); + limit_block lb2(std::vector{5, 10}); + Assert::AreEqual(7.0, lb2.evaluate(7)); + limit_block lb3(std::vector{-10, -5}); + Assert::AreEqual(-5.0, lb3.evaluate(15)); } TEST_METHOD(TestCondition) { - mathblocks mb(1); - Assert::AreEqual(-1.0, mb.condition(9)); - mathblocks mb2(10.5); - Assert::AreEqual(1.0, mb2.condition(5.5)); - mathblocks mb3(7.5); - Assert::AreEqual(0.0, mb3.condition(7.5)); + condition_block cb(std::vector{9}); + Assert::AreEqual(-1.0, cb.evaluate(1)); + condition_block cb2(std::vector{5.5}); + Assert::AreEqual(1.0, cb2.evaluate(10.5)); + condition_block cb3(std::vector{7.5}); + Assert::AreEqual(0.0, cb3.evaluate(7.5)); } TEST_METHOD(TestSequence) From bfaa737d611ae474404d8d094ff86f4f5af1dc2b Mon Sep 17 00:00:00 2001 From: Lucano Zganec Date: Fri, 23 Nov 2018 15:52:15 +0100 Subject: [PATCH 3/5] Implemented save/load from a text file, added some new tests --- FinalExercise/FinalExercise/FinalExercise.cpp | Bin 8362 -> 9968 bytes FinalExercise/FinalExercise/mathblocks.cpp | 212 +++++++++++++++++- FinalExercise/FinalExercise/mathblocks.h | 53 ++++- .../FinalExerciseTests/mathblockstest.cpp | 33 ++- 4 files changed, 275 insertions(+), 23 deletions(-) diff --git a/FinalExercise/FinalExercise/FinalExercise.cpp b/FinalExercise/FinalExercise/FinalExercise.cpp index 022b07cbdfb09a896c54ca616b673e8450af6be3..95eae6e10b9f32dccc4e3ba8bf43330d6b90bac2 100644 GIT binary patch delta 1086 zcmbtT%}x_h7(KMmaca~+nG$KhDTL68h!Tx4RBJTG4a&+0tQsvXHqw^NOo&8GAHWs) z*s^iyie&c27rH6Ep^j}-<#hPA*GVl5OJ{U3Ud#KQ z-n~#XQNUA1+Hl}9tEzICUq!QPzhP^K=XKhfJblOg1Fl=J< zKaJ_3HTiLIR&&^Im#D4m)QU?cChdp|9&%s62KKQE3o|I<64KC7fx>Ci8|32&u~vxM z;cB5N?M%Ksz!y~UE7hr+>MR#-w{#%R+^H=?BInZX?eP`$R=*;vP<@KwyTl{_iYUZ #include +#include const std::string identity_block::block_name = "Identity"; const std::string addition_block::block_name = "Addition"; @@ -10,6 +11,110 @@ const std::string power_block::block_name = "Power"; const std::string limit_block::block_name = "Limit"; const std::string condition_block::block_name = "Condition"; +template +std::shared_ptr block_factory::create_block(std::vector& evaluationParameters) +{ + if (evaluationParameters.size() == 0) + { + return std::make_shared(BlockType()); + } + + return std::make_shared(BlockType(evaluationParameters)); +} + +void block_saver::save_blocks_to_file(const std::string& filePath, mathblocks& mathblocks) +{ + std::ofstream file; + file.open(filePath); + + if (file.is_open()) + { + auto sequence = mathblocks.get_sequence(); + for (auto& block : sequence) + { + file << block->get_block_name() << "="; + auto evaluation_constants = block->get_evaluation_constants(); + for (size_t i = 0; i < evaluation_constants.size(); ++i) + { + if (i == 0) + { + file << evaluation_constants[i]; + } + else + { + file << "," << evaluation_constants[i]; + } + } + file << std::endl; + } + + file.close(); + } +} + +void block_saver::read_blocks_from_file(const std::string& filePath, mathblocks& mathblocks) +{ + std::ifstream file; + file.open(filePath); + + if (file.is_open()) + { + std::string line; + + while (std::getline(file, line)) + { + //Extract Block Name + const std::size_t nameDelimiterIndex = line.find_first_of("="); + std::string blockName = line.substr(0, nameDelimiterIndex); + + //Extract Block Evaluation Parameters + std::string evaluationParametersStr = line.substr(nameDelimiterIndex + 1, line.length()); + std::vector evaluationParameters; + + if (!evaluationParametersStr.empty()) + { + std::size_t lastDelimiterIndex = 0; + std::size_t delimiterIndex; + while ((delimiterIndex = evaluationParametersStr.find_first_of(",", lastDelimiterIndex)) != std::string::npos) + { + double parameter = std::stod(evaluationParametersStr.substr(lastDelimiterIndex, delimiterIndex)); + evaluationParameters.push_back(parameter); + lastDelimiterIndex = delimiterIndex; + } + double parameter = std::stod(evaluationParametersStr.substr(lastDelimiterIndex, evaluationParametersStr.length())); + evaluationParameters.push_back(parameter); + } + + if (blockName._Equal(identity_block::block_name)) + { + mathblocks.add_block(block_factory::create_block(evaluationParameters)); + } + else if (blockName._Equal(addition_block::block_name)) + { + mathblocks.add_block(block_factory::create_block(evaluationParameters)); + } + else if (blockName._Equal(multiplication_block::block_name)) + { + mathblocks.add_block(block_factory::create_block(evaluationParameters)); + } + else if (blockName._Equal(power_block::block_name)) + { + mathblocks.add_block(block_factory::create_block(evaluationParameters)); + } + else if (blockName._Equal(limit_block::block_name)) + { + mathblocks.add_block(block_factory::create_block(evaluationParameters)); + } + else if (blockName._Equal(condition_block::block_name)) + { + mathblocks.add_block(block_factory::create_block(evaluationParameters)); + } + } + + file.close(); + } +} + void mathblocks::add_block(std::shared_ptr block) { sequence.push_back(block); @@ -17,19 +122,60 @@ void mathblocks::add_block(std::shared_ptr block) void mathblocks::delete_block(int blockIndex) { - sequence.erase(sequence.begin()+blockIndex); + try + { + if (sequence.at(blockIndex)) + { + sequence.erase(sequence.begin() + blockIndex); + } + } + catch (const std::exception& e) + { + std::cout << "There are no blocks at given index"; + } } void mathblocks::move_block_to_beginning(int blockIndex) { - std::move(sequence.begin() + blockIndex, sequence.begin() + blockIndex, sequence.begin()); + try + { + if (sequence.at(blockIndex)) + { + std::move(sequence.begin() + blockIndex, sequence.begin() + blockIndex, sequence.begin()); + } + } + catch (const std::exception& e) + { + std::cout << "There are no blocks at given index"; + } } void mathblocks::print_sequence() { - for(auto& block : sequence) + if (sequence.size() == 0) { - std::cout << block->get_block_name() << "\t"; + std::cout << "No blocks in the sequence" << std::endl; + return; + } + + for (auto& block : sequence) + { + std::cout << block->get_block_name() << "("; + std::vector evaluation_constants = block->get_evaluation_constants(); + + for(size_t i = 0; i < evaluation_constants.size(); ++i) + { + if (i == 0) + { + std::cout << evaluation_constants[i]; + } + else + { + std::cout << "," << evaluation_constants[i]; + } + } + + std::cout << ")" << std::endl; } std::cout << std::endl; @@ -47,24 +193,45 @@ double mathblocks::run_sequence(double value) return lastValue; } +int mathblocks::get_sequence_size() const +{ + return sequence.size(); +} + +std::vector> mathblocks::get_sequence() const +{ + return sequence; +} + +identity_block::identity_block(std::vector& evaluation_constants) +{ + //Do nothing, used only for template block_factory::create_block + //TODO: Look into "constexpr if" for possible solution to avoid this +} + double identity_block::evaluate(double input) { return input; } +std::vector identity_block::get_evaluation_constants() +{ + return std::vector{}; +} + std::string identity_block::get_block_name() { return block_name; } -addition_block::addition_block(std::vector evaluation_constants) +addition_block::addition_block(std::vector& evaluation_constants) { std::swap(this->evaluation_constants, evaluation_constants); } double addition_block::evaluate(double input) { - for(auto value : evaluation_constants) + for (auto value : evaluation_constants) { input += value; } @@ -72,12 +239,17 @@ double addition_block::evaluate(double input) return input; } +std::vector addition_block::get_evaluation_constants() +{ + return evaluation_constants; +} + std::string addition_block::get_block_name() { return block_name; } -multiplication_block::multiplication_block(std::vector evaluation_constants) +multiplication_block::multiplication_block(std::vector& evaluation_constants) { std::swap(this->evaluation_constants, evaluation_constants); } @@ -92,12 +264,17 @@ double multiplication_block::evaluate(double input) return input; } +std::vector multiplication_block::get_evaluation_constants() +{ + return evaluation_constants; +} + std::string multiplication_block::get_block_name() { return block_name; } -power_block::power_block(std::vector evaluation_constants) +power_block::power_block(std::vector& evaluation_constants) { std::swap(this->evaluation_constants, evaluation_constants); } @@ -112,12 +289,17 @@ double power_block::evaluate(double input) return input; } +std::vector power_block::get_evaluation_constants() +{ + return evaluation_constants; +} + std::string power_block::get_block_name() { return block_name; } -limit_block::limit_block(std::vector evaluation_constants) +limit_block::limit_block(std::vector& evaluation_constants) { std::swap(this->evaluation_constants, evaluation_constants); } @@ -136,12 +318,17 @@ double limit_block::evaluate(double input) return input; } +std::vector limit_block::get_evaluation_constants() +{ + return evaluation_constants; +} + std::string limit_block::get_block_name() { return block_name; } -condition_block::condition_block(std::vector evaluation_constants) +condition_block::condition_block(std::vector& evaluation_constants) { std::swap(this->evaluation_constants, evaluation_constants); } @@ -160,6 +347,11 @@ double condition_block::evaluate(double input) return 0; } +std::vector condition_block::get_evaluation_constants() +{ + return evaluation_constants; +} + std::string condition_block::get_block_name() { return block_name; diff --git a/FinalExercise/FinalExercise/mathblocks.h b/FinalExercise/FinalExercise/mathblocks.h index cae5f49..a185320 100644 --- a/FinalExercise/FinalExercise/mathblocks.h +++ b/FinalExercise/FinalExercise/mathblocks.h @@ -2,8 +2,23 @@ #include #include +class mathblocks; class base_block; +class block_factory +{ +public: + template + static std::shared_ptr create_block(std::vector& evaluationParameters); +}; + +class block_saver +{ +public: + static void save_blocks_to_file(const std::string& filePath, mathblocks& mathblocks); + static void read_blocks_from_file(const std::string& filePath, mathblocks& mathblocks); +}; + class mathblocks { public: @@ -12,6 +27,8 @@ class mathblocks void move_block_to_beginning(int blockIndex); void print_sequence(); double run_sequence(double value); + int get_sequence_size() const; + std::vector> get_sequence() const; private: std::vector> sequence; }; @@ -21,78 +38,92 @@ class base_block public: virtual ~base_block() = default; virtual double evaluate(double input) = 0; + virtual std::vector get_evaluation_constants() = 0; virtual std::string get_block_name() = 0; }; -class identity_block : public base_block +class identity_block final : public base_block { public: static const std::string block_name; + identity_block() = default; + identity_block(std::vector& evaluation_constants); double evaluate(double input) override; + std::vector get_evaluation_constants() override; std::string get_block_name() override; }; -class addition_block : public base_block +class addition_block final : public base_block { public: static const int constants_amount = 1; static const std::string block_name; - addition_block(std::vector evaluation_constants); + addition_block() = default; + addition_block(std::vector& evaluation_constants); double evaluate(double input) override; + std::vector get_evaluation_constants() override; std::string get_block_name() override; private: std::vector evaluation_constants; }; -class multiplication_block : public base_block +class multiplication_block final : public base_block { public: static const int constants_amount = 1; static const std::string block_name; - multiplication_block(std::vector evaluation_constants); + multiplication_block() = default; + multiplication_block(std::vector& evaluation_constants); double evaluate(double input) override; + std::vector get_evaluation_constants() override; std::string get_block_name() override; private: std::vector evaluation_constants; }; -class power_block : public base_block +class power_block final : public base_block { public: static const int constants_amount = 1; static const std::string block_name; - power_block(std::vector evaluation_constants); + power_block() = default; + power_block(std::vector& evaluation_constants); double evaluate(double input) override; + std::vector get_evaluation_constants() override; std::string get_block_name() override; private: std::vector evaluation_constants; }; -class limit_block : public base_block +class limit_block final : public base_block { public: static const int constants_amount = 1; static const std::string block_name; - limit_block(std::vector evaluation_constants); + limit_block() = default; + limit_block(std::vector& evaluation_constants); double evaluate(double input) override; + std::vector get_evaluation_constants() override; std::string get_block_name() override; private: std::vector evaluation_constants; }; -class condition_block : public base_block +class condition_block final : public base_block { public: static const int constants_amount = 1; static const std::string block_name; - condition_block(std::vector evaluation_constants); + condition_block() = default; + condition_block(std::vector& evaluation_constants); double evaluate(double input) override; + std::vector get_evaluation_constants() override; std::string get_block_name() override; private: std::vector evaluation_constants; diff --git a/FinalExercise/FinalExerciseTests/mathblockstest.cpp b/FinalExercise/FinalExerciseTests/mathblockstest.cpp index 18e168e..dc91bfa 100644 --- a/FinalExercise/FinalExerciseTests/mathblockstest.cpp +++ b/FinalExercise/FinalExerciseTests/mathblockstest.cpp @@ -71,9 +71,38 @@ namespace FinalExerciseTests Assert::AreEqual(0.0, cb3.evaluate(7.5)); } - TEST_METHOD(TestSequence) + TEST_METHOD(TestAddBlock) { - + mathblocks mb; + identity_block ib; + mb.add_block(std::make_shared(ib)); + Assert::AreEqual(1, mb.get_sequence_size()); + } + + TEST_METHOD(TestRunSequence) + { + mathblocks mb; + addition_block ab(std::vector{5.0}); + identity_block ib; + power_block pb(std::vector{2.0}); + mb.add_block(std::make_shared(ab)); + mb.add_block(std::make_shared(pb)); + mb.add_block(std::make_shared(ib)); + Assert::AreEqual(49.0, mb.run_sequence(2)); + } + + TEST_METHOD(TestDeleteBlock) + { + mathblocks mb; + addition_block ab(std::vector{5.0}); + identity_block ib; + power_block pb(std::vector{2.0}); + mb.add_block(std::make_shared(ab)); + mb.add_block(std::make_shared(pb)); + mb.add_block(std::make_shared(ib)); + mb.delete_block(0); + Assert::AreEqual(2, mb.get_sequence_size()); + Assert::AreEqual(25.0, mb.run_sequence(5)); } }; } \ No newline at end of file From 9bf7cf9038c3d75d0c3e40506231594cd9a30c79 Mon Sep 17 00:00:00 2001 From: Lucano Zganec Date: Fri, 23 Nov 2018 15:58:35 +0100 Subject: [PATCH 4/5] Added test for sequence save/load --- FinalExercise/FinalExercise/FinalExercise.cpp | Bin 9968 -> 9968 bytes FinalExercise/FinalExercise/mathblocks.cpp | 2 +- FinalExercise/FinalExercise/mathblocks.h | 2 +- .../FinalExerciseTests/mathblockstest.cpp | 18 ++++++++++++++++++ 4 files changed, 20 insertions(+), 2 deletions(-) diff --git a/FinalExercise/FinalExercise/FinalExercise.cpp b/FinalExercise/FinalExercise/FinalExercise.cpp index 95eae6e10b9f32dccc4e3ba8bf43330d6b90bac2..7d15e7cadee6f1d8c2ccebb1e93f2bd6a168104e 100644 GIT binary patch delta 16 Xcmez1`@wgEnJjY-L;hw<+3lPFJeCF} delta 16 Xcmez1`@wgEnJjY=L+WNr+3lPFJc{5.0}); + identity_block ib; + power_block pb(std::vector{2.0}); + mb.add_block(std::make_shared(ab)); + mb.add_block(std::make_shared(pb)); + mb.add_block(std::make_shared(ib)); + Assert::AreEqual(3, mb.get_sequence_size()); + Assert::AreEqual(100.0, mb.run_sequence(5)); + block_saver::save_blocks_to_file("test", mb); + mathblocks mb2; + block_saver::load_blocks_from_file("test", mb2); + Assert::AreEqual(3, mb2.get_sequence_size()); + Assert::AreEqual(100.0, mb2.run_sequence(5)); + } }; } \ No newline at end of file From a2b28d3d82c15d5cdde7b2871be35a13ad112c35 Mon Sep 17 00:00:00 2001 From: Lucano Zganec Date: Fri, 7 Dec 2018 16:37:25 +0100 Subject: [PATCH 5/5] Fixed move blocks not working and limit taking only a single parameter, added sequence evaluation with multiple values from a text file (rough version) --- FinalExercise/FinalExercise/FinalExercise.cpp | Bin 9968 -> 11880 bytes FinalExercise/FinalExercise/mathblocks.cpp | 3 ++- FinalExercise/FinalExercise/mathblocks.h | 2 +- .../FinalExerciseTests/mathblockstest.cpp | 13 +++++++++++++ 4 files changed, 16 insertions(+), 2 deletions(-) diff --git a/FinalExercise/FinalExercise/FinalExercise.cpp b/FinalExercise/FinalExercise/FinalExercise.cpp index 7d15e7cadee6f1d8c2ccebb1e93f2bd6a168104e..cd5191f1d16a5ae95b88e4173b8fb5c996d7b32a 100644 GIT binary patch delta 979 zcmZ`&OG_J36h32O%*1DWkjZ;SMI9(gQ4qAabk`QajkL6cn6$x3Mlz{=KnvY;*Ntc6 zq6PIA2=fcN>ZZ7GDM;(Ci!SsZ^tmIZv9o?W+-6Hm=MSzWDe7Om4TrAY3o_0Y5kZTryBqJ<#F zKl9p>-o->~g)!Zjz|?$<%O$j{;kurdMSLxK8DiFvH;18O-}CtXTz;q)#d+))$Z0{l z+ndWN(_5T^bCDcw;oUB+VZ1_S2R*u&P`6(81LS*A-pgl4BTD$YqL+heVrHVUsN+`D zN#~QY_DqJ$A04-SMg9#5Jg+)lTTrw>zGng#;l%0mWb%oc;hUi`{-BJT;?`6##Qm}0 zw75eCy+;>qc delta 52 zcmV-40L%aAT<}e>h5?f%A0D&J0rUZr6eXOKU;_rT!~|#qv$6=F1hdW!G!c_%9UPM& K9}2S+B~%G62okFR diff --git a/FinalExercise/FinalExercise/mathblocks.cpp b/FinalExercise/FinalExercise/mathblocks.cpp index 041d3c0..fe2195b 100644 --- a/FinalExercise/FinalExercise/mathblocks.cpp +++ b/FinalExercise/FinalExercise/mathblocks.cpp @@ -3,6 +3,7 @@ #include #include #include +#include const std::string identity_block::block_name = "Identity"; const std::string addition_block::block_name = "Addition"; @@ -141,7 +142,7 @@ void mathblocks::move_block_to_beginning(int blockIndex) { if (sequence.at(blockIndex)) { - std::move(sequence.begin() + blockIndex, sequence.begin() + blockIndex, sequence.begin()); + std::rotate(sequence.begin(), sequence.begin() + blockIndex, sequence.begin() + blockIndex + 1); } } catch (const std::exception& e) diff --git a/FinalExercise/FinalExercise/mathblocks.h b/FinalExercise/FinalExercise/mathblocks.h index 4bec191..29e95a2 100644 --- a/FinalExercise/FinalExercise/mathblocks.h +++ b/FinalExercise/FinalExercise/mathblocks.h @@ -102,7 +102,7 @@ class power_block final : public base_block class limit_block final : public base_block { public: - static const int constants_amount = 1; + static const int constants_amount = 2; static const std::string block_name; limit_block() = default; diff --git a/FinalExercise/FinalExerciseTests/mathblockstest.cpp b/FinalExercise/FinalExerciseTests/mathblockstest.cpp index 54c17b6..b255f3b 100644 --- a/FinalExercise/FinalExerciseTests/mathblockstest.cpp +++ b/FinalExercise/FinalExerciseTests/mathblockstest.cpp @@ -105,6 +105,19 @@ namespace FinalExerciseTests Assert::AreEqual(25.0, mb.run_sequence(5)); } + TEST_METHOD(TestMove) + { + mathblocks mb; + addition_block ab(std::vector{5.0}); + identity_block ib; + power_block pb(std::vector{2.0}); + mb.add_block(std::make_shared(ab)); + mb.add_block(std::make_shared(ib)); + mb.add_block(std::make_shared(pb)); + mb.move_block_to_beginning(1); + Assert::AreEqual(std::string("Identity"), mb.get_sequence()[0]->get_block_name()); + } + TEST_METHOD(TestSaveAndLoadSequence) { mathblocks mb;