Savelyev_Vyacheslav

OS_SORT_L3

May 8th, 2021 (edited)
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.64 KB | None | 0 0
  1. // SORT_L3.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.  
  56. int* setArr(int* arr, int size) {
  57.     int i = 0;
  58.     while (i < size) {
  59.         arr[i] = (rand() % 100) + 1;
  60.         // std::this_thread::sleep_for(std::chrono::milliseconds(1000));
  61.         i = i + 1;
  62.     }
  63.     return arr;
  64. }
  65.  
  66.  
  67.  
  68. void BubbleSort(int A[], int N) //сортировка пузырьком
  69. {
  70.     int i, j, count, key;
  71.     for (i = 0; i < N; i++)
  72.     {
  73.         for (j = 0; j < N - 1; j++)
  74.         {
  75.             key = j + 1;
  76.             count = A[key];
  77.             if (A[j] > A[key])
  78.             {
  79.                 A[key] = A[j];
  80.                 A[j] = count;
  81.             }
  82.         }
  83.     }
  84.     cout << " массив отсортирован: "<<endl;
  85.    // for (i = 0; i < N; i++) cout << A[i] << " "; //вывод массива
  86. }
  87.  
  88.  
  89. void quicksort(int* mas, int first, int last)
  90. {
  91.     int mid, count;
  92.     int f = first, l = last;
  93.     mid = mas[(f + l) / 2]; //вычисление опорного элемента
  94.     do
  95.     {
  96.         while (mas[f] < mid) f++;
  97.         while (mas[l] > mid) l--;
  98.         if (f <= l) //перестановка элементов
  99.         {
  100.             count = mas[f];
  101.             mas[f] = mas[l];
  102.             mas[l] = count;
  103.             f++;
  104.             l--;
  105.         }
  106.     } while (f < l);
  107.     if (first < l) quicksort(mas, first, l);
  108.     if (f < last) quicksort(mas, f, last);
  109. }
  110. void printArr(int* arr3, int n, char* argv[]) {
  111.     // LiveTimer timeLive;
  112.     int i = 0;
  113.     while (i < n) {
  114.         // std::this_thread::sleep_for(std::chrono::milliseconds(1000));
  115.         cout << argv[1] << ":[" << i << "]" << " " << arr3[i] << endl;
  116.         i = i + 1;
  117.     }
  118.  
  119. }
  120.  
  121.  
  122. ///////////////////////////////////////
  123. LPWSTR winApiFormat(char text[], wchar_t wtext2[128]) {
  124.     mbstowcs(wtext2, text, strlen(text) + 1);//Plus null
  125.     LPWSTR ptr = wtext2;
  126.     return ptr;
  127. }
  128. void titleSet(char a[]) {
  129.     wchar_t wtext2[128];
  130.     LPWSTR ptr2 = winApiFormat(a, wtext2);
  131.     SetConsoleTitle(ptr2);
  132. }
  133.  
  134. void setCharArg(char* cmd, char* arg) {
  135.     sprintf(cmd + strlen(cmd), " %s", arg);
  136. }
  137.  
  138. void controlProcess(int argc, char* argv[]) {
  139.     char title[128] = "SORT_L3";
  140.  
  141.     if (argc != 2) {
  142.         cerr << "Sort: Snachalo  zapustite MASTER.EXE.\n";
  143.         exit(1);
  144.     }
  145.  
  146.     //int pid = atoi(argv[1]);
  147.     // cout << "----------------" << argv[3] << "\n";
  148.     cout << "arguments: " << "\n";
  149.     cout << "----------------" << argv[1] << "\n";
  150.     cout << "----------------" << argv[0] << "\n";
  151.     setCharArg(title, argv[1]);
  152.     // setCharArg(title, argv[3]);
  153.     titleSet(title);
  154.  
  155.  
  156. }
  157.  
  158. void MutexStartOpen(HANDLE hMutex, char* argv[]) {
  159.     char mutextName[128] = "MasterMutext";
  160.     char cmd3[128] = "";
  161.     wchar_t wtext3[128];
  162.     strcpy(cmd3, mutextName);
  163.     LPWSTR ProcessLPWSTR = winApiFormat(cmd3, wtext3);
  164.     hMutex = OpenMutex(MUTEX_ALL_ACCESS, FALSE, ProcessLPWSTR);
  165.     if (hMutex == NULL) { cout << "OponMutext failed ERROR" << endl; }
  166.     else cout << "OpenMutext SUCCESS " << endl;
  167.     cout << "***---2" << hMutex << endl;
  168.     WaitForSingleObject(hMutex, INFINITE);
  169.     //getchar();
  170. }
  171. /////////////////////////////////////////////////
  172. int main(int argc, char* argv[])
  173. {
  174.     HANDLE hMutex;
  175.     MutexStartOpen(&hMutex, argv);
  176.  
  177.  
  178.     LiveTimer timeLive;
  179.     timeLive.Tstrt();
  180.  
  181.  
  182.     setlocale(LC_ALL, "ru");
  183.    
  184.     controlProcess(argc, argv);
  185.  
  186.     const int size = 100;
  187.     int arr[size];
  188.     int* arr2 = setArr(arr, size);
  189.     printArr(arr, size, argv);
  190.  
  191.     BubbleSort(arr, size);
  192.  
  193.     printArr(arr, size, argv);
  194.  
  195.  
  196.  
  197.     timeLive.iteration = size;
  198.     timeLive.Tend();
  199.     timeLive.printT();
  200.     cout << "Sort: EXIT\n";
  201.     ReleaseMutex(hMutex);
  202.     CloseHandle(hMutex);
  203.     //cin.get();
  204.     //Sleep(1);
  205.     return 0;
  206. }
  207.  
  208. // Запуск программы: CTRL+F5 или меню "Отладка" > "Запуск без отладки"
  209. // Отладка программы: F5 или меню "Отладка" > "Запустить отладку"
  210.  
  211. // Советы по началу работы
  212. //   1. В окне обозревателя решений можно добавлять файлы и управлять ими.
  213. //   2. В окне Team Explorer можно подключиться к системе управления версиями.
  214. //   3. В окне "Выходные данные" можно просматривать выходные данные сборки и другие сообщения.
  215. //   4. В окне "Список ошибок" можно просматривать ошибки.
  216. //   5. Последовательно выберите пункты меню "Проект" > "Добавить новый элемент", чтобы создать файлы кода, или "Проект" > "Добавить существующий элемент", чтобы добавить в проект существующие файлы кода.
  217. //   6. Чтобы снова открыть этот проект позже, выберите пункты меню "Файл" > "Открыть" > "Проект" и выберите SLN-файл.
  218.  
Add Comment
Please, Sign In to add comment