Advertisement
ewa_tabor

templ

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