Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <random>
- #include <utility>
- #include <iomanip>
- #include <string>
- #include <clocale>
- using namespace std;
- void rshift(int* arr, const int size);
- void rshift(int* arr, const int size, int count);
- void lshift(int* arr, const int size);
- void lshift(int* arr, const int size, int count);
- int random_int(int first, int last);
- void fill_random_int(int* arr, const int size, int first, int last);
- void print(int* arr, const int size, streamsize width, string message = "");
- void test_right_shift(int first, int last, streamsize width);
- void test_left_shift(int first, int last, streamsize width);
- void pause();
- int main() {
- int first = 1, last = 9;
- streamsize width = 4;
- setlocale(LC_CTYPE, "Russian");
- test_right_shift(first, last, width);
- test_left_shift(first, last, width);
- pause();
- }
- // Тестирует сдвиг влево на указанное количество элементов
- void test_left_shift(int first, int last, streamsize width) {
- const int size = 10;
- int arr[size];
- fill_random_int(arr, size, first, last);
- print(arr, size, width, "\nИсходный массив");
- cout << "\nКоличество элементов сдвига влево: ";
- int count;
- cin >> count;
- lshift(arr, size, count);
- print(arr, size, width, "\nМассив после сдвига:\n\n");
- }
- // Тестирует сдвиг вправо на указанное количество элементов
- void test_right_shift(int first, int last, streamsize width) {
- const int size = 10;
- int arr[size];
- fill_random_int(arr, size, first, last);
- print(arr, size, width, "\nИсходный массив");
- cout << "\nКоличество элементов сдвига вправо: ";
- int count;
- cin >> count;
- rshift(arr, size, count);
- print(arr, size, width, "\nМассив после сдвига:\n\n");
- }
- // Форматированный вывод массива в консоль, сопровождённый по желанию сообщением
- void print(int* arr, const int size, streamsize width, string message) {
- cout << message;
- for (int i = 0; i < size; ++i) cout << setw(width) << arr[i];
- cout << endl;
- }
- // Сдвигает массив на один элемент вправо
- void rshift(int* arr, const int size) {
- if (1 == size) return;
- if (2 == size) {
- swap(arr[0], arr[1]);
- return;
- }
- int last_index = size - 1;
- int last_element = arr[last_index];
- for (int current = last_index, prev = last_index - 1; prev >= 0; --current, --prev) arr[current] = arr[prev];
- arr[0] = last_element;
- }
- // Сдвигает массив на указанное количество элементов вправо
- void rshift(int* arr, const int size, int count) {
- for (int i = 0; i < count; ++i) rshift(arr, size);
- }
- // Сдвигает массив на один элемент влево
- void lshift(int* arr, const int size) {
- if (1 == size) return;
- if (2 == size) {
- swap(arr[0], arr[1]);
- return;
- }
- int last_index = size - 1;
- int first_element = arr[0];
- for (int current = 0, next = 1; next != size; ++current, ++next) arr[current] = arr[next];
- arr[last_index] = first_element;
- }
- // Сдвигает массив на указанное количество элементов влево
- void lshift(int* arr, const int size, int count) {
- for (int i = 0; i < count; ++i) lshift(arr, size);
- }
- // Заполняет массив случайными числами в указанных пределах
- void fill_random_int(int* arr, const int size, int first, int last) {
- for (int i = 0; i < size; ++i) arr[i] = random_int(first, last);
- }
- // Генерирует случайное число типа int в указанных пределах
- int random_int(int first, int last) {
- if (first > last) swap(first, last);
- uniform_int_distribution<int> rand(first, last);
- random_device rnd;
- return rand(rnd);
- }
- // Не даёт консоли закрыться
- void pause() {
- cin.sync();
- cin.get();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement