Advertisement
ewa_tabor

Wzorzec mam nadzieje bez bledow

May 7th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.07 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdio.h>
  3. using namespace std;
  4.  
  5.   template <class T>
  6.     class para {
  7.       public:
  8.       T x,y;
  9.  
  10.       para<T>(){
  11.     T x,y;
  12.       }
  13.  
  14.       para<T>(T x1, T y1){
  15.         para<T> o1;
  16.         o1.x=x1;
  17.         o1.y=y1;
  18.       }
  19.  
  20.  
  21.       para<T> &operator = (para<T> &o1){
  22.         x=o1.x;
  23.         y=o1.y;
  24.         return *this;
  25.       }
  26.  
  27.       para<T> &operator + (para<T> &o1){
  28.         static para<T> wynik;
  29.         wynik.x=this->x+o1.x;
  30.         wynik.y=this->y+o1.y;
  31.  
  32.     return wynik;
  33.       }
  34. };
  35.  
  36. template <class T>
  37.   istream &operator >>(istream &s, para<T> &o1){
  38.     s>>o1.x>>o1.y;
  39.     return s;
  40.   }
  41. template <class T>
  42.   ostream  &operator << (ostream &s, para<T> &o1){
  43.     s<<o1.x<<endl<<o1.y<<endl;
  44.     return s;
  45.   }
  46.  
  47. template <class T>
  48.     class LISTA;
  49. template<class T>
  50.     class EL {
  51.         template <class>
  52.         friend class LISTA;
  53.             template <class U>
  54.         friend ostream & operator<< (ostream &s1, LISTA<U> & o1);
  55.         template <class U>
  56.         friend istream & operator>> (istream &s1, LISTA<U> &o1);
  57.         T wartosc;
  58.         class EL<T> *next;
  59.     };
  60. template<class T>
  61.     class LISTA {
  62.         class EL<T> *head;
  63.         template <class U>
  64.             friend ostream & operator<< (ostream &s1, LISTA<U> & o1);
  65.         template <class U>
  66.         friend istream & operator>> (istream &s1, LISTA<U> &o1);
  67.         public:
  68.         LISTA<T>(){
  69.             head=NULL;
  70.         }
  71.         LISTA<T>(class LISTA &o1){
  72.             if(o1.head==NULL){
  73.                 head=NULL;
  74.                 return;
  75.             }
  76.             auto *nowy = new EL<T>;
  77.             nowy->wartosc=o1.head->wartosc;
  78.             EL<T> *temp=o1.head;
  79.             EL<T> *prev=nowy;
  80.             head=nowy;
  81.             temp=temp->next;
  82.             while (temp!=NULL){
  83.                 EL<T> *elemencik=new EL<T>;
  84.                 elemencik->wartosc=temp->wartosc;
  85.                 prev->next=elemencik;
  86.                 prev=elemencik;
  87.                 temp=temp->next;
  88.             }
  89.         }
  90.         ~LISTA<T>(){
  91.             while(head!=NULL){
  92.                 class EL<T> *toDel;
  93.                 toDel=head;
  94.                 head=head->next;
  95.                 delete toDel;
  96.             }
  97.         }
  98.  
  99.        LISTA<T> operator =(const class LISTA<T> &o1){
  100.             if (this == &o1) return *this;
  101.             while (this->head!=NULL){
  102.                class EL<T> *toDel;
  103.                 toDel=this->head;
  104.                 this->head=this->head->next;
  105.                 delete toDel;
  106.             }
  107.         if (o1.head==NULL){
  108.                 this->head=NULL;
  109.                 return *this;
  110.         }
  111.         auto *nowy = new EL<T>;
  112.         nowy->wartosc=o1.head->wartosc;
  113.         EL<T> *temp=o1.head;
  114.         EL<T> *prev=nowy;
  115.         head=nowy;
  116.         temp=temp->next;
  117.         while(temp!=NULL){
  118.             auto *elemencik=new EL<T>;
  119.             elemencik->wartosc=temp->wartosc;
  120.             prev->next=elemencik;
  121.             prev=elemencik;
  122.             temp=temp->next;
  123.         }
  124.         prev->next=NULL;
  125.         return *this;
  126.         }
  127.  
  128.         LISTA<T> & operator + (LISTA<T> &o1){
  129.           static LISTA<T> temp;
  130.           temp.~LISTA();
  131.           if (this->head==NULL && o1.head==NULL) return temp;
  132.           EL<T> *current = new EL<T>;
  133.           EL<T> *node;
  134.           EL<T> *i = this->head;
  135.           EL<T> *j = o1.head;
  136.           current->wartosc=i->wartosc;
  137.           temp.head=current;
  138.           while (i->next!=NULL){
  139.             node=new EL<T>;
  140.             current->next=node;
  141.             i=i->next;
  142.             node->wartosc=i->wartosc;
  143.             current=node;
  144.           }
  145.           node=new EL<T>;
  146.           node->wartosc=j->wartosc;
  147.           current->next=node;
  148.           while (j!=NULL){
  149.             node=new EL<T>;
  150.             current->next=node;
  151.             node->wartosc=j->wartosc;
  152.             current=node;
  153.             j=j->next;
  154.           }
  155.           current->next=NULL;
  156.           return temp;
  157.         }
  158.  
  159.         bool operator == (const class LISTA<T> &o1){
  160.         EL<T> *temp1=this->head;
  161.         EL<T> *temp2=o1.head;
  162.         int flaga=0;
  163.         while(true){
  164.             if (temp1==NULL) flaga+=1;
  165.             if (temp2==NULL) flaga+=1;
  166.             if (flaga==1) return false;
  167.             else if (flaga==2) return true;
  168.             if (temp1->wartosc!=temp2->wartosc) return false;
  169.             temp1=temp1->next;
  170.             temp2=temp2->next;
  171.         }
  172.         }
  173.         bool operator != (const class LISTA<T> &o1){
  174.             EL<T> *temp1=this->head;
  175.             EL<T> *temp2=o1.head;
  176.             int flaga=0;
  177.             while (true){
  178.                 if (temp1==NULL) flaga+=1;
  179.                 if (temp2==NULL) flaga+=1;
  180.                 if (flaga==1) return true;
  181.                 if (flaga==2) return false;
  182.                 if(temp1->wartosc==temp2->wartosc) return false;
  183.                 temp1=temp1->next;
  184.                 temp2=temp2->next;
  185.             }
  186.         }
  187.  
  188.     };
  189. template<class T>
  190.     ostream &operator<< (ostream &s1, LISTA<T> &o1){
  191.         EL<T> *temp=o1.head;
  192.         while(temp!=NULL){
  193.             s1 << temp->wartosc<<" ";
  194.             temp=temp->next;
  195.         }
  196.         return s1;
  197.     }
  198.  
  199. template<class T>
  200.     istream &operator>> (istream &s1, LISTA<T> &o1){
  201.         auto *element = new EL<T>;
  202.         s1 >> element->wartosc;
  203.         if(o1.head==NULL) {
  204.             o1.head = element;
  205.             o1.head->next=NULL;
  206.             return s1;
  207.         }
  208.         EL<T> *n=o1.head;
  209.         while(n->next!=NULL){
  210.             n = n->next;
  211.         }
  212.         n->next = element;
  213.         element->next = NULL;
  214.         return s1;
  215.     }
  216.  
  217.  
  218.  
  219.  
  220. class MACIERZ{
  221.   friend ostream & operator<< (ostream & s1, MACIERZ & o1);
  222.   friend istream & operator>>(istream & s1, MACIERZ & o1);
  223.  
  224.  
  225.   double *ws;
  226.   int rozm;
  227.   public:
  228.  
  229.   MACIERZ(){
  230.     ws=NULL;
  231.   }
  232.   MACIERZ(int rozm, int wart){
  233.     rozm = rozm;
  234.     ws = new double[rozm*rozm];
  235.     for ( int i = 0; i < rozm*rozm; i++){
  236.             ws[i] = wart;
  237.     }
  238.  
  239.   }
  240.  
  241.   MACIERZ(const MACIERZ &o1){
  242.     rozm = o1.rozm;
  243.     ws = new double[o1.rozm*o1.rozm];
  244.     for ( int i = 0; i < rozm*rozm; i++){
  245.             ws[i] = o1.ws[i];
  246.     }
  247.   }
  248.  
  249.   ~MACIERZ(){
  250.     if (ws!=NULL){
  251.       delete [] ws;
  252.       ws=NULL;
  253.     }
  254.   }
  255.  
  256.     MACIERZ & operator=(const MACIERZ & o1){
  257.     if (this == &o1) return *this;
  258.  
  259.     this->rozm = o1.rozm;
  260.     if (this->ws!=NULL){
  261.               delete [] this->ws;
  262.           }
  263.     if (o1.rozm>0){
  264.  
  265.           this->ws = new double[o1.rozm*o1.rozm];
  266.           for (int i=0;i<(o1.rozm*o1.rozm);i++){
  267.               this->ws[i] = o1.ws[i];
  268.           }
  269.     }
  270.  
  271.     return *this;
  272.     }
  273.  
  274.  
  275.     MACIERZ  operator+ (const MACIERZ &o1){
  276.         MACIERZ wynik(*this);
  277.         if (this->rozm != o1.rozm){
  278.             cout <<"Nie można dodać macierzy o różnych wymiarach!";
  279.             return wynik;
  280.         }
  281.         else{
  282.             for (int i=0;i< o1.rozm*o1.rozm; i++){
  283.                 wynik.ws[i] += o1.ws[i];
  284.             }
  285.             return wynik;
  286.         }
  287.  
  288.     }
  289.  
  290.     MACIERZ  operator- (const MACIERZ &o1){
  291.         MACIERZ wynik= MACIERZ(*this);
  292.         if (this->rozm != o1.rozm){
  293.             cout <<"Nie można wykonać" <<endl;
  294.             return wynik;
  295.         }
  296.         else{
  297.             for (int i=0;i< o1.rozm*o1.rozm; i++){
  298.                 wynik.ws[i] -= o1.ws[i];
  299.             }
  300.         return wynik;
  301.  
  302.         }
  303.     }
  304.  
  305.     MACIERZ  operator*(const MACIERZ &o1){
  306.         MACIERZ wynik= MACIERZ(*this);
  307.     for (int k=0; k<o1.rozm*o1.rozm;k++){
  308.         wynik.ws[k]=0;
  309.     }
  310.         if (this->rozm != o1.rozm){
  311.             cout <<"Nie można mnozyc macierzy roznych rozmiarow!" <<endl;
  312.             return wynik;
  313.         }
  314.         else{
  315.             for (int i=0; i<o1.rozm*o1.rozm;i+=o1.rozm){
  316.                 for (int j=0; j<o1.rozm;j++){
  317.                     for (int k=0; k<o1.rozm;k++){
  318.                         wynik.ws[i+j]+=this->ws[k+i]*o1.ws[k*o1.rozm+j];
  319.                     }
  320.                 }
  321.             }
  322.  
  323.  
  324.         return wynik;
  325.  
  326.         }
  327.     }
  328.  
  329.  
  330.     int operator==(const MACIERZ &o1){
  331.         if (this->rozm != o1.rozm){
  332.             cout <<"Macierze różnych rozmów!" <<endl;
  333.             return 0;
  334.         }
  335.         else{
  336.             int flaga=0;
  337.             for(int i=0;i< o1.rozm*o1.rozm; i++){
  338.                 if(this->ws[i] != o1.ws[i]) flaga+=1;
  339.             }
  340.  
  341.             if(flaga==0) return 1;
  342.             else{
  343.                 return 0;
  344.             }
  345.         }
  346.     }
  347.  
  348.     int operator>=(const MACIERZ &o1){
  349. //operatory >= i <= porownuja sume elemntow macierzy
  350.         if (this->rozm != o1.rozm){
  351.             cout <<"Macierze różnych rozmiarów!" <<endl;
  352.             return 0;
  353.         }
  354.         else{
  355.             int s1=0;
  356.             int s2=0;
  357.             for(int i=0;i< this->rozm*this->rozm; i++){
  358.                 s1+=this->ws[i];
  359.             }
  360.  
  361.             for(int i=0;i< o1.rozm*o1.rozm; i++){
  362.                 s2+=o1.ws[i];
  363.             }
  364.  
  365.             if(s1>=s2) return 1;
  366.             else{
  367.                 return 0;
  368.             }
  369.         }
  370.     }
  371.  
  372.     int operator<=(const MACIERZ &o1){
  373.         if (this->rozm != o1.rozm){
  374.             cout <<"Macierze sa różnych rozmiarów!" <<endl;
  375.             return 0;
  376.         }
  377.         else{
  378.             int s1=0;
  379.             int s2=0;
  380.             for(int i=0;i< this->rozm*this->rozm; i++){
  381.                 s1+=this->ws[i];
  382.             }
  383.  
  384.             for(int i=0;i< o1.rozm*o1.rozm; i++){
  385.                 s2+=o1.ws[i];
  386.             }
  387.  
  388.             if(s1<=s2) return 1;
  389.             else{
  390.                 return 0;
  391.             }
  392.         }
  393.     }
  394.  
  395.     int operator!=(const MACIERZ &o1){
  396.         if (this->rozm != o1.rozm){
  397.             cout <<"Macierze sa różnych rozmiarów!" <<endl;
  398.             return 0;
  399.         }
  400.         else{
  401.             int flaga=0;
  402.             for(int i=0;i< this->rozm*this->rozm; i++){
  403.                 if (this->ws[i] != o1.ws[i])flaga+=1;
  404.             }
  405.  
  406.  
  407.             if(flaga!=0) return 1;
  408.             else{
  409.                 return 0;
  410.             }
  411.         }
  412.     }
  413.  
  414. //koniec klasy MACIERZ
  415. };
  416.  
  417. ostream & operator<< (ostream & s1, MACIERZ & o1){
  418.   if (o1.ws!=NULL){
  419.       for(int i=0, j=1;i<(o1.rozm*o1.rozm);i++, j++){
  420.           cout << o1.ws[i] <<" " ;
  421.           if (j==o1.rozm){
  422.               s1<<endl;
  423.               j=0;
  424.           }
  425.       }
  426.   }
  427.   return s1;
  428. }
  429.  
  430. istream & operator>>(istream & s1, MACIERZ & o1){
  431.   s1>>o1.rozm;
  432.   if (o1.ws!=NULL){
  433.           delete [] o1.ws;
  434.       o1.ws=NULL;
  435.       }
  436.   if (o1.rozm>0){
  437.       o1.ws = new double[o1.rozm*o1.rozm];
  438.       for (int i=0;i<(o1.rozm*o1.rozm);i++){
  439.           cin >> o1.ws[i];
  440.       }
  441.   }
  442.   return s1;
  443. }
  444.  
  445.  
  446.  
  447.  
  448. int main(){
  449.     LISTA<MACIERZ> a,b,c,d,e,f;
  450.     cin>>a>>b;
  451.     c=a+b;
  452.     d=e+f;
  453.     cout<<d;
  454.     return 0;
  455.     cout<<"Wpisz trzy elementy (macierze) L1: ";
  456.     cin>>a>>a>>a;
  457.     cout<<"Lista: "<<a;
  458.     /*cout<<"Wpisz dwa elementy L2 (main): ";
  459.     cin>>b>>b;
  460.     c=a+b;
  461.     cout<<c;*/
  462.  
  463.  
  464.     return 0;
  465. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement