Advertisement
HyperSensualNarwhal

Циклический сдвиг одномерного массива

Jan 15th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.09 KB | None | 0 0
  1. #include <iostream>
  2. #include <ctime>
  3. #include <conio.h>
  4.  
  5. using namespace std;
  6.  
  7. #define TAB '\t';
  8.  
  9. void main()
  10. {
  11.     srand(time(NULL));
  12.  
  13.     const int sz = 5;
  14.  
  15.     int alpha[sz];
  16.  
  17.     int tmp, m, b = 0;
  18.     char snsr = 0;
  19.  
  20.  
  21.     int psh = (setlocale(0, ""), cout << "Введите количество элементов для сдвига: ", cin >> psh, cout << endl, psh);
  22.  
  23.     for (int i = 0; i < sz; i++)
  24.         cout << (alpha[i] = i) << TAB;
  25.    
  26.     while (snsr != 27)
  27.     {
  28.  
  29.         snsr = getch();
  30.         system("cls");
  31.         if (snsr == 65 || snsr == 97) m = -1; // A
  32.         if (snsr == 68 || snsr == 100) m = 1; // D
  33.  
  34.         if (m == -1)
  35.         {
  36.             for (int i = 0; i < psh; ++i)
  37.             {
  38.                 for (int j = b; j < sz - 1; ++j)
  39.                 {
  40.                     tmp = alpha[j];
  41.                     alpha[j] = alpha[j + 1];
  42.                     alpha[j + 1] = tmp;
  43.  
  44.                 }
  45.             }
  46.         }
  47.         else
  48.         {
  49.             for (int i = 0; i < psh; ++i)
  50.             {
  51.                 for (int j = sz - 1; j > b; --j)
  52.                 {
  53.                     tmp = alpha[j];
  54.                     alpha[j] = alpha[j - 1];
  55.                     alpha[j - 1] = tmp;
  56.                 }
  57.             }
  58.         }
  59.  
  60.         cout << endl;
  61.  
  62.         for (int i = 0; i < sz; i++)
  63.             cout << alpha[i] << TAB;
  64.        
  65.     }
  66.  
  67.     cout << endl;
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement