Savelyev_Vyacheslav

OS_laba2_Sort

May 5th, 2021 (edited)
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.88 KB | None | 0 0
  1. // Sort.cpp : Этот файл содержит функцию "main". Здесь начинается и заканчивается выполнение программы.
  2. //
  3.  
  4. #define _CRT_SECURE_NO_WARNINGS
  5. #include <iostream>
  6. #include <thread>
  7. #include <ctime>
  8. #include <cstdlib>
  9.  
  10. #include <windows.h>
  11. #include <stdio.h>
  12. #include <string.h>
  13.  
  14. using namespace std;
  15.  
  16.  
  17. class LiveTimer
  18. {
  19.  
  20. public:
  21.  
  22.     int iteration;
  23.     int DurationMilliSEC;
  24.     int fMIN;
  25.     int fSEC;
  26.     int fMilliSEC;
  27.     void  Tstrt()
  28.     {
  29.         T_strt = std::chrono::high_resolution_clock::now();
  30.         chrono::duration<float> Tstrt = T_strt - T_strt;
  31.         cout << "Tstrt: " << Tstrt.count() << endl;
  32.     }
  33.     void  Tend()
  34.     {
  35.         T_end = std::chrono::high_resolution_clock::now();
  36.         chrono::duration<float> duration = (T_end - T_strt) * 1000;
  37.         LiveTimer::DurationMilliSEC = duration.count();
  38.         LiveTimer::fMIN = (duration.count() / 1000 / 60);
  39.         LiveTimer::fSEC = int(duration.count() / 1000) % 60;
  40.         LiveTimer::fMilliSEC = int(duration.count()) - fMIN * 60 * 1000 - fSEC * 1000;
  41.         chrono::duration<float> Tstrt = T_strt - T_strt;
  42.         cout << "Tend: " << duration.count() << endl;
  43.     }
  44.     void printT() {
  45.         cout << " MilliSEC/iteration: " << LiveTimer::DurationMilliSEC / iteration << endl;
  46.         cout << "MIN: " << LiveTimer::fMIN << " SEC: " << LiveTimer::fSEC << " MilliSEC: " << LiveTimer::fMilliSEC << endl;
  47.     }
  48. private:
  49.     std::chrono::time_point<std::chrono::steady_clock> T_strt, T_end;
  50.  
  51. };
  52.  
  53.  
  54.  
  55. int timeSistem() {
  56.     time_t now;
  57.     struct tm nowLocal;
  58.     now = time(NULL); //het the time for the os
  59.     nowLocal = *localtime(&now);
  60.     //cout << "Today: " << nowLocal.tm_mday << " " << nowLocal.tm_mon + 1 << " " << nowLocal.tm_year + 1900 << endl;
  61.     cout << "Time: " << " " << nowLocal.tm_hour << " " << nowLocal.tm_min << " " << nowLocal.tm_sec << endl;
  62.     return 0;
  63. }
  64.  
  65. int* setArr(int* arr, int size) {
  66.     int i = 0;
  67.     while (i < size) {
  68.         arr[i] = (rand() % 10000) + 1;
  69.         // std::this_thread::sleep_for(std::chrono::milliseconds(1000));
  70.         i = i + 1;
  71.     }
  72.     return arr;
  73. }
  74.  
  75.  
  76.  
  77.  
  78. void quicksort(int* mas, int first, int last)
  79. {
  80.     int mid, count;
  81.     int f = first, l = last;
  82.     mid = mas[(f + l) / 2]; //вычисление опорного элемента
  83.     do
  84.     {
  85.         while (mas[f] < mid) f++;
  86.         while (mas[l] > mid) l--;
  87.         if (f <= l) //перестановка элементов
  88.         {
  89.             count = mas[f];
  90.             mas[f] = mas[l];
  91.             mas[l] = count;
  92.             f++;
  93.             l--;
  94.         }
  95.     } while (f < l);
  96.     if (first < l) quicksort(mas, first, l);
  97.     if (f < last) quicksort(mas, f, last);
  98. }
  99. void printArr(int* arr3, int n) {
  100.     // LiveTimer timeLive;
  101.     int i = 0;
  102.     while (i < n) {
  103.         // std::this_thread::sleep_for(std::chrono::milliseconds(1000));
  104.         cout << "i" << i << " " << arr3[i] << endl;
  105.         i = i + 1;
  106.     }
  107.     //cin.get();
  108. }
  109.  
  110.  
  111. ///////////////////////////////////////
  112. LPWSTR winApiFormat(char text[], wchar_t wtext2[128]) {
  113.     mbstowcs(wtext2, text, strlen(text) + 1);//Plus null
  114.     LPWSTR ptr = wtext2;
  115.     return ptr;
  116. }
  117. void titleSet(char a[]) {
  118.     wchar_t wtext2[128];
  119.     LPWSTR ptr2 = winApiFormat(a, wtext2);
  120.     SetConsoleTitle(ptr2);
  121. }
  122.  
  123. void setCharArg(char* cmd, char* arg) {
  124.     sprintf(cmd + strlen(cmd), " %s", arg);
  125. }
  126.  
  127. void controlProcess(int argc, char* argv[]) {
  128.     char title[128] = "Sort";
  129.    
  130.     if (argc != 4) {
  131.         cerr << "Sort: Snachalo  zapustite MASTER.EXE.\n";
  132.         exit(1);
  133.     }
  134.  
  135.     int pid = atoi(argv[1]);
  136.     cout << "----------------" << argv[3] << "\n";
  137.     cout << "----------------" << argv[2] << "\n";
  138.     cout << "----------------" << argv[1] << "\n";
  139.     cout << "----------------" << argv[0] << "\n";
  140.     setCharArg(title, argv[2]);
  141.     setCharArg(title, argv[3]);
  142.     titleSet(title);
  143.  
  144.    
  145. }
  146. /////////////////////////////////////////////////
  147. int main(int argc, char* argv[])
  148. {
  149.     LiveTimer timeLive;
  150.     timeLive.Tstrt();
  151.  
  152.    
  153.     setlocale(LC_ALL, "ru");
  154.     int c = 0;
  155.    
  156.    
  157.     controlProcess(argc, argv);
  158.    
  159.     const int size = 100;
  160.     int arr[size];
  161.     int* arr2 = setArr(arr, size);
  162.     printArr(arr, size);
  163.     quicksort(arr, 0, size - 1);
  164.     printArr(arr, size);
  165.  
  166.  
  167.     timeLive.iteration = size;
  168.     timeLive.Tend();
  169.     timeLive.printT();
  170.     cout << "Sort: EXIT\n";
  171.     //cin.get();
  172.     Sleep(1000);
  173.     return 0;
  174. }
  175.  
  176. // Запуск программы: CTRL+F5 или меню "Отладка" > "Запуск без отладки"
  177. // Отладка программы: F5 или меню "Отладка" > "Запустить отладку"
  178.  
  179. // Советы по началу работы
  180. //   1. В окне обозревателя решений можно добавлять файлы и управлять ими.
  181. //   2. В окне Team Explorer можно подключиться к системе управления версиями.
  182. //   3. В окне "Выходные данные" можно просматривать выходные данные сборки и другие сообщения.
  183. //   4. В окне "Список ошибок" можно просматривать ошибки.
  184. //   5. Последовательно выберите пункты меню "Проект" > "Добавить новый элемент", чтобы создать файлы кода, или "Проект" > "Добавить существующий элемент", чтобы добавить в проект существующие файлы кода.
  185. //   6. Чтобы снова открыть этот проект позже, выберите пункты меню "Файл" > "Открыть" > "Проект" и выберите SLN-файл.
  186.  
Add Comment
Please, Sign In to add comment