Advertisement
ewa_tabor

Poprawione?

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