From f88292f91e572123df67eae9d17c7e52a0893d61 Mon Sep 17 00:00:00 2001 From: Sancene <56795876+Sancene@users.noreply.github.com> Date: Fri, 19 Jun 2020 17:25:25 +0300 Subject: [PATCH] lab5 added --- lab5/Complex/Complex.sln | 41 +++ lab5/Complex/Complex/CComplex.cpp | 127 +++++++++ lab5/Complex/Complex/CComplex.h | 37 +++ lab5/Complex/Complex/Complex.cpp | 17 ++ lab5/Complex/Complex/Complex.vcxproj | 163 +++++++++++ lab5/Complex/Complex/Complex.vcxproj.filters | 30 ++ lab5/Complex_Tests/Complex_Tests.cpp | 259 ++++++++++++++++++ lab5/Complex_Tests/Complex_Tests.vcxproj | 159 +++++++++++ .../Complex_Tests.vcxproj.filters | 30 ++ 9 files changed, 863 insertions(+) create mode 100644 lab5/Complex/Complex.sln create mode 100644 lab5/Complex/Complex/CComplex.cpp create mode 100644 lab5/Complex/Complex/CComplex.h create mode 100644 lab5/Complex/Complex/Complex.cpp create mode 100644 lab5/Complex/Complex/Complex.vcxproj create mode 100644 lab5/Complex/Complex/Complex.vcxproj.filters create mode 100644 lab5/Complex_Tests/Complex_Tests.cpp create mode 100644 lab5/Complex_Tests/Complex_Tests.vcxproj create mode 100644 lab5/Complex_Tests/Complex_Tests.vcxproj.filters diff --git a/lab5/Complex/Complex.sln b/lab5/Complex/Complex.sln new file mode 100644 index 0000000..6276bde --- /dev/null +++ b/lab5/Complex/Complex.sln @@ -0,0 +1,41 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.29926.136 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Complex", "Complex\Complex.vcxproj", "{C24F982D-5F7D-459D-8025-6B57E59B41DA}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Complex_Tests", "..\Complex_Tests\Complex_Tests.vcxproj", "{4681918E-6C97-450A-B927-C4B4D457FC95}" +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 + {C24F982D-5F7D-459D-8025-6B57E59B41DA}.Debug|x64.ActiveCfg = Debug|x64 + {C24F982D-5F7D-459D-8025-6B57E59B41DA}.Debug|x64.Build.0 = Debug|x64 + {C24F982D-5F7D-459D-8025-6B57E59B41DA}.Debug|x86.ActiveCfg = Debug|Win32 + {C24F982D-5F7D-459D-8025-6B57E59B41DA}.Debug|x86.Build.0 = Debug|Win32 + {C24F982D-5F7D-459D-8025-6B57E59B41DA}.Release|x64.ActiveCfg = Release|x64 + {C24F982D-5F7D-459D-8025-6B57E59B41DA}.Release|x64.Build.0 = Release|x64 + {C24F982D-5F7D-459D-8025-6B57E59B41DA}.Release|x86.ActiveCfg = Release|Win32 + {C24F982D-5F7D-459D-8025-6B57E59B41DA}.Release|x86.Build.0 = Release|Win32 + {4681918E-6C97-450A-B927-C4B4D457FC95}.Debug|x64.ActiveCfg = Debug|x64 + {4681918E-6C97-450A-B927-C4B4D457FC95}.Debug|x64.Build.0 = Debug|x64 + {4681918E-6C97-450A-B927-C4B4D457FC95}.Debug|x86.ActiveCfg = Debug|Win32 + {4681918E-6C97-450A-B927-C4B4D457FC95}.Debug|x86.Build.0 = Debug|Win32 + {4681918E-6C97-450A-B927-C4B4D457FC95}.Release|x64.ActiveCfg = Release|x64 + {4681918E-6C97-450A-B927-C4B4D457FC95}.Release|x64.Build.0 = Release|x64 + {4681918E-6C97-450A-B927-C4B4D457FC95}.Release|x86.ActiveCfg = Release|Win32 + {4681918E-6C97-450A-B927-C4B4D457FC95}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {A598DE0F-34B7-4175-990B-D4B7179FDCE9} + EndGlobalSection +EndGlobal diff --git a/lab5/Complex/Complex/CComplex.cpp b/lab5/Complex/Complex/CComplex.cpp new file mode 100644 index 0000000..2bc581b --- /dev/null +++ b/lab5/Complex/Complex/CComplex.cpp @@ -0,0 +1,127 @@ +#include "CComplex.h" + +CComplex::CComplex(double real, double image) + : m_real(real), m_image(image) +{ +} + +double CComplex::Re() const +{ + return m_real; +} + +double CComplex::Im() const +{ + return m_image; +} + +double CComplex::GetMagnitude() const +{ + return sqrt(pow(Re(), 2) + pow(Im(), 2)); +} + +double CComplex::GetArgument() const +{ + return atan(Im() / Re()); +} + +CComplex const operator+(const CComplex& first, const CComplex& second) +{ + return CComplex(first.Re() + second.Re(), first.Im() + second.Im()); +} + +CComplex const operator-(const CComplex& first, const CComplex& second) +{ + return CComplex(first.Re() - second.Re(), first.Im() - second.Im()); +} + +CComplex const operator*(const CComplex& first, const CComplex& second) +{ + double real = first.Re() * second.Re() - first.Im() * second.Im(); + double image = first.Re() * second.Im() + first.Im() * second.Re(); + return CComplex(real, image); +} + +CComplex const operator/(const CComplex& first, const CComplex& second) +{ + if (second.Re() == 0 && second.Im() == 0) + { + throw std::invalid_argument("Division by 0"); + } + + double real = (first.Re() * second.Re() + first.Im() * second.Im()) / (pow(second.Re(), 2) + pow(second.Im(), 2)); + double image = (second.Re() * first.Im() - first.Re() * second.Im()) / (pow(second.Re(), 2) + pow(second.Im(), 2)); + return CComplex(real, image); +} + +CComplex const CComplex::operator +() const +{ + return *this; +} + +CComplex const CComplex::operator -() const +{ + return CComplex(-m_real, -m_image); +} + +CComplex& CComplex::operator+=(const CComplex& first) +{ + *this = *this + first; + return *this; +} + +CComplex& CComplex::operator-=(const CComplex& first) +{ + *this = *this - first; + return *this; +} + +CComplex& CComplex::operator*=(const CComplex& first) +{ + *this = *this * first; + return *this; +} + +CComplex& CComplex::operator/=(const CComplex& first) +{ + *this = *this / first; + return *this; +} + +bool CComplex::operator==(const CComplex& first) const +{ + return ((fabs(Re() - first.Re()) < DBL_MIN) && (fabs(Im() - first.Im()) < DBL_MIN)); +} + +bool CComplex::operator!=(const CComplex& first) const +{ + return !(*this == first); +} + +std::ostream& operator<<(std::ostream& os, const CComplex& complex) +{ + os << complex.Re(); + if (complex.Im() >= 0) + { + os << "+"; + } + os << complex.Im() << "i"; + + return os; +} + +std::istream& operator>>(std::istream& is, CComplex& complex) +{ + double real, image; + + if ((is >> real) && (is >> image) && (is.get() == 'i')) + { + complex = CComplex(real, image); + } + else + { + is.setstate(std::ios_base::failbit); + } + + return is; +} \ No newline at end of file diff --git a/lab5/Complex/Complex/CComplex.h b/lab5/Complex/Complex/CComplex.h new file mode 100644 index 0000000..fbe7de0 --- /dev/null +++ b/lab5/Complex/Complex/CComplex.h @@ -0,0 +1,37 @@ +#pragma once +#include +#include "math.h" +#include "float.h" +#include + +class CComplex +{ +public: + CComplex(double real = 0, double image = 0); + double Re() const; + double Im() const; + double GetMagnitude() const; + double GetArgument() const; + + CComplex const operator +()const; + CComplex const operator -()const; + + CComplex& operator+=(const CComplex& first); + CComplex& operator-=(const CComplex& first); + CComplex& operator*=(const CComplex& first); + CComplex& operator/=(const CComplex& first); + + bool operator==(const CComplex& first) const; + bool operator!=(const CComplex& first) const; +private: + double m_real; + double m_image; +}; + +CComplex const operator+(const CComplex& first, const CComplex& second); +CComplex const operator-(const CComplex& first, const CComplex& second); +CComplex const operator*(const CComplex& first, const CComplex& second); +CComplex const operator/(const CComplex& first, const CComplex& second); + +std::ostream& operator<<(std::ostream& os, const CComplex& complex); +std::istream& operator>>(std::istream& is, CComplex& complex); \ No newline at end of file diff --git a/lab5/Complex/Complex/Complex.cpp b/lab5/Complex/Complex/Complex.cpp new file mode 100644 index 0000000..ead2860 --- /dev/null +++ b/lab5/Complex/Complex/Complex.cpp @@ -0,0 +1,17 @@ +#include +#include +#include "CComplex.h" + +int main() +{ + std::string line; + + while (getline(std::cin, line)) + { + std::istringstream ss(line); + CComplex complex; + + ss >> complex; + std::cout << complex << std::endl; + } +} \ No newline at end of file diff --git a/lab5/Complex/Complex/Complex.vcxproj b/lab5/Complex/Complex/Complex.vcxproj new file mode 100644 index 0000000..4031804 --- /dev/null +++ b/lab5/Complex/Complex/Complex.vcxproj @@ -0,0 +1,163 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 16.0 + {C24F982D-5F7D-459D-8025-6B57E59B41DA} + Win32Proj + Complex + 10.0 + + + + Application + true + v142 + Unicode + + + Application + false + v142 + true + Unicode + + + Application + true + v142 + Unicode + + + Application + false + v142 + true + Unicode + + + + + + + + + + + + + + + + + + + + + true + + + true + + + false + + + false + + + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + stdcpp17 + + + Console + true + + + + + + + Level3 + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + stdcpp17 + + + Console + true + + + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + stdcpp17 + + + Console + true + true + true + + + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + stdcpp17 + + + Console + true + true + true + + + + + + + + + + + + + \ No newline at end of file diff --git a/lab5/Complex/Complex/Complex.vcxproj.filters b/lab5/Complex/Complex/Complex.vcxproj.filters new file mode 100644 index 0000000..659a1d7 --- /dev/null +++ b/lab5/Complex/Complex/Complex.vcxproj.filters @@ -0,0 +1,30 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;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 + + + + + Исходные файлы + + + Исходные файлы + + + + + Файлы заголовков + + + \ No newline at end of file diff --git a/lab5/Complex_Tests/Complex_Tests.cpp b/lab5/Complex_Tests/Complex_Tests.cpp new file mode 100644 index 0000000..ec40d7b --- /dev/null +++ b/lab5/Complex_Tests/Complex_Tests.cpp @@ -0,0 +1,259 @@ +#define _USE_MATH_DEFINES +#define CATCH_CONFIG_MAIN +#include "../../catch2/catch.hpp" +#include "../Complex/Complex/CComplex.h" +#include +#include + +using namespace std; + +TEST_CASE("Should return real part of complex number") +{ + CComplex A(5, 10); + CHECK(A.Re() == 5); +} + +TEST_CASE("Should return imaginary part of complex number") +{ + CComplex A(5, 10); + CHECK(A.Im() == 10); +} + +TEST_CASE("Must return the modulus of a complex number") +{ + CComplex A(5, 10); + CHECK(A.GetMagnitude() - 11.1803 < 0.0001); +} + +TEST_CASE("Must return an argument of a complex number") +{ + CComplex A(5, 10); + CHECK(A.GetArgument() - 63.4349 < 0.0001); +} + +TEST_CASE("Binary plus should sum two complex numbers") +{ + CComplex A(5, 10); + CComplex B(2, 5); + CComplex C = A + B; + CHECK(C.Re() == 7); + CHECK(C.Im() == 15); +} + +TEST_CASE("Binary plus should sum complex and real number") +{ + CComplex A(5, 10); + CComplex B(2, 5); + CComplex C = 5.6 + A; + CHECK(C.Re() == 10.6); + CHECK(C.Im() == 10); + + C = B + 5.6; + CHECK(C.Re() == 7.6); + CHECK(C.Im() == 5); +} + +TEST_CASE("Binary minus should subtracts a complex number from a complex number") +{ + CComplex A(5, 10); + CComplex B(2, 5); + CComplex C = A - B; + CHECK(C.Re() == 3); + CHECK(C.Im() == 5); +} + +TEST_CASE("Binary minus should get the difference of a complex number and a real number") +{ + CComplex A(5, 10); + CComplex B(2, 5); + CComplex C = A - 5; + CHECK(C.Re() == 0); + CHECK(C.Im() == 10); + + C = 5 - B; + CHECK(C.Re() == 3); + CHECK(C.Im() == -5); +} + +TEST_CASE("Should multiply complex numbers") +{ + CComplex A(5, 10); + CComplex B(0, 5); + CComplex C = A * B; + CHECK(C.Re() == -50); + CHECK(C.Im() == 25); +} + +TEST_CASE("Should multiply a complex number and a real number") +{ + CComplex A(5, 10); + CComplex B(2, 5); + CComplex C = A * 3; + CHECK(C.Re() == 15); + CHECK(C.Im() == 30); + + C = 3 * B; + CHECK(C.Re() == 6); + CHECK(C.Im() == 15); +} + +TEST_CASE("Should divide a complex number by a complex number") +{ + CComplex A(-1, 3); + CComplex B(1, 2); + CComplex C = A / B; + CHECK(C.Re() == 1); + CHECK(C.Im() == 1); +} + +TEST_CASE("Should't divide a complex number by zero") +{ + CComplex A(5, 10); + CComplex B(2, 5); + CComplex C(0, 0); + CHECK_THROWS(A / C); + CHECK_THROWS(A / 0); +} + +TEST_CASE("Division should get the quotient of a complex number and a real number") +{ + CComplex A(5, 10); + CComplex B(2, -2); + CComplex C = A / 5; + CHECK(C.Re() == 1); + CHECK(C.Im() == 2); + + C = 5 / B; + CHECK(C.Re() == 1.25); + CHECK(C.Im() == 1.25); +} + +TEST_CASE("Unary plus should return a copy of a complex number") +{ + CComplex A(5, 10); + CComplex C = +A; + CHECK(C.Re() == 5); + CHECK(C.Im() == 10); +} + +TEST_CASE("Unary minus should return the opposite complex number") +{ + CComplex A(5, 10); + CComplex C = -A; + CHECK(C.Re() == -5); + CHECK(C.Im() == -10); +} + +TEST_CASE("+= must add to the complex number combining with the assignment") +{ + CComplex C(5, 10); + CComplex B(5, 10); + C += B; + CHECK(C.Re() == 10); + CHECK(C.Im() == 20); + + C += 4; + CHECK(C.Re() == 14); + CHECK(C.Im() == 20); +} + +TEST_CASE("-= must subtract from a complex number combining with the assignment") +{ + CComplex C(5, 10); + CComplex B(2, 4); + C -= B; + CHECK(C.Re() == 3); + CHECK(C.Im() == 6); + + C -= 4; + CHECK(C.Re() == -1); + CHECK(C.Im() == 6); +} + +TEST_CASE("*= must multiply a complex number combining with the assignment") +{ + CComplex B(2, 10); + CComplex C(2, 4); + C *= B; + CHECK(C.Re() == -36); + CHECK(C.Im() == 28); + + C *= 2; + CHECK(C.Re() == -72); + CHECK(C.Im() == 56); +} + +TEST_CASE("/= must divide a complex number combining with the assignment") +{ + CComplex C(2, 10); + CComplex B(2, 4); + C /= B; + CHECK(C.Re() == 2.2); + CHECK(C.Im() == 0.6); + + C /= 2; + CHECK(C.Re() == 1.1); + CHECK(C.Im() == 0.3); +} + +TEST_CASE("/= mustn't divide a complex number by zero combining with the assignment") +{ + CComplex A(5, 10); + CComplex C(0, 0); + CHECK_THROWS(A /= C); + CHECK_THROWS(A /= 0); +} + +TEST_CASE("== сompares a complex numbers with a complex number") +{ + CComplex C(5, 10); + CComplex A(5, 10); + CComplex B(4, 10); + CHECK(C == A); + + CHECK(!(A == B)); +} + +TEST_CASE("!= must check complex numbers for the inequality") +{ + CComplex A(4, 10); + CComplex B(5, 10); + CHECK(A != B); + + CComplex C(4, 10); + CHECK(!(C != A)); +} + +TEST_CASE("Input operator should read a complex numbers") +{ + CComplex C; + (std::stringstream)"-3.5-4.8i" >> C; + CHECK(C.Re() == -3.5); + CHECK(C.Im() == -4.8); + + (std::stringstream)"4+2i" >> C; + CHECK(C.Re() == 4); + CHECK(C.Im() == 2); +} + +TEST_CASE("If complex number failed to read, stream has a failbit state") +{ + CComplex C; + std::stringstream strm; + strm << "4+2"; + strm >> C; + CHECK(strm.fail()); +} + +TEST_CASE("Output operator should print a complex number") +{ + CComplex C(-3.5, -4.8); + std::stringstream strmC; + strmC << C; + CHECK(strmC.str() == "-3.5-4.8i"); + + CComplex D(4, 2); + std::stringstream strmD; + strmD << D; + CHECK(strmD.str() == "4+2i"); +} \ No newline at end of file diff --git a/lab5/Complex_Tests/Complex_Tests.vcxproj b/lab5/Complex_Tests/Complex_Tests.vcxproj new file mode 100644 index 0000000..f959509 --- /dev/null +++ b/lab5/Complex_Tests/Complex_Tests.vcxproj @@ -0,0 +1,159 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 16.0 + {4681918E-6C97-450A-B927-C4B4D457FC95} + Win32Proj + ComplexTests + 10.0 + + + + Application + true + v142 + Unicode + + + Application + false + v142 + true + Unicode + + + Application + true + v142 + Unicode + + + Application + false + v142 + true + Unicode + + + + + + + + + + + + + + + + + + + + + true + + + true + + + false + + + false + + + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + + + Level3 + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + + + + + + + \ No newline at end of file diff --git a/lab5/Complex_Tests/Complex_Tests.vcxproj.filters b/lab5/Complex_Tests/Complex_Tests.vcxproj.filters new file mode 100644 index 0000000..9f2a78a --- /dev/null +++ b/lab5/Complex_Tests/Complex_Tests.vcxproj.filters @@ -0,0 +1,30 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;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 + + + + + Исходные файлы + + + Исходные файлы + + + + + Файлы заголовков + + + \ No newline at end of file