Vla_DOS

8

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