Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <fstream>
- //#include <codecvt>
- using namespace std;
- #define RET_OK (0)
- #define RET_AGN (1)
- #define RET_EXT (2)
- #define RET_ERR (-1)
- #define M_TYPE __int64 // type of matrix
- class Matrix
- {
- private:
- M_TYPE** a;
- bool isnull;
- public:
- struct matrixSize
- {
- int rows;
- int cols;
- };
- matrixSize size;
- Matrix(int rows, int cols)
- {
- isnull = 1;
- a = new M_TYPE* [rows];
- for(int i = 0; i < rows; i++)
- a[i] = new M_TYPE [cols];
- for(int i = 0; i < rows; i++)
- for(int j = 0; j < cols; j++)
- a[i][j] = 0;
- size.cols = cols;
- size.rows = rows;
- }
- void InputF(ifstream &in)
- {
- //string checker;
- try
- {
- for(int i = 0; i < size.rows; i++)
- for(int j = 0; j < size.cols; j++)
- {
- if(!(in >> a[i][j]))
- {
- exception ex;
- throw ex;
- }
- /*
- in >> checker;
- for(int k = 0; k < (int)checker.size(); k++)
- {
- if(checker[k] < '0' || checker[k] > '9')
- {
- exception ex;
- throw ex;
- }
- }
- __int64 ex = stoll(checker);
- */
- //a[i][j] = stoll(checker);
- }
- }
- catch(...)
- {
- return;
- }
- isnull = 0;
- return;
- }
- void Output(ostream &out)
- {
- unsigned char d[11] = {
- 201, // 0
- 205, // 1
- 203, // 2
- 187, // 3
- 186, // 4
- 204, // 5
- 206, // 6
- 185, // 7
- 200, // 8
- 202, // 9
- 188 // 10
- };
- int* max_colc = new int [size.cols];
- for(int i = 0; i < size.cols; i++)
- for(int j = 0; j < size.rows; j++)
- {
- string s = to_string(static_cast<__int64>(a[j][i]));
- max_colc[i] = max(max_colc[i], static_cast<int>(s.size()));
- }
- for(int i = 0; i < size.rows; i++)
- {
- switch(i)
- {
- case 0:
- {
- // head
- out << d[0];
- for(int j = 0; j < size.cols; j++)
- {
- for(int k = 0; k < max_colc[j] + 2; k++)
- out << d[1];
- out << (j == size.cols - 1 ? d[3] : d[2]);
- }
- out << endl;
- // inf
- out << d[4] << ' ';
- for(int j = 0; j < size.cols; j++)
- {
- string s = to_string(static_cast<__int64>(a[i][j]));
- int elem_c = s.size();
- for(int k = 0; k < (max_colc[j] - elem_c) / 2; k++) // | 123 |
- out << ' ';
- string arr_size_str = to_string(static_cast<__int64>(a[i][j]));
- int arr_size = arr_size_str.size();
- if(max_colc[j] % 2)
- {
- if(arr_size % 2)
- out << a[i][j];
- else
- {
- if(a[i][j] >= 0)
- out << 0 << a[i][j];
- else
- out << ' ' << a[i][j];
- }
- }
- else
- {
- if(arr_size % 2)
- {
- if(a[i][j] >= 0)
- out << 0 << a[i][j];
- else
- out << ' ' << a[i][j];
- }
- else
- out << a[i][j];
- }
- for(int k = 0; k < (max_colc[j] - elem_c) / 2; k++) // | 123 |
- out << ' ';
- out << ' ' << d[4] << ' ';
- }
- out << endl;
- // bottom
- if(size.rows == 1)
- {
- out << d[8];
- for(int j = 0; j < size.cols; j++)
- {
- for(int k = 0; k < max_colc[j] + 2; k++)
- out << d[1];
- out << (j == size.cols - 1 ? d[10] : d[9]);
- }
- }
- break;
- }
- default:
- {
- // head
- out << d[5];
- for(int j = 0; j < size.cols; j++)
- {
- for(int k = 0; k < max_colc[j] + 2; k++)
- out << d[1];
- out << (j == size.cols - 1 ? d[7] : d[6]);
- }
- out << endl;
- // inf
- out << d[4] << ' ';
- for(int j = 0; j < size.cols; j++)
- {
- string s = to_string(static_cast<__int64>(a[i][j]));
- int elem_c = s.size();
- for(int k = 0; k < (max_colc[j] - elem_c) / 2; k++) // | 123 |
- out << ' ';
- string arr_size_str = to_string(static_cast<__int64>(a[i][j]));
- int arr_size = arr_size_str.size();
- if(max_colc[j] % 2)
- {
- if(arr_size % 2)
- out << a[i][j];
- else
- {
- if(a[i][j] >= 0)
- out << 0 << a[i][j];
- else
- out << ' ' << a[i][j];
- }
- }
- else
- {
- if(arr_size % 2)
- {
- if(a[i][j] >= 0)
- out << 0 << a[i][j];
- else
- out << ' ' << a[i][j];
- }
- else
- out << a[i][j];
- }
- for(int k = 0; k < (max_colc[j] - elem_c) / 2; k++) // | 123 |
- out << ' ';
- out << ' ' << d[4] << ' ';
- }
- out << endl;
- // bottom
- if(i == size.rows - 1)
- {
- out << d[8];
- for(int j = 0; j < size.cols; j++)
- {
- for(int k = 0; k < max_colc[j] + 2; k++)
- out << d[1];
- out << (j == size.cols - 1 ? d[10] : d[9]);
- }
- }
- break;
- }
- }
- }
- out << "\n\n";
- return;
- }
- void Set(int row, int col, M_TYPE value)
- {
- a[row][col] = value;
- isnull = 0;
- return;
- }
- M_TYPE Get(int row, int col)
- {
- return a[row][col];
- }
- bool IsNull()
- {
- return isnull;
- }
- bool IsSquare()
- {
- return (size.cols == size.rows);
- }
- };
- void clearScreen();
- void work(bool &);
- int chooseMode(int &, bool);
- int checkMode(int &);
- void catchWrong(char &);
- int chooseAction(int &);
- void pause();
- void sumMatrix(Matrix a, Matrix b, Matrix c, Matrix::matrixSize size);
- int chooseNext();
- void multMatrixToMatrix(Matrix a, Matrix b, Matrix c, Matrix::matrixSize size, int);
- void multMatrixToNumber(Matrix a, int, Matrix c, Matrix::matrixSize size);
- int getNumberForMult(int &);
- int getMatrixForChoose(int &);
- void powMatrix(Matrix a, int, Matrix c, Matrix::matrixSize size);
- void transposeMatrix(Matrix a, Matrix c, Matrix::matrixSize size);
- void getDeterminant(Matrix a, __int64 &det, int n);
- __int64 getMinor(Matrix a, int n, int ii, int jj);
- //__int64 getDeterminant(Matrix a, int n);
- //void getNewMatrix(Matrix a, Matrix c, int n, int ii, int jj);
- int main()
- {
- bool w = 1;
- while(w)
- work(w);
- return 0;
- }
- void work(bool &w)
- {
- int mode;
- if(chooseMode(mode, true) == RET_OK)
- {
- switch(checkMode(mode))
- {
- case RET_AGN:
- {
- clearScreen();
- return;
- }
- case RET_EXT:
- {
- w = 0;
- return;
- }
- case RET_ERR:
- {
- w = 0;
- return;
- }
- case RET_OK:
- {
- switch(mode)
- {
- case 1:
- {
- ifstream in("input.txt");
- if(in == NULL)
- {
- clearScreen();
- cout << "\nThere's something wrong with \"input.txt\" file. \nAre you sure you created it?\n\nAnyway, program will exit.\n\n";
- w = 0;
- pause();
- return;
- }
- //ofstream out("output.txt");
- int row, col;
- in >> row >> col;
- Matrix a(row, col);
- a.InputF(in);
- in >> row >> col;
- Matrix b(row, col);
- b.InputF(in);
- if(a.IsNull() || b.IsNull())
- {
- clearScreen();
- cout << "\nThere's something wrong with \"input.txt\" file. \nAre you sure you wrote data correctly?\n\nAnyway, program will exit.\n\n";
- w = 0;
- pause();
- return;
- }
- clearScreen();
- cout << "So, we read two matrixes from \"input.txt\": \nMatrix A: \n";
- a.Output(cout);
- cout << "Matrix B: \n";
- b.Output(cout);
- pause();
- clearScreen();
- int action = 0;
- if(chooseAction(action) == RET_ERR)
- {
- char ch = '\0';
- do
- {
- catchWrong(ch);
- if(ch == 'y')
- {
- w = 0;
- return;
- }
- else if(ch == 'n')
- return;
- } while(ch != 'y' && ch != 'n');
- }
- switch(action)
- {
- case 1: // a + b
- {
- clearScreen();
- if(a.size.rows != b.size.rows || a.size.cols != b.size.cols)
- {
- cout << "Matrix sizes are mismatch!\nProgram will be exit\n\n";
- w = 0;
- pause();
- return;
- }
- cout << "We add this matrix (A): \n";
- a.Output(cout);
- cout << "To this (B): \n";
- b.Output(cout);
- cout << "And we have such result: \n";
- Matrix c(a.size.rows, a.size.cols);
- sumMatrix(a, b, c, c.size);
- c.Output(cout);
- int next = chooseNext();
- if(next == RET_AGN)
- return;
- else
- {
- w = 0;
- return;
- }
- break;
- }
- case 2: // a * b
- {
- clearScreen();
- if(a.size.rows != b.size.cols)
- {
- cout << "Matrix sizes are mismatch!\nProgram will be exit\n\n";
- w = 0;
- pause();
- return;
- }
- cout << "We multiplicate this matrix (A): \n";
- a.Output(cout);
- cout << "To this (B): \n";
- b.Output(cout);
- cout << "And we have such result: \n";
- Matrix c(a.size.rows, a.size.cols);
- Matrix::matrixSize size;
- size.cols = b.size.cols;
- size.rows = a.size.rows;
- multMatrixToMatrix(a, b, c, size, b.size.cols);
- c.Output(cout);
- int next = chooseNext();
- if(next == RET_AGN)
- return;
- else
- {
- w = 0;
- return;
- }
- break;
- }
- case 3: // n * a
- {
- clearScreen();
- cout << "We multiplicate matrix to some number. List: \n1. A\n2. B\nChoose: ";
- int matr;
- if(getMatrixForChoose(matr) == RET_ERR)
- {
- char ch = '\0';
- do
- {
- catchWrong(ch);
- if(ch == 'y')
- {
- w = 0;
- return;
- }
- else if(ch == 'n')
- return;
- } while(ch != 'y' && ch != 'n');
- }
- matr == 237 ? a.Output(cout) : b.Output(cout);
- cout << "To this number (enter it faster): ";
- int num;
- if(getNumberForMult(num) == RET_ERR)
- {
- char ch = '\0';
- do
- {
- catchWrong(ch);
- if(ch == 'y')
- {
- w = 0;
- return;
- }
- else if(ch == 'n')
- return;
- } while(ch != 'y' && ch != 'n');
- }
- cout << "And we have such result: \n";
- Matrix::matrixSize size = (matr == 237 ? a.size : b.size);
- Matrix c(size.rows, size.cols);
- if(matr == 237)
- multMatrixToNumber(a, num, c, size);
- else
- multMatrixToNumber(b, num, c, size);
- c.Output(cout);
- int next = chooseNext();
- if(next == RET_AGN)
- return;
- else
- {
- w = 0;
- return;
- }
- break;
- }
- case 4: // a ^ n
- {
- clearScreen();
- cout << "We power matrix to some exponent. List: \n1. A\n2. B\nChoose: ";
- int matr;
- if(getMatrixForChoose(matr) == RET_ERR)
- {
- char ch = '\0';
- do
- {
- catchWrong(ch);
- if(ch == 'y')
- {
- w = 0;
- return;
- }
- else if(ch == 'n')
- return;
- } while(ch != 'y' && ch != 'n');
- }
- matr == 237 ? a.Output(cout) : b.Output(cout);
- if(matr == 237)
- {
- if(!a.IsSquare())
- {
- cout << "\n\nCome on, dude, you see, matrix is non-square?\nYeah, you see. I won't power it, good bye!\n";
- w = 0;
- pause();
- return;
- }
- }
- else
- {
- if(!b.IsSquare())
- {
- cout << "\n\nCome on, dude, you see, matrix is non-square?\nYeah, you see. I won't power it, good bye!\n";
- w = 0;
- pause();
- return;
- }
- }
- cout << "To this exponent (enter it, come on): ";
- int num;
- if(getNumberForMult(num) == RET_ERR)
- {
- char ch = '\0';
- do
- {
- catchWrong(ch);
- if(ch == 'y')
- {
- w = 0;
- return;
- }
- else if(ch == 'n')
- return;
- } while(ch != 'y' && ch != 'n');
- }
- cout << "And we have such result: \n";
- Matrix::matrixSize size = (matr == 237 ? a.size : b.size);
- Matrix c(size.rows, size.cols);
- if(matr == 237)
- powMatrix(a, num, c, size);
- else
- powMatrix(b, num, c, size);
- c.Output(cout);
- int next = chooseNext();
- if(next == RET_AGN)
- return;
- else
- {
- w = 0;
- return;
- }
- break;
- }
- case 5: // trans a
- {
- clearScreen();
- cout << "We transpose matrix. List: \n1. A\n2. B\nChoose matrix: ";
- int matr;
- if(getMatrixForChoose(matr) == RET_ERR)
- {
- char ch = '\0';
- do
- {
- catchWrong(ch);
- if(ch == 'y')
- {
- w = 0;
- return;
- }
- else if(ch == 'n')
- return;
- } while(ch != 'y' && ch != 'n');
- }
- matr == 237 ? a.Output(cout) : b.Output(cout);
- cout << "And we have such result: \n";
- Matrix::matrixSize size;
- size.rows = (matr == 237 ? a.size.cols : b.size.cols);
- size.cols = (matr == 237 ? a.size.rows : b.size.rows);
- Matrix c(size.rows, size.cols);
- if(matr == 237)
- transposeMatrix(a, c, size);
- else
- transposeMatrix(b, c, size);
- c.Output(cout);
- int next = chooseNext();
- if(next == RET_AGN)
- return;
- else
- {
- w = 0;
- return;
- }
- break;
- }
- case 6:
- {
- clearScreen();
- cout << "We get determinant of one of matrixes. List: \n1. A\n2. B\nChoose: ";
- int matr;
- if(getMatrixForChoose(matr) == RET_ERR)
- {
- char ch = '\0';
- do
- {
- catchWrong(ch);
- if(ch == 'y')
- {
- w = 0;
- return;
- }
- else if(ch == 'n')
- return;
- } while(ch != 'y' && ch != 'n');
- }
- if(matr == 237)
- {
- if(!a.IsSquare())
- {
- cout << "\n\nCome on, dude, you see, matrix is non-square?\nYeah, you see. I won't power it, good bye!\n";
- w = 0;
- pause();
- return;
- }
- }
- else
- {
- if(!b.IsSquare())
- {
- cout << "\n\nCome on, dude, you see, matrix is non-square?\nYeah, you see. I won't power it, good bye!\n";
- w = 0;
- pause();
- return;
- }
- }
- matr == 237 ? a.Output(cout) : b.Output(cout);
- cout << "And determinant of matrix is: ";
- __int64 det = 0;
- matr == 237 ? getDeterminant(a, det, a.size.rows) : getDeterminant(b, det, b.size.rows);
- cout << det << "\n";
- int next = chooseNext();
- if(next == RET_AGN)
- return;
- else
- {
- w = 0;
- return;
- }
- break;
- }
- } // switch
- }
- }
- break;
- }
- }
- }
- else
- {
- char ch = '\0';
- do
- {
- catchWrong(ch);
- if(ch == 'y')
- {
- w = 0;
- return;
- }
- else if(ch == 'n')
- return;
- } while(ch != 'y' && ch != 'n');
- }
- return;
- }
- int chooseMode(int &mode, bool first)
- {
- if(!first) cin.ignore(numeric_limits<streamsize>::max(), '\n');
- clearScreen();
- printf("\nSet of 2 matrix: A & B\nChoose how would you create matrixes: \n1) Read from file \"INPUT.TXT\"\n2) Use the great random\n3) Enter manually\n\nEnter number: ");
- if(scanf("%i", &mode) == 1)
- {
- return RET_OK;
- }
- else
- return RET_ERR;
- }
- int checkMode(int &mode)
- {
- int ret = RET_OK;
- if(mode < 1 || mode > 3)
- {
- try
- {
- char ch = '\0';
- printf("\n");
- do
- {
- catchWrong(ch);
- if(ch == 'y')
- ret = RET_EXT;
- else if(ch == 'n')
- ret = RET_AGN;
- } while(ch != 'y' && ch != 'n');
- }
- catch(...)
- {
- ret = RET_ERR;
- }
- }
- return ret;
- }
- void catchWrong(char &ch)
- {
- printf("You wrote something wrong, are you gonna exit? (y/n): ");
- cin.ignore(numeric_limits<streamsize>::max(), '\n');
- scanf("%c", &ch);
- tolower(ch);
- return;
- }
- void clearScreen()
- {
- system("cls");
- return;
- }
- int chooseAction(int &action)
- {
- cout << "What would you like to do with them? That's a list: \n1. Add A + B\n2. Multiply A * B\n3. Mulpitly n * (A | B)\n4. Exponentiation (A | B) ^ n\n5. Transposition (A | B)\n6. Determination (A | B)\n\nTell me please: ";
- if(scanf("%i", &action) == 1)
- {
- if(action < 1 || action > 6)
- return RET_ERR;
- return RET_OK;
- }
- else
- return RET_ERR;
- }
- void pause()
- {
- system("pause");
- return;
- }
- int chooseNext()
- {
- cin.ignore(numeric_limits<streamsize>::max(), '\n');
- char ch = '\0';
- cout << "Would you like to do something more? (y/n): ";
- if(scanf("%c", &ch) == 1)
- {
- if(ch == 'y')
- return RET_AGN;
- else if(ch == 'n')
- return RET_EXT;
- else
- return RET_ERR;
- }
- else
- return RET_ERR;
- return RET_OK;
- }
- void sumMatrix(Matrix a, Matrix b, Matrix c, Matrix::matrixSize size)
- {
- for(int i = 0; i < size.rows; i++)
- {
- for(int j = 0; j < size.cols; j++)
- {
- c.Set(i, j, a.Get(i, j) + b.Get(i, j));
- }
- }
- return;
- }
- void multMatrixToMatrix(Matrix a, Matrix b, Matrix c, Matrix::matrixSize size, int fork)
- {
- Matrix m(size.rows, size.cols);
- for(int i = 0; i < size.rows; i++)
- {
- for(int j = 0; j < size.cols; j++)
- {
- M_TYPE res = 0, aa = 0, bb = 0;
- for(int k = 0; k < fork; k++)
- {
- aa = a.Get(i, k);
- bb = b.Get(k, j);
- res += aa * bb;
- }
- m.Set(i, j, res);
- }
- }
- for(int i = 0; i < size.rows; i++)
- for(int j = 0; j < size.cols; j++)
- c.Set(i, j, m.Get(i, j));
- return;
- }
- void multMatrixToNumber(Matrix a, int n, Matrix c, Matrix::matrixSize size)
- {
- for(int i = 0; i < size.rows; i++)
- for(int j = 0; j < size.cols; j++)
- c.Set(i, j, a.Get(i, j) * n);
- return;
- }
- int getMatrixForChoose(int &matrix)
- {
- int matrixx;
- cin.ignore(numeric_limits<streamsize>::max(), 10);
- if(scanf("%i", &matrixx) == 1)
- {
- if(matrixx == 1)
- matrix = 237;
- else if(matrixx == 2)
- matrix = 238;
- else
- return RET_ERR;
- return RET_OK;
- }
- else
- return RET_ERR;
- return RET_ERR;
- }
- int getNumberForMult(int &num)
- {
- //cin.ignore(numeric_limits<streamsize>::max(), '\n');
- if(scanf("%i", &num) == 1)
- return RET_OK;
- else
- return RET_ERR;
- return RET_ERR;
- }
- void powMatrix(Matrix a, int n, Matrix c, Matrix::matrixSize size)
- {
- multMatrixToNumber(a, 1, c, size);
- for(int i = 1; i < n; i++)
- multMatrixToMatrix(c, a, c, a.size, a.size.rows);
- return;
- }
- void transposeMatrix(Matrix a, Matrix c, Matrix::matrixSize size)
- {
- for(int i = 0; i < size.rows; i++)
- for(int j = 0; j < size.cols; j++)
- c.Set(i, j, a.Get(j, i));
- return;
- }
- void getDeterminant(Matrix a, __int64 &det, int n)
- {
- if(n == 1)
- {
- det = a.Get(0, 0);
- return;
- }
- else if(n == 2)
- {
- det = a.Get(0, 0) * a.Get(1, 1) - a.Get(0, 1) * a.Get(1, 0);
- return;
- }
- for(int i = 0; i < n; i++)
- det += ((1 + i) % 2 ? a.Get(0, i) * getMinor(a, n, 0, i) : -1 * a.Get(0, i) * getMinor(a, n, 0, i));
- return;
- }
- __int64 getMinor(Matrix a, int n, int ii, int jj)
- {
- Matrix b(n - 1, n - 1);
- bool shj, shi = 0;
- for(int i = 0; i < n; i++)
- {
- if(i == ii)
- {
- shi = 1;
- continue;
- }
- shj = 0;
- for(int j = 0; j < n; j++)
- {
- if(j == jj)
- {
- shj = 1;
- continue;
- }
- if(shj)
- {
- if(shi)
- b.Set(i - 1, j - 1, a.Get(i, j));
- else
- b.Set(i, j - 1, a.Get(i, j));
- }
- else
- {
- if(shi)
- b.Set(i - 1, j, a.Get(i, j));
- else
- b.Set(i, j, a.Get(i, j));
- }
- }
- }
- __int64 det = 0;
- getDeterminant(b, det, n - 1);
- return det;
- }
- /*
- #region trash
- char c[11] = {
- '╔', // 0
- '═', // 1
- '╦', // 2
- '╗', // 3
- '║', // 4
- '╠', // 5
- '╬', // 6
- '╣', // 7
- '╚', // 8
- '╩', // 9
- '╝' // 10
- };
- // wchar_t : L'\u2554';
- //a.Output(cout);
- //std::locale loc(std::locale::classic(), new std::codecvt_utf8<wchar_t>);
- //out.imbue(loc);
- setlocale(LC_ALL, "en-US");
- matrixToFile(out, a, a_size.rows, a_size.cols);
- matrixToConsole(cout, a, a_size.rows, a_size.cols);
- void matrixToFile(ofstream &, int**, int, int);
- void matrixToConsole(ostream &, int**, int, int);
- void matrixToFile(ofstream &out, int** a, int row, int col)
- {
- unsigned char d[11] = {
- 201, // 0
- 205, // 1
- 203, // 2
- 187, // 3
- 186, // 4
- 204, // 5
- 206, // 6
- 185, // 7
- 200, // 8
- 202, // 9
- 188 // 10
- };
- int* max_colc = new int [col];
- for(int i = 0; i < col; i++)
- for(int j = 0; j < row; j++)
- {
- string s = to_string(static_cast<__int64>(a[j][i]));
- max_colc[i] = max(max_colc[i], static_cast<int>(s.size()));
- }
- for(int i = 0; i < row; i++)
- {
- switch(i)
- {
- case 0:
- {
- // head
- out << d[0];
- for(int j = 0; j < col; j++)
- {
- for(int k = 0; k < max_colc[j] + 2; k++)
- out << d[1];
- out << (j == col - 1 ? d[3] : d[2]);
- }
- out << endl;
- // inf
- out << d[4] << ' ';
- for(int j = 0; j < col; j++)
- {
- string s = to_string(static_cast<__int64>(a[i][j]));
- int elem_c = s.size();
- for(int k = 0; k < (max_colc[j] - elem_c) / 2; k++) // | 123 |
- out << ' ';
- string arr_size_str = to_string(static_cast<__int64>(a[i][j]));
- int arr_size = arr_size_str.size();
- if(max_colc[j] % 2)
- {
- if(arr_size % 2)
- out << a[i][j];
- else
- out << 0 << a[i][j];
- }
- else
- {
- if(arr_size % 2)
- out << 0 << a[i][j];
- else
- out << a[i][j];
- }
- for(int k = 0; k < (max_colc[j] - elem_c) / 2; k++) // | 123 |
- out << ' ';
- out << ' ' << d[4] << ' ';
- }
- out << endl;
- // bottom
- if(row == 1)
- {
- out << d[8];
- for(int j = 0; j < col; j++)
- {
- for(int k = 0; k < max_colc[j] + 2; k++)
- out << d[1];
- out << (j == col - 1 ? d[10] : d[9]);
- }
- }
- break;
- }
- default:
- {
- // head
- out << d[5];
- for(int j = 0; j < col; j++)
- {
- for(int k = 0; k < max_colc[j] + 2; k++)
- out << d[1];
- out << (j == col - 1 ? d[7] : d[6]);
- }
- out << endl;
- // inf
- out << d[4] << ' ';
- for(int j = 0; j < col; j++)
- {
- string s = to_string(static_cast<__int64>(a[i][j]));
- int elem_c = s.size();
- for(int k = 0; k < (max_colc[j] - elem_c) / 2; k++) // | 123 |
- out << ' ';
- string arr_size_str = to_string(static_cast<__int64>(a[i][j]));
- int arr_size = arr_size_str.size();
- if(max_colc[j] % 2)
- {
- if(arr_size % 2)
- out << a[i][j];
- else
- out << 0 << a[i][j];
- }
- else
- {
- if(arr_size % 2)
- out << 0 << a[i][j];
- else
- out << a[i][j];
- }
- for(int k = 0; k < (max_colc[j] - elem_c) / 2; k++) // | 123 |
- out << ' ';
- out << ' ' << d[4] << ' ';
- }
- out << endl;
- // bottom
- if(i == row - 1)
- {
- out << d[8];
- for(int j = 0; j < col; j++)
- {
- for(int k = 0; k < max_colc[j] + 2; k++)
- out << d[1];
- out << (j == col - 1 ? d[10] : d[9]);
- }
- }
- break;
- }
- }
- }
- out << "\n\n";
- }
- void matrixToConsole(ostream &out, int** a, int row, int col)
- {
- unsigned char d[11] = {
- 201, // 0
- 205, // 1
- 203, // 2
- 187, // 3
- 186, // 4
- 204, // 5
- 206, // 6
- 185, // 7
- 200, // 8
- 202, // 9
- 188 // 10
- };
- wchar_t c[11] = {
- '╔', // 0
- '═', // 1
- '╦', // 2
- '╗', // 3
- '║', // 4
- '╠', // 5
- '╬', // 6
- '╣', // 7
- '╚', // 8
- '╩', // 9
- '╝' // 10
- };
- wchar_t ex[10000];
- for(int i = 0; i < 10000; i++)
- ex[i] = i;
- int* max_colc = new int [col];
- for(int i = 0; i < col; i++)
- for(int j = 0; j < row; j++)
- {
- string s = to_string(static_cast<__int64>(a[j][i]));
- max_colc[i] = max(max_colc[i], static_cast<int>(s.size()));
- }
- for(int i = 0; i < row; i++)
- {
- switch(i)
- {
- case 0:
- {
- // head
- out << d[0];
- for(int j = 0; j < col; j++)
- {
- for(int k = 0; k < max_colc[j] + 2; k++)
- out << d[1];
- out << (j == col - 1 ? d[3] : d[2]);
- }
- out << endl;
- // inf
- out << d[4] << ' ';
- for(int j = 0; j < col; j++)
- {
- string s = to_string(static_cast<__int64>(a[i][j]));
- int elem_c = s.size();
- for(int k = 0; k < (max_colc[j] - elem_c) / 2; k++) // | 123 |
- out << ' ';
- string arr_size_str = to_string(static_cast<__int64>(a[i][j]));
- int arr_size = arr_size_str.size();
- if(max_colc[j] % 2)
- {
- if(arr_size % 2)
- out << a[i][j];
- else
- out << 0 << a[i][j];
- }
- else
- {
- if(arr_size % 2)
- out << 0 << a[i][j];
- else
- out << a[i][j];
- }
- for(int k = 0; k < (max_colc[j] - elem_c) / 2; k++) // | 123 |
- out << ' ';
- out << ' ' << d[4] << ' ';
- }
- out << endl;
- // bottom
- if(row == 1)
- {
- out << d[8];
- for(int j = 0; j < col; j++)
- {
- for(int k = 0; k < max_colc[j] + 2; k++)
- out << d[1];
- out << (j == col - 1 ? d[10] : d[9]);
- }
- }
- break;
- }
- default:
- {
- // head
- out << d[5];
- for(int j = 0; j < col; j++)
- {
- for(int k = 0; k < max_colc[j] + 2; k++)
- out << d[1];
- out << (j == col - 1 ? d[7] : d[6]);
- }
- out << endl;
- // inf
- out << d[4] << ' ';
- for(int j = 0; j < col; j++)
- {
- string s = to_string(static_cast<__int64>(a[i][j]));
- int elem_c = s.size();
- for(int k = 0; k < (max_colc[j] - elem_c) / 2; k++) // | 123 |
- out << ' ';
- string arr_size_str = to_string(static_cast<__int64>(a[i][j]));
- int arr_size = arr_size_str.size();
- if(max_colc[j] % 2)
- {
- if(arr_size % 2)
- out << a[i][j];
- else
- out << 0 << a[i][j];
- }
- else
- {
- if(arr_size % 2)
- out << 0 << a[i][j];
- else
- out << a[i][j];
- }
- for(int k = 0; k < (max_colc[j] - elem_c) / 2; k++) // | 123 |
- out << ' ';
- out << ' ' << d[4] << ' ';
- }
- out << endl;
- // bottom
- if(i == row - 1)
- {
- out << d[8];
- for(int j = 0; j < col; j++)
- {
- for(int k = 0; k < max_colc[j] + 2; k++)
- out << d[1];
- out << (j == col - 1 ? d[10] : d[9]);
- }
- }
- break;
- }
- }
- }
- out << "\n\n";
- }
- void getNewMatrix(Matrix a, Matrix c, int n, int ii, int jj)
- {
- int i, j, di = 0, dj;
- for(i = 0; i < n - 1; i++)
- {
- if(i == ii)
- di = 1;
- dj = 0;
- for(j = 0; j < n - 1; j++)
- {
- if(j == jj)
- dj = 1;
- c.Set(i, j, a.Get(i + di, j + dj));
- }
- }
- }
- __int64 getDeterminant(Matrix a, int n)
- {
- __int64 det = 0;
- int k = 1;
- Matrix c(n, n);
- if(n == 1)
- {
- det = a.Get(0, 0);
- return det;
- }
- else if(n == 2)
- {
- det = a.Get(0, 0) * a.Get(1, 1) - a.Get(0, 1) * a.Get(1, 0);
- return det;
- }
- for(int i = 0; i < n; i++)
- {
- getNewMatrix(a, c, n, i, 0);
- det += k * a.Get(i, 0) * getDeterminant(c, n - 1);
- k = -k;
- }
- return det;
- }
- #endregion
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement