Vla_DOS

Untitled

Jun 17th, 2022
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.32 KB | None | 0 0
  1. #include <iostream>
  2. #include <conio.h>
  3. #include <time.h>
  4. #include <chrono>
  5. #include <fstream>
  6. #include <sstream>
  7. #include <algorithm>
  8. #include <string>
  9.  
  10. using namespace std;
  11.  
  12. void sortByInserts(int arr[], int N) {
  13.     int buff = 0;
  14.     int i, j;
  15.  
  16.     for (i = 1; i < N; i++)
  17.     {
  18.         buff = arr[i];
  19.         for (j = i - 1; j >= 0 && arr[j] > buff; j--)
  20.             arr[j + 1] = arr[j];
  21.  
  22.         arr[j + 1] = buff;
  23.     }
  24. }
  25. void ReadFile(string patch) {
  26.     std::string line;
  27.  
  28.     std::ifstream in(patch); // окрываем файл для чтения
  29.     if (in.is_open())
  30.     {
  31.         while (getline(in, line))
  32.         {
  33.             std::cout << line << std::endl;
  34.         }
  35.     }
  36.     in.close();
  37. }
  38.  
  39. int WriteToArr(string patch, int* mass) {
  40.     int temp;
  41.  
  42.     std::ifstream fl(patch);
  43.     int index = 0;
  44.     if (!fl)
  45.     {
  46.         cout << "File not found" << std::endl;
  47.     }
  48.     else {
  49.         while (fl >> temp) {
  50.             mass[index] = temp;
  51.             index++;
  52.         }
  53.     }
  54.     fl.close();
  55.     return index;
  56. }
  57. void Write(string path, int* arr, int size) {
  58.     fstream f;
  59.     f.open(path, fstream::in | fstream::out);
  60.  
  61.     if (f.is_open() == NULL)
  62.     {
  63.         cout << "error!";
  64.     }
  65.     for (int i = 0; i < size; i++)
  66.         f << arr[i] << " ";
  67.     f.close();
  68. }
  69. void PrintArr(int* arr, int size) {
  70.     for (int i = 0; i < size; i++) {
  71.         cout << arr[i] << "\t";
  72.     }
  73. }
  74. int main() {
  75.     setlocale(LC_CTYPE, "");
  76.     string path10 = "input__10.txt";
  77.     string path100 = "input__100.txt";
  78.     string path1000 = "input__1000.txt";
  79.     string path10000 = "input__10000.txt";
  80.     int num;
  81.     cout << "Кiлькiсть елементiв: ";
  82.     cin >> num;
  83.     int* mass = new int[num];
  84.  
  85.     for (int i = 0; i < num; i++)
  86.         mass[i] = rand() % 47;
  87.  
  88.     Write(path10, mass, num);
  89.  
  90.  
  91.     ReadFile(path10);
  92.  
  93.     int index = WriteToArr(path10, mass);
  94.  
  95.     cout << "\nВiдсортований масив:" << endl;
  96.     auto start = chrono::high_resolution_clock::now();
  97.     sortByInserts(mass, index);
  98.     auto finish = chrono::high_resolution_clock::now();
  99.     PrintArr(mass, index);
  100.     cout << "\nЧас сортування: " << chrono::duration_cast<chrono::duration<double>>(finish - start).count() << " секунд\n";
  101.  
  102.  
  103.     return 0;
  104. }
Advertisement
Add Comment
Please, Sign In to add comment