Advertisement
Leeen

lab for Gaidel' №3 var 25

Nov 5th, 2018
346
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.62 KB | None | 0 0
  1. // Лабораторная для Гайделя №3 вариант 25
  2.  
  3. #include<iostream>
  4. #include<vector>
  5.  
  6. using namespace std;
  7.  
  8.  
  9. //ввод длины
  10. int inputRows()
  11. {
  12.     cout << "input the number of rows: ";
  13.     int rows;
  14.     cin >> rows;
  15.     cin.clear();
  16.  
  17.     if (cin.get() != '\n' || rows <= 0) {
  18.         while (cin.get() != '\n');
  19.         cout << "error inputing! try again" << endl;
  20.         return inputRows();
  21.     }
  22.  
  23.     return rows;
  24. }
  25.  
  26.  
  27. // ввод высоты
  28. int inputColumns()
  29. {
  30.     cout << "input the number of columns: ";
  31.     int columns;
  32.     cin >> columns;
  33.     cin.clear();
  34.  
  35.     if (cin.get() != '\n' || columns <= 0) {
  36.         while (cin.get() != '\n');
  37.         cout << "error inputing! try again" << endl;
  38.         return inputColumns();
  39.     }
  40.  
  41.     return columns;
  42. }
  43.  
  44. //Продолжение
  45. bool Prod()
  46. {
  47.     cout << "Continue Y/N" << endl;
  48.     char yn;
  49.     cin >> yn;
  50.     cin.clear();
  51.     if (yn == 'Y' && cin.get() == '\n') return true;
  52.     else if (yn == 'N' && cin.get() == '\n') return false;
  53.     else
  54.     {
  55.         while (cin.get() != '\n');
  56.         cout << "Error. Try again" << endl;
  57.         Prod();
  58.     }
  59. }
  60.  
  61.  
  62. int main()
  63. {
  64.  
  65.     do {
  66.         int rows = inputRows();
  67.         int columns = inputColumns();
  68.  
  69.         vector < vector <int> > setVector(rows, vector <int>(columns));
  70.  
  71.  
  72.        
  73.         //заполнение массива
  74.         for (int i = 0; i < rows; i++)
  75.         {
  76.             for (int j = 0; j < columns; j++)
  77.             {
  78.                 bool vecto = true;
  79.  
  80.                 while (vecto) {
  81.                 cout << "input element (" << i + 1 << ", " << j + 1 << "): ";
  82.                 cin >> setVector[i][j];
  83.                 cin.clear();
  84.                 if (cin.get() != '\n' || setVector[i][j] < 0)
  85.                 {
  86.                     while (cin.get() != '\n') vecto = true;
  87.                     cout << "error inputing! try again" << endl;
  88.                 }
  89.                 else
  90.                     vecto = false;
  91.                 }
  92.             }
  93.         }
  94.            
  95.        
  96.         cout << endl;
  97.  
  98.         //вывод массива
  99.         for (int i = 0; i < rows; i++)
  100.         {
  101.             for (int j = 0; j < columns; j++)
  102.             {
  103.                 cout << setVector[i][j] << ' ';
  104.             }
  105.             cout << endl;
  106.         }
  107.  
  108.         cout << endl << "result:" << endl;
  109.  
  110.         //сортировка строк матрицы
  111.         for (int i = 0; i < rows - 1; i++)
  112.         {
  113.             for (int y = i + 1; y < rows; y++)
  114.             {
  115.                 for (int j = 0; j < columns; j++)
  116.                 {
  117.                     if (setVector[i][j] > setVector[y][j])
  118.                     {
  119.                         for (int x = 0; x < columns; x++)
  120.                             swap(setVector[i][x], setVector[y][x]);
  121.  
  122.                         break;
  123.                     }
  124.                     else if (setVector[i][j] < setVector[y][j]) {
  125.                         break;
  126.                     }
  127.                 }
  128.             }
  129.         }
  130.  
  131.         //вывод результата
  132.         for (int i = 0; i < rows; i++)
  133.             {
  134.                 for (int j = 0; j < columns; j++)
  135.                 {
  136.                     cout << setVector[i][j] << ' ';
  137.                 }
  138.                 cout << endl;
  139.             }
  140.         } while (Prod());
  141.  
  142.         system("pause");
  143.         return 0;
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement