Advertisement
Sarnapa

Matrix_Calculator

Mar 31st, 2015
448
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 23.88 KB | None | 0 0
  1. ////////////////////////////////////////////////////// matrix.h ///////////////////////////////////////////////////////////////
  2.  
  3. #ifndef matrix_h
  4. #define matrix_h
  5. #include <cmath>
  6. #include "identity_matrix.h"
  7.  
  8.  
  9. enum LU_decision {BOTTOM,TOP}; // decydujemy czy interesuja nas gorna macierz trojkatna czy dolna
  10.  
  11. class Matrix {
  12.     unsigned int columns; // liczba kolumn
  13.     unsigned int rows; // liczba wierszy
  14.     double **mx; // macierz dynamiczna
  15.  
  16.     public:
  17.     Matrix(unsigned int,unsigned int); // konstruktor dla prostokatnych
  18.     Matrix(unsigned int); // konstruktor dla kwadratowych
  19.     Matrix(const Matrix&); // konstruktor kopiujacy
  20.     Matrix(const Id_Matrix&); // konstruktor kopiujacy dla macierzy jednostkowej
  21.     ~Matrix(); // destruktor
  22.  
  23.     void load_data(); // do wczytywania danych do macierzy
  24.     void show() const; // metoda stala do pokazywania macierzy
  25.     void show(LU_decision) const; // do wyswietlania macierzy trojkatnych w rozkladzie LU
  26.     Matrix multiply(const Matrix&, const Matrix&); // mnozenie dla potegowania (2 rozne czynniki)
  27.     Matrix multiply(const Matrix&); // mnozenie dla potegowania (1 czynnik)
  28.     Matrix exponentation(unsigned int); // potegowanie macierzy kwadratowych (tylko dla wykladnikow nieujemnych)
  29.     bool lu(); // rozklad LU macierzy kwadratowej
  30.     Matrix tranpose(); // do transponowania
  31.  
  32.     friend Matrix operator+(const Matrix&, const Matrix&); // dodawanie macierzy
  33.     friend Matrix operator-(const Matrix&, const Matrix&); // odejmowanie macierzy
  34.     friend Matrix operator*(const Matrix&, const Matrix&); // mnozenie macierzy
  35.     friend Matrix operator*(const int, const Matrix&); // mnozenie przez skalar
  36.     Matrix& operator=(const Matrix &mx1) { // operator przypisywania dla macierzy (dla potegowania)
  37.         if(this!=&mx1) { // uwazamy na przypisanie &mx1=&mx1
  38.             for(unsigned int i=0;i<this->rows;i++) {
  39.                 for(unsigned int j=0;j<this->columns;j++) {
  40.                     this->mx[i][j]=mx1.mx[i][j];
  41.                 }
  42.             }
  43.         }
  44.         return *this;
  45.     };
  46. };
  47.  
  48. #endif
  49.  
  50. ////////////////////////////////////////////////////////// matrix.cpp ///////////////////////////////////////////////////////////
  51.  
  52. #include "matrix.h"
  53.  
  54. #define EPSILON 1e-18 // do wyznaczania rozkladu LU --> otoczenie zera
  55.  
  56. using namespace std;
  57.  
  58. Matrix::Matrix(unsigned int _rows, unsigned int _columns) { // konstruktor dla prostokatnych macierzy
  59.     cout << "Macierz created" << endl;
  60.     rows = _rows;
  61.     columns = _columns;
  62.     mx = new double *[rows]; // tworzymy tablice wskaznikow wierszy
  63.     for(unsigned int i=0;i<rows;i++) {
  64.         mx[i] = new double [columns]; // teraz tworzymy rows tablic o rozmiarze columns (tablice wierszy)
  65.     }
  66. }
  67.  
  68. Matrix::Matrix(unsigned int _order) { // konstruktor dla kwadratowych
  69.     cout << "Macierz created" << endl;
  70.     rows = _order;
  71.     columns = _order;
  72.     mx = new double *[rows]; // tworzymy tablice wskaznikow wierszy
  73.     for(unsigned int i=0;i<rows;i++) {
  74.         mx[i] = new double [columns]; // teraz tworzymy rows tablic o rozmiarze columns (tablice wierszy)
  75.     }
  76. }
  77.  
  78. Matrix::Matrix(const Matrix &mx1) {  // konstruktor kopiujacy
  79.     cout << "Macierz created" << endl;
  80.     rows = mx1.rows;
  81.     columns = mx1.columns;
  82.     mx = new double *[rows]; // tworzymy tablice wskaznikow wierszy
  83.     for(unsigned int i=0;i<rows;i++) {
  84.         mx[i] = new double [columns];
  85.     }
  86.     for(unsigned int i=0;i<rows;i++) {
  87.         for(unsigned int j=0;j<columns;j++) {
  88.             mx[i][j]=mx1.mx[i][j];
  89.         }
  90.     }
  91. }
  92.  
  93. Matrix::Matrix(const Id_Matrix &id_mx1) {  // konstruktor kopiujacy dla macierzy jednostkowej(dla potegowania)
  94.     cout << "Macierz created" << endl;
  95.     rows = id_mx1.order;
  96.     columns = id_mx1.order;
  97.     mx = new double *[rows]; // tworzymy tablice wskaznikow wierszy
  98.     for(unsigned int i=0;i<rows;i++) {
  99.         mx[i] = new double [columns];
  100.     }
  101.     for(unsigned int i=0;i<rows;i++) {
  102.         for(unsigned int j=0;j<columns;j++) {
  103.             mx[i][j]=id_mx1.mx[i][j];
  104.         }
  105.     }
  106. }
  107.  
  108. Matrix::~Matrix() { // destruktor
  109.     for(unsigned int i=0;i<rows;i++) {
  110.         delete [] mx[i]; // niszczymy tablice wierszy
  111.     }
  112.     delete [] mx; // niszczymy tablice wskaznikow wierszy
  113.     cout << "Macierz zostala zniszczona" << endl;
  114. }
  115.  
  116. Matrix operator+(const Matrix &mx1, const Matrix &mx2) {
  117.     if((mx1.columns==mx2.columns)&&(mx1.rows==mx2.rows)) {
  118.         Matrix mx3 = Matrix(mx1.rows,mx1.columns);
  119.         for(unsigned int i=0;i<mx1.rows;i++) {
  120.             for(unsigned int j=0;j<mx1.columns;j++) {
  121.                 mx3.mx[i][j]=mx1.mx[i][j]+mx2.mx[i][j];
  122.             }
  123.         }
  124.         cout << "Wynik:" << endl;
  125.         mx3.show();
  126.         return mx3;
  127.     }
  128.     else {
  129.         Matrix mx3 = Matrix(0,0);
  130.         cout << "Rozmiary macierzy nie sa takie same. Dodawanie jest niemozliwe." << endl;
  131.         return mx3;
  132.     }
  133. }
  134.  
  135. Matrix operator-(const Matrix &mx1, const Matrix &mx2) {
  136.     if((mx1.columns==mx2.columns)&&(mx1.rows==mx2.rows)) {
  137.         Matrix mx3 = Matrix(mx1); // uzywam konstruktora kopiujacego
  138.         for(unsigned int i=0;i<mx1.rows;i++) {
  139.             for(unsigned int j=0;j<mx1.columns;j++) {
  140.                 mx3.mx[i][j]-=mx2.mx[i][j];
  141.             }
  142.         }
  143.         cout << "Wynik:" << endl;
  144.         mx3.show();
  145.         return mx3;
  146.     }
  147.     else {
  148.         Matrix mx3 = Matrix(0,0);
  149.         cout << "Rozmiary macierzy nie sa takie same. Odejmowanie jest niemozliwe." << endl;
  150.         return mx3;
  151.     }
  152. }
  153.  
  154. Matrix operator*(const Matrix &mx1, const Matrix &mx2) {
  155.     double s; // suma kolejnych mnozen
  156.     if(mx1.columns==mx2.rows) {
  157.         Matrix mx3 = Matrix(mx1.rows,mx2.columns);
  158.         for(unsigned int i=0;i<mx1.rows;i++) {
  159.             for(unsigned int j=0;j<mx2.columns;j++) {
  160.                 s = 0;
  161.                 for(unsigned int k=0;k<mx1.columns;k++) { // mx2.rows tez moze byc
  162.                     s += mx1.mx[i][k]*mx2.mx[k][j];
  163.                 }
  164.                 mx3.mx[i][j]=s;
  165.             }
  166.         }
  167.         cout << "Wynik:" << endl;
  168.         mx3.show();
  169.         return mx3;
  170.     }
  171.     else {
  172.         Matrix mx3 = Matrix(0,0);
  173.         cout << "Liczba kolumn pierwszej macierzy nie jest równa liczbie wierszy drugiej. Mnozenie jest niemozliwe." << endl;
  174.         return mx3;
  175.     }
  176. }
  177.  
  178. Matrix operator*(const int s, const Matrix &mx1) {
  179.     Matrix mx2 = Matrix(mx1);
  180.     for(unsigned int i=0;i<mx1.rows;i++) {
  181.         for(unsigned int j=0;j<mx1.columns;j++) {
  182.             mx2.mx[i][j]=mx1.mx[i][j]*s;
  183.         }
  184.     }
  185.     cout << "Wynik:" << endl;
  186.     mx2.show();
  187.     return mx2;
  188. }
  189.  
  190. void Matrix::load_data() {
  191.     double val; // wartosc wpisywana przez uzytkownika
  192.     for(unsigned int i=0;i<rows;i++) {
  193.         for(unsigned int j=0;j<columns;j++) {
  194.             cout << "Wpisz wartosc w polu: " << i+1 << " wiersz " << "i " << j+1 << " kolumna" << endl;
  195.             cin >> val;
  196.             if(cin.good()) {
  197.                 mx[i][j]=val;
  198.             }
  199.             else {
  200.                 cout << "Wpisales zla wartosc " << endl;
  201.                 j--; // wracamy do pola gdzie zostala wpisana zla dana
  202.                 cin.clear(); //czyscimy flagi bledu
  203.                 cin.ignore( 1000, '\n' ); // zamiast cin.sync bo moze nie chodzic na linuxie...ignoruje do 1000
  204.                                           //znakow w strumieniu... jesli napotkamy \n to koniec procesu
  205.             }
  206.         }
  207.     }
  208. }
  209.  
  210. void Matrix::show() const {
  211.         for(unsigned int i=0;i<rows;i++) {
  212.             cout << "|";
  213.             for(unsigned int j=0;j<columns;j++) {
  214.                 cout << mx[i][j] << " ";
  215.             }
  216.             cout << "|" << endl;
  217.         }
  218.         cout << endl;
  219. }
  220.  
  221. void Matrix::show(LU_decision d) const {
  222.         if(d==BOTTOM) {
  223.             for(unsigned int i=0;i<rows;i++) {
  224.                 cout << "|";
  225.                 for(unsigned int j=0;j<columns;j++) {
  226.                     if(j<i) {
  227.                         cout << mx[i][j] << " ";
  228.                     }
  229.                     else if(j==i)
  230.                         cout << "1" << " ";
  231.                     else
  232.                         cout << "0" << " ";
  233.                 }
  234.                 cout << "|" << endl;
  235.             }
  236.             cout << endl;
  237.         }
  238.         else {
  239.             for(unsigned int i=0;i<rows;i++) {
  240.                 cout << "|";
  241.                 for(unsigned int j=0;j<columns;j++) {
  242.                     if(j>=i) {
  243.                         cout << mx[i][j] << " ";
  244.                     }
  245.                     else
  246.                         cout << "0" << " ";
  247.                 }
  248.                 cout << "|" << endl;
  249.             }
  250.             cout << endl;
  251.         }
  252. }
  253.  
  254. Matrix Matrix::multiply(const Matrix &mx1, const Matrix &mx2) {
  255.     double s;
  256.     Matrix mx3 = Matrix(mx1.rows,mx2.columns);
  257.     for(unsigned int i=0;i<mx1.rows;i++) {
  258.         for(unsigned int j=0;j<mx2.columns;j++) {
  259.             s = 0;
  260.             for(unsigned int k=0;k<mx1.columns;k++) { // mx2.rows tez moze byc
  261.                 s += mx1.mx[i][k]*mx2.mx[k][j];
  262.             }
  263.             mx3.mx[i][j]=s;
  264.         }
  265.     }
  266.     return mx3;
  267. }
  268.  
  269. Matrix Matrix::multiply(const Matrix &mx1) {
  270.     double s;
  271.     Matrix mx2 = Matrix(mx1.rows,mx1.columns);
  272.     for(unsigned int i=0;i<mx1.rows;i++) {
  273.         for(unsigned int j=0;j<mx1.columns;j++) {
  274.             s = 0;
  275.             for(unsigned int k=0;k<mx1.columns;k++) { // mx2.rows tez moze byc
  276.                 s += mx1.mx[i][k]*mx1.mx[k][j];
  277.             }
  278.             mx2.mx[i][j]=s;
  279.         }
  280.     }
  281.     return mx2;
  282. }
  283.  
  284. Matrix Matrix::exponentation(unsigned int s) { // potegowanie
  285.     Id_Matrix id_mx(rows); // ustawiamy macierz jednostkowa
  286.     id_mx.load_data();
  287.     Matrix mx1(*this);
  288.     Matrix mx2(id_mx); // kopiujemy macierz jednostkowa (mnozenie jest tylko dla klasy matrix)
  289.     while(s) {
  290.         if(s&1) { // badamy czy najmlodszym bitem liczby s jest 1
  291.             mx2 = multiply(mx1,mx2);
  292.         }
  293.         s = s >> 1; // przesuniecie bitowe w prawo o 1 pozycje
  294.         if(!s) break;
  295.         mx1 = multiply(mx1);
  296.     }
  297.     return mx2;
  298. }
  299.  
  300. bool Matrix::lu() { // rozklad LU
  301.     if(rows==columns) {
  302.         for(unsigned int i=0;i<rows-1;i++) {
  303.             if(fabs(mx[i][i])>EPSILON) { // nie dzielimy przez zero
  304.                 for(unsigned int j=i+1;j<rows;j++) { // normalizacja kolumny (odejmujemy od kazdego elementu pod dany element na przekatnej
  305.                     mx[j][i]=mx[j][i]/mx[i][i];
  306.                 }
  307.                 for(unsigned int j=i+1;j<rows;j++) { // modyfikacja podmacierzy
  308.                     for(unsigned int k=i+1;k<rows;k++) {
  309.                         mx[j][k] = mx[j][k]-(mx[j][i]*mx[i][k]);
  310.                     }
  311.                 }
  312.             }
  313.             else {
  314.                 cout << "Rozklad LU nie jest mozliwy." << endl;
  315.                 return false;
  316.             }
  317.         }
  318.         return true;
  319.     }
  320.     else {
  321.         cout << "Rozklad LU jest tylko dla macierzy kwadratowych." << endl;
  322.         return false;
  323.     }
  324. }
  325.  
  326. Matrix Matrix::tranpose() {
  327.     Matrix mx2(columns,rows);
  328.     for(unsigned int i=0;i<rows;i++) {
  329.         for(unsigned int j=0;j<columns;j++) {
  330.             mx2.mx[j][i] = mx[i][j];
  331.         }
  332.     }
  333.     return mx2;
  334. }
  335.  
  336. //////////////////////////////////////////////////////////// identity_matrix.h //////////////////////////////////////////////////
  337.  
  338. #ifndef identity_matrix_h
  339. #define identity_matrix_h
  340. #include <iostream>
  341.  
  342. class Id_Matrix {
  343.     public:
  344.     unsigned int order; // stopien
  345.     double **mx; // macierz dynamiczna
  346.     Id_Matrix(unsigned int); // konstruktor
  347.     Id_Matrix(const Id_Matrix&); // konstruktor kopiujacy
  348.     ~Id_Matrix(); // destruktor
  349.  
  350.     void load_data(); // do wczytywania danych do macierzy
  351.     void show() const; // metoda stala do pokazywania macierzy
  352. };
  353.  
  354. #endif
  355.  
  356. ///////////////////////////////////////////////////////////// identity_matrix.cpp //////////////////////////////////////////////
  357.  
  358. #include "identity_matrix.h"
  359.  
  360. using namespace std;
  361.  
  362. Id_Matrix::Id_Matrix(unsigned int _order) { //konstruktor
  363.     order = _order;
  364.     mx = new double *[order]; // tworzymy tablice wskaznikow wierszy
  365.     for(unsigned int i=0;i<order;i++) {
  366.         mx[i] = new double [order]; // teraz tworzymy rows tablic o rozmiarze columns (tablice wierszy)
  367.     }
  368. }
  369.  
  370. Id_Matrix::Id_Matrix(const Id_Matrix &mx1) {  // konstruktor kopiujacy
  371.     cout << "Macierz jednostkowa created" << endl;
  372.     order = mx1.order;
  373.     mx = new double *[order]; // tworzymy tablice wskaznikow wierszy
  374.     for(unsigned int i=0;i<order;i++) {
  375.         mx[i] = new double [order];
  376.     }
  377.     for(unsigned int i=0;i<order;i++) {
  378.         for(unsigned int j=0;j<order;j++) {
  379.             mx[i][j]=mx1.mx[i][j];
  380.         }
  381.     }
  382. }
  383.  
  384. Id_Matrix::~Id_Matrix() { // destruktor
  385.     for(unsigned int i=0;i<order;i++) {
  386.         delete [] mx[i]; // niszczymy tablice wierszy
  387.     }
  388.     delete [] mx; // niszczymy tablice wskaznikow wierszy
  389.     cout << "Macierz jednostkowa zostala zniszczona" << endl;
  390. }
  391.  
  392. void Id_Matrix::load_data() {
  393.     for(unsigned int i=0;i<order;i++) {
  394.         for(unsigned int j=0;j<order;j++) {
  395.             if(i==j)
  396.                 mx[i][j]=1;
  397.             else
  398.                 mx[i][j]=0;
  399.         }
  400.     }
  401. }
  402.  
  403. void Id_Matrix::show() const {
  404.         for(unsigned int i=0;i<order;i++) {
  405.             cout << "|";
  406.             for(unsigned int j=0;j<order;j++) {
  407.                 cout << mx[i][j] << " ";
  408.             }
  409.             cout << "|" << endl;
  410.         }
  411.         cout << endl;
  412. }
  413.  
  414. /////////////////////////////////////////////////////// Diagonal_matrix. h ////////////////////////////////////////////////////
  415.  
  416. #ifndef diagonal_matrix_h
  417. #define diagonal_matrix_h
  418. #include <iostream>
  419.  
  420. class D_Matrix {
  421.     unsigned int order; // stopien
  422.     double **mx; // macierz dynamiczna
  423.  
  424.     public:
  425.     D_Matrix(unsigned int); // konstruktor
  426.     D_Matrix(const D_Matrix&); // konstruktor kopiujacy
  427.     ~D_Matrix(); // destruktor
  428.  
  429.     void load_data(); // do wczytywania danych do macierzy
  430.     void show() const; // metoda stala do pokazywania macierzy
  431.     void determinant(); // wyznacznik dla macierzy diagonalnych
  432. };
  433.  
  434. #endif
  435.  
  436. ////////////////////////////////////////////////////// Diagonal_matrix.cpp ///////////////////////////////////////////////////
  437.  
  438. #include "diagonal_matrix.h"
  439.  
  440. using namespace std;
  441.  
  442. D_Matrix::D_Matrix(unsigned int _order): order(_order) { //konstruktor ( z lista inicjalizacyjna)
  443.     cout << "Macierz diagonalna created" << endl;
  444.     mx = new double *[order]; // tworzymy tablice wskaznikow wierszy
  445.     for(unsigned int i=0;i<order;i++) {
  446.         mx[i] = new double [order]; // teraz tworzymy rows tablic o rozmiarze columns (tablice wierszy)
  447.     }
  448. }
  449.  
  450. D_Matrix::D_Matrix(const D_Matrix &mx1): order(mx1.order) {  // konstruktor kopiujacy
  451.     cout << "Macierz diagonalna created" << endl;
  452.     mx = new double *[order]; // tworzymy tablice wskaznikow wierszy
  453.     for(unsigned int i=0;i<order;i++) {
  454.         mx[i] = new double [order];
  455.     }
  456.     for(unsigned int i=0;i<order;i++) {
  457.         for(unsigned int j=0;j<order;j++) {
  458.             mx[i][j]=mx1.mx[i][j];
  459.         }
  460.     }
  461. }
  462.  
  463. D_Matrix::~D_Matrix() { // destruktor
  464.     for(unsigned int i=0;i<order;i++) {
  465.         delete [] mx[i]; // niszczymy tablice wierszy
  466.     }
  467.     delete [] mx; // niszczymy tablice wskaznikow wierszy
  468.     cout << "Macierz diagoonalna zostala zniszczona" << endl;
  469. }
  470.  
  471. void D_Matrix::load_data() {
  472.     double val;
  473.     for(unsigned int i=0;i<order;i++) {
  474.         for(unsigned int j=0;j<order;j++) {
  475.             if(i==j) {
  476.                 cout << "Wpisz wartosc w polu: " << i+1 << " wiersz " << "i " << j+1 << " kolumna" << endl;
  477.                 cin >> val;
  478.                 if(cin.good()) {
  479.                     mx[i][j]=val;
  480.                 }
  481.                 else {
  482.                     cout << "Wpisales zla wartosc " << endl;
  483.                     j--; // wracamy do pola gdzie zostala wpisana zla dana
  484.                     cin.clear(); //czyscimy flagi bledu
  485.                     cin.ignore( 1000, '\n' ); // zamiast cin.sync bo moze nie chodzic na linuxie...ignoruje do 1000
  486.                                           //znakow w strumieniu... jesli napotkamy \n to koniec procesu
  487.                 }
  488.             }
  489.             else
  490.                 mx[i][j]=0;
  491.         }
  492.     }
  493. }
  494.  
  495. void D_Matrix::show() const {
  496.     for(unsigned int i=0;i<order;i++) {
  497.         cout << "|";
  498.         for(unsigned int j=0;j<order;j++) {
  499.             cout << mx[i][j] << " ";
  500.         }
  501.         cout << "|" << endl;
  502.     }
  503.     cout << endl;
  504. }
  505.  
  506. void D_Matrix::determinant() {
  507.     double d=1; // wyznacznik
  508.     unsigned int i=0; // pomocniczy
  509.     while(i<order) {
  510.         d *= mx[i][i];
  511.         i++;
  512.     }
  513.     cout << "Wyznacznik tej macierzy wynosi:" << d << endl;
  514. }
  515.  
  516. ///////////////////////////////////////////////////// other.h /////////////////////////////////////////////////////////////////
  517.  
  518. #ifndef other_h
  519. #define other_h
  520. #include "matrix.h"
  521. #include "diagonal_matrix.h"
  522.  
  523. void menu();
  524. unsigned int load_unsigned_int(const char*); // do liczby wierszy, kolumn
  525. int load_int(const char*); // do skalara
  526. void interface();
  527.  
  528. #endif
  529.  
  530.  
  531. //////////////////////////////////////////////////// other.cpp ///////////////////////////////////////////////////////////////
  532.  
  533. #include "other.h"
  534.  
  535. using namespace std;
  536.  
  537. void menu() {
  538.     cout << "Menu programu: " << endl;
  539.     cout << "1. Dodawanie macierzy " << endl;
  540.     cout << "2. Odejmowanie macierzy " << endl;
  541.     cout << "3. Mnozenie macierzy " << endl;
  542.     cout << "4. Mnozenie macierzy przez skalar " << endl;
  543.     cout << "5. Potegowanie macierzy (tylko dla macierzy kwadaratowych)" << endl;
  544.     cout << "6. Rozklad LU (tylko dla macierzy kwadratowych)" << endl;
  545.     cout << "7. Transponowanie macierzy" << endl;
  546.     cout << "8. Wyznacznik (na razie tylko dla diagonalnych)" << endl;
  547.     cout << "9. Koniec programu" << endl;
  548.     cout << "Co chcesz zrobic ? Wpisz odpowiednia cyfre" << endl;
  549. }
  550.  
  551. void clean_cin () {
  552.     cout << "Wpisales zla wartosc " << endl;
  553.     cin.clear(); //czyscimy flagi bledu
  554.     cin.ignore( 1000, '\n' ); // zamiast cin.sync bo moze nie chodzic na linuxie...ignoruje do 1000
  555.                                       //znakow w strumieniu... jesli napotkamy \n to koniec procesu
  556. }
  557.  
  558. unsigned int load_unsigned_int(const char *str) { // do wczytywania kolumn i wierszy macierzy
  559.     unsigned int i;
  560.     cout << str << endl;
  561.     cin >> i;
  562.     while(cin.fail() || (i==0)) {
  563.         clean_cin();
  564.         cout << str << endl;
  565.         cin >> i;
  566.     }
  567.     return i;
  568. }
  569.  
  570. int load_int(const char *str) { // do wczytywania skalara
  571.     int i; // skalar
  572.     cout << str << endl;
  573.     cin >> i;
  574.     while(cin.fail()) {
  575.         clean_cin();
  576.         cout << str << endl;
  577.         cin >> i;
  578.     }
  579.     return i;
  580. }
  581.  
  582. void interface() {
  583.     int choice=-1;
  584.     unsigned int _columns, _rows,_order;
  585.     bool lu_bool; // czy rozklad LU mozliwy czy nie
  586.     while(choice!=9) {
  587.         switch(choice){
  588.             case 1: {
  589.                 _rows = load_unsigned_int("Podaj liczbe wierszy");
  590.                 _columns = load_unsigned_int("Podal licze kolumn");
  591.                 Matrix mx1(_rows,_columns);
  592.                 mx1.load_data();
  593.                 _rows = load_unsigned_int("Podaj liczbe wierszy");
  594.                 _columns = load_unsigned_int("Podaj liczbe kolumn");
  595.                 Matrix mx2(_rows,_columns);
  596.                 mx2.load_data();
  597.                 cout << "Macierze, ktore dodajemy:" << endl;
  598.                 mx1.show();
  599.                 mx2.show();
  600.                 Matrix mx3 = (mx1)+(mx2);
  601.                 break;
  602.             }
  603.             case 2: {
  604.                 _rows = load_unsigned_int("Podaj liczbe wierszy");
  605.                 _columns = load_unsigned_int("Podaj liczbe kolumn");
  606.                 Matrix mx1(_rows,_columns);
  607.                 mx1.load_data();
  608.                 _rows = load_unsigned_int("Podaj liczbw wierszy");
  609.                 _columns = load_unsigned_int("Podaj liczbe kolumn");
  610.                 Matrix mx2(_rows,_columns);
  611.                 mx2.load_data();
  612.                 cout << "Macierze, ktore odejmujemy:" << endl;
  613.                 mx1.show();
  614.                 mx2.show();
  615.                 Matrix mx3 = (mx1)-(mx2);
  616.                 break;
  617.             }
  618.             case 3: {
  619.                 _rows = load_unsigned_int("Podaj liczbe wierszy");
  620.                 _columns = load_unsigned_int("Podaj liczbe kolumn");
  621.                 Matrix mx1(_rows,_columns);
  622.                 mx1.load_data();
  623.                 _rows = load_unsigned_int("Podaj liczbw wierszy");
  624.                 _columns = load_unsigned_int("Podaj liczbe kolumn");
  625.                 Matrix mx2(_rows,_columns);
  626.                 mx2.load_data();
  627.                 cout << "Macierze, ktore mnozymy:" << endl;
  628.                 mx1.show();
  629.                 mx2.show();
  630.                 Matrix mx3 = (mx1)*(mx2);
  631.                 break;
  632.             }
  633.             case 4: {
  634.                 _rows = load_unsigned_int("Podaj liczbe wierszy");
  635.                 _columns = load_unsigned_int("Podaj liczbe kolumn");
  636.                 Matrix mx1(_rows,_columns);
  637.                 mx1.load_data();
  638.                 const int s = load_int("Podaj wartosc skalara");
  639.                 cout << "Macierz, ktora mnozysz przez wybrany skalar:" << endl;
  640.                 mx1.show();
  641.                 cout << "Wartosc skalara: " << s << endl << endl;
  642.                 Matrix mx2 = (s)*(mx1);
  643.                 break;
  644.             }
  645.             case 5: {
  646.                 _order = load_unsigned_int("Podaj stopien macierzy");
  647.                 Matrix mx1(_order);
  648.                 mx1.load_data();
  649.                 int s = load_unsigned_int("Podaj wartosc wykladnika");
  650.                 cout << "Macierz, ktora potegujesz:" << endl;
  651.                 mx1.show();
  652.                 cout << "Wartosc wykladnika: " << s << endl;
  653.                 Matrix mx2 = mx1.exponentation(s);
  654.                 cout << "Wynikowa macierza:" << endl;
  655.                 mx2.show();
  656.                 break;
  657.             }
  658.             case 6: {
  659.                 _order = load_unsigned_int("Podaj stopien macierzy");
  660.                 Matrix mx1(_order);
  661.                 mx1.load_data();
  662.                 cout << "Macierz,ktora bedzie poddana rozkladowi:" << endl;
  663.                 mx1.show();
  664.                 lu_bool=mx1.lu();
  665.                 if(lu_bool==true) {
  666.                     cout << "Wynikowe macierze" << endl;
  667.                     mx1.show(BOTTOM);
  668.                     mx1.show(TOP);
  669.                 }
  670.                 break;
  671.             }
  672.             case 7: {
  673.                 _rows = load_unsigned_int("Podaj liczbe wierszy");
  674.                 _columns = load_unsigned_int("Podaj liczbe kolumn");
  675.                 Matrix mx1(_rows,_columns);
  676.                 mx1.load_data();
  677.                 cout << "Macierz, ktora transponujesz:" << endl;
  678.                 mx1.show();
  679.                 Matrix mx2 = mx1.tranpose();
  680.                 cout << "Macierz wynikowa:" << endl;
  681.                 mx2.show();
  682.                 break;
  683.             }
  684.             case 8: {
  685.                 _order = load_unsigned_int("Podaj stopien macierzy diagonalnej");
  686.                 D_Matrix mx1(_order);
  687.                 mx1.load_data();
  688.                 cout << "Macierz diagonalna, dla ktorej liczysz wyznacznik:" << endl;
  689.                 mx1.show();
  690.                 mx1.determinant();
  691.             }
  692.         }
  693.         if ((choice > 9)||(choice==0))
  694.             cout << "Podales zla liczbe" << endl;
  695.         menu();
  696.         cin >> choice;
  697.         if(cin.fail()) {
  698.             clean_cin();
  699.         }
  700.     }
  701. }
  702.  
  703. ///////////////////////////////////////////////////////////// main.cpp ////////////////////////////////////////////////////////
  704.  
  705. #include "other.h"
  706.  
  707. using namespace std;
  708.  
  709. int main() {
  710.     cout << "Witam w kalkulatorze macierzy" << endl;
  711.     interface();
  712.     cout << "Dziekujemy za skorzystanie z naszego kalkulatora" << endl;
  713.     return 0;
  714. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement