Advertisement
wilk_maciej

lista_jako_wzorzec

Apr 9th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.09 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. class List;
  11. class Element;
  12.  
  13. template<class T>
  14. class Element{
  15.   public:
  16.   T war;
  17.   class Element *next;
  18.   Element(){
  19.     war=T;
  20.     next=NULL;
  21.   }
  22. };
  23.  
  24. class List{
  25.  
  26.   friend ostream & operator << (ostream & s1, List & o1);
  27.   friend istream & operator >> (istream & s1, List & o1);
  28.  
  29.   class Element *pierwszy;
  30.   public:
  31.   List(){
  32.     pierwszy=NULL;
  33.   }
  34.  
  35.   List (class List &o1){
  36.     if (o1.pierwszy==NULL){
  37.       pierwszy=NULL;
  38.     }
  39.     else{
  40.       class Element *nowy = new Element;
  41.       nowy->war=o1.pierwszy->war;
  42.       Element *i =o1.pierwszy;
  43.       Element *poprz = nowy;
  44.       pierwszy=nowy;
  45.       i=i->next;
  46.       while (i!=NULL){
  47.         class Element *elem = new Element;
  48.         elem->war=i->war;
  49.         poprz->next=elem;
  50.         poprz=elem;
  51.         i=i->next;
  52.       }
  53.       poprz->next=NULL;
  54.     }
  55.   }
  56.  
  57.  
  58.  
  59.   ~List(){
  60.     while (pierwszy!=NULL){
  61.       if (pierwszy->next != NULL){
  62.         class Element *nowy=pierwszy->next;
  63.         delete pierwszy;
  64.         pierwszy = nowy;
  65.       }
  66.       else{
  67.         delete pierwszy;
  68.         pierwszy = NULL;
  69.       }
  70.     }
  71.   }
  72.  
  73.   List & operator = (List &o1)
  74.     {
  75.     if (this==&o1)
  76.       return *this;
  77.     else{
  78.     while (pierwszy!=NULL){
  79.       if (pierwszy->next != NULL){
  80.         class Element *nowy=pierwszy->next;
  81.         delete pierwszy;
  82.         pierwszy = nowy;
  83.       }
  84.       else{
  85.         delete pierwszy;
  86.         pierwszy = NULL;
  87.       }
  88.     }
  89.     }
  90.     if (o1.pierwszy==NULL){
  91.       return *this;
  92.     }
  93.     else{
  94.       Element *current = new Element;
  95.       Element *node;
  96.       Element *wsk = o1.pierwszy;
  97.       current->war=wsk->war;
  98.       this->pierwszy=current;
  99.       while (wsk->next!=NULL)
  100.       {
  101.         node = new Element;
  102.         current->next=node;
  103.         wsk=wsk->next;
  104.         node->war=wsk->war;
  105.         current=node;
  106.       }
  107.       return *this;
  108.     }
  109.   }
  110.  
  111.   friend List & operator + (List &o1, List &o2){
  112.     static List temp;
  113.     if (o1.pierwszy==NULL && o2.pierwszy==NULL){
  114.       return temp;
  115.     }
  116.     else if (o1.pierwszy==NULL && o2.pierwszy != NULL){
  117.       temp=o2;
  118.       return temp;
  119.     }
  120.     else if (o1.pierwszy!=NULL && o2.pierwszy==NULL){
  121.       temp=o1;
  122.       return temp;
  123.     }
  124.  
  125.     Element *current = new Element;
  126.     Element *node;
  127.     Element *i = o1.pierwszy;
  128.     Element *j = o2.pierwszy;
  129.     current->war=i->war;
  130.     temp.pierwszy=current;
  131.     while (i->next!=NULL)
  132.     {
  133.       node=new Element;
  134.       current->next=node;
  135.       i=i->next;
  136.       node->war=i->war;
  137.       current=node;
  138.     }
  139.     node=new Element;
  140.     node->war=j->war;
  141.     current->next=node;
  142.     while (j!=NULL)
  143.     {
  144.       node=new Element;
  145.       current->next=node;
  146.       node->war=j->war;
  147.       current=node;
  148.       j=j->next;
  149.     }
  150.     return temp;
  151.   }
  152.  
  153.   int operator == (List &o1){
  154.     if (this->pierwszy ==NULL && o1.pierwszy==NULL){
  155.       return true;
  156.     }
  157.     if (this->pierwszy ==NULL && o1.pierwszy!=NULL){
  158.       return false;
  159.     }
  160.  
  161.     if (o1.pierwszy!=NULL){
  162.       Element *head=o1.pierwszy;
  163.       Element *headthis=this->pierwszy;
  164.       while (head->next!=NULL)
  165.       {
  166.         if (head->war!=headthis->war)
  167.           return false;
  168.         head=head->next;
  169.         headthis=headthis->next;
  170.       }
  171.       return true;
  172.     }
  173.     else return false;
  174.   }
  175.  
  176.   bool operator != (List &o1){
  177.     if (this->pierwszy==NULL && o1.pierwszy==NULL){
  178.       return false;
  179.     }
  180.  
  181.     else if (this->pierwszy ==NULL && o1.pierwszy!=NULL){
  182.       return true;
  183.     }
  184.  
  185.     else if (this->pierwszy !=NULL && o1.pierwszy==NULL){
  186.       return true;
  187.     }
  188.     else{
  189.       bool wynik;
  190.       Element *head=o1.pierwszy;
  191.       Element *headthis=this->pierwszy;
  192.       while (head->next!=NULL && headthis->next!=NULL)
  193.       {
  194.         if (head->war!=headthis->war)
  195.           wynik=true;
  196.         else{
  197.           wynik=false;
  198.           break;
  199.         }
  200.         head=head->next;
  201.         headthis=headthis->next;
  202.       }
  203.       if (head->next==NULL && headthis ->next!=NULL){
  204.         wynik=false;
  205.       }
  206.       return wynik;
  207.     }
  208.   }
  209. };
  210.  
  211.  
  212.  
  213. ostream & operator<< (ostream & s1, class List & o1){
  214.   Element *n = o1.pierwszy;
  215.   while(n){
  216.     s1 << n->war << ", " ;
  217.     n = n->next;
  218.   }
  219.   cout << endl;
  220.   return s1;
  221. }
  222.  
  223.  
  224. istream & operator >>(istream &s1, class List &o1){
  225.   class Element *element = new Element;
  226.   cout <<"Podaj wartość elementu listy ";
  227.   s1 >> element->war;
  228.   cout <<endl;
  229.   if (o1.pierwszy==NULL){
  230.     o1.pierwszy=element;
  231.     o1.pierwszy->next=NULL;
  232.   }
  233.   else{
  234.   class Element *wsk =o1.pierwszy;
  235.   while(wsk->next != NULL){
  236.     wsk=wsk->next;
  237.   }
  238.   wsk->next = element;
  239.   element->next = NULL;
  240.   }
  241.   return s1;
  242. }
  243.  
  244.  
  245. int main()
  246. {
  247.   List o1;
  248.   List o2;
  249.   List o4;
  250.   int rozm1, rozm2;
  251.   cout<<"podaj rozmiar 1 listy: "<<endl;
  252.   cin>>rozm1;
  253.   for (int i=0; i<rozm1; i++)
  254.     cin>>o1;
  255.   cout<<"podaj rozmiar 2 listy: "<<endl;
  256.   cin>>rozm2;
  257.   for (int i=0; i<rozm2; i++)
  258.     cin>>o2;
  259.   cout<<"pierwsza lista: ";
  260.   cout<<o1;
  261.   cout<<"druga lista: ";
  262.   cout<<o2;
  263.   cout<<"lista 3 - kopia listy 1: ";
  264.   List o3 (o1);
  265.   cout<<o3;
  266.   cout<<"dodawanie listy 2 do listy 1: ";
  267.   cout<<o1+o2;
  268.   cout<<"dodawanie listy 1 do listy 2: ";
  269.   cout<<o2+o1;
  270.   if (o1==o3)
  271.     cout<<"działa 1"<<endl;
  272.   if(o1!=o2)
  273.     cout<<"działa 2"<<endl;
  274.   o4=o1+o2;
  275.   cout<<"lista 4=1+2: ";
  276.   cout<<o4;
  277.   o1.~List();
  278.  
  279.   return 0;
  280. }
  281.  
  282. //zaprzyjaźnić wzorzec klasy we wzorcu klasy sprawdzić jak to jest w necie
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement