Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.82 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <time.h>
  4.  
  5. using namespace std;
  6.  
  7. void wypisz(int** A,int a,int b)
  8. {
  9.     for(int w=0;w<a;w++)
  10.     {
  11.         for(int k=0;k<b;k++)
  12.         {
  13.             cout << A[w][k] << "\t";
  14.         }
  15.         cout << endl;
  16.     }
  17. }
  18.  
  19. int** losuj(int a,int b)
  20. {
  21.     int** A=new int*[a];
  22.     for(int i=0;i<a;i++)
  23.     {
  24.         A[i]=new int[b];
  25.     }
  26.  
  27.     for(int w=0;w<a;w++)
  28.     {
  29.         for(int k=0;k<b;k++)
  30.         {
  31.             A[w][k]=rand()%10;
  32.         }
  33.     }
  34.     return A;
  35. }
  36.  
  37. int** pomnoz(int **A,int a,int b,int l)
  38. {
  39.     int** B=new int*[a];
  40.     for(int i=0;i<a;i++)
  41.     {
  42.         B[i]=new int[b];
  43.     }
  44.  
  45.     for(int w=0;w<a;w++)
  46.     {
  47.         for(int k=0;k<b;k++)
  48.         {
  49.             B[w][k]=A[w][k]*l;
  50.         }
  51.     }
  52.     return B;
  53. }
  54.  
  55. float srednia(int **A,int a,int b)
  56. {
  57.     float s=0;
  58.     for(int w=0;w<a;w++)
  59.     {
  60.         for(int k=0;k<b;k++)
  61.         {
  62.             s+=A[w][k];
  63.         }
  64.     }
  65.     return s/(a*b);
  66. }
  67.  
  68. int maks(int **A,int a,int b)
  69. {
  70.     int m=A[0][0];
  71.     for(int w=0;w<a;w++)
  72.     {
  73.         for(int k=0;k<b;k++)
  74.         {
  75.             if(A[w][k]>m)
  76.                 m=A[w][k];
  77.         }
  78.     }
  79.     return m;
  80. }
  81.  
  82. int mini(int **A,int a,int b)
  83. {
  84.     int m=A[0][0];
  85.     for(int w=0;w<a;w++)
  86.     {
  87.         for(int k=0;k<b;k++)
  88.         {
  89.             if(A[w][k]<m)
  90.                 m=A[w][k];
  91.         }
  92.     }
  93.     return m;
  94. }
  95.  
  96. int** dodaj(int **A,int **B,int a,int b)
  97. {
  98.     int** C=new int*[a];
  99.     for(int i=0;i<a;i++)
  100.     {
  101.         C[i]=new int[b];
  102.     }
  103.     for(int w=0;w<a;w++)
  104.     {
  105.         for(int k=0;k<b;k++)
  106.         {
  107.             C[w][k]=A[w][k]+B[w][k];
  108.         }
  109.     }
  110.     return C;
  111. }
  112.  
  113. int** odejmij(int **A,int **B,int a,int b)
  114. {
  115.     int** C=new int*[a];
  116.     for(int i=0;i<a;i++)
  117.     {
  118.         C[i]=new int[b];
  119.     }
  120.     for(int w=0;w<a;w++)
  121.     {
  122.         for(int k=0;k<b;k++)
  123.         {
  124.             C[w][k]=A[w][k]-B[w][k];
  125.         }
  126.     }
  127.     return C;
  128. }
  129.  
  130. int** pomnoz(int **A,int **B,int a,int b, int L)
  131. {
  132.     int** C=new int*[a];
  133.     for(int i=0;i<a;i++)
  134.     {
  135.         C[i]=new int[L];
  136.     }
  137.  
  138.     for(int w=0;w<a;w++)
  139.     {
  140.         for(int k=0;k<L;k++)
  141.         {
  142.             C[w][k]=0;
  143.             for(int i=0;i<b;i++)
  144.             {
  145.                 C[w][k]+=A[w][i]*B[i][k];
  146.             }
  147.         }
  148.     }
  149.     return C;
  150. }
  151.  
  152. int wyznacznik(int **A,int a)
  153. {
  154.     if(a==2)
  155.     {
  156.         return A[0][0]*A[1][1]-A[0][1]*A[1][0];
  157.     }
  158.     else if(a==1)
  159.     {
  160.         return A[0][0];
  161.     }
  162.     else if(a==3)
  163.     {
  164.         return A[0][0]*A[1][1]*A[2][2]+A[1][0]*A[2][1]*A[0][2]+A[2][0]*A[0][1]*A[1][2]-A[0][2]*A[1][1]*A[2][0]+A[1][2]*A[2][1]*A[0][0]+A[2][2]*A[0][1]*A[1][0];
  165.     }
  166.     else
  167.     {
  168.         cout << "Podana macierz jest za duza"<< endl;
  169.     }
  170. }
  171.  
  172. char wyborDzialania()
  173. {
  174.     char x;
  175.     cout << "podaj dzialanie:  + dodawanie, - odejmowanie, * mnozenie, d wyznacznik, k koniec" << endl;
  176.     cin >> x;
  177.     return x;
  178. }
  179.  
  180. int main()
  181. {
  182.     char x='a';
  183.  
  184.     while(x!='k')
  185.     {
  186.         int a,b,c,d;
  187.         x=wyborDzialania();
  188.         int **A;
  189.         int **B;
  190.         if(x=='+' || x=='-' || x=='*')
  191.         {
  192.             cout << "podaj wymiary pierwszej macierzy" << endl;
  193.             cin >> a;
  194.             cin >> b;
  195.             cout << "podaj wymiary drugiej macierzy" << endl;
  196.             cin >> c;
  197.             cin >> d;
  198.             A=new int*[a];
  199.             for(int i=0;i<a;i++)
  200.             {
  201.                 A[i]=new int[b];
  202.             }
  203.             B=new int*[c];
  204.             for(int i=0;i<c;i++)
  205.             {
  206.                 B[i]=new int[d];
  207.             }
  208.             cout << "podaj wspolczynniki 1 macierzy (wierszami)" << endl;
  209.             for(int w=0;w<a;w++)
  210.             {
  211.                 cout << "Wiersz: " << w+1 << endl;
  212.                 for(int k=0;k<b;k++)
  213.                 {
  214.                     cin >> A[w][k];
  215.                 }
  216.             }
  217.             cout << "podaj wspolczynniki 2 macierzy (wierszami)" << endl;
  218.             for(int w=0;w<c;w++)
  219.             {
  220.                 cout << "Wiersz: " << w+1 << endl;
  221.                 for(int k=0;k<d;k++)
  222.                 {
  223.                     cin >> B[w][k];
  224.                 }
  225.             }
  226.         }
  227.         else if(x!='k')
  228.         {
  229.             cout << "podaj wymiary macierzy" << endl;
  230.             cin >> a;
  231.             cin >> b;
  232.             A=new int*[a];
  233.             for(int i=0;i<a;i++)
  234.             {
  235.                 A[i]=new int[b];
  236.             }
  237.             cout << "podaj wspolczynniki macierzy (wierszami)" << endl;
  238.             for(int w=0;w<a;w++)
  239.             {
  240.                 cout << "Wiersz: " << w+1 << endl;
  241.                 for(int k=0;k<b;k++)
  242.                 {
  243.                     cin >> A[w][k];
  244.                 }
  245.             }
  246.         }
  247.  
  248.         if(x=='p')
  249.         {
  250.             int l;
  251.             cout << "podaj liczbe" << endl;
  252.             cin >> l;
  253.         }
  254.         if(x=='+')
  255.         {
  256.             if(a==c && b==d)
  257.             {
  258.                 cout << "suma macierzy: " << endl;
  259.                 wypisz(A,a,b);
  260.                 cout << "i macierzy: " << endl;
  261.                 wypisz(B,c,d);
  262.                 cout << "wynosi: " << endl;
  263.                 wypisz(dodaj(A,B,a,b),a,b);
  264.             }
  265.             else
  266.                 cout << "wymiary sie nie zgadzaja" << endl;
  267.         }
  268.         else if(x=='-')
  269.         {
  270.             if(a==c && b==d)
  271.             {
  272.  
  273.                 cout << "roznica macierzy: " << endl;
  274.                 wypisz(A,a,b);
  275.                 cout << "i macierzy: " << endl;
  276.                 wypisz(B,c,d);
  277.                 cout << "wynosi: " << endl;
  278.                 wypisz(odejmij(A,B,a,b),a,b);
  279.             }
  280.             else
  281.                 cout << "wymiary sie nie zgadzaja" << endl;
  282.         }
  283.  
  284.         else if(x=='*')
  285.         {
  286.                if(b==c)
  287.             {
  288.  
  289.                 cout << "iloczyn macierzy: " << endl;
  290.                 wypisz(A,a,b);
  291.                 cout << "i macierzy: " << endl;
  292.                 wypisz(B,c,d);
  293.                 cout << "wynosi: " << endl;
  294.                 wypisz(pomnoz(A,B,a,b),a,b);
  295.             }
  296.             else
  297.                 cout << "wymiary sie nie zgadzaja" << endl;
  298.         }
  299.         else if(x=='d')
  300.         {
  301.             if(a==b)
  302.             {
  303.                 cout << "wyznacznik macierzy: " << endl;
  304.                 wypisz(A,a,b);
  305.                 cout << "wynosi: " << wyznacznik(A,a) << endl;
  306.             }
  307.             else
  308.                 cout << "macierz nie jest kwadratowa" << endl;
  309.  
  310.         }
  311.         else if(x=='p')
  312.         {
  313.  
  314.         }
  315.         else if(x=='k')
  316.         {
  317.             cout << "dziekuje za uwage" << endl;
  318.         }
  319.         else
  320.         {
  321.             cout << "wprowadziles zly znak" << endl;
  322.         }
  323.     }
  324.  
  325.     return 0;
  326. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement