From 57fe6d131ee4dd1a0746482220f489ed516223ed Mon Sep 17 00:00:00 2001 From: Woodey Date: Wed, 22 Dec 2021 23:13:39 +0300 Subject: [PATCH] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D1=8F=201?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Part 1/Functions.h | 19 +++++++++++++------ Part 1/main.cpp | 4 ++-- Part 2/Functions.h | 30 ++++++++++++++++-------------- Part 2/Structure.h | 18 ++++++++++++------ Part 2/main.cpp | 6 +++--- 5 files changed, 46 insertions(+), 31 deletions(-) diff --git a/Part 1/Functions.h b/Part 1/Functions.h index ca58ada..2069b81 100644 --- a/Part 1/Functions.h +++ b/Part 1/Functions.h @@ -20,6 +20,7 @@ void createData_1(const std::string& filePath){ } } +template void createData_2(const std::string& filePath){ std::ofstream file(filePath, std::ios::trunc); if (!file.is_open()) { @@ -27,9 +28,11 @@ void createData_2(const std::string& filePath){ } srand(time(nullptr)); - std::vector randVal(100);//!!! Функции не должны зависеть от конкретных типов. Есть value_Type. + using type = typename _Cont::value_type; + + _Cont randVal(100);//(ИСПР)!!! Функции не должны зависеть от конкретных типов. Есть value_Type. std::generate(randVal.begin(), randVal.end(), [](){return rand() % 101 - 50;}); - std::copy(randVal.begin(), randVal.end(), std::ostream_iterator(file, "\n"));//!!! Функции не должны зависеть от конкретных типов. Есть value_Type. + std::copy(randVal.begin(), randVal.end(), std::ostream_iterator(file, "\n"));//(ИСПР)!!! Функции не должны зависеть от конкретных типов. Есть value_Type. } template @@ -40,7 +43,9 @@ _Cont loadData_1(const std::string& filePath){ throw PathError(); } - int buffer; //!!! Функции не должны зависеть от конкретных типов. Есть value_Type. + using type = typename _Cont::value_type; + + type buffer; //(ИСПР)!!! Функции не должны зависеть от конкретных типов. Есть value_Type. while (file >> buffer) { result.push_back(buffer); @@ -55,8 +60,10 @@ _Cont loadData_2(const std::string& filePath) { if (!file.is_open()) { throw PathError(); } - std::istream_iterator first(file);//!!! Функции не должны зависеть от конкретных типов. Есть value_Type. - std::istream_iterator last;//!!! Функции не должны зависеть от конкретных типов. Есть value_Type. + using type = typename _Cont::value_type; + + std::istream_iterator first(file);//(ИСПР)!!! Функции не должны зависеть от конкретных типов. Есть value_Type. + std::istream_iterator last;//(ИСПР)!!! Функции не должны зависеть от конкретных типов. Есть value_Type. result.resize(100); std::copy(first, last, result.begin()); return result; @@ -89,7 +96,7 @@ void Modify(_Iter iterBeg, _Iter iterEnd) { } template -void Modify(_Cont container, _Func func) { //!!! Почему container передается по знаяению? +void Modify(_Cont& container, _Func func) { //!!!(ИСПР) Почему container передается по значению? std::for_each(container.begin(), container.end(), func); } diff --git a/Part 1/main.cpp b/Part 1/main.cpp index a1264fc..d8d271e 100644 --- a/Part 1/main.cpp +++ b/Part 1/main.cpp @@ -6,8 +6,8 @@ #include "Functions.h" int main() { - const std::string filePath = "..I_O-streams-and-stl/Part 1/file.txt"; //!!! Файл находится? После .. "/" не нужен? - const std::string resFilePath = "..I_O-streams-and-stl/Part 1/resFile.txt"; //!!! Файл находится? После .. "/" не нужен? + const std::string filePath = "../I_O-streams-and-stl/Part 1/file.txt"; //!!!(ИСПР) Файл находится? После .. "/" не нужен? + const std::string resFilePath = "../I_O-streams-and-stl/Part 1/resFile.txt"; //!!!(ИСПР) Файл находится? После .. "/" не нужен? std::vector vec1; std::list list1; diff --git a/Part 2/Functions.h b/Part 2/Functions.h index 480447e..eea0eea 100644 --- a/Part 2/Functions.h +++ b/Part 2/Functions.h @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include "Structure.h" @@ -94,15 +95,15 @@ void Replace_Account(int accNum, const BAccount& acc, const std::string& bFilePa void Search_by_Number(const int num, const std::string& mainBPath, const std::string& newBPath) { - //!!! По заданию нужно использовать алгоритмы + //(ИСПР)!!! По заданию нужно использовать алгоритмы std::map map; Create_Map(map, mainBPath); std::vector res; - for (const auto& iter : map) { - if (iter.second.number == num) res.push_back(iter.second); - } + std::for_each(map.begin(), map.end(), [res, num](const auto& el){ + if (el.second.number == num) res.push_back(el.second); + }); std::ofstream newFile(newBPath, std::ios::binary | std::ios::trunc); if (!newFile.is_open()) { @@ -113,17 +114,18 @@ void Search_by_Number(const int num, const std::string& mainBPath, const std::st } } -void Search_by_Code(const long AccCode, const std::string& mainBPath, const std::string& newBPath) +void Search_by_Code(const long accCode, const std::string& mainBPath, const std::string& newBPath) { - //!!! По заданию нужно использовать алгоритмы + //(ИСПР)!!! По заданию нужно использовать алгоритмы std::map map; Create_Map(map, mainBPath); std::vector res; + + std::for_each(map.begin(), map.end(), [res, accCode](const auto& el){ + if (el.second.code == accCode) res.push_back(el.second); + }); - for (const auto& iter : map) { - if (iter.second.code == AccCode) res.push_back(iter.second); - } std::ofstream newFile(newBPath, std::ios::binary | std::ios::trunc); if (!newFile.is_open()) { @@ -136,16 +138,16 @@ void Search_by_Code(const long AccCode, const std::string& mainBPath, const std: void Search_by_Surname(const std::string& surn, const std::string& mainBPath, const std::string& newBPath) { - //!!! По заданию нужно использовать алгоритмы + //(ИСПР)!!! По заданию нужно использовать алгоритмы std::map map; Create_Map(map, mainBPath); std::vector res; - for (const auto& elem : map) { - if (elem.second.surname == surn) res.push_back(elem.second); - } - + std::for_each(map.begin(), map.end(), [res, surn](const auto& el){ + if (el.second.surname == surn) res.push_back(el.second); + }); + std::ofstream newFile(newBPath, std::ios::binary | std::ios::trunc); if (!newFile.is_open()) { throw PathError(); diff --git a/Part 2/Structure.h b/Part 2/Structure.h index 5442087..91686cb 100644 --- a/Part 2/Structure.h +++ b/Part 2/Structure.h @@ -7,19 +7,25 @@ struct BAccount { int number; long code; - std::string surname; //!!! Для корректной работы с бинарными файлами использовать нужно статические массивы char + char surname[150]; //(ИСПР)!!! Для корректной работы с бинарными файлами использовать нужно статические массивы char double sum; - std::string creationDate; //!!! Для корректной работы с бинарными файлами использовать нужно статические массивы char + char creationDate[150]; //(ИСПР)!!! Для корректной работы с бинарными файлами использовать нужно статические массивы char double interest; - BAccount(const BAccount& el): number(el.number), code(el.code), surname(el.surname), sum(el.sum), creationDate(el.creationDate), interest(el.interest){} + BAccount(const BAccount& el): number(el.number), code(el.code), sum(el.sum), interest(el.interest){ + strcpy(surname, el.surname); + strcpy(creationDate, el.creationDate); + } BAccount(int number_, long code_, - const std::string& surname_, + const char surname_[], double sum_, - const std::string& creationDate_, - double interest_): number(number_), code(code_), surname(surname_), sum(sum_), creationDate(creationDate_), interest(interest_){} + const char creationDate_[], + double interest_): number(number_), code(code_), sum(sum_), interest(interest_){ + strcpy(surname, surname_); + strcpy(creationDate, creationDate_); + } BAccount() = default; diff --git a/Part 2/main.cpp b/Part 2/main.cpp index 24a102d..8fbeb36 100644 --- a/Part 2/main.cpp +++ b/Part 2/main.cpp @@ -4,9 +4,9 @@ int main(){ - std::string bfile = "..I_O-streams-and-stl/Part 2/bfile.bin"; - std::string tfile = "..I_O-streams-and-stl/Part 2/tfile.txt"; - std::string resBfile = "..I_O-streams-and-stl/Part 2/resBfile.bin"; + std::string bfile = "../I_O-streams-and-stl/Part 2/bfile.bin"; + std::string tfile = "../I_O-streams-and-stl/Part 2/tfile.txt"; + std::string resBfile = "../I_O-streams-and-stl/Part 2/resBfile.bin"; std::string surn; BAccount acc; -- GitLab