Advertisement
Guest User

Untitled

a guest
Nov 19th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.21 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3. #include "stdafx.h"
  4.  
  5. using namespace std;
  6.  
  7. class macierz
  8. {
  9.     int size;
  10.     int *array_p;
  11.  
  12. public:
  13.  
  14.  
  15.     macierz(int a)
  16.     {
  17.         size = a;
  18.  
  19.         array_p = new int[a*a];
  20.     }
  21.  
  22.     ~macierz()
  23.     {
  24.         delete[] array_p;
  25.     }
  26.  
  27.     void fill()
  28.     {
  29.         int *temp = array_p;
  30.         int i1, i2;
  31.         system("cls");
  32.  
  33.         for (i1 = 0; i1<size; i1++)
  34.         {
  35.             for (i2 = 0; i2<size; i2++)
  36.             {
  37.                 cout << "Type in [" << i1 + 1 << ";" << i2 + 1 << "] array value:" << endl;
  38.                 cin >> *temp;
  39.                 temp++;
  40.                 system("cls");
  41.             }
  42.         }
  43.     }
  44.  
  45.     void display()
  46.     {
  47.         int *temp = array_p;
  48.         int i1, i2;
  49.  
  50.         for (i1 = 0; i1<size; i1++)
  51.         {
  52.             for (i2 = 0; i2<size; i2++)
  53.             {
  54.                 cout << *temp << "\t";
  55.                 temp++;
  56.             }
  57.             cout << endl;
  58.         }
  59.     }
  60.  
  61.     friend macierz operator+(const macierz &a, const macierz &b)
  62.     {
  63.  
  64.         if (a.size == b.size)
  65.         {
  66.             int x;
  67.             x = a.size
  68.                 array c(x);
  69.  
  70.             int i;
  71.             for (i = 0; i<x*x; i++)
  72.             {
  73.                 c.array_p[i] = a.array_p[i] + b.array_p[i];
  74.             }
  75.  
  76.             return c;
  77.         }
  78.         else
  79.         {
  80.             cout << "Error, arrays are mismatching sizes";
  81.         }
  82.  
  83.  
  84.     }
  85.  
  86.     friend macierz operator-(const macierz &a, const macierz &b)
  87.     {
  88.  
  89.         if (a.n_cols == b.n_cols)
  90.         {
  91.             int x;
  92.             x = a.size;
  93.             array c(x);
  94.  
  95.             int i;
  96.             for (i = 0; i<x*x; i++)
  97.             {
  98.                 c.array_p[i] = a.array_p[i] - b.array_p[i];
  99.             }
  100.  
  101.             return c;
  102.         }
  103.         else
  104.         {
  105.             cout << "Error, arrays are mismatching sizes";
  106.         }
  107.  
  108.     }
  109.  
  110.     //to nie jest mnozenie macierzy kwadratowych ale moze sie przydac
  111.     /*
  112.     friend macierz operator*(macierz &a,macierz &b)
  113.     {
  114.     if(a.n_cols==b.n_rows)
  115.     {
  116.     array temp(a.n_rows,b.n_cols);
  117.  
  118.     for(int i=0;i<temp.n_rows;i++)
  119.     {
  120.     for(int j=0;j<temp.n_cols;j++)
  121.     {
  122.     temp.array_p[i*temp.n_cols+j]=0;
  123.     for(int k=0;k<a.n_cols;k++)
  124.     {
  125.     temp.array_p[i*temp.n_cols+j]+=a.array_p[i*a.n_cols+k]*b.array_p[k*b.n_cols+j];
  126.     }
  127.     }
  128.     }
  129.     return temp;
  130.     }
  131.  
  132.     if(b.n_cols==a.n_rows)
  133.     {
  134.     array temp(b.n_rows,a.n_cols);
  135.  
  136.     for(int i=0;i<temp.n_rows;i++)
  137.     {
  138.     for(int j=0;j<temp.n_cols;j++)
  139.     {
  140.     temp.array_p[i*temp.n_cols+j]=0;
  141.     for(int k=0;k<a.n_cols;k++)
  142.     {
  143.     temp.array_p[i*temp.n_cols+j]+=a.array_p[i*a.n_cols+k]*b.array_p[k*b.n_cols+j];
  144.     }
  145.     }
  146.     }
  147.     return temp;
  148.     }
  149.     else
  150.     {
  151.  
  152.     }
  153.     }
  154.     */
  155.  
  156.     friend ostream& operator<<(ostream &wyjscie, const macierz &a)
  157.     {
  158.         int *temp = a.array_p;
  159.         int i1, i2;
  160.  
  161.         for (i1 = 0; i1<a.size; i1++)
  162.         {
  163.             for (i2 = 0; i2<a.size; i2++)
  164.             {
  165.                 wyjscie << *temp << "\t";
  166.                 temp++;
  167.             }
  168.             cout << endl;
  169.         }
  170.  
  171.         return wyjscie;
  172.     }
  173.  
  174.  
  175. };
  176.  
  177. int main()
  178. {
  179.     int choice;
  180.  
  181.     while (choice != 0) {
  182.         cout << "===========MENU===========\t";
  183.         cout << "==========================\t";
  184.         cout << "== 1. add arrays\t";
  185.         cout << "== 2. substract arrays\t";
  186.         cout << "== 3. multiply arrays\t";
  187.         cout << "== \t";
  188.         cout << "== 0. program go commit die\t";
  189.         cout << "==========================\t";
  190.  
  191.         cin >> choice;
  192.  
  193.         if (choice == 0) {
  194.             cout << "Me is kill, rip :(";
  195.         }
  196.         else if (choice == 1) {
  197.             int sizee;
  198.             cout << "Type array's size:\t";
  199.             cin >> sizee;
  200.             macierz a(sizee), b(sizee), c(sizee);
  201.             cout << "Fill first array:\t";
  202.             a.fill;
  203.             cout << "Fill second array:\t";
  204.             b.fill;
  205.             c = a + b;
  206.             cout << c;
  207.  
  208.         }
  209.     }
  210.  
  211.  
  212. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement