From 93c7cb0b3b6c8679afd8a6f9c4cd6d6f9409bdd4 Mon Sep 17 00:00:00 2001 From: Daniel <59575049+lemoentjiez@users.noreply.github.com> Date: Fri, 21 Apr 2023 23:06:09 +0300 Subject: [PATCH] Add project files. --- Lab.sln | 31 +++++ Lab/Lab.cpp | 28 +++++ Lab/Lab.vcxproj | 163 +++++++++++++++++++++++++ Lab/Lab.vcxproj.filters | 42 +++++++ Lab/global.cpp | 3 + Lab/global.h | 6 + Lab/operations.cpp | 258 ++++++++++++++++++++++++++++++++++++++++ Lab/operations.h | 14 +++ Lab/triple.cpp | 132 ++++++++++++++++++++ Lab/triple.h | 31 +++++ 10 files changed, 708 insertions(+) create mode 100644 Lab.sln create mode 100644 Lab/Lab.cpp create mode 100644 Lab/Lab.vcxproj create mode 100644 Lab/Lab.vcxproj.filters create mode 100644 Lab/global.cpp create mode 100644 Lab/global.h create mode 100644 Lab/operations.cpp create mode 100644 Lab/operations.h create mode 100644 Lab/triple.cpp create mode 100644 Lab/triple.h diff --git a/Lab.sln b/Lab.sln new file mode 100644 index 0000000..073f7da --- /dev/null +++ b/Lab.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.33214.272 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Lab", "Lab\Lab.vcxproj", "{A12EB4A7-1661-4043-AC3E-5E303447400C}" +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 + {A12EB4A7-1661-4043-AC3E-5E303447400C}.Debug|x64.ActiveCfg = Debug|x64 + {A12EB4A7-1661-4043-AC3E-5E303447400C}.Debug|x64.Build.0 = Debug|x64 + {A12EB4A7-1661-4043-AC3E-5E303447400C}.Debug|x86.ActiveCfg = Debug|Win32 + {A12EB4A7-1661-4043-AC3E-5E303447400C}.Debug|x86.Build.0 = Debug|Win32 + {A12EB4A7-1661-4043-AC3E-5E303447400C}.Release|x64.ActiveCfg = Release|x64 + {A12EB4A7-1661-4043-AC3E-5E303447400C}.Release|x64.Build.0 = Release|x64 + {A12EB4A7-1661-4043-AC3E-5E303447400C}.Release|x86.ActiveCfg = Release|Win32 + {A12EB4A7-1661-4043-AC3E-5E303447400C}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {74D84430-7BDE-4C9E-AEFE-64042027E7C7} + EndGlobalSection +EndGlobal diff --git a/Lab/Lab.cpp b/Lab/Lab.cpp new file mode 100644 index 0000000..b66ffd1 --- /dev/null +++ b/Lab/Lab.cpp @@ -0,0 +1,28 @@ +#include "triple.h" +#include "operations.h" +#include "global.h" +#include + +using namespace std; + +int main(int argc,char* argv[]) { + triple number1, number2; + triple res; + char action; + //setPrecision(atoi(argv[3])); + cin >> number1 >> number2 >> action >> precision; + switch (action) { + case '+': res = number1 + number2; + break; + case '-': res = number1 - number2; + break; + case '*': res = number1 * number2; + break; + case '/': res = number1 / number2; + break; + default: cout << "No such operation retard!"; + break; + } + cout << res; +} + diff --git a/Lab/Lab.vcxproj b/Lab/Lab.vcxproj new file mode 100644 index 0000000..45c5d85 --- /dev/null +++ b/Lab/Lab.vcxproj @@ -0,0 +1,163 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 16.0 + Win32Proj + {a12eb4a7-1661-4043-ac3e-5e303447400c} + Lab + 10.0 + + + + Application + true + v142 + Unicode + + + Application + false + v142 + true + Unicode + + + Application + true + v142 + Unicode + + + Application + false + v142 + true + Unicode + + + + + + + + + + + + + + + + + + + + + true + + + false + + + true + + + false + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + C:\LIBS\boost_1_81_0;%(AdditionalIncludeDirectories) + + + Console + true + C:\LIBS\boost_1_81_0\libs;%(AdditionalLibraryDirectories) + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + C:\LIBS\boost_1_81_0;%(AdditionalIncludeDirectories) + + + Console + true + true + true + C:\LIBS\boost_1_81_0\libs;%(AdditionalLibraryDirectories) + + + + + Level3 + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + MaxSpeed + Speed + true + + + Console + true + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + Speed + + + Console + true + true + true + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Lab/Lab.vcxproj.filters b/Lab/Lab.vcxproj.filters new file mode 100644 index 0000000..36bd097 --- /dev/null +++ b/Lab/Lab.vcxproj.filters @@ -0,0 +1,42 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;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 + + + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + + + Header Files + + + Header Files + + + Header Files + + + \ No newline at end of file diff --git a/Lab/global.cpp b/Lab/global.cpp new file mode 100644 index 0000000..7c3e195 --- /dev/null +++ b/Lab/global.cpp @@ -0,0 +1,3 @@ +#include "global.h" + +int precision = 0; \ No newline at end of file diff --git a/Lab/global.h b/Lab/global.h new file mode 100644 index 0000000..faa9452 --- /dev/null +++ b/Lab/global.h @@ -0,0 +1,6 @@ +#pragma once +#ifndef GLOBAL_H +#define GLOBAL_H + +extern int precision; +#endif \ No newline at end of file diff --git a/Lab/operations.cpp b/Lab/operations.cpp new file mode 100644 index 0000000..8e5b6e7 --- /dev/null +++ b/Lab/operations.cpp @@ -0,0 +1,258 @@ +#include +#include +#include +#include +#include "triple.h" +#include "global.h" +#include "operations.h" + +using namespace std; + +triple operations::add(triple& a, triple& b) { + if (a.negative && !b.negative) { + a.negative = false; + triple res = b - a; + return res; + } + if (!a.negative && b.negative) { + b.negative = false; + triple res = a - b; + return res; + } + if (a.negative && b.negative) { + a.negative = false; + b.negative = false; + triple res = a + b; + res.negative = true; + a.negative = true; + b.negative = true; + return res; + } + triple res; + int next = 0; + int int_size = max(a.integer.size(), b.integer.size()); + int float_size = max(a.floating.size(), b.floating.size()); + vector int_result; + vector floating_result; + while (a.integer.size() < int_size) + a.integer.insert(a.integer.begin(), 0); + while (b.integer.size() < int_size) + b.integer.insert(b.integer.begin(), 0); + while (a.floating.size() < float_size) + a.floating.push_back(0); + while (b.floating.size() < float_size) + b.floating.push_back(0); + + for (auto av = a.floating.rbegin(), bv = b.floating.rbegin(); av != a.floating.rend() && bv != b.floating.rend(); ++av, ++bv) { + int sum = *av + *bv + next; + floating_result.insert(floating_result.begin(), sum % 10); + next = sum / 10; + } + for (auto av = a.integer.rbegin(), bv = b.integer.rbegin(); av != a.integer.rend() && bv != b.integer.rend(); ++av, ++bv) { + int sum = *av + *bv + next; + int_result.insert(int_result.begin(), sum % 10); + next = sum / 10; + } + if (next != 0) int_result.insert(int_result.begin(), next); + res.integer = int_result; + res.floating = floating_result; + return res; +} + +triple operations::subtract(triple& a, triple& b) { + triple res; + int borrow = 0; + bool negative = false; + int int_size = max(a.integer.size(), b.integer.size()); + int float_size = max(a.floating.size(), b.floating.size()); + vector int_result; + vector floating_result; + while (a.integer.size() < int_size) + a.integer.insert(a.integer.begin(), 0); + while (b.integer.size() < int_size) + b.integer.insert(b.integer.begin(), 0); + while (a.floating.size() < float_size) + a.floating.push_back(0); + while (b.floating.size() < float_size) + b.floating.push_back(0); + + if (a < b) { + swap(a, b); + negative = true; + } + + if (a.negative && !b.negative) { + res = a + b; + res.negative = true; + return res; + } + else if (!a.negative && b.negative) { + res = a + b; + res.negative = false; + return res; + } + + for (int i = float_size - 1, j = float_size - 1; i >= 0 || j >= 0; i--, j--) { + int digit1 = (i >= 0) ? a.floating[i] : 0; + int digit2 = (j >= 0) ? b.floating[j] : 0; + int diff = digit1 - digit2 - borrow; + if (diff < 0) { + diff += 10; + borrow = 1; + } + else borrow = 0; + floating_result.insert(floating_result.begin(), diff); + } + + for (int i = int_size - 1, j = int_size - 1; i >= 0 || j >= 0; i--, j--) { + int digit1 = (i >= 0) ? a.integer[i] : 0; + int digit2 = (j >= 0) ? b.integer[j] : 0; + int diff = digit1 - digit2 - borrow; + if (diff < 0) { + diff += 10; + borrow = 1; + } + else borrow = 0; + int_result.insert(int_result.begin(), diff); + } + + while (!int_result.empty() && int_result.front() == 0) int_result.erase(int_result.begin()); + while (!floating_result.empty() && floating_result.back() == 0) floating_result.pop_back(); + if (int_result.empty()) { + int_result.push_back(0); + } + res.negative = negative; + res.integer = int_result; + res.floating = floating_result; + return res; +} + +triple operations::multiply(triple& a, triple& b) { + triple res; + vector int_result, floating_result; + vector num1, num2; + int decimal = a.floating.size() + b.floating.size(); + num1 = a.integer; + num2 = b.integer; + + num1.insert(num1.end(), a.floating.begin(), a.floating.end()); + num2.insert(num2.end(), b.floating.begin(), b.floating.end()); + + vector result(num1.size() + num2.size()); + + if (a.negative && !b.negative || !a.negative && b.negative) res.negative = true; + + for (int i = a.integer.size() + a.floating.size() - 1; i >= 0; i--) { + int carry = 0; + for (int j = b.integer.size() + b.floating.size() - 1; j >= 0; j--) { + int prod = num1[i] * num2[j] + result[i + j + 1] + carry; + result[i + j + 1] = prod % 10; + carry = prod / 10; + } + result[i] += carry; + } + + int i = 0; + for (auto it = result.rbegin(); it != result.rend() && i < decimal; it++, i++) + floating_result.insert(floating_result.begin(), *it); + i = 0; + for (auto it = result.begin(); it != result.end() && i < result.size() - decimal; it++, i++) + int_result.push_back(*it); + + while (!int_result.empty() && int_result.front() == 0) int_result.erase(int_result.begin()); + while (!floating_result.empty() && floating_result.back() == 0) floating_result.pop_back(); + res.integer = int_result; + res.floating = floating_result; + return res; +} + +triple operations::divide(triple& a, triple& b) { + vector dividend; + vector divisor; + for (auto i : a.integer) dividend.push_back(i); + for (auto i : a.floating) dividend.push_back(i); + for (auto i : b.integer) divisor.push_back(i); + for (auto i : b.floating) divisor.push_back(i); + if (a.floating.size() > b.floating.size()) { + int add = a.floating.size() - b.floating.size(); + while (add--) divisor.push_back(0); + } + if (a.floating.size() < b.floating.size()) { + int add = b.floating.size() - a.floating.size(); + while (add--) dividend.push_back(0); + } + + triple tempb; + int count = 0; + triple result; + result.integer = { 0 }; + result.floating = {}; + + triple one; + triple ten; + triple zero; + zero.integer = { 0 }; + zero.floating = { 0 }; + ten.integer = { 1, 0 }; + ten.floating = {}; + one.integer = { 1 }; + one.floating = { 0 }; + + triple res = a; + int currentPrecision = 0; + while (!(res < b)) { + tempb = b; + res = res - tempb; + if (!res.negative) result = result + one; + } + result.floating.clear(); + bool first = false; + res.negative = false; + while (currentPrecision < precision) { + res.negative = false; + while (res < b && res != zero && res != b) { + if (res.integer.empty() && res.floating.empty() || (res.integer.size() == 1 && res.integer.at(0) == 0) && (res.floating.size() == 1 && res.floating.at(0) == 0)) { + break; + } + if (first) result.floating.push_back(0); + first = true; + res = res * ten; + } + first = false; + count = 0; + while (!(res < b) && !(res == zero)) { + tempb = b; + res = res - tempb; + count++; + } + res.negative = false; + if (count != 0) result.floating.push_back(count); + currentPrecision++; + } + return result; +} + + +triple operator+(triple& a,triple& b) { + operations op; + triple res = op.add(a, b); + return res; +} + +triple operator-(triple& a, triple& b) { + operations op; + triple res = op.subtract(a, b); + return res; +} + +triple operator*(triple& a, triple& b) { + operations op; + triple res = op.multiply(a, b); + return res; +} + +triple operator/(triple& a, triple& b) { + operations op; + triple res = op.divide(a, b); + return res; +} diff --git a/Lab/operations.h b/Lab/operations.h new file mode 100644 index 0000000..6f59280 --- /dev/null +++ b/Lab/operations.h @@ -0,0 +1,14 @@ +#include +#include +#include +#include "triple.h" + +class operations { +public: + triple add(triple& a, triple& b) ; + triple subtract(triple& a, triple& b); + triple multiply(triple& a, triple& b); + triple divide(triple& a, triple& b); + friend triple operator+(triple& a, triple& b); + friend triple operator-(triple& a, triple& b); +}; diff --git a/Lab/triple.cpp b/Lab/triple.cpp new file mode 100644 index 0000000..b2446b3 --- /dev/null +++ b/Lab/triple.cpp @@ -0,0 +1,132 @@ +#include +#include +#include +#include +#include "triple.h" +#include "operations.h" +using namespace std; + +bool negative = false; +vector integer; +vector floating; +int decimal_pos = -1; + +void triple::input() { + std::string line; + std::getline(std::cin, line); + + bool negative = false; + if (line[0] == '-') { + negative = true; + line = line.substr(1); + } + + decimal_pos = line.find('.'); + if (decimal_pos != std::string::npos) { + std::transform(line.begin(), line.begin() + decimal_pos, std::back_inserter(integer), + [](char c) { return c - '0'; }); + std::transform(line.begin() + decimal_pos + 1, line.end(), std::back_inserter(floating), + [](char c) { return c - '0'; }); + } + else { + std::transform(line.begin(), line.end(), std::back_inserter(integer), + [](char c) { return c - '0'; }); + } + + this->negative = negative; +} + + +void triple::print() { + if (negative) cout << '-'; + for (int i : integer) cout << i; + if (floating.size() == 0) { + cout << ".0"; + return; + } + else { + cout << '.'; + for (int x : floating) cout << x; + } + cout << '\n'; +} + +istream& operator>>(istream& in, triple& hf) { + hf.input(); + return in; +} + +ostream& operator<<(ostream& out, triple& hf) { + hf.print(); + return out; +} + +bool operator<(triple& a, triple& b) { + if (a.negative && !b.negative) return true; + if (!a.negative && b.negative) return false; + if (a.negative && b.negative) { + swap(a, b); + } + int int_size_a = a.integer.size(); + int int_size_b = b.integer.size(); + if (int_size_a != int_size_b) { + return int_size_a < int_size_b; + } + for (int i = 0; i < int_size_a; ++i) { + if (a.integer[i] != b.integer[i]) { + return a.integer[i] < b.integer[i]; + } + } + int float_size_a = a.floating.size(); + int float_size_b = b.floating.size(); + if (float_size_a != float_size_b) { + return float_size_a < float_size_b; + } + for (int i = 0; i < float_size_a; ++i) { + if (a.floating[i] != b.floating[i]) { + return a.floating[i] < b.floating[i]; + } + } + return false; +} + +bool operator>(triple& a, triple& b) { + if (a.negative && !b.negative) return false; + if (!a.negative && b.negative) return true; + if (a.negative && b.negative) { + swap(a, b); + } + int int_size_a = a.integer.size(); + int int_size_b = b.integer.size(); + if (int_size_a != int_size_b) { + return int_size_a < int_size_b; + } + for (int i = 0; i < int_size_a; ++i) { + if (a.integer[i] != b.integer[i]) { + return a.integer[i] > b.integer[i]; + } + } + int float_size_a = a.floating.size(); + int float_size_b = b.floating.size(); + if (float_size_a != float_size_b) { + return float_size_a > float_size_b; + } + for (int i = 0; i < float_size_a; ++i) { + if (a.floating[i] != b.floating[i]) { + return a.floating[i] > b.floating[i]; + } + } + return false; +} + +bool operator==(triple& a, triple& b) { + if (a.integer == b.integer && a.floating == b.floating) return true; + return false; +} + +bool operator!=(triple& a, triple& b) { + if (a.integer == b.integer && a.floating == b.floating) return false; + return true; +} + + diff --git a/Lab/triple.h b/Lab/triple.h new file mode 100644 index 0000000..da3292d --- /dev/null +++ b/Lab/triple.h @@ -0,0 +1,31 @@ +#ifndef TRIPLE_H +#define TRIPLE_H + +#include +#include +#include +#include + +using namespace std; + +class triple { +public: + bool negative; + std::vector integer; + std::vector floating; + int decimal_pos; + void input(); + void print(); + friend istream& operator>>(istream& in, triple& hf); + friend ostream& operator<<(ostream& out, triple& hf); + friend triple operator+(triple& a, triple& b); + friend triple operator-(triple& a, triple& b); + friend triple operator*(triple& a, triple& b); + friend triple operator/(triple& a, triple& b); + friend bool operator<(triple& a, triple& b); + friend bool operator>(triple& a, triple& b); + friend bool operator==(triple& a, triple& b); + friend bool operator!=(triple& a, triple& b); +}; + +#endif