Vla_DOS

Untitled

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