diff --git a/Part 1/Functions.h b/Part 1/Functions.h index ca58ada339a8476a8f97033d86b5a09540d30d1e..2069b8130d7634ce126c6b385bc7ebc94ec8d08a 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 a1264fc353f783198a04a3069aaecf00071a9726..d8d271ebee3b0986f350dbfd6fbf6a52a4fd6754 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 480447e8813d6ba98c10b300065114dd3cbd0c77..eea0eea133347ef10f63f98dd67c25ee0ca80d26 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 5442087c832e6a720b47ae3410b7d2fed19107f3..91686cb23a81ef700de1d5b12f249945eb8c5da6 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 24a102d3318a3855042eef621ed8c785475d4438..8fbeb36b466bb2478b734a137d7b31530cead091 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;