Advertisement
ewa_tabor

Do poprawki

Apr 23rd, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.90 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){
  74.             head=NULL;
  75.             return;
  76.         }
  77.         auto *nowy = new EL<T>;
  78.         nowy->wartosc=o1.head->wartosc;
  79.         EL<T> *temp=o1.head;
  80.         EL<T> *prev=nowy;
  81.         head=nowy;
  82.         temp=temp->next;
  83.         while (temp!=NULL){
  84.             EL<T> *elemencik=new EL<T>;
  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.        //Popraw, wyciek pamieci, wyczysc this, i pierwszy warunek jest glupi
  100.         LISTA<T> operator =(const class LISTA<T> &o1){
  101.         if (o1.head==NULL) return *this;
  102.         auto *nowy = new EL<T>;
  103.         nowy->wartosc=o1.head->wartosc;
  104.         EL<T> *temp=o1.head;
  105.         EL<T> *prev=nowy;
  106.         head=nowy;
  107.         temp=temp->next;
  108.         while(temp!=NULL){
  109.             auto *elemencik=new EL<T>;
  110.             elemencik->wartosc=temp->wartosc;
  111.             prev->next=elemencik;
  112.             prev=elemencik;
  113.             temp=temp->next;
  114.         }
  115.         prev->next=NULL;
  116.         return *this;
  117.         }
  118.         LISTA<T> & operator + (LISTA<T> &o1){
  119.           static LISTA<T> temp;
  120.           if (this->head==NULL && o1.head==NULL)
  121.         return temp;
  122.           EL<T> *current = new EL<T>;
  123.           EL<T> *node;
  124.           EL<T> *i = this->head;
  125.           EL<T> *j = o1.head;
  126.           current->wartosc=i->wartosc;
  127.           temp.head=current;
  128.           while (i->next!=NULL){
  129.           node=new EL<T>;
  130.           current->next=node;
  131.           i=i->next;
  132.           node->wartosc=i->wartosc;
  133.           current=node;
  134.           }
  135.           node=new EL<T>;
  136.           node->wartosc=j->wartosc;
  137.           current->next=node;
  138.           while (j!=NULL){
  139.           node=new EL<T>;
  140.           current->next=node;
  141.           node->wartosc=j->wartosc;
  142.           current=node;
  143.           j=j->next;
  144.           }
  145.           return temp;
  146.         }
  147.        
  148.         bool operator == (const class LISTA<T> &o1){
  149.         EL<T> *temp1=this->head;
  150.         EL<T> *temp2=o1.head;
  151.         int flaga=0;
  152.         while(true){
  153.             if (temp1==NULL) flaga+=1;
  154.             if (temp2==NULL) flaga+=1;
  155.             if (flaga==1) return false;
  156.             else if (flaga==2) return true;
  157.             if (temp1->wartosc!=temp2->wartosc) return false;
  158.             temp1=temp1->next;
  159.             temp2=temp2->next;
  160.         }
  161.         }
  162.         bool operator != (const class LISTA<T> &o1){
  163.         EL<T> *temp1=this->head;
  164.         EL<T> *temp2=o1.head;
  165.         int flaga=0;
  166.         while (true){
  167.             if (temp1==NULL) flaga+=1;
  168.             if (temp2==NULL) flaga+=1;
  169.             if (flaga==1) return true;
  170.             if (flaga==2) return false;
  171.             if(temp1->wartosc==temp2->wartosc) return false;
  172.         }
  173.         }
  174.      
  175.     };
  176. template<class T>
  177.     ostream &operator<< (ostream &s1, LISTA<T> &o1){
  178.         EL<T> *temp=o1.head;
  179.         while(temp!=NULL){
  180.         s1 << temp->wartosc<<" ";
  181.         temp=temp->next;
  182.         }
  183.         return s1;
  184.     }
  185.        
  186. template<class T>
  187.     istream &operator>> (istream &s1, LISTA<T> &o1){
  188.         auto *element = new EL<T>;
  189.         s1 >> element->wartosc;
  190.         if(o1.head==NULL) {
  191.         o1.head = element;
  192.         o1.head->next=NULL;
  193.         return s1;
  194.         }
  195.         EL<T> *n=o1.head;
  196.         while(n->next!=NULL){
  197.         n = n->next;
  198.         }
  199.         n->next = element;
  200.         element->next = NULL;
  201.         return s1;
  202.     }
  203.  
  204.  
  205.  
  206.  
  207. class MACIERZ{
  208.   friend ostream & operator<< (ostream & s1, MACIERZ & o1);
  209.   friend istream & operator>>(istream & s1, MACIERZ & o1);
  210.  
  211.  
  212.   double *ws;
  213.   int rozm;
  214.   public:
  215.  
  216.   MACIERZ(){
  217.     ws=NULL;
  218.   }
  219.   MACIERZ(int rozm, int wart){
  220.     rozm = rozm;
  221.     ws = new double[rozm*rozm];
  222.     for ( int i = 0; i < rozm*rozm; i++){
  223.             ws[i] = wart;
  224.     }
  225.  
  226.   }
  227.  
  228.   MACIERZ(const MACIERZ &o1){
  229.     rozm = o1.rozm;
  230.     ws = new double[o1.rozm*o1.rozm];
  231.     for ( int i = 0; i < rozm*rozm; i++){
  232.             ws[i] = o1.ws[i];
  233.     }
  234.   }
  235.  
  236.   ~MACIERZ(){
  237.     if (ws!=NULL){
  238.       delete [] ws;
  239.       ws=NULL;
  240.     }
  241.   }
  242.  
  243.     MACIERZ & operator=(const MACIERZ & o1){
  244.     if (this == &o1) return *this;
  245.  
  246.     this->rozm = o1.rozm;
  247.     if (this->ws!=NULL){
  248.               delete [] this->ws;
  249.           }
  250.     if (o1.rozm>0){
  251.          
  252.           this->ws = new double[o1.rozm*o1.rozm];
  253.           for (int i=0;i<(o1.rozm*o1.rozm);i++){
  254.               this->ws[i] = o1.ws[i];
  255.           }
  256.     }
  257.  
  258.     return *this;
  259.     }
  260.  
  261.  
  262.     MACIERZ  operator+ (const MACIERZ &o1){
  263.         MACIERZ wynik(*this);
  264.         if (this->rozm != o1.rozm){
  265.             cout <<"Nie można dodać macierzy o różnych wymiarach!";
  266.             return wynik;
  267.         }
  268.         else{
  269.             for (int i=0;i< o1.rozm*o1.rozm; i++){
  270.                 wynik.ws[i] += o1.ws[i];
  271.             }
  272.             return wynik;
  273.         }
  274.  
  275.     }
  276.  
  277.     MACIERZ  operator- (const MACIERZ &o1){
  278.         MACIERZ wynik= MACIERZ(*this);
  279.         if (this->rozm != o1.rozm){
  280.             cout <<"Nie można wykonać" <<endl;
  281.             return wynik;
  282.         }
  283.         else{
  284.             for (int i=0;i< o1.rozm*o1.rozm; i++){
  285.                 wynik.ws[i] -= o1.ws[i];
  286.             }
  287.         return wynik;
  288.  
  289.         }
  290.     }
  291.  
  292.     MACIERZ  operator*(const MACIERZ &o1){
  293.         MACIERZ wynik= MACIERZ(*this);
  294.     for (int k=0; k<o1.rozm*o1.rozm;k++){
  295.         wynik.ws[k]=0;
  296.     }
  297.         if (this->rozm != o1.rozm){
  298.             cout <<"Nie można mnozyc macierzy roznych rozmiarow!" <<endl;
  299.             return wynik;
  300.         }
  301.         else{
  302.             for (int i=0; i<o1.rozm*o1.rozm;i+=o1.rozm){
  303.                 for (int j=0; j<o1.rozm;j++){
  304.                     for (int k=0; k<o1.rozm;k++){
  305.                         wynik.ws[i+j]+=this->ws[k+i]*o1.ws[k*o1.rozm+j];
  306.                     }
  307.                 }
  308.             }
  309.            
  310.            
  311.         return wynik;
  312.  
  313.         }
  314.     }
  315.  
  316.  
  317.     int operator==(const MACIERZ &o1){
  318.         if (this->rozm != o1.rozm){
  319.             cout <<"Macierze różnych rozmów!" <<endl;
  320.             return 0;
  321.         }
  322.         else{
  323.             int flaga=0;
  324.             for(int i=0;i< o1.rozm*o1.rozm; i++){
  325.                 if(this->ws[i] != o1.ws[i]) flaga+=1;
  326.             }
  327.  
  328.             if(flaga==0) return 1;
  329.             else{
  330.                 return 0;
  331.             }
  332.         }
  333.     }
  334.  
  335.     int operator>=(const MACIERZ &o1){
  336. //operatory >= i <= porownuja sume elemntow macierzy
  337.         if (this->rozm != o1.rozm){
  338.             cout <<"Macierze różnych rozmiarów!" <<endl;
  339.             return 0;
  340.         }
  341.         else{
  342.             int s1=0;
  343.             int s2=0;
  344.             for(int i=0;i< this->rozm*this->rozm; i++){
  345.                 s1+=this->ws[i];
  346.             }
  347.  
  348.             for(int i=0;i< o1.rozm*o1.rozm; i++){
  349.                 s2+=o1.ws[i];
  350.             }
  351.  
  352.             if(s1>=s2) return 1;
  353.             else{
  354.                 return 0;
  355.             }
  356.         }
  357.     }
  358.  
  359.     int operator<=(const MACIERZ &o1){
  360.         if (this->rozm != o1.rozm){
  361.             cout <<"Macierze sa różnych rozmiarów!" <<endl;
  362.             return 0;
  363.         }
  364.         else{
  365.             int s1=0;
  366.             int s2=0;
  367.             for(int i=0;i< this->rozm*this->rozm; i++){
  368.                 s1+=this->ws[i];
  369.             }
  370.  
  371.             for(int i=0;i< o1.rozm*o1.rozm; i++){
  372.                 s2+=o1.ws[i];
  373.             }
  374.  
  375.             if(s1<=s2) return 1;
  376.             else{
  377.                 return 0;
  378.             }
  379.         }
  380.     }
  381.  
  382.     int operator!=(const MACIERZ &o1){
  383.         if (this->rozm != o1.rozm){
  384.             cout <<"Macierze sa różnych rozmiarów!" <<endl;
  385.             return 0;
  386.         }
  387.         else{
  388.             int flaga=0;
  389.             for(int i=0;i< this->rozm*this->rozm; i++){
  390.                 if (this->ws[i] != o1.ws[i])flaga+=1;
  391.             }
  392.  
  393.  
  394.             if(flaga!=0) return 1;
  395.             else{
  396.                 return 0;
  397.             }
  398.         }
  399.     }
  400.  
  401. //koniec klasy MACIERZ
  402. };
  403.  
  404. ostream & operator<< (ostream & s1, MACIERZ & o1){
  405.   if (o1.ws!=NULL){
  406.       for(int i=0, j=1;i<(o1.rozm*o1.rozm);i++, j++){
  407.           cout << o1.ws[i] <<" " ;
  408.           if (j==o1.rozm){
  409.               s1<<endl;
  410.               j=0;
  411.           }
  412.       }
  413.   }
  414.   return s1;
  415. }
  416.  
  417. istream & operator>>(istream & s1, MACIERZ & o1){
  418.   s1>>o1.rozm;
  419.   if (o1.ws!=NULL){
  420.           delete [] o1.ws;
  421.       o1.ws=NULL;
  422.       }
  423.   if (o1.rozm>0){
  424.       o1.ws = new double[o1.rozm*o1.rozm];
  425.       for (int i=0;i<(o1.rozm*o1.rozm);i++){
  426.           cin >> o1.ws[i];
  427.       }
  428.   }
  429.   return s1;
  430. }
  431.  
  432.  
  433.  
  434.  
  435. int main(){
  436.     LISTA<MACIERZ> a,b,c;
  437.         cout<<"Wpisz trzy elementy L1: ";
  438.     cin>>a>>a>>a;
  439.     a=b;
  440.     cout<<a;
  441.     /*LISTA<MACIERZ> d(a);
  442.     cout<<d;
  443.     cout<<"Wpisz dwa elementy L2 (main): ";
  444.     cin>>b>>b;
  445.     c=a+b;
  446.     cout<<c;*/
  447.  
  448.  
  449.     return 0;
  450. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement