Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // SORT_L3.cpp : Этот файл содержит функцию "main". Здесь начинается и заканчивается выполнение программы.
- //
- #define _CRT_SECURE_NO_WARNINGS
- #include <iostream>
- #include <thread>
- #include <ctime>
- #include <cstdlib>
- #include <windows.h>
- #include <stdio.h>
- #include <string.h>
- using namespace std;
- class LiveTimer
- {
- public:
- int iteration;
- int DurationMilliSEC;
- int fMIN;
- int fSEC;
- int fMilliSEC;
- void Tstrt()
- {
- T_strt = std::chrono::high_resolution_clock::now();
- chrono::duration<float> Tstrt = T_strt - T_strt;
- cout << "Tstrt: " << Tstrt.count() << endl;
- }
- void Tend()
- {
- T_end = std::chrono::high_resolution_clock::now();
- chrono::duration<float> duration = (T_end - T_strt) * 1000;
- LiveTimer::DurationMilliSEC = duration.count();
- LiveTimer::fMIN = (duration.count() / 1000 / 60);
- LiveTimer::fSEC = int(duration.count() / 1000) % 60;
- LiveTimer::fMilliSEC = int(duration.count()) - fMIN * 60 * 1000 - fSEC * 1000;
- chrono::duration<float> Tstrt = T_strt - T_strt;
- cout << "Tend: " << duration.count() << endl;
- }
- void printT() {
- cout << " MilliSEC/iteration: " << LiveTimer::DurationMilliSEC / iteration << endl;
- cout << "MIN: " << LiveTimer::fMIN << " SEC: " << LiveTimer::fSEC << " MilliSEC: " << LiveTimer::fMilliSEC << endl;
- }
- private:
- std::chrono::time_point<std::chrono::steady_clock> T_strt, T_end;
- };
- int* setArr(int* arr, int size) {
- int i = 0;
- while (i < size) {
- arr[i] = (rand() % 100) + 1;
- // std::this_thread::sleep_for(std::chrono::milliseconds(1000));
- i = i + 1;
- }
- return arr;
- }
- void BubbleSort(int A[], int N) //сортировка пузырьком
- {
- int i, j, count, key;
- for (i = 0; i < N; i++)
- {
- for (j = 0; j < N - 1; j++)
- {
- key = j + 1;
- count = A[key];
- if (A[j] > A[key])
- {
- A[key] = A[j];
- A[j] = count;
- }
- }
- }
- cout << " массив отсортирован: "<<endl;
- // for (i = 0; i < N; i++) cout << A[i] << " "; //вывод массива
- }
- void quicksort(int* mas, int first, int last)
- {
- int mid, count;
- int f = first, l = last;
- mid = mas[(f + l) / 2]; //вычисление опорного элемента
- do
- {
- while (mas[f] < mid) f++;
- while (mas[l] > mid) l--;
- if (f <= l) //перестановка элементов
- {
- count = mas[f];
- mas[f] = mas[l];
- mas[l] = count;
- f++;
- l--;
- }
- } while (f < l);
- if (first < l) quicksort(mas, first, l);
- if (f < last) quicksort(mas, f, last);
- }
- void printArr(int* arr3, int n, char* argv[]) {
- // LiveTimer timeLive;
- int i = 0;
- while (i < n) {
- // std::this_thread::sleep_for(std::chrono::milliseconds(1000));
- cout << argv[1] << ":[" << i << "]" << " " << arr3[i] << endl;
- i = i + 1;
- }
- }
- ///////////////////////////////////////
- LPWSTR winApiFormat(char text[], wchar_t wtext2[128]) {
- mbstowcs(wtext2, text, strlen(text) + 1);//Plus null
- LPWSTR ptr = wtext2;
- return ptr;
- }
- void titleSet(char a[]) {
- wchar_t wtext2[128];
- LPWSTR ptr2 = winApiFormat(a, wtext2);
- SetConsoleTitle(ptr2);
- }
- void setCharArg(char* cmd, char* arg) {
- sprintf(cmd + strlen(cmd), " %s", arg);
- }
- void controlProcess(int argc, char* argv[]) {
- char title[128] = "SORT_L3";
- if (argc != 2) {
- cerr << "Sort: Snachalo zapustite MASTER.EXE.\n";
- exit(1);
- }
- //int pid = atoi(argv[1]);
- // cout << "----------------" << argv[3] << "\n";
- cout << "arguments: " << "\n";
- cout << "----------------" << argv[1] << "\n";
- cout << "----------------" << argv[0] << "\n";
- setCharArg(title, argv[1]);
- // setCharArg(title, argv[3]);
- titleSet(title);
- }
- void MutexStartOpen(HANDLE hMutex, char* argv[]) {
- char mutextName[128] = "MasterMutext";
- char cmd3[128] = "";
- wchar_t wtext3[128];
- strcpy(cmd3, mutextName);
- LPWSTR ProcessLPWSTR = winApiFormat(cmd3, wtext3);
- hMutex = OpenMutex(MUTEX_ALL_ACCESS, FALSE, ProcessLPWSTR);
- if (hMutex == NULL) { cout << "OponMutext failed ERROR" << endl; }
- else cout << "OpenMutext SUCCESS " << endl;
- cout << "***---2" << hMutex << endl;
- WaitForSingleObject(hMutex, INFINITE);
- //getchar();
- }
- /////////////////////////////////////////////////
- int main(int argc, char* argv[])
- {
- HANDLE hMutex;
- MutexStartOpen(&hMutex, argv);
- LiveTimer timeLive;
- timeLive.Tstrt();
- setlocale(LC_ALL, "ru");
- controlProcess(argc, argv);
- const int size = 100;
- int arr[size];
- int* arr2 = setArr(arr, size);
- printArr(arr, size, argv);
- BubbleSort(arr, size);
- printArr(arr, size, argv);
- timeLive.iteration = size;
- timeLive.Tend();
- timeLive.printT();
- cout << "Sort: EXIT\n";
- ReleaseMutex(hMutex);
- CloseHandle(hMutex);
- //cin.get();
- //Sleep(1);
- return 0;
- }
- // Запуск программы: CTRL+F5 или меню "Отладка" > "Запуск без отладки"
- // Отладка программы: F5 или меню "Отладка" > "Запустить отладку"
- // Советы по началу работы
- // 1. В окне обозревателя решений можно добавлять файлы и управлять ими.
- // 2. В окне Team Explorer можно подключиться к системе управления версиями.
- // 3. В окне "Выходные данные" можно просматривать выходные данные сборки и другие сообщения.
- // 4. В окне "Список ошибок" можно просматривать ошибки.
- // 5. Последовательно выберите пункты меню "Проект" > "Добавить новый элемент", чтобы создать файлы кода, или "Проект" > "Добавить существующий элемент", чтобы добавить в проект существующие файлы кода.
- // 6. Чтобы снова открыть этот проект позже, выберите пункты меню "Файл" > "Открыть" > "Проект" и выберите SLN-файл.
Add Comment
Please, Sign In to add comment