NickAndNick

Перестановка столбцов матрицы

Jun 27th, 2015
249
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <valarray>
  3. #include <random>
  4. #include <iomanip>
  5. #include <numeric>
  6. #include <algorithm>
  7.  
  8. using namespace std;
  9.  
  10. int randomize();
  11. void random_fill(valarray<int>& matrix);
  12. void print(valarray<int>& matrix, const size_t columns, const streamsize width = 5);
  13. void print(valarray<int>& line, const streamsize width = 5);
  14. valarray<int> permutation(valarray<int>& matrix, const size_t rows, const size_t columns);
  15. int specification(valarray<int>& column);
  16. int sum(valarray<int>& column);
  17. void print_sum(valarray<int>& matrix, const size_t rows, const size_t columns, const streamsize width = 5);
  18.  
  19. int main() {
  20.     setlocale(LC_CTYPE, "Russian");
  21.  
  22.     cout << "\n\t Введите количество строк матрицы: ";
  23.     size_t rows;
  24.     cin >> rows;
  25.  
  26.     cout << "\t Введите количество столбцов матрицы: ";
  27.     size_t columns;
  28.     cin >> columns;
  29.  
  30.     size_t size = rows * columns;
  31.     valarray<int> matrix(size);
  32.  
  33.     random_fill(matrix);
  34.     print(matrix, columns);
  35.     valarray<int> spec = permutation(matrix, rows, columns);
  36.     print(matrix, columns);
  37.     print(spec);
  38.     print_sum(matrix, rows, columns);
  39.  
  40.     cin.sync();
  41.     cin.get();
  42. }
  43.  
  44. int specification(valarray<int>& column) {
  45.     return abs(accumulate(begin(column), end(column), 0, [](int sum, int next) { return next < 0 ? sum + next : sum; }));
  46. }
  47.  
  48. int sum(valarray<int>& column) {
  49.     return accumulate(begin(column), end(column), 0);
  50. }
  51.  
  52. void print_sum(valarray<int>& matrix, const size_t rows, const size_t columns, const streamsize width) {
  53.     valarray<int> temp;
  54.  
  55.     cout << endl;
  56.  
  57.     for (size_t index = 0; index < columns; ++index) {
  58.         slice slise_matrix(index, rows, columns);
  59.         temp = matrix[slise_matrix];
  60.         if (specification(temp)) cout << setw(width) << sum(temp);
  61.         else cout << setw(width) << '-';
  62.     }
  63.  
  64.     cout << endl;
  65. }
  66.  
  67. valarray<int> permutation(valarray<int>& matrix, const size_t rows, const size_t columns) {
  68.     valarray<int> specifications(columns);
  69.     valarray<valarray<int>> temp(columns);
  70.  
  71.     for (size_t index = 0; index < columns; ++index) {
  72.         slice slise_matrix(index, rows, columns);
  73.         temp[index] = matrix[slise_matrix];
  74.         specifications[index] = specification(temp[index]);
  75.     }
  76.  
  77.     print(specifications);
  78.  
  79.     for (size_t n = 1; n < columns; ++n)
  80.         for (size_t m = 0; m < columns - n; ++m)
  81.             if (specifications[m] > specifications[m + 1]) {
  82.                 swap(specifications[m], specifications[m + 1]);
  83.                 swap(temp[m], temp[m + 1]);
  84.             }
  85.  
  86.     for (size_t j = 0, k = 0; j < rows; ++j) for (size_t i = 0; i < columns; ++i) matrix[k++] = temp[i][j];
  87.  
  88.     return specifications;
  89. }
  90.  
  91. void print(valarray<int>& line, const streamsize width) {
  92.     cout << endl;
  93.     for (auto element : line) cout << setw(width) << element;
  94.     cout << endl;
  95. }
  96.  
  97. void print(valarray<int>& matrix, const size_t columns, const streamsize width) {
  98.     size_t column = 0;
  99.  
  100.     cout << endl;
  101.  
  102.     for (auto element : matrix) {
  103.         cout << setw(width) << element;
  104.         if (++column % columns) continue;
  105.         cout << '\n';
  106.     }
  107. }
  108.  
  109. void random_fill(valarray<int>& matrix) {
  110.     generate(begin(matrix), end(matrix), randomize);
  111. }
  112.  
  113. int randomize() {
  114.     uniform_int_distribution<int> rand(-10, 10);
  115.     random_device rnd;
  116.     return rand(rnd);
  117. }
RAW Paste Data