Advertisement
wilk_maciej

l_j_wzorzec

Apr 16th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 12.32 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdio.h>
  3. using namespace std;
  4.  
  5. // OBIEKT LISTA                                                                                                             DONE
  6. // operator wejscia dodaje 1 element, wyjscia wypisuje wszystkie el     DONE
  7. // konstruktor, destruktor, konstruktor kopiujący                                          DONE
  8. // + do listy dopisuje na końcu drógą                                                                    DONE
  9. // =, ==, !=                                                                                                                    DONE
  10. template<typename T>
  11. class List;
  12.  
  13. template<typename T>
  14. class Element;
  15.  
  16. class Macierze{
  17.   friend ostream & operator<< (ostream & s1, Macierze & o1);
  18.   friend istream & operator>>(istream & s1, Macierze & o1);
  19.    
  20.  
  21.   double *ws;
  22.   int rozmiar;
  23.   public:
  24.    
  25.   Macierze(){
  26.     ws=NULL;
  27.   }
  28.   Macierze(int rozm, int wart){
  29.     rozmiar = rozm;
  30.     ws = new double[rozm*rozm];
  31.     for ( int i = 0; i < rozmiar*rozmiar; i++){
  32.             ws[i] = wart;
  33.     }
  34.  
  35.   }
  36.  
  37.   Macierze(const Macierze &o1){  // kopiujacy konstruktor, potrzebny przy dodawaniu
  38.     rozmiar = o1.rozmiar;
  39.     ws = new double[o1.rozmiar*o1.rozmiar];
  40.     for ( int i = 0; i < rozmiar*rozmiar; i++){
  41.             ws[i] = o1.ws[i];
  42.     }
  43.     }
  44.  
  45.   ~Macierze(){
  46.     if (ws!=NULL){
  47.       delete [] ws;
  48.       ws=NULL;
  49.     }
  50.   }
  51.  
  52.     Macierze & operator=(const Macierze & o1){
  53. //zwolnić pamięć dla lewej str
  54. //zarezerwować pamieć
  55. //przepisać pamieć
  56.     if (this == &o1) return *this;
  57.     if (this->ws!=NULL){
  58.       delete [] this->ws;
  59.     }
  60.     this->rozmiar = o1.rozmiar;
  61.  
  62.     if (o1.rozmiar>0){
  63.           this->ws = new double[o1.rozmiar*o1.rozmiar];
  64.           for (int i=0;i<(o1.rozmiar*o1.rozmiar);i++){
  65.               this->ws[i] = o1.ws[i];
  66.           }
  67.     }
  68.  
  69.     return *this;
  70.     }
  71.  
  72.  
  73.     Macierze  operator+(const Macierze &o1){
  74.         Macierze wynik= Macierze(*this);
  75.         if (this->rozmiar != o1.rozmiar){
  76.             cout <<"Nie można dodać różnych macierzy!";
  77.             return wynik;
  78.         }
  79.         else{
  80.             for (int i=0;i< o1.rozmiar*o1.rozmiar; i++){
  81.                 wynik.ws[i] += o1.ws[i];
  82.             }
  83.             return wynik;
  84.         }
  85.  
  86.     }
  87.  
  88.     Macierze  operator-(const Macierze &o1){
  89.         Macierze wynik= Macierze(*this);
  90.         if (this->rozmiar != o1.rozmiar){
  91.             cout <<"nie można wykonać" <<endl;
  92.             return wynik;
  93.         }
  94.         else{
  95.             for (int i=0;i< o1.rozmiar*o1.rozmiar; i++){
  96.                 wynik.ws[i] -= o1.ws[i];
  97.             }
  98.         return wynik;
  99.  
  100.         }
  101.     }
  102.  
  103.     Macierze  operator*(const Macierze &o1){
  104. //działa tylko dla pierwszego wiersza
  105.         Macierze wynik= Macierze(o1.rozmiar, 0);
  106.         cout <<wynik <<endl;
  107.         if (this->rozmiar != o1.rozmiar){
  108.             cout <<"nie można wykonać" <<endl;
  109.             return wynik;
  110.         }
  111.         else{
  112.  
  113.  
  114.  
  115.             int n=o1.rozmiar;
  116.             for (int i=0; i<n*n;i+=n){
  117.                 for (int j=0; j<n;j++){
  118.                     for (int k=0; k<n;k++){
  119.                         cout<<i <<"=i  " <<i+j << "=i+j  XX ";
  120.                         wynik.ws[i+j]+=this->ws[k+i]*o1.ws[k*n+j];
  121.                     }
  122.                 cout <<endl;
  123.                 }
  124.             }
  125.            
  126.            
  127.         return wynik;
  128.  
  129.         }
  130.     }
  131.  
  132.  
  133.     int operator==(const Macierze &o1){
  134.         if (this->rozmiar != o1.rozmiar){
  135.             cout <<"Macierze różnych rozmiarów!" <<endl;
  136.             return 0;
  137.         }
  138.         else{
  139.             int flaga=0;
  140.             for(int i=0;i< o1.rozmiar*o1.rozmiar; i++){
  141.                 if(this->ws[i] == o1.ws[i]) flaga+=1;
  142.             }
  143.  
  144.             if(flaga==0) return 1;
  145.             else{
  146.                 return 0;
  147.             }
  148.         }
  149.     }
  150.  
  151.     int operator>=(const Macierze &o1){
  152. //operatory >= <= działają na zasadzie: zlicza sumę wszystkich elementów jednego i drugiego, a później porównuje te liczby
  153.         if (this->rozmiar != o1.rozmiar){
  154.             cout <<"Macierze różnych rozmiarów! Nie porównam" <<endl;
  155.             return 0;
  156.         }
  157.         else{
  158.             int suma1=0;
  159.             int suma2=0;
  160.             for(int i=0;i< this->rozmiar*this->rozmiar; i++){
  161.                 suma1+=this->ws[i];
  162.             }
  163.  
  164.             for(int i=0;i< o1.rozmiar*o1.rozmiar; i++){
  165.                 suma2+=o1.ws[i];
  166.             }
  167.  
  168.             if(suma1>=suma2) return 1;
  169.             else{
  170.                 return 0;
  171.             }
  172.         }
  173.     }
  174.  
  175.     int operator<=(const Macierze &o1){
  176.         if (this->rozmiar != o1.rozmiar){
  177.             cout <<"Macierze różnych rozmiarów! Nie porównam" <<endl;
  178.             return 0;
  179.         }
  180.         else{
  181.             int suma1=0;
  182.             int suma2=0;
  183.             for(int i=0;i< this->rozmiar*this->rozmiar; i++){
  184.                 suma1+=this->ws[i];
  185.             }
  186.  
  187.             for(int i=0;i< o1.rozmiar*o1.rozmiar; i++){
  188.                 suma2+=o1.ws[i];
  189.             }
  190.  
  191.             if(suma1<=suma2) return 1;
  192.             else{
  193.                 return 0;
  194.             }
  195.         }
  196.     }
  197.    
  198.     int operator!=(const Macierze &o1){
  199.         if (this->rozmiar != o1.rozmiar){
  200.             cout <<"Macierze różnych rozmiarów! Nie porównam" <<endl;
  201.             return 0;
  202.         }
  203.         else{
  204.             int roznica=0;
  205.             for(int i=0;i< this->rozmiar*this->rozmiar; i++){
  206.                 if (this->ws[i] != o1.ws[i])roznica+=1;
  207.             }
  208.  
  209.  
  210.             if(roznica!=0) return 1;
  211.             else{
  212.                 return 0;
  213.             }
  214.         }
  215.     }
  216.  
  217. //koniec klasy macierze
  218. };
  219.  
  220. ostream & operator<< (ostream & s1, Macierze & o1){
  221.   if (o1.ws!=NULL){
  222.       for(int i=0, j=1;i<(o1.rozmiar*o1.rozmiar);i++, j++){
  223.           cout << o1.ws[i] <<" " ;
  224.           if (j==o1.rozmiar){
  225.               s1<<endl;
  226.               j=0;
  227.           }
  228.       }
  229.   }
  230.   return s1;
  231. }
  232.  
  233. istream & operator>>(istream & s1, Macierze & o1){
  234.   s1>>o1.rozmiar;
  235.  
  236.   if (o1.rozmiar>0){
  237.       if (o1.ws!=NULL){
  238.           delete [] o1.ws;
  239.       }
  240.       o1.ws = new double[o1.rozmiar*o1.rozmiar];
  241.       for (int i=0;i<(o1.rozmiar*o1.rozmiar);i++){
  242.           cin >> o1.ws[i];
  243.       }
  244.   }
  245.   return s1;
  246. }
  247.  
  248.  
  249.  
  250. template<class T>
  251. class Element{
  252.   friend class List<T>;
  253.   template<typename S>
  254.     friend ostream & operator << (ostream & s1, List<S> & o1);
  255.   template<typename S>
  256.     friend istream & operator >> (istream & s1, List<S> & o1);
  257.   public:
  258.   T war;
  259.   class Element<T> *next;
  260. };
  261.  
  262.  
  263. template<class T>
  264. class List{
  265.  
  266.   template<typename S>
  267.     friend ostream & operator << (ostream & s1, List<S> & o1);
  268.   template<typename S>
  269.     friend istream & operator >> (istream & s1, List<S> & o1);
  270.  
  271.  
  272.   class Element<T> *pierwszy;
  273.   public:
  274.   List<T>(){
  275.     pierwszy=NULL;
  276.   }
  277.  
  278.   List<T> (const class List<T> &o1){
  279.     if (o1.pierwszy==NULL){
  280.       pierwszy=NULL;
  281.       return;
  282.     }
  283.     class Element<T> *nowy = new Element<T>;
  284.     nowy->war=o1.pierwszy->war;
  285.     Element<T> *i =o1.pierwszy;
  286.     Element<T> *poprz = nowy;
  287.     pierwszy=nowy;
  288.     i=i->next;
  289.     while (i!=NULL){
  290.       class Element<T> *elem = new Element<T>;
  291.       elem->war=i->war;
  292.       poprz->next=elem;
  293.       poprz=elem;
  294.       i=i->next;
  295.     }
  296.     poprz->next=NULL;
  297.   }
  298.  
  299.  
  300.   ~List<T>(){
  301.     while (pierwszy!=NULL){
  302.       class Element<T> *toDelete;
  303.       toDelete = pierwszy;
  304.       pierwszy = pierwszy->next;
  305.       delete toDelete;
  306.     }
  307.     pierwszy=NULL;
  308.   }
  309.  
  310.   List<T> & operator = (const class List<T> &o1){
  311.     if (this==&o1){
  312.       return *this;
  313.     }
  314.     if (o1.pierwszy==NULL && this->pierwszy == NULL){
  315.       return *this;
  316.     }
  317.     if(o1.pierwszy==NULL){
  318.       this->~List();
  319.       return *this;
  320.     }
  321.     if(this->pierwszy != NULL) this->~List();
  322.     Element<T> *n_element = new Element<T>;
  323.     n_element->war = o1.pierwszy->war;
  324.     Element<T> *i = o1.pierwszy;
  325.     Element<T> *prev = n_element;
  326.     this->pierwszy = n_element;
  327.     i = i->next;
  328.     while(i != NULL){
  329.         Element<T> *_element = new Element<T>;
  330.         _element->war = i->war;
  331.         prev->next = _element;
  332.         prev = _element;
  333.         i = i->next;
  334.     }
  335.     prev->next = NULL;
  336.     return *this;
  337.   }
  338.  
  339.   List<T> operator+(const class List<T> &o1){
  340.     if(this->pierwszy == NULL && o1.pierwszy == NULL) return *this;
  341.     if(this->pierwszy != NULL && o1.pierwszy == NULL){
  342.  
  343.         List<T> new_list;
  344.     if (this->pierwszy==NULL){
  345.       new_list.pierwszy=NULL;
  346.     }
  347.     else{
  348.       class Element<T> *nowy = new Element<T>;
  349.       nowy->war=this->pierwszy->war;
  350.       Element<T> *i =this->pierwszy;
  351.       Element<T> *poprz = nowy;
  352.       new_list.pierwszy=nowy;
  353.       i=i->next;
  354.       while (i!=NULL){
  355.         class Element<T> *elem = new Element<T>;
  356.         elem->war=i->war;
  357.         poprz->next=elem;
  358.         poprz=elem;
  359.         i=i->next;
  360.       }
  361.           poprz->next=NULL;
  362.         }
  363.         return new_list;
  364.     }
  365.     if(this->pierwszy == NULL && o1.pierwszy != NULL){
  366.         List<T> new_list(o1);
  367.         return new_list;
  368.     }
  369.     else {
  370.         List<T> new_list;
  371.     if (this->pierwszy==NULL){
  372.       new_list.pierwszy=NULL;
  373.     }
  374.     else{
  375.       class Element<T> *nowy = new Element<T>;
  376.       nowy->war=this->pierwszy->war;
  377.       Element<T> *i =this->pierwszy;
  378.       Element<T> *poprz = nowy;
  379.       new_list.pierwszy=nowy;
  380.       i=i->next;
  381.       while (i!=NULL){
  382.         class Element<T> *elem = new Element<T>;
  383.         elem->war=i->war;
  384.         poprz->next=elem;
  385.         poprz=elem;
  386.         i=i->next;
  387.       }
  388.           poprz->next=NULL;
  389.         }        
  390.         Element<T> *i = new_list.pierwszy;
  391.         while (i->next) i = i->next;
  392.         Element<T> *j = o1.pierwszy;
  393.         Element<T> *prev = new Element<T>;
  394.         prev->war = o1.pierwszy->war;
  395.         i->next = prev;
  396.         j = j->next;
  397.         while (j) {
  398.             Element<T> *new_el = new Element<T>;
  399.             new_el->war = j->war;
  400.             prev->next = new_el;
  401.             prev = new_el;
  402.             j = j->next;
  403.         }
  404.         prev->next = NULL;
  405.         return new_list;
  406.     }
  407.   }
  408.  
  409.  
  410.   bool operator == (List<T> &o1){
  411.     if (this->pierwszy ==NULL && o1.pierwszy==NULL){
  412.       return true;
  413.     }
  414.     if (this->pierwszy ==NULL && o1.pierwszy!=NULL){
  415.       return false;
  416.     }
  417.     if (this->pierwszy !=NULL && o1.pierwszy==NULL){
  418.       return false;
  419.     }
  420.  
  421.     if (o1.pierwszy!=NULL){
  422.       Element<T> *head=o1.pierwszy;
  423.       Element<T> *headthis=this->pierwszy;
  424.       while (head->next!=NULL){
  425.         if (head->war!=headthis->war)
  426.           return false;
  427.         head=head->next;
  428.         headthis=headthis->next;
  429.       }
  430.       return true;
  431.     }
  432.     return false;
  433.   }
  434.  
  435.   bool operator != (List<T> &o1){
  436.     if (this->pierwszy==NULL && o1.pierwszy==NULL){
  437.       return false;
  438.     }
  439.  
  440.     else if (this->pierwszy ==NULL && o1.pierwszy!=NULL){
  441.       return true;
  442.     }
  443.  
  444.     else if (this->pierwszy !=NULL && o1.pierwszy==NULL){
  445.       return true;
  446.     }
  447.     else{
  448.       bool wynik;
  449.       Element<T> *head=o1.pierwszy;
  450.       Element<T> *headthis=this->pierwszy;
  451.       while (head->next!=NULL && headthis->next!=NULL){
  452.         if (head->war!=headthis->war)
  453.           wynik=true;
  454.         else{
  455.           wynik=false;
  456.           break;
  457.         }
  458.         head=head->next;
  459.         headthis=headthis->next;
  460.       }
  461.       if (head->next==NULL && headthis ->next!=NULL){
  462.         wynik=false;
  463.       }
  464.       return wynik;
  465.     }
  466.   }
  467. };
  468.  
  469.  
  470. template<typename S>
  471.   ostream & operator<< (ostream &s1, class List<S> &o1){
  472.   Element<S> *n = o1.pierwszy;
  473.   while(n){
  474.     s1 << n->war << " " ;
  475.     n = n->next;
  476.   }
  477.   return s1;
  478. }
  479.  
  480.  
  481. template<typename S>
  482. istream & operator >>(istream &s1, class List<S> &o1){
  483.  
  484.   class Element<S> *element = new Element<S>;
  485.   s1 >> element->war;
  486.   if (o1.pierwszy==NULL){
  487.     o1.pierwszy=element;
  488.     o1.pierwszy->next=NULL;
  489.   }
  490.   else{
  491.     class Element<S> *wsk =o1.pierwszy;
  492.     while(wsk->next != NULL){
  493.       wsk=wsk->next;
  494.     }
  495.     wsk->next = element;
  496.     element->next = NULL;
  497.   }
  498.   return s1;
  499. }
  500.  
  501.  
  502. int main(){
  503.   List<Macierze> z1, z2, z3;
  504.   cin >> z1;
  505.   cin >> z1;
  506.   cout << "Lista macierzy 1: " <<endl << z1;
  507.   cin >> z2;
  508.   z3 = z2+z1;
  509.   z3 = z2+z1;
  510.   cout <<"Suma list: "<< endl << z3;
  511.   return 0;
  512.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement