Guest User

Untitled

a guest
May 22nd, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.80 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3.  
  4. using namespace std;
  5.  
  6.  
  7. class CPosition
  8. {
  9. private:
  10. int _row; // íîìåð ñòðîêè
  11. int _col; // íîìåð ñòîëáöà
  12.  
  13. public:
  14. CPosition()
  15. {
  16. _row = -1;
  17. }
  18. CPosition(int row, int col)
  19. {
  20. _row = row;
  21. _col = col;
  22. }
  23. void printPosition();
  24.  
  25. };
  26.  
  27. void CPosition::printPosition()
  28. {
  29. if (_row == -1)
  30. cout << "Position is undefined" << endl;
  31. else
  32. cout << "Position: (" << _row << ", " << _col << ")" << endl;
  33. }
  34.  
  35.  
  36. class CMatrix
  37. {
  38. private:
  39.  
  40. int _rows; //êîë-âî ñòðîê
  41. int _cols; //êîë-âî ñòîëáöîâ
  42. int *_matrix; //
  43.  
  44. public:
  45. CMatrix(int rows, int cols);
  46. ~CMatrix();
  47.  
  48. int getElement(int row, int col);
  49. CPosition getPosition(int value);
  50.  
  51. void printMatrix();
  52. };
  53.  
  54. CMatrix::CMatrix(int rows, int cols)
  55. { _rows = rows;
  56. _cols = cols;
  57. _matrix = new int[rows*cols];
  58.  
  59. for (int i = 0; i < rows*cols; ++i)
  60. _matrix[i] = 0;
  61.  
  62. int phase, i, j , k;
  63. phase = 0; i = 0; j = cols - 1; k = 1;
  64.  
  65. while (true)
  66. {
  67. if ((i >= 0) && (i < rows) && (j >= 0) && (j < cols) && (_matrix[j + _cols*i] == 0))
  68. {_matrix[j + _cols*i] = k; k++;}
  69. if ((i == (rows - 1) ) && (j == 0)) break;
  70. switch (phase)
  71. {
  72. case 0: j--;phase = 1;break;
  73. case 1: if (i == (rows - 1)) {phase = 2;} else {i++;j++;} break;
  74. case 2: j--;phase = 3;break;
  75. case 3: if (i == 0) {phase = 0;} else {i--;j--;} break;
  76. }
  77.  
  78. }
  79. }
  80.  
  81. CMatrix::~CMatrix()
  82. {
  83. delete _matrix;
  84. }
  85.  
  86.  
  87. int CMatrix::getElement(int row, int col)
  88. {
  89. if ((row > _rows) || (col > _cols) || (row <= 0) || (col <= 0))
  90.  
  91. return -99999;
  92.  
  93. return _matrix[(row - 1) * _cols + (col - 1) ];
  94. }
  95.  
  96. CPosition CMatrix::getPosition(int value)
  97. {
  98. for (int i = 0; i < _rows; ++i)
  99. for (int j = 0; j < _cols; ++j)
  100. {
  101. if (this->getElement(i+1, j+1) == value)
  102. return CPosition(i+1, j+1);
  103. }
  104.  
  105. return CPosition();
  106. }
  107.  
  108. void CMatrix::printMatrix()
  109. {
  110. for (int i = 0; i < _rows; ++i)
  111. {
  112. for (int j = 0; j < _cols; ++j)
  113. {
  114. cout << setw(3) << _matrix[j + _cols*i] << " ";
  115. }
  116. cout << endl;
  117. }
  118. }
  119.  
  120. int main(int argc, char *argv[])
  121. {
  122. int rows, cols, i, j;
  123.  
  124. cout << "Enter quantity of rows: ";
  125. cin >> rows;
  126. cout << "Enter quantity of cols: ";
  127. cin >> cols;
  128.  
  129. CMatrix m(rows, cols);
  130. m.printMatrix();
  131.  
  132. cout << "--------" << endl;
  133.  
  134.  
  135. cout << "Enter quantity of i: ";
  136. cin >> i;
  137. cout << "Enter quantity of j: ";
  138. cin >> j;
  139.  
  140. cout << m.getElement(i,j) << " " << endl;
  141.  
  142. cout << "--------" << endl;
  143.  
  144. cout << "Value: " ;
  145. cin >> i;
  146.  
  147. m.getPosition(i).printPosition();
  148.  
  149. system("Pause");
  150. return EXIT_SUCCESS;
  151. }
Add Comment
Please, Sign In to add comment