Advertisement
ret_0

matrix

Apr 19th, 2018
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 28.68 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4. //#include <codecvt>
  5. using namespace std;
  6.  
  7. #define RET_OK      (0)
  8. #define RET_AGN     (1)
  9. #define RET_EXT     (2)
  10. #define RET_ERR     (-1)
  11. #define M_TYPE      __int64         // type of matrix
  12.  
  13. class Matrix
  14. {
  15.     private:
  16.  
  17.         M_TYPE** a;
  18.         bool isnull;
  19.  
  20.     public:
  21.  
  22.         struct matrixSize
  23.         {
  24.             int rows;
  25.             int cols;
  26.         };
  27.         matrixSize size;
  28.  
  29.         Matrix(int rows, int cols)
  30.         {
  31.             isnull = 1;
  32.             a = new M_TYPE* [rows];
  33.             for(int i = 0; i < rows; i++)
  34.                 a[i] = new M_TYPE [cols];
  35.             for(int i = 0; i < rows; i++)
  36.                 for(int j = 0; j < cols; j++)
  37.                     a[i][j] = 0;
  38.             size.cols = cols;
  39.             size.rows = rows;
  40.         }
  41.  
  42.         void InputF(ifstream &in)
  43.         {
  44.             //string checker;
  45.             try
  46.             {
  47.                 for(int i = 0; i < size.rows; i++)
  48.                     for(int j = 0; j < size.cols; j++)
  49.                     {
  50.                         if(!(in >> a[i][j]))
  51.                         {
  52.                             exception ex;
  53.                             throw ex;
  54.                         }
  55.                         /*
  56.                         in >> checker;
  57.                         for(int k = 0; k < (int)checker.size(); k++)
  58.                         {
  59.                             if(checker[k] < '0' || checker[k] > '9')
  60.                             {
  61.                                 exception ex;
  62.                                 throw ex;
  63.                             }
  64.                         }
  65.                         __int64 ex = stoll(checker);
  66.                         */
  67.                         //a[i][j] = stoll(checker);
  68.                     }
  69.             }
  70.             catch(...)
  71.             {
  72.                 return;
  73.             }
  74.             isnull = 0;
  75.             return;
  76.         }
  77.  
  78.         void Output(ostream &out)
  79.         {
  80.             unsigned char d[11] = {
  81.                             201, // 0
  82.                             205, // 1
  83.                             203, // 2
  84.                             187, // 3
  85.                             186, // 4
  86.                             204, // 5
  87.                             206, // 6
  88.                             185, // 7
  89.                             200, // 8
  90.                             202, // 9
  91.                             188 // 10
  92.             };
  93.             int* max_colc = new int [size.cols];
  94.             for(int i = 0; i < size.cols; i++)
  95.                 for(int j = 0; j < size.rows; j++)
  96.                 {
  97.                     string s = to_string(static_cast<__int64>(a[j][i]));
  98.                     max_colc[i] = max(max_colc[i], static_cast<int>(s.size()));
  99.                 }
  100.             for(int i = 0; i < size.rows; i++)
  101.             {
  102.                 switch(i)
  103.                 {
  104.                     case 0:
  105.                     {
  106.                         // head
  107.                         out << d[0];
  108.                         for(int j = 0; j < size.cols; j++)
  109.                         {
  110.                             for(int k = 0; k < max_colc[j] + 2; k++)
  111.                                 out << d[1];
  112.                             out << (j == size.cols - 1 ? d[3] : d[2]);
  113.                         }
  114.        
  115.                         out << endl;
  116.        
  117.                             // inf
  118.                         out << d[4] << ' ';
  119.                         for(int j = 0; j < size.cols; j++)
  120.                         {
  121.                             string s = to_string(static_cast<__int64>(a[i][j]));
  122.                             int elem_c = s.size();
  123.        
  124.                             for(int k = 0; k < (max_colc[j] - elem_c) / 2; k++) // |  123 |
  125.                                 out << ' ';
  126.                             string arr_size_str = to_string(static_cast<__int64>(a[i][j]));
  127.                             int arr_size = arr_size_str.size();
  128.                            
  129.                             if(max_colc[j] % 2)
  130.                             {
  131.                                 if(arr_size % 2)
  132.                                     out << a[i][j];
  133.                                 else
  134.                                 {
  135.                                     if(a[i][j] >= 0)
  136.                                         out << 0 << a[i][j];
  137.                                     else
  138.                                         out << ' ' << a[i][j];
  139.                                 }
  140.                             }
  141.                             else
  142.                             {
  143.                                 if(arr_size % 2)
  144.                                 {
  145.                                     if(a[i][j] >= 0)
  146.                                         out << 0 << a[i][j];
  147.                                     else
  148.                                         out << ' ' << a[i][j];
  149.                                 }
  150.                                 else
  151.                                     out << a[i][j];
  152.                             }
  153.        
  154.                             for(int k = 0; k < (max_colc[j] - elem_c) / 2; k++) // | 123  |
  155.                                 out << ' ';
  156.        
  157.                             out << ' ' << d[4] << ' ';
  158.                         }
  159.                         out << endl;
  160.        
  161.                         // bottom
  162.                         if(size.rows == 1)
  163.                         {
  164.                             out << d[8];
  165.                             for(int j = 0; j < size.cols; j++)
  166.                             {
  167.                                 for(int k = 0; k < max_colc[j] + 2; k++)
  168.                                     out << d[1];
  169.                                 out << (j == size.cols - 1 ? d[10] : d[9]);
  170.                             }
  171.        
  172.                         }
  173.                         break;
  174.                     }
  175.                     default:
  176.                     {
  177.                         // head
  178.                         out << d[5];
  179.                         for(int j = 0; j < size.cols; j++)
  180.                         {
  181.                             for(int k = 0; k < max_colc[j] + 2; k++)
  182.                                 out << d[1];
  183.                             out << (j == size.cols - 1 ? d[7] : d[6]);
  184.                         }
  185.                         out << endl;
  186.        
  187.                         // inf
  188.                         out << d[4] << ' ';
  189.                         for(int j = 0; j < size.cols; j++)
  190.                         {
  191.                             string s = to_string(static_cast<__int64>(a[i][j]));
  192.                             int elem_c = s.size();
  193.                             for(int k = 0; k < (max_colc[j] - elem_c) / 2; k++) // |  123 |
  194.                                 out << ' ';
  195.        
  196.                             string arr_size_str = to_string(static_cast<__int64>(a[i][j]));
  197.                             int arr_size = arr_size_str.size();
  198.        
  199.                             if(max_colc[j] % 2)
  200.                             {
  201.                                 if(arr_size % 2)
  202.                                     out << a[i][j];
  203.                                 else
  204.                                 {
  205.                                     if(a[i][j] >= 0)
  206.                                         out << 0 << a[i][j];
  207.                                     else
  208.                                         out << ' ' << a[i][j];
  209.                                 }
  210.                             }
  211.                             else
  212.                             {
  213.                                 if(arr_size % 2)
  214.                                 {
  215.                                     if(a[i][j] >= 0)
  216.                                         out << 0 << a[i][j];
  217.                                     else
  218.                                         out << ' ' << a[i][j];
  219.                                 }
  220.                                 else
  221.                                     out << a[i][j];
  222.                             }
  223.        
  224.                             for(int k = 0; k < (max_colc[j] - elem_c) / 2; k++) // | 123  |
  225.                                 out << ' ';
  226.                             out << ' ' << d[4] << ' ';
  227.                         }
  228.                         out << endl;
  229.        
  230.                         // bottom
  231.                         if(i == size.rows - 1)
  232.                         {
  233.                             out << d[8];
  234.                             for(int j = 0; j < size.cols; j++)
  235.                             {
  236.                                 for(int k = 0; k < max_colc[j] + 2; k++)
  237.                                     out << d[1];
  238.                                 out << (j == size.cols - 1 ? d[10] : d[9]);
  239.                             }
  240.        
  241.                         }
  242.                         break;
  243.                     }
  244.                 }
  245.             }
  246.             out << "\n\n";
  247.             return;
  248.         }
  249.  
  250.         void Set(int row, int col, M_TYPE value)
  251.         {
  252.             a[row][col] = value;
  253.             isnull = 0;
  254.             return;
  255.         }
  256.        
  257.         M_TYPE Get(int row, int col)
  258.         {
  259.             return a[row][col];
  260.         }
  261.  
  262.         bool IsNull()
  263.         {
  264.             return isnull;
  265.         }
  266.  
  267.         bool IsSquare()
  268.         {
  269.             return (size.cols == size.rows);
  270.         }
  271.  
  272. };
  273.  
  274. void clearScreen();
  275. void work(bool &);
  276. int chooseMode(int &, bool);
  277. int checkMode(int &);
  278. void catchWrong(char &);
  279. int chooseAction(int &);
  280. void pause();
  281. void sumMatrix(Matrix a, Matrix b, Matrix c, Matrix::matrixSize size);
  282. int chooseNext();
  283. void multMatrixToMatrix(Matrix a, Matrix b, Matrix c, Matrix::matrixSize size, int);
  284. void multMatrixToNumber(Matrix a, int, Matrix c, Matrix::matrixSize size);
  285. int getNumberForMult(int &);
  286. int getMatrixForChoose(int &);
  287. void powMatrix(Matrix a, int, Matrix c, Matrix::matrixSize size);
  288. void transposeMatrix(Matrix a, Matrix c, Matrix::matrixSize size);
  289. void getDeterminant(Matrix a, __int64 &det, int n);
  290. __int64 getMinor(Matrix a, int n, int ii, int jj);
  291. //__int64 getDeterminant(Matrix a, int n);
  292. //void getNewMatrix(Matrix a, Matrix c, int n, int ii, int jj);
  293.  
  294. int main()
  295. {
  296.     bool w = 1;
  297.     while(w)
  298.         work(w);
  299.     return 0;
  300. }
  301.  
  302. void work(bool &w)
  303. {
  304.     int mode;
  305.     if(chooseMode(mode, true) == RET_OK)
  306.     {
  307.         switch(checkMode(mode))
  308.         {
  309.             case RET_AGN:
  310.                 {
  311.                     clearScreen();
  312.                     return;
  313.                 }
  314.             case RET_EXT:
  315.                 {
  316.                     w = 0;
  317.                     return;
  318.                 }
  319.             case RET_ERR:
  320.                 {
  321.                     w = 0;
  322.                     return;
  323.                 }
  324.             case RET_OK:
  325.                 {
  326.                     switch(mode)
  327.                     {
  328.                         case 1:
  329.                             {
  330.                                 ifstream in("input.txt");
  331.                                 if(in == NULL)
  332.                                 {
  333.                                     clearScreen();
  334.                                     cout << "\nThere's something wrong with \"input.txt\" file. \nAre you sure you created it?\n\nAnyway, program will exit.\n\n";
  335.                                     w = 0;
  336.                                     pause();
  337.                                     return;
  338.                                 }
  339.                                 //ofstream out("output.txt");
  340.                                 int row, col;
  341.  
  342.                                 in >> row >> col;
  343.                                 Matrix a(row, col);
  344.                                 a.InputF(in);
  345.                                 in >> row >> col;
  346.                                 Matrix b(row, col);
  347.                                 b.InputF(in);
  348.                                 if(a.IsNull() || b.IsNull())
  349.                                 {
  350.                                     clearScreen();
  351.                                     cout << "\nThere's something wrong with \"input.txt\" file. \nAre you sure you wrote data correctly?\n\nAnyway, program will exit.\n\n";
  352.                                     w = 0;
  353.                                     pause();
  354.                                     return;
  355.                                 }
  356.  
  357.                                 clearScreen();
  358.                                 cout << "So, we read two matrixes from \"input.txt\": \nMatrix A: \n";
  359.                                 a.Output(cout);
  360.                                 cout << "Matrix B: \n";
  361.                                 b.Output(cout);
  362.                                 pause();
  363.  
  364.                                 clearScreen();
  365.                                 int action = 0;
  366.                                 if(chooseAction(action) == RET_ERR)
  367.                                 {
  368.                                     char ch = '\0';
  369.                                     do
  370.                                     {
  371.                                         catchWrong(ch);
  372.                                         if(ch == 'y')
  373.                                         {
  374.                                             w = 0;
  375.                                             return;
  376.                                         }
  377.                                         else if(ch == 'n')
  378.                                             return;
  379.                                     } while(ch != 'y' && ch != 'n');
  380.                                 }
  381.                                 switch(action)
  382.                                 {
  383.                                     case 1: // a + b
  384.                                         {
  385.                                             clearScreen();
  386.                                             if(a.size.rows != b.size.rows || a.size.cols != b.size.cols)
  387.                                             {
  388.                                                 cout << "Matrix sizes are mismatch!\nProgram will be exit\n\n";
  389.                                                 w = 0;
  390.                                                 pause();
  391.                                                 return;
  392.                                             }
  393.                                             cout << "We add this matrix (A): \n";
  394.                                             a.Output(cout);
  395.                                             cout << "To this (B): \n";
  396.                                             b.Output(cout);
  397.                                             cout << "And we have such result: \n";
  398.                                             Matrix c(a.size.rows, a.size.cols);
  399.                                             sumMatrix(a, b, c, c.size);
  400.                                             c.Output(cout);
  401.                                             int next = chooseNext();
  402.                                             if(next == RET_AGN)
  403.                                                 return;
  404.                                             else
  405.                                             {
  406.                                                 w = 0;
  407.                                                 return;
  408.                                             }
  409.                                             break;
  410.                                         }
  411.  
  412.                                     case 2: // a * b
  413.                                         {
  414.                                             clearScreen();
  415.                                             if(a.size.rows != b.size.cols)
  416.                                             {
  417.                                                 cout << "Matrix sizes are mismatch!\nProgram will be exit\n\n";
  418.                                                 w = 0;
  419.                                                 pause();
  420.                                                 return;
  421.                                             }
  422.                                             cout << "We multiplicate this matrix (A): \n";
  423.                                             a.Output(cout);
  424.                                             cout << "To this (B): \n";
  425.                                             b.Output(cout);
  426.                                             cout << "And we have such result: \n";
  427.                                             Matrix c(a.size.rows, a.size.cols);
  428.                                             Matrix::matrixSize size;
  429.                                             size.cols = b.size.cols;
  430.                                             size.rows = a.size.rows;
  431.                                             multMatrixToMatrix(a, b, c, size, b.size.cols);
  432.                                             c.Output(cout);
  433.                                             int next = chooseNext();
  434.                                             if(next == RET_AGN)
  435.                                                 return;
  436.                                             else
  437.                                             {
  438.                                                 w = 0;
  439.                                                 return;
  440.                                             }
  441.                                             break;
  442.                                         }
  443.  
  444.                                     case 3: // n * a
  445.                                         {
  446.                                             clearScreen();
  447.                                             cout << "We multiplicate matrix to some number. List: \n1. A\n2. B\nChoose: ";
  448.                                             int matr;
  449.                                             if(getMatrixForChoose(matr) == RET_ERR)
  450.                                             {
  451.                                                 char ch = '\0';
  452.                                                 do
  453.                                                 {
  454.                                                     catchWrong(ch);
  455.                                                     if(ch == 'y')
  456.                                                     {
  457.                                                         w = 0;
  458.                                                         return;
  459.                                                     }
  460.                                                     else if(ch == 'n')
  461.                                                         return;
  462.                                                 } while(ch != 'y' && ch != 'n');
  463.                                             }
  464.                                             matr == 237 ? a.Output(cout) : b.Output(cout);
  465.                                             cout << "To this number (enter it faster): ";
  466.                                             int num;
  467.                                             if(getNumberForMult(num) == RET_ERR)
  468.                                             {
  469.                                                 char ch = '\0';
  470.                                                 do
  471.                                                 {
  472.                                                     catchWrong(ch);
  473.                                                     if(ch == 'y')
  474.                                                     {
  475.                                                         w = 0;
  476.                                                         return;
  477.                                                     }
  478.                                                     else if(ch == 'n')
  479.                                                         return;
  480.                                                 } while(ch != 'y' && ch != 'n');
  481.                                             }
  482.                                             cout << "And we have such result: \n";
  483.                                             Matrix::matrixSize size = (matr == 237 ? a.size : b.size);
  484.                                             Matrix c(size.rows, size.cols);
  485.                                             if(matr == 237)
  486.                                                 multMatrixToNumber(a, num, c, size);
  487.                                             else
  488.                                                 multMatrixToNumber(b, num, c, size);
  489.                                             c.Output(cout);
  490.                                             int next = chooseNext();
  491.                                             if(next == RET_AGN)
  492.                                                 return;
  493.                                             else
  494.                                             {
  495.                                                 w = 0;
  496.                                                 return;
  497.                                             }
  498.                                             break;
  499.                                         }
  500.  
  501.                                     case 4: // a ^ n
  502.                                         {
  503.                                             clearScreen();
  504.                                             cout << "We power matrix to some exponent. List: \n1. A\n2. B\nChoose: ";
  505.                                             int matr;
  506.                                             if(getMatrixForChoose(matr) == RET_ERR)
  507.                                             {
  508.                                                 char ch = '\0';
  509.                                                 do
  510.                                                 {
  511.                                                     catchWrong(ch);
  512.                                                     if(ch == 'y')
  513.                                                     {
  514.                                                         w = 0;
  515.                                                         return;
  516.                                                     }
  517.                                                     else if(ch == 'n')
  518.                                                         return;
  519.                                                 } while(ch != 'y' && ch != 'n');
  520.                                             }
  521.                                             matr == 237 ? a.Output(cout) : b.Output(cout);
  522.                                             if(matr == 237)
  523.                                             {
  524.                                                 if(!a.IsSquare())
  525.                                                 {
  526.                                                     cout << "\n\nCome on, dude, you see, matrix is non-square?\nYeah, you see. I won't power it, good bye!\n";
  527.                                                     w = 0;
  528.                                                     pause();
  529.                                                     return;
  530.                                                 }
  531.                                             }
  532.                                             else
  533.                                             {
  534.                                                 if(!b.IsSquare())
  535.                                                 {
  536.                                                     cout << "\n\nCome on, dude, you see, matrix is non-square?\nYeah, you see. I won't power it, good bye!\n";
  537.                                                     w = 0;
  538.                                                     pause();
  539.                                                     return;
  540.                                                 }
  541.                                             }
  542.                                             cout << "To this exponent (enter it, come on): ";
  543.                                             int num;
  544.                                             if(getNumberForMult(num) == RET_ERR)
  545.                                             {
  546.                                                 char ch = '\0';
  547.                                                 do
  548.                                                 {
  549.                                                     catchWrong(ch);
  550.                                                     if(ch == 'y')
  551.                                                     {
  552.                                                         w = 0;
  553.                                                         return;
  554.                                                     }
  555.                                                     else if(ch == 'n')
  556.                                                         return;
  557.                                                 } while(ch != 'y' && ch != 'n');
  558.                                             }
  559.                                             cout << "And we have such result: \n";
  560.                                             Matrix::matrixSize size = (matr == 237 ? a.size : b.size);
  561.                                             Matrix c(size.rows, size.cols);
  562.                                             if(matr == 237)
  563.                                                 powMatrix(a, num, c, size);
  564.                                             else
  565.                                                 powMatrix(b, num, c, size);
  566.                                             c.Output(cout);
  567.                                             int next = chooseNext();
  568.                                             if(next == RET_AGN)
  569.                                                 return;
  570.                                             else
  571.                                             {
  572.                                                 w = 0;
  573.                                                 return;
  574.                                             }
  575.                                             break;
  576.                                         }
  577.  
  578.                                     case 5: // trans a
  579.                                         {
  580.                                             clearScreen();
  581.                                             cout << "We transpose matrix. List: \n1. A\n2. B\nChoose matrix: ";
  582.                                             int matr;
  583.                                             if(getMatrixForChoose(matr) == RET_ERR)
  584.                                             {
  585.                                                 char ch = '\0';
  586.                                                 do
  587.                                                 {
  588.                                                     catchWrong(ch);
  589.                                                     if(ch == 'y')
  590.                                                     {
  591.                                                         w = 0;
  592.                                                         return;
  593.                                                     }
  594.                                                     else if(ch == 'n')
  595.                                                         return;
  596.                                                 } while(ch != 'y' && ch != 'n');
  597.                                             }
  598.                                             matr == 237 ? a.Output(cout) : b.Output(cout);
  599.                                             cout << "And we have such result: \n";
  600.                                             Matrix::matrixSize size;
  601.                                             size.rows = (matr == 237 ? a.size.cols : b.size.cols);
  602.                                             size.cols = (matr == 237 ? a.size.rows : b.size.rows);
  603.                                             Matrix c(size.rows, size.cols);
  604.                                             if(matr == 237)
  605.                                                 transposeMatrix(a, c, size);
  606.                                             else
  607.                                                 transposeMatrix(b, c, size);
  608.                                             c.Output(cout);
  609.                                             int next = chooseNext();
  610.                                             if(next == RET_AGN)
  611.                                                 return;
  612.                                             else
  613.                                             {
  614.                                                 w = 0;
  615.                                                 return;
  616.                                             }
  617.                                             break;
  618.                                         }
  619.  
  620.                                     case 6:
  621.                                         {
  622.                                             clearScreen();
  623.                                             cout << "We get determinant of one of matrixes. List: \n1. A\n2. B\nChoose: ";
  624.                                             int matr;
  625.                                             if(getMatrixForChoose(matr) == RET_ERR)
  626.                                             {
  627.                                                 char ch = '\0';
  628.                                                 do
  629.                                                 {
  630.                                                     catchWrong(ch);
  631.                                                     if(ch == 'y')
  632.                                                     {
  633.                                                         w = 0;
  634.                                                         return;
  635.                                                     }
  636.                                                     else if(ch == 'n')
  637.                                                         return;
  638.                                                 } while(ch != 'y' && ch != 'n');
  639.                                             }
  640.                                             if(matr == 237)
  641.                                             {
  642.                                                 if(!a.IsSquare())
  643.                                                 {
  644.                                                     cout << "\n\nCome on, dude, you see, matrix is non-square?\nYeah, you see. I won't power it, good bye!\n";
  645.                                                     w = 0;
  646.                                                     pause();
  647.                                                     return;
  648.                                                 }
  649.                                             }
  650.                                             else
  651.                                             {
  652.                                                 if(!b.IsSquare())
  653.                                                 {
  654.                                                     cout << "\n\nCome on, dude, you see, matrix is non-square?\nYeah, you see. I won't power it, good bye!\n";
  655.                                                     w = 0;
  656.                                                     pause();
  657.                                                     return;
  658.                                                 }
  659.                                             }
  660.                                             matr == 237 ? a.Output(cout) : b.Output(cout);
  661.                                             cout << "And determinant of matrix is: ";
  662.                                             __int64 det = 0;
  663.                                             matr == 237 ? getDeterminant(a, det, a.size.rows) : getDeterminant(b, det, b.size.rows);
  664.                                             cout << det << "\n";
  665.                                             int next = chooseNext();
  666.                                             if(next == RET_AGN)
  667.                                                 return;
  668.                                             else
  669.                                             {
  670.                                                 w = 0;
  671.                                                 return;
  672.                                             }
  673.                                             break;
  674.                                         }
  675.                                 } // switch
  676.                             }
  677.                     }
  678.                     break;
  679.                 }
  680.         }
  681.     }
  682.     else
  683.     {
  684.         char ch = '\0';
  685.         do
  686.         {
  687.             catchWrong(ch);
  688.             if(ch == 'y')
  689.             {
  690.                 w = 0;
  691.                 return;
  692.             }
  693.             else if(ch == 'n')
  694.                 return;
  695.         } while(ch != 'y' && ch != 'n');
  696.     }
  697.     return;
  698. }
  699.  
  700. int chooseMode(int &mode, bool first)
  701. {
  702.     if(!first) cin.ignore(numeric_limits<streamsize>::max(), '\n');
  703.     clearScreen();
  704.     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: ");
  705.     if(scanf("%i", &mode) == 1)
  706.     {
  707.         return RET_OK;
  708.     }
  709.     else
  710.         return RET_ERR;
  711. }
  712.  
  713. int checkMode(int &mode)
  714. {
  715.     int ret = RET_OK;
  716.     if(mode < 1 || mode > 3)
  717.     {
  718.         try
  719.         {
  720.             char ch = '\0';
  721.             printf("\n");
  722.             do
  723.             {
  724.                 catchWrong(ch);
  725.                 if(ch == 'y')
  726.                     ret = RET_EXT;
  727.                 else if(ch == 'n')
  728.                     ret = RET_AGN;
  729.             } while(ch != 'y' && ch != 'n');
  730.         }
  731.         catch(...)
  732.         {
  733.             ret = RET_ERR;
  734.         }
  735.     }
  736.     return ret;
  737. }
  738.  
  739. void catchWrong(char &ch)
  740. {
  741.     printf("You wrote something wrong, are you gonna exit? (y/n): ");
  742.     cin.ignore(numeric_limits<streamsize>::max(), '\n');
  743.     scanf("%c", &ch);
  744.     tolower(ch);
  745.     return;
  746. }
  747.  
  748. void clearScreen()
  749. {
  750.     system("cls");
  751.     return;
  752. }
  753.  
  754. int chooseAction(int &action)
  755. {
  756.     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: ";
  757.     if(scanf("%i", &action) == 1)
  758.     {
  759.         if(action < 1 || action > 6)
  760.             return RET_ERR;
  761.         return RET_OK;
  762.     }
  763.     else
  764.         return RET_ERR;
  765. }
  766.  
  767. void pause()
  768. {
  769.     system("pause");
  770.     return;
  771. }
  772.  
  773. int chooseNext()
  774. {
  775.     cin.ignore(numeric_limits<streamsize>::max(), '\n');
  776.     char ch = '\0';
  777.     cout << "Would you like to do something more? (y/n): ";
  778.     if(scanf("%c", &ch) == 1)
  779.     {
  780.         if(ch == 'y')
  781.             return RET_AGN;
  782.         else if(ch == 'n')
  783.             return RET_EXT;
  784.         else
  785.             return RET_ERR;
  786.     }
  787.     else
  788.         return RET_ERR;
  789.     return RET_OK;
  790. }
  791.  
  792. void sumMatrix(Matrix a, Matrix b, Matrix c, Matrix::matrixSize size)
  793. {
  794.     for(int i = 0; i < size.rows; i++)
  795.     {
  796.         for(int j = 0; j < size.cols; j++)
  797.         {
  798.             c.Set(i, j, a.Get(i, j) + b.Get(i, j));
  799.         }
  800.     }
  801.     return;
  802. }
  803.  
  804. void multMatrixToMatrix(Matrix a, Matrix b, Matrix c, Matrix::matrixSize size, int fork)
  805. {
  806.     Matrix m(size.rows, size.cols);
  807.     for(int i = 0; i < size.rows; i++)
  808.     {
  809.         for(int j = 0; j < size.cols; j++)
  810.         {
  811.             M_TYPE res = 0, aa = 0, bb = 0;
  812.             for(int k = 0; k < fork; k++)
  813.             {
  814.                 aa = a.Get(i, k);
  815.                 bb = b.Get(k, j);
  816.                 res += aa * bb;
  817.             }
  818.             m.Set(i, j, res);
  819.         }
  820.     }
  821.     for(int i = 0; i < size.rows; i++)
  822.         for(int j = 0; j < size.cols; j++)
  823.             c.Set(i, j, m.Get(i, j));
  824.     return;
  825. }
  826.  
  827. void multMatrixToNumber(Matrix a, int n, Matrix c, Matrix::matrixSize size)
  828. {
  829.     for(int i = 0; i < size.rows; i++)
  830.         for(int j = 0; j < size.cols; j++)
  831.             c.Set(i, j, a.Get(i, j) * n);
  832.     return;
  833. }
  834.  
  835. int getMatrixForChoose(int &matrix)
  836. {
  837.     int matrixx;
  838.     cin.ignore(numeric_limits<streamsize>::max(), 10);
  839.     if(scanf("%i", &matrixx) == 1)
  840.     {
  841.         if(matrixx == 1)
  842.             matrix = 237;
  843.         else if(matrixx == 2)
  844.             matrix = 238;
  845.         else
  846.             return RET_ERR;
  847.         return RET_OK;
  848.     }
  849.     else
  850.         return RET_ERR;
  851.     return RET_ERR;
  852. }
  853.  
  854. int getNumberForMult(int &num)
  855. {
  856.     //cin.ignore(numeric_limits<streamsize>::max(), '\n');
  857.     if(scanf("%i", &num) == 1)
  858.         return RET_OK;
  859.     else
  860.         return RET_ERR;
  861.     return RET_ERR;
  862. }
  863.  
  864. void powMatrix(Matrix a, int n, Matrix c, Matrix::matrixSize size)
  865. {
  866.     multMatrixToNumber(a, 1, c, size);
  867.     for(int i = 1; i < n; i++)
  868.         multMatrixToMatrix(c, a, c, a.size, a.size.rows);
  869.     return;
  870. }
  871.  
  872. void transposeMatrix(Matrix a, Matrix c, Matrix::matrixSize size)
  873. {
  874.     for(int i = 0; i < size.rows; i++)
  875.         for(int j = 0; j < size.cols; j++)
  876.             c.Set(i, j, a.Get(j, i));
  877.     return;
  878. }
  879.  
  880. void getDeterminant(Matrix a, __int64 &det, int n)
  881. {
  882.     if(n == 1)
  883.     {
  884.         det = a.Get(0, 0);
  885.         return;
  886.     }
  887.     else if(n == 2)
  888.     {
  889.         det = a.Get(0, 0) * a.Get(1, 1) - a.Get(0, 1) * a.Get(1, 0);
  890.         return;
  891.     }
  892.     for(int i = 0; i < n; i++)
  893.         det += ((1 + i) % 2 ? a.Get(0, i) * getMinor(a, n, 0, i) : -1 * a.Get(0, i) * getMinor(a, n, 0, i));
  894.     return;
  895. }
  896.  
  897. __int64 getMinor(Matrix a, int n, int ii, int jj)
  898. {
  899.     Matrix b(n - 1, n - 1);
  900.     bool shj, shi = 0;
  901.     for(int i = 0; i < n; i++)
  902.     {
  903.         if(i == ii)
  904.         {
  905.             shi = 1;
  906.             continue;
  907.         }
  908.         shj = 0;
  909.         for(int j = 0; j < n; j++)
  910.         {
  911.             if(j == jj)
  912.             {
  913.                 shj = 1;
  914.                 continue;
  915.             }
  916.             if(shj)
  917.             {
  918.                 if(shi)
  919.                     b.Set(i - 1, j - 1, a.Get(i, j));
  920.                 else
  921.                     b.Set(i, j - 1, a.Get(i, j));
  922.             }
  923.             else
  924.             {
  925.                 if(shi)
  926.                     b.Set(i - 1, j, a.Get(i, j));
  927.                 else
  928.                     b.Set(i, j, a.Get(i, j));
  929.             }
  930.         }
  931.     }
  932.     __int64 det = 0;
  933.     getDeterminant(b, det, n - 1);
  934.     return det;
  935. }
  936.  
  937.  
  938. /*
  939. #region trash
  940.  
  941. char c[11] = {
  942.                     '╔', // 0
  943.                     '═', // 1
  944.                     '╦', // 2
  945.                     '╗', // 3
  946.                     '║', // 4
  947.                     '╠', // 5
  948.                     '╬', // 6
  949.                     '╣', // 7
  950.                     '╚', // 8
  951.                     '╩', // 9
  952.                     '╝' // 10
  953.     };
  954.     // wchar_t : L'\u2554';
  955.  
  956.  
  957.     //a.Output(cout);
  958.  
  959.                                 //std::locale loc(std::locale::classic(), new std::codecvt_utf8<wchar_t>);
  960.                                 //out.imbue(loc);
  961.                                 setlocale(LC_ALL, "en-US");
  962.                                
  963.                                 matrixToFile(out, a, a_size.rows, a_size.cols);
  964.                                 matrixToConsole(cout, a, a_size.rows, a_size.cols);    
  965.                                
  966. void matrixToFile(ofstream &, int**, int, int);
  967. void matrixToConsole(ostream &, int**, int, int);
  968.  
  969.  
  970. void matrixToFile(ofstream &out, int** a, int row, int col)
  971. {
  972.     unsigned char d[11] = {
  973.                     201, // 0
  974.                     205, // 1
  975.                     203, // 2
  976.                     187, // 3
  977.                     186, // 4
  978.                     204, // 5
  979.                     206, // 6
  980.                     185, // 7
  981.                     200, // 8
  982.                     202, // 9
  983.                     188 // 10
  984.     };
  985.     int* max_colc = new int [col];
  986.     for(int i = 0; i < col; i++)
  987.         for(int j = 0; j < row; j++)
  988.         {
  989.             string s = to_string(static_cast<__int64>(a[j][i]));
  990.             max_colc[i] = max(max_colc[i], static_cast<int>(s.size()));
  991.         }
  992.     for(int i = 0; i < row; i++)
  993.     {
  994.         switch(i)
  995.         {
  996.             case 0:
  997.             {
  998.                 // head
  999.                 out << d[0];
  1000.                 for(int j = 0; j < col; j++)
  1001.                 {
  1002.                     for(int k = 0; k < max_colc[j] + 2; k++)
  1003.                         out << d[1];
  1004.                     out << (j == col - 1 ? d[3] : d[2]);
  1005.                 }
  1006.  
  1007.                 out << endl;
  1008.  
  1009.                 // inf
  1010.                 out << d[4] << ' ';
  1011.                 for(int j = 0; j < col; j++)
  1012.                 {
  1013.                     string s = to_string(static_cast<__int64>(a[i][j]));
  1014.                     int elem_c = s.size();
  1015.  
  1016.                     for(int k = 0; k < (max_colc[j] - elem_c) / 2; k++) // |  123 |
  1017.                         out << ' ';
  1018.                     string arr_size_str = to_string(static_cast<__int64>(a[i][j]));
  1019.                     int arr_size = arr_size_str.size();
  1020.                    
  1021.                     if(max_colc[j] % 2)
  1022.                     {
  1023.                         if(arr_size % 2)
  1024.                             out << a[i][j];
  1025.                         else
  1026.                             out << 0 << a[i][j];
  1027.                     }
  1028.                     else
  1029.                     {
  1030.                         if(arr_size % 2)
  1031.                             out << 0 << a[i][j];
  1032.                         else
  1033.                             out << a[i][j];
  1034.                     }
  1035.  
  1036.                     for(int k = 0; k < (max_colc[j] - elem_c) / 2; k++) // | 123  |
  1037.                         out << ' ';
  1038.  
  1039.                     out << ' ' << d[4] << ' ';
  1040.                 }
  1041.                 out << endl;
  1042.  
  1043.                 // bottom
  1044.                 if(row == 1)
  1045.                 {
  1046.                     out << d[8];
  1047.                     for(int j = 0; j < col; j++)
  1048.                     {
  1049.                         for(int k = 0; k < max_colc[j] + 2; k++)
  1050.                             out << d[1];
  1051.                         out << (j == col - 1 ? d[10] : d[9]);
  1052.                     }
  1053.  
  1054.                 }
  1055.                 break;
  1056.             }
  1057.             default:
  1058.             {
  1059.                 // head
  1060.                 out << d[5];
  1061.                 for(int j = 0; j < col; j++)
  1062.                 {
  1063.                     for(int k = 0; k < max_colc[j] + 2; k++)
  1064.                         out << d[1];
  1065.                     out << (j == col - 1 ? d[7] : d[6]);
  1066.                 }
  1067.                 out << endl;
  1068.  
  1069.                 // inf
  1070.                 out << d[4] << ' ';
  1071.                 for(int j = 0; j < col; j++)
  1072.                 {
  1073.                     string s = to_string(static_cast<__int64>(a[i][j]));
  1074.                     int elem_c = s.size();
  1075.                     for(int k = 0; k < (max_colc[j] - elem_c) / 2; k++) // |  123 |
  1076.                         out << ' ';
  1077.  
  1078.                     string arr_size_str = to_string(static_cast<__int64>(a[i][j]));
  1079.                     int arr_size = arr_size_str.size();
  1080.  
  1081.                     if(max_colc[j] % 2)
  1082.                     {
  1083.                         if(arr_size % 2)
  1084.                             out << a[i][j];
  1085.                         else
  1086.                             out << 0 << a[i][j];
  1087.                     }
  1088.                     else
  1089.                     {
  1090.                         if(arr_size % 2)
  1091.                             out << 0 << a[i][j];
  1092.                         else
  1093.                             out << a[i][j];
  1094.                     }
  1095.  
  1096.                     for(int k = 0; k < (max_colc[j] - elem_c) / 2; k++) // | 123  |
  1097.                         out << ' ';
  1098.                     out << ' ' << d[4] << ' ';
  1099.                 }
  1100.                 out << endl;
  1101.  
  1102.                 // bottom
  1103.                 if(i == row - 1)
  1104.                 {
  1105.                     out << d[8];
  1106.                     for(int j = 0; j < col; j++)
  1107.                     {
  1108.                         for(int k = 0; k < max_colc[j] + 2; k++)
  1109.                             out << d[1];
  1110.                         out << (j == col - 1 ? d[10] : d[9]);
  1111.                     }
  1112.  
  1113.                 }
  1114.                 break;
  1115.             }
  1116.         }
  1117.     }
  1118.     out << "\n\n";
  1119. }
  1120.  
  1121. void matrixToConsole(ostream &out, int** a, int row, int col)
  1122. {
  1123.     unsigned char d[11] = {
  1124.                     201, // 0
  1125.                     205, // 1
  1126.                     203, // 2
  1127.                     187, // 3
  1128.                     186, // 4
  1129.                     204, // 5
  1130.                     206, // 6
  1131.                     185, // 7
  1132.                     200, // 8
  1133.                     202, // 9
  1134.                     188 // 10
  1135.     };
  1136.     wchar_t c[11] = {
  1137.                     '╔', // 0
  1138.                     '═', // 1
  1139.                     '╦', // 2
  1140.                     '╗', // 3
  1141.                     '║', // 4
  1142.                     '╠', // 5
  1143.                     '╬', // 6
  1144.                     '╣', // 7
  1145.                     '╚', // 8
  1146.                     '╩', // 9
  1147.                     '╝' // 10
  1148.     };
  1149.     wchar_t ex[10000];
  1150.     for(int i = 0; i < 10000; i++)
  1151.         ex[i] = i;
  1152.     int* max_colc = new int [col];
  1153.     for(int i = 0; i < col; i++)
  1154.         for(int j = 0; j < row; j++)
  1155.         {
  1156.             string s = to_string(static_cast<__int64>(a[j][i]));
  1157.             max_colc[i] = max(max_colc[i], static_cast<int>(s.size()));
  1158.         }
  1159.     for(int i = 0; i < row; i++)
  1160.     {
  1161.         switch(i)
  1162.         {
  1163.             case 0:
  1164.             {
  1165.                 // head
  1166.                 out << d[0];
  1167.                 for(int j = 0; j < col; j++)
  1168.                 {
  1169.                     for(int k = 0; k < max_colc[j] + 2; k++)
  1170.                         out << d[1];
  1171.                     out << (j == col - 1 ? d[3] : d[2]);
  1172.                 }
  1173.  
  1174.                 out << endl;
  1175.  
  1176.                 // inf
  1177.                 out << d[4] << ' ';
  1178.                 for(int j = 0; j < col; j++)
  1179.                 {
  1180.                     string s = to_string(static_cast<__int64>(a[i][j]));
  1181.                     int elem_c = s.size();
  1182.  
  1183.                     for(int k = 0; k < (max_colc[j] - elem_c) / 2; k++) // |  123 |
  1184.                         out << ' ';
  1185.                     string arr_size_str = to_string(static_cast<__int64>(a[i][j]));
  1186.                     int arr_size = arr_size_str.size();
  1187.                    
  1188.                     if(max_colc[j] % 2)
  1189.                     {
  1190.                         if(arr_size % 2)
  1191.                             out << a[i][j];
  1192.                         else
  1193.                             out << 0 << a[i][j];
  1194.                     }
  1195.                     else
  1196.                     {
  1197.                         if(arr_size % 2)
  1198.                             out << 0 << a[i][j];
  1199.                         else
  1200.                             out << a[i][j];
  1201.                     }
  1202.  
  1203.                     for(int k = 0; k < (max_colc[j] - elem_c) / 2; k++) // | 123  |
  1204.                         out << ' ';
  1205.  
  1206.                     out << ' ' << d[4] << ' ';
  1207.                 }
  1208.                 out << endl;
  1209.  
  1210.                 // bottom
  1211.                 if(row == 1)
  1212.                 {
  1213.                     out << d[8];
  1214.                     for(int j = 0; j < col; j++)
  1215.                     {
  1216.                         for(int k = 0; k < max_colc[j] + 2; k++)
  1217.                             out << d[1];
  1218.                         out << (j == col - 1 ? d[10] : d[9]);
  1219.                     }
  1220.  
  1221.                 }
  1222.                 break;
  1223.             }
  1224.             default:
  1225.             {
  1226.                 // head
  1227.                 out << d[5];
  1228.                 for(int j = 0; j < col; j++)
  1229.                 {
  1230.                     for(int k = 0; k < max_colc[j] + 2; k++)
  1231.                         out << d[1];
  1232.                     out << (j == col - 1 ? d[7] : d[6]);
  1233.                 }
  1234.                 out << endl;
  1235.  
  1236.                 // inf
  1237.                 out << d[4] << ' ';
  1238.                 for(int j = 0; j < col; j++)
  1239.                 {
  1240.                     string s = to_string(static_cast<__int64>(a[i][j]));
  1241.                     int elem_c = s.size();
  1242.                     for(int k = 0; k < (max_colc[j] - elem_c) / 2; k++) // |  123 |
  1243.                         out << ' ';
  1244.  
  1245.                     string arr_size_str = to_string(static_cast<__int64>(a[i][j]));
  1246.                     int arr_size = arr_size_str.size();
  1247.  
  1248.                     if(max_colc[j] % 2)
  1249.                     {
  1250.                         if(arr_size % 2)
  1251.                             out << a[i][j];
  1252.                         else
  1253.                             out << 0 << a[i][j];
  1254.                     }
  1255.                     else
  1256.                     {
  1257.                         if(arr_size % 2)
  1258.                             out << 0 << a[i][j];
  1259.                         else
  1260.                             out << a[i][j];
  1261.                     }
  1262.  
  1263.                     for(int k = 0; k < (max_colc[j] - elem_c) / 2; k++) // | 123  |
  1264.                         out << ' ';
  1265.                     out << ' ' << d[4] << ' ';
  1266.                 }
  1267.                 out << endl;
  1268.  
  1269.                 // bottom
  1270.                 if(i == row - 1)
  1271.                 {
  1272.                     out << d[8];
  1273.                     for(int j = 0; j < col; j++)
  1274.                     {
  1275.                         for(int k = 0; k < max_colc[j] + 2; k++)
  1276.                             out << d[1];
  1277.                         out << (j == col - 1 ? d[10] : d[9]);
  1278.                     }
  1279.  
  1280.                 }
  1281.                 break;
  1282.             }
  1283.         }
  1284.     }
  1285.     out << "\n\n";
  1286. }
  1287.  
  1288. void getNewMatrix(Matrix a, Matrix c, int n, int ii, int jj)
  1289. {
  1290.     int i, j, di = 0, dj;
  1291.     for(i = 0; i < n - 1; i++)
  1292.     {
  1293.         if(i == ii)
  1294.             di = 1;
  1295.         dj = 0;
  1296.         for(j = 0; j < n - 1; j++)
  1297.         {
  1298.             if(j == jj)
  1299.                 dj = 1;
  1300.             c.Set(i, j, a.Get(i + di, j + dj));
  1301.         }
  1302.     }
  1303. }
  1304.  
  1305. __int64 getDeterminant(Matrix a, int n)
  1306. {
  1307.     __int64 det = 0;
  1308.     int k = 1;
  1309.     Matrix c(n, n);
  1310.     if(n == 1)
  1311.     {
  1312.         det = a.Get(0, 0);
  1313.         return det;
  1314.     }
  1315.     else if(n == 2)
  1316.     {
  1317.         det = a.Get(0, 0) * a.Get(1, 1) - a.Get(0, 1) * a.Get(1, 0);
  1318.         return det;
  1319.     }
  1320.     for(int i = 0; i < n; i++)
  1321.     {
  1322.         getNewMatrix(a, c, n, i, 0);   
  1323.         det += k * a.Get(i, 0) * getDeterminant(c, n - 1);
  1324.         k = -k;
  1325.     }
  1326.     return det;
  1327. }
  1328.  
  1329. #endregion
  1330. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement