Advertisement
wilk_maciej

Podjebane od Agaty

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