Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.28 KB | None | 0 0
  1. #include <iostream>
  2. #include <chrono>
  3.  
  4. using namespace std;
  5.  
  6. int first, last; // сортировка
  7.  
  8. void fillArray(int* const Arr_A, int const size)
  9. {  
  10.     int sposob;
  11.     cout << "Выберите способ задания массива" << endl
  12.         << "1.Рандом" << endl
  13.         << "2.Ручной ввод" << endl;
  14.     cin >> sposob;
  15.     switch (sposob)
  16.     {
  17.     case 1:
  18.         srand(time(NULL));
  19.         cout << "Arr_A=";
  20.         for (int i = 0; i < size; i++) {
  21.             Arr_A[i] = rand() % 100;
  22.             cout << " " << Arr_A[i];
  23.         }
  24.         break;
  25.  
  26.     case 2:
  27.         cout << "Arr_A=";
  28.         for (int i = 0; i < size; i++) {
  29.             cin >> Arr_A[i];
  30.         }
  31.         break;
  32.  
  33.     default:
  34.         cout << "Нет такого способа\n" << endl;
  35.         return fillArray(Arr_A, size);
  36.         break;
  37.     }
  38. }
  39.  
  40. void showArray(int* const Arr_A, int const size)
  41. {
  42.     cout << "Массив Arr_A: ";
  43.     for (int i = 0; i < size; i++) cout << Arr_A[i] << ",";
  44. }
  45.  
  46. void quicksort(int* mas, int first, int last)
  47. {
  48.     int mid, count;        
  49.     int f = first, l = last;
  50.     mid = mas[(f + l) / 2]; //Вычисление опорного элемента
  51.     do
  52.     {
  53.         while (mas[f] < mid) f++;
  54.         while (mas[l] > mid) l--;
  55.         if (f <= l) //перестановка элементов
  56.         {
  57.             count = mas[f];
  58.             mas[f] = mas[l];
  59.             mas[l] = count;
  60.             f++;
  61.             l--;
  62.         }
  63.     } while (f < l);
  64.     if (first < l) quicksort(mas, first, l);
  65.     if (f < last) quicksort(mas, f, last);
  66.     }
  67.  
  68. void valueMinMax(int* mas, int n)
  69. {
  70.     int aver = (mas[0] + mas[n - 1]) / 2;
  71.     int k = 0;
  72.     cout << "Average = " << aver << endl;
  73.     for (int i = 0; i < n; i++)
  74.     {
  75.         if (mas[i] == aver) k++;
  76.         if (aver < mas[i]) break;
  77.     }
  78.     cout << endl << "Количество чисел массива равных среднему между min и max = " << k;
  79. }
  80.  
  81. void ab(int* mas, int n) {
  82.     int a, b;
  83.     int counta = 0, countb = 0;
  84.  
  85.     cout << "\nВведите число a:";
  86.     cin >> a;
  87.     cout << "Введите число b:";
  88.     cin >> b;
  89.     for (int i = 0; i < n; i++) {
  90.         if (mas[i] < a) {
  91.             counta++;
  92.         }
  93.         if (mas[i] > b) {
  94.             countb++;
  95.         }
  96.     }
  97.     cout << "Чисел меньше a:" << counta << endl;
  98.     cout << "Числе больше b:" << countb << endl;
  99.  
  100. }
  101.  
  102. void symmetry(int*& mas, int& size) {
  103.     int* Arr_B = new int[size * 2];
  104.     for (int i = 0; i < size; i++) {
  105.         Arr_B[i] = mas[i];
  106.         Arr_B[size * 2 - 1 - i] = mas[i];
  107.     }
  108.     delete[] mas;
  109.     mas = Arr_B;
  110.     cout << "Симметричный массив =";
  111.     for (int i = 0; i < size * 2; i++) cout << mas[i] << " ";
  112.  
  113. }
  114.  
  115. void timeDel(int*& mas, int& size)
  116. {
  117.     int indexDel;
  118.     cout << "\nВведите номер элемента для удаления:";
  119.     cin >> indexDel;
  120.     if (indexDel > size) {
  121.         cout << "Такого элемента нет! МАССИВ МАНЬШЕ!";
  122.         return ;
  123.     }
  124.     else {
  125.         std::chrono::time_point<std::chrono::system_clock> start, end;
  126.         start = std::chrono::system_clock::now();
  127.  
  128.         size--;
  129.  
  130.         for (int i = indexDel - 1; i < size; i++)
  131.         {
  132.             mas[i] = mas[i + 1];
  133.         }
  134.         int* masResized = new int[size];
  135.  
  136.         for (int i = 0; i < size; i++) masResized[i] = mas[i];
  137.  
  138.         delete[] mas;
  139.  
  140.         mas = masResized;
  141.  
  142.         end = std::chrono::system_clock::now();
  143.         std::chrono::duration<double> elapsed_seconds = end - start;
  144.  
  145.         cout << "Время затраченное на удаление = " << elapsed_seconds.count() << "s\n";
  146.         cout << "Новый массив:";
  147.         for (int i = 0; i < size; i++) cout << mas[i] << " ";
  148.         cout << endl;
  149.     }
  150.  
  151.    
  152.  
  153.  
  154. }
  155.  
  156. void timeAdd(int*& mas, int& size)
  157. {
  158.     int indexAdd, value;
  159.     cout << "\nНа какую позицию добавить элемент? " << "Позиция: ";
  160.     cin >> indexAdd;
  161.     cout << "Введитче число: ";
  162.     cin >> value;
  163.  
  164.     std::chrono::time_point<std::chrono::system_clock> start, end;
  165.     start = std::chrono::system_clock::now();
  166.  
  167.     size++;
  168.     int* masResized = new int[size];
  169.     masResized[indexAdd - 1] = value;
  170.     for (int i = 0; i < size; i++)
  171.     {
  172.         if (i < indexAdd - 1) {
  173.             masResized[i] = mas[i];
  174.         }
  175.         else if (i >= indexAdd - 1) {
  176.             masResized[i + 1] = mas[i];
  177.         }
  178.  
  179.     }
  180.  
  181.     delete[] mas;
  182.     mas = masResized;
  183.  
  184.     end = std::chrono::system_clock::now();
  185.     std::chrono::duration<double> elapsed_seconds = end - start;
  186.     cout << "Время затраченное на вставку = " << elapsed_seconds.count() << "s\n";
  187.  
  188.     cout << "Новый массив:";
  189.     for (int i = 0; i < size; i++) cout << mas[i] << " ";
  190.     cout << endl;
  191. }
  192.  
  193. void timeFind(int* mas, int size)
  194. {
  195.     int value;
  196.     cout << "\nВведите значние, которое хотите найти: ";
  197.     cin >> value;
  198.  
  199.     std::chrono::time_point<std::chrono::system_clock> start, end;
  200.     start = std::chrono::system_clock::now();
  201.  
  202.     int f = 0;
  203.     for (int i = 0; i < size; i++)
  204.     {
  205.         if (mas[i] == value) {
  206.             f++;
  207.             cout << "Такой же элемент под номером: " << i + 1;
  208.         }
  209.     }
  210.     if (f == 0) {
  211.         cout << "Таких элементов нет";
  212.     }
  213.  
  214.     end = std::chrono::system_clock::now();
  215.     std::chrono::duration<double> elapsed_seconds = end - start;
  216.     cout << "\nВремя затраченное на поиск = " << elapsed_seconds.count() << "s\n";
  217. }
  218.  
  219. void timeSwap(int* mas, int size)
  220. {
  221.     int index_1, index_2;
  222.     int tmp;
  223.     cout << "\nВведите первый номер элемента: ";
  224.     cin >> index_1;
  225.     cout << "Введите второй номер элемента: ";
  226.     cin >> index_2;
  227.  
  228.     std::chrono::time_point<std::chrono::system_clock> start, end;
  229.     start = std::chrono::system_clock::now();
  230.  
  231.     tmp = mas[index_1-1];
  232.     mas[index_1-1] = mas[index_2-1];
  233.     mas[index_2-1] = tmp;
  234.  
  235.     end = std::chrono::system_clock::now();
  236.     std::chrono::duration<double> elapsed_seconds = end - start;
  237.     cout << "Время затраченное на обмен = " << elapsed_seconds.count() << "s\n";
  238.     cout << "Новый массив:";
  239.     for (int i = 0; i < size; i++) cout << mas[i] << " ";
  240.  
  241.  
  242.  
  243. }
  244.  
  245. void main() {
  246.  
  247.     setlocale(0, "");
  248.     int size;
  249.     cout << "Введите размерность массива ";
  250.     while (!(cin >> size) || (cin.peek() != '\n'))
  251.     {
  252.         cin.clear();
  253.         while (cin.get() != '\n');
  254.         cout << "Ошибка ввода.Введите целое число!" << endl;
  255.     }
  256.     cout << "Размерность массива = " << size << endl;
  257.  
  258.     int* Arr_A = new int[size];
  259.  
  260.     fillArray(Arr_A, size);
  261.  
  262.     first = 0;                          //Сортировка
  263.     last = size - 1;
  264.     quicksort(Arr_A, first, last);
  265.     cout << endl << "Arr_A Sort=";
  266.     for (int i = 0; i < size; i++) cout << Arr_A[i] << " ";
  267.  
  268.     cout << endl << "Max=" << Arr_A[size - 1] << ", " << "Min=" << Arr_A[0] << endl; //Максимальное и минимальное значение
  269.                        
  270.     cout << endl;                      
  271.     valueMinMax(Arr_A, size);
  272.  
  273.     cout << endl;
  274.     ab(Arr_A, size);                        // больше a, меньше b
  275.  
  276.     timeDel(Arr_A,size);
  277.     timeAdd(Arr_A, size);
  278.     timeFind(Arr_A, size);
  279.     timeSwap(Arr_A, size);
  280.  
  281.  
  282.     symmetry(Arr_A, size);
  283.     delete[] Arr_A;
  284.     cout << endl;
  285.        
  286.     int repeat;
  287.     cout << "Выполнить программу повторно?" << endl
  288.         << "1.Да"
  289.         << "2.Нет" << endl;
  290.     cin >> repeat;
  291.     if (repeat == 1) {
  292.         main();
  293.     }
  294.     system("pause");
  295. }
  296.  
  297.  
  298.  
  299.  
  300.  
  301.  
  302. //while (!(cin >> size) || (cin.peek() != '\n'))
  303. //{
  304. //  cin.clear();
  305. //  while (cin.get() != '\n');
  306. //  cout << "Ошибка ввода.Введите целое число!" << endl;
  307. //}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement