Advertisement
NickAndNick

Двумерные динамические массивы. Удаление заданных колонок.

Dec 15th, 2012
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.65 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <string>
  4. #include <ctime>
  5.  
  6. using namespace std;
  7.  
  8. bool __cdecl random_matrix(int **, const size_t, const size_t, const int _left = -5, int _rigth = 51);
  9. void __cdecl show_matrix(int **, const size_t, const size_t);
  10. bool __cdecl is_positive_column(int **, const size_t, const size_t);
  11. void __cdecl delete_column(int **, int **, const size_t, const size_t, const size_t);
  12. void __cdecl copy_matrix(int **, int **, const size_t, const size_t);
  13. void __cdecl delete_matrix(int **, size_t);
  14. int ** __cdecl create_matrix(int **, const size_t, const size_t);
  15.  
  16. int main() {
  17.     srand(unsigned(time(NULL)));
  18.     wcout.imbue(locale("rus_rus.866"));
  19.  
  20.     wcout << L"Введите строки: ";
  21.     size_t rows;
  22.     cin >> rows;
  23.     wcout << L"Введите столбцы: ";
  24.     size_t cols;
  25.     cin >> cols;
  26.  
  27.     int ** matrix = NULL;
  28.     matrix = create_matrix(matrix, rows, cols);
  29.  
  30.     if (random_matrix(matrix, rows, cols)) {
  31.         wcout << L"Исходная матрица:\n";
  32.         show_matrix(matrix, rows, cols);
  33.  
  34.         for (size_t index = 0; index < cols; index++)
  35.             if (is_positive_column(matrix, rows, index)) {
  36.                 int ** temp = NULL;
  37.                 temp = create_matrix(temp, rows, cols);
  38.                 copy_matrix(temp, matrix, rows, cols);
  39.                 delete_matrix(matrix, rows);
  40.                 size_t new_cols = cols - 1;
  41.                 matrix = create_matrix(matrix, rows, new_cols);
  42.                 delete_column(matrix, temp, rows, cols, index);
  43.                 --cols;
  44.                 --index;
  45.                 delete_matrix(temp, rows);
  46.                 temp = NULL;
  47.             }
  48.  
  49.         wcout << L"Изменённая матрица:\n";
  50.         show_matrix(matrix, rows, cols);
  51.     } else wcout << L"\aНе удалось заполнить матрицу!\n";
  52.  
  53.     delete_matrix(matrix, rows);
  54.     matrix = NULL;
  55.  
  56.     cin.get(); cin.get();
  57. }
  58.  
  59. inline bool random_matrix(int ** _matrix, const size_t _rows, const size_t _cols, const int _left, int _rigth) {
  60.     if (_left >= _rigth) return false;
  61.  
  62.     if (_left < 0 && _rigth >= 0) _rigth += abs(_left);
  63.     else if (_left < 0 && _rigth < 0) _rigth = abs(_left) - abs(_rigth);
  64.     else if (_left >= 0 && _rigth > 0) _rigth -= _left;
  65.  
  66.     for (size_t row = 0; row < _rows; row++)
  67.         for (size_t col = 0; col < _cols; col++)
  68.             _matrix[row][col] = rand() % _rigth + _left;
  69.  
  70.     return true;
  71. }
  72.  
  73. void show_matrix(int ** _matrix, const size_t _rows, const size_t _cols) {
  74.     if (_cols && _rows)
  75.         for (size_t row = 0; row < _rows; row++, cout << endl)
  76.             for (size_t col = 0; col < _cols; col++)
  77.                 cout << setw(5) << _matrix[row][col];
  78.     else wcout << L"Нет данных для отображения!\n";
  79. }
  80.  
  81. bool is_positive_column(int ** _matrix, const size_t _rows, const size_t _col_index) {
  82.     for (size_t row = 0; row < _rows; row++)
  83.         if (_matrix[row][_col_index] < 0)
  84.             return false;
  85.     return true;
  86. }
  87.  
  88. void delete_column(int ** _matrix, int ** _temp, const size_t _rows, const size_t _cols, const size_t _col_index_del) {
  89.     size_t next;
  90.  
  91.     for (size_t row = 0; row < _rows; row++) {
  92.         next = 0;
  93.  
  94.         for (size_t col = 0; col < _cols; col++)
  95.             if (col != _col_index_del)
  96.                 _matrix[row][next++] = _temp[row][col];
  97.     }
  98. }
  99.  
  100. void copy_matrix(int ** _in_matrix, int ** _from_matrix, const size_t _rows, const size_t _cols) {
  101.     for (size_t row = 0; row < _rows; row++)
  102.         for (size_t col = 0; col < _cols; col++)
  103.             _in_matrix[row][col] = _from_matrix[row][col];
  104. }
  105.  
  106. void delete_matrix(int ** _matrix, size_t _rows) {
  107.     for (size_t row = 0; row < _rows; row++)
  108.         delete[] _matrix[row];
  109.     delete[] _matrix;
  110. }
  111.  
  112. int ** create_matrix(int ** _matrix, const size_t _rows, const size_t _cols) {
  113.     _matrix = new int * [_rows];
  114.  
  115.     for (size_t row = 0; row < _rows; row++)
  116.         _matrix[row] = new int [_cols];
  117.  
  118.     return _matrix;
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement