diff --git a/Part 2/Head.h b/Part 2/Head.h new file mode 100644 index 0000000000000000000000000000000000000000..c7141559644dde10fdacad9e00aa8ff133092e81 --- /dev/null +++ b/Part 2/Head.h @@ -0,0 +1,278 @@ +#pragma once +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +class Random { + int num; +public: + Random(int l) : num(l) {} + int operator() () { return rand() % 100 - 49; } +}; + + +class Func +{ + int min; +public: + Func(int c = 0) :min(c) {} + int operator()(int& a) + { + return min * a; + } +}; + +void CreateData() +{ + srand(time(NULL)); + ofstream fout; + int num; + fout.open("NewFile.txt"); + if (fout.is_open()) + { + cout << " Файл успешно открыт!" << endl; + } + else throw ("Ошибка открытия файла"); + for (unsigned i = 0; i < 100; ++i) + { + fout << rand() % 100 - 49 << setw(8); + } + fout.close(); + ifstream fin("NewFile.txt"); + cout << endl<<"Содержимое файла" << endl; + while (!fin.eof()) + { + fin >> num; + cout << num << setw(8); + } + cout << endl; + fin.close(); + +} + + +void createData() +{ + srand(time(NULL)); + ofstream fout; + int num; + fout.open("NewFile.txt"); + if (fout.is_open()) + { + cout << " Файл успешно открыт!" << endl; + } + else throw ("Ошибка открытия файла"); + vector vect; + vect.resize(100); + Random r(0); + generate(vect.begin(), vect.end(), r); + copy(vect.begin(), vect.end(), ostream_iterator(fout, " ")); + fout.close(); + ifstream fin("NewFile.txt"); + cout <> num) + { + cout << num << setw(8); + } + cout << endl; + fin.close(); +} + + +template +void LoadData(Container& cont) +{ + ifstream fin; + fin.open("NewFile.txt"); + if (fin.is_open()) + { + cout << " Файл успешно открыт!" << endl; + } + else throw ("Ошибка открытия файла"); + int num; + cont.clear(); + while (!fin.eof()) + { + fin >> num; + cont.push_back(num); + } + cont.pop_back(); + cout << endl << "Содержимое контейнера:" << endl; + for (const auto& elem : cont) + { + cout << elem << setw(8); + } + cont.clear(); + fin.close(); + cout << endl; +} + +template < typename Container > +void loadData(Container& cont) +{ + ifstream fin; + fin.open("NewFile.txt"); + if (fin.is_open()) + { + cout << " Файл успешно открыт!" << endl; + } + else throw ("Ошибка открытия файла"); + istream_iterator iterstart(fin); + istream_iterator iterend; + cont.clear(); + copy(iterstart, iterend, back_inserter(cont)); + cout << endl <<"Содержимое контейнера:" << endl; + for (const auto& elem : cont) + { + cout << elem << setw(8); + } + cout << endl; +} + +template +void modify_for_each(const Container& cont) +{ + Container p_cont(cont); + auto result = min_element(cont.begin(), cont.end()); + cout << " Минимальное число = " << *result << endl; + int min = *result; + for_each(p_cont.begin(), p_cont.end(), [min](int& a) { + a *= min; + } + ); + cout << endl << "Содержимое контейнера:" << endl; + for (const auto& elem : p_cont) + { + cout << elem << setw(8); + } + cout << endl; +} + +template +void modify_iterator(Iterator begin, Iterator end) +{ + + auto result = min_element(begin, end); + cout << " Минимальное число = " << *result << endl; + int min = *result; + for_each(begin,end, [min](int& a) { + a *= min; + } + ); + cout << endl << "Содержимое контейнера:" << endl; + for_each(begin, end, [](const int& a) { + cout << a << setw(8); + } + ); + cout << endl; +} + +template +void modify_container( Container& cont) +{ + + int sum = 0; + double srd = 0; + for_each(cont.begin(), cont.end(), [&sum](int a) + { + sum += abs(a); + }); + srd = sum / cont.size(); + cont.push_back(sum); + cont.push_back(srd); + cout << endl << "Содержимое контейнера:" << endl; + for (const auto& elem : cont) + { + cout << elem << setw(8); + } + cout << endl; +} + +template +void modify_transform(Container& cont) +{ + auto result = min_element(cont.begin(), cont.end()); + cout << " Минимальное число = " << *result << endl; + int min = *result; + ofstream fout; + int num; + ostream_iterator iterstart(fout, " "); + Func f(min); + fout.open("Result.txt"); + if (fout.is_open()) + { + cout << " Файл успешно открыт!" << endl; + } + else throw ("Ошибка открытия файла"); + transform(cont.begin(), cont.end(), iterstart, f); + fout.close(); + ifstream fin("Result.txt"); + cout << endl << "Содержимое файла" << endl; + while (fin >> num) + { + cout << num << setw(8); + } + cout << endl; + fin.close(); +} + + + +template +void output_result(const Container& cont) +{ + auto iter = cont.begin(); + ofstream fout; + int num; + fout.open("NewFile.txt"); + if (fout.is_open()) + { + cout << " Файл успешно открыт!" << endl; + } + else throw ("Ошибка открытия файла"); + for (auto iter = cont.begin(); iter != cont.end(); ++iter) + { + fout << *iter << setw(8); + } + fout.close(); + ifstream fin("NewFile.txt"); + cout << endl << "Содержимое файла" << endl; + while (fin >> num) + { + cout << num << setw(8); + } + cout << endl; + fin.close(); +} + +template +void OutputResult(const Container& cont) +{ + ofstream fout; + int num; + fout.open("NewFile.txt"); + if (fout.is_open()) + { + cout << " Файл успешно открыт!" << endl; + } + else throw ("Ошибка открытия файла"); + ostream_iterator iter(fout, " "); + copy(cont.begin(), cont.end(), iter); + fout.close(); + ifstream fin("NewFile.txt"); + cout << endl << "Содержимое файла" << endl; + while (fin >> num) + { + cout << num << setw(8); + } + cout << endl; + fin.close(); +} \ No newline at end of file