Advertisement
NickAndNick

Сдвиг в массиве

Apr 21st, 2016
327
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.20 KB | None | 0 0
  1. #include <iostream>
  2. #include <random>
  3. #include <utility>
  4. #include <iomanip>
  5. #include <string>
  6. #include <clocale>
  7.  
  8. using namespace std;
  9.  
  10. void rshift(int* arr, const int size);
  11. void rshift(int* arr, const int size, int count);
  12. void lshift(int* arr, const int size);
  13. void lshift(int* arr, const int size, int count);
  14. int random_int(int first, int last);
  15. void fill_random_int(int* arr, const int size, int first, int last);
  16. void print(int* arr, const int size, streamsize width, string message = "");
  17. void test_right_shift(int first, int last, streamsize width);
  18. void test_left_shift(int first, int last, streamsize width);
  19. void pause();
  20.  
  21. int main() {
  22.     int first = 1, last = 9;
  23.     streamsize width = 4;
  24.     setlocale(LC_CTYPE, "Russian");
  25.     test_right_shift(first, last, width);
  26.     test_left_shift(first, last, width);
  27.     pause();
  28. }
  29.  
  30. // Тестирует сдвиг влево на указанное количество элементов
  31. void test_left_shift(int first, int last, streamsize width) {
  32.     const int size = 10;
  33.     int arr[size];
  34.     fill_random_int(arr, size, first, last);
  35.     print(arr, size, width, "\nИсходный массив");
  36.     cout << "\nКоличество элементов сдвига влево: ";
  37.     int count;
  38.     cin >> count;
  39.     lshift(arr, size, count);
  40.     print(arr, size, width, "\nМассив после сдвига:\n\n");
  41. }
  42.  
  43. // Тестирует сдвиг вправо на указанное количество элементов
  44. void test_right_shift(int first, int last, streamsize width) {
  45.     const int size = 10;
  46.     int arr[size];
  47.     fill_random_int(arr, size, first, last);
  48.     print(arr, size, width, "\nИсходный массив");
  49.     cout << "\nКоличество элементов сдвига вправо: ";
  50.     int count;
  51.     cin >> count;
  52.     rshift(arr, size, count);
  53.     print(arr, size, width, "\nМассив после сдвига:\n\n");
  54. }
  55.  
  56. // Форматированный вывод массива в консоль, сопровождённый по желанию сообщением
  57. void print(int* arr, const int size, streamsize width, string message) {
  58.     cout << message;
  59.     for (int i = 0; i < size; ++i) cout << setw(width) << arr[i];
  60.     cout << endl;
  61. }
  62.  
  63. // Сдвигает массив на один элемент вправо
  64. void rshift(int* arr, const int size) {
  65.     if (1 == size) return;
  66.     if (2 == size) {
  67.         swap(arr[0], arr[1]);
  68.         return;
  69.     }
  70.     int last_index = size - 1;
  71.     int last_element = arr[last_index];
  72.     for (int current = last_index, prev = last_index - 1; prev >= 0; --current, --prev) arr[current] = arr[prev];
  73.     arr[0] = last_element;
  74. }
  75.  
  76. // Сдвигает массив на указанное количество элементов вправо
  77. void rshift(int* arr, const int size, int count) {
  78.     for (int i = 0; i < count; ++i) rshift(arr, size);
  79. }
  80.  
  81. // Сдвигает массив на один элемент влево
  82. void lshift(int* arr, const int size) {
  83.     if (1 == size) return;
  84.     if (2 == size) {
  85.         swap(arr[0], arr[1]);
  86.         return;
  87.     }
  88.     int last_index = size - 1;
  89.     int first_element = arr[0];
  90.     for (int current = 0, next = 1; next != size; ++current, ++next) arr[current] = arr[next];
  91.     arr[last_index] = first_element;
  92. }
  93.  
  94. // Сдвигает массив на указанное количество элементов влево
  95. void lshift(int* arr, const int size, int count) {
  96.     for (int i = 0; i < count; ++i) lshift(arr, size);
  97. }
  98.  
  99. // Заполняет массив случайными числами в указанных пределах
  100. void fill_random_int(int* arr, const int size, int first, int last) {
  101.     for (int i = 0; i < size; ++i) arr[i] = random_int(first, last);
  102. }
  103.  
  104. // Генерирует случайное число типа int в указанных пределах
  105. int random_int(int first, int last) {
  106.     if (first > last) swap(first, last);
  107.     uniform_int_distribution<int> rand(first, last);
  108.     random_device rnd;
  109.     return rand(rnd);
  110. }
  111.  
  112. // Не даёт консоли закрыться
  113. void pause() {
  114.     cin.sync();
  115.     cin.get();
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement