Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- using namespace std;
- class CPosition
- {
- private:
- int _row; // íîìåð ñòðîêè
- int _col; // íîìåð ñòîëáöà
- public:
- CPosition()
- {
- _row = -1;
- }
- CPosition(int row, int col)
- {
- _row = row;
- _col = col;
- }
- void printPosition();
- };
- void CPosition::printPosition()
- {
- if (_row == -1)
- cout << "Position is undefined" << endl;
- else
- cout << "Position: (" << _row << ", " << _col << ")" << endl;
- }
- class CMatrix
- {
- private:
- int _rows; //êîë-âî ñòðîê
- int _cols; //êîë-âî ñòîëáöîâ
- int *_matrix; //
- public:
- CMatrix(int rows, int cols);
- ~CMatrix();
- int getElement(int row, int col);
- CPosition getPosition(int value);
- void printMatrix();
- };
- CMatrix::CMatrix(int rows, int cols)
- { _rows = rows;
- _cols = cols;
- _matrix = new int[rows*cols];
- for (int i = 0; i < rows*cols; ++i)
- _matrix[i] = 0;
- int phase, i, j , k;
- phase = 0; i = 0; j = cols - 1; k = 1;
- while (true)
- {
- if ((i >= 0) && (i < rows) && (j >= 0) && (j < cols) && (_matrix[j + _cols*i] == 0))
- {_matrix[j + _cols*i] = k; k++;}
- if ((i == (rows - 1) ) && (j == 0)) break;
- switch (phase)
- {
- case 0: j--;phase = 1;break;
- case 1: if (i == (rows - 1)) {phase = 2;} else {i++;j++;} break;
- case 2: j--;phase = 3;break;
- case 3: if (i == 0) {phase = 0;} else {i--;j--;} break;
- }
- }
- }
- CMatrix::~CMatrix()
- {
- delete _matrix;
- }
- int CMatrix::getElement(int row, int col)
- {
- if ((row > _rows) || (col > _cols) || (row <= 0) || (col <= 0))
- return -99999;
- return _matrix[(row - 1) * _cols + (col - 1) ];
- }
- CPosition CMatrix::getPosition(int value)
- {
- for (int i = 0; i < _rows; ++i)
- for (int j = 0; j < _cols; ++j)
- {
- if (this->getElement(i+1, j+1) == value)
- return CPosition(i+1, j+1);
- }
- return CPosition();
- }
- void CMatrix::printMatrix()
- {
- for (int i = 0; i < _rows; ++i)
- {
- for (int j = 0; j < _cols; ++j)
- {
- cout << setw(3) << _matrix[j + _cols*i] << " ";
- }
- cout << endl;
- }
- }
- int main(int argc, char *argv[])
- {
- int rows, cols, i, j;
- cout << "Enter quantity of rows: ";
- cin >> rows;
- cout << "Enter quantity of cols: ";
- cin >> cols;
- CMatrix m(rows, cols);
- m.printMatrix();
- cout << "--------" << endl;
- cout << "Enter quantity of i: ";
- cin >> i;
- cout << "Enter quantity of j: ";
- cin >> j;
- cout << m.getElement(i,j) << " " << endl;
- cout << "--------" << endl;
- cout << "Value: " ;
- cin >> i;
- m.getPosition(i).printPosition();
- system("Pause");
- return EXIT_SUCCESS;
- }
Add Comment
Please, Sign In to add comment