Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*dzielenie na nagłówki, definicje na zewnątrz(operator zasiegu), nauczyc sie, nie przerabiac*/
- #include <iostream>
- #include <stdio.h>
- using namespace std;
- template <class T>
- class para {
- public:
- T x,y;
- para<T>(){
- T x,y;
- }
- para<T>(T x1, T y1){
- para<T> o1;
- o1.x=x1;
- o1.y=y1;
- }
- para<T> &operator = (para<T> &o1){
- x=o1.x;
- y=o1.y;
- return *this;
- }
- para<T> &operator + (para<T> &o1){
- static para<T> wynik;
- wynik.x=this->x+o1.x;
- wynik.y=this->y+o1.y;
- return wynik;
- }
- };
- template <class T>
- istream &operator >>(istream &s, para<T> &o1){
- s>>o1.x>>o1.y;
- return s;
- }
- template <class T>
- ostream &operator << (ostream &s, para<T> &o1){
- s<<o1.x<<endl<<o1.y<<endl;
- return s;
- }
- template <class T>
- class LISTA;
- template<class T>
- class EL {
- template <class>
- friend class LISTA;
- template <class U>
- friend ostream & operator<< (ostream &s1, LISTA<U> & o1);
- template <class U>
- friend istream & operator>> (istream &s1, LISTA<U> &o1);
- T wartosc;
- class EL<T> *next;
- };
- template<class T>
- class LISTA {
- class EL<T> *head;
- template <class U>
- friend ostream & operator<< (ostream &s1, LISTA<U> & o1);
- template <class U>
- friend istream & operator>> (istream &s1, LISTA<U> &o1);
- public:
- LISTA<T>(){
- head=NULL;
- }
- LISTA<T>(class LISTA &o1){
- if(o1.head==NULL) return;
- auto *nowy = new EL<T>;
- nowy->wartosc=o1.head->wartosc;
- EL<T> *temp=o1.head;
- EL<T> *prev=nowy;
- head=nowy;
- temp=temp->next;
- while (temp!=NULL){
- EL<T> *elemencik=new EL<T>;
- prev->next=elemencik;
- prev=elemencik;
- temp=temp->next;
- }
- }
- ~LISTA<T>(){
- while(head!=NULL){
- class EL<T> *toDel;
- toDel=head;
- head=head->next;
- delete toDel;
- }
- }
- LISTA<T> operator =(const class LISTA<T> &o1){
- if (o1.head==NULL) return *this;
- auto *nowy = new EL<T>;
- nowy->wartosc=o1.head->wartosc;
- EL<T> *temp=o1.head;
- EL<T> *prev=nowy;
- head=nowy;
- temp=temp->next;
- while(temp!=NULL){
- auto *elemencik=new EL<T>;
- elemencik->wartosc=temp->wartosc;
- prev->next=elemencik;
- prev=elemencik;
- temp=temp->next;
- }
- prev->next=NULL;
- return *this;
- }
- LISTA<T> & operator + (LISTA<T> &o1){
- static LISTA<T> temp;
- if (this->head==NULL && o1.head==NULL)
- return temp;
- EL<T> *current = new EL<T>;
- EL<T> *node;
- EL<T> *i = this->head;
- EL<T> *j = o1.head;
- current->wartosc=i->wartosc;
- temp.head=current;
- while (i->next!=NULL){
- node=new EL<T>;
- current->next=node;
- i=i->next;
- node->wartosc=i->wartosc;
- current=node;
- }
- node=new EL<T>;
- node->wartosc=j->wartosc;
- current->next=node;
- while (j!=NULL){
- node=new EL<T>;
- current->next=node;
- node->wartosc=j->wartosc;
- current=node;
- j=j->next;
- }
- return temp;
- }
- bool operator == (const class LISTA<T> &o1){
- EL<T> *temp1=this->head;
- EL<T> *temp2=o1.head;
- int flaga=0;
- while(true){
- if (temp1==NULL) flaga+=1;
- if (temp2==NULL) flaga+=1;
- if (flaga==1) return false;
- else if (flaga==2) return true;
- if (temp1->wartosc!=temp2->wartosc) return false;
- temp1=temp1->next;
- temp2=temp2->next;
- }
- }
- bool operator != (const class LISTA<T> &o1){
- EL<T> *temp1=this->head;
- EL<T> *temp2=o1.head;
- int flaga=0;
- while (true){
- if (temp1==NULL) flaga+=1;
- if (temp2==NULL) flaga+=1;
- if (flaga==1) return true;
- if (flaga==2) return false;
- if(temp1->wartosc==temp2->wartosc) return false;
- }
- }
- };
- template<class T>
- ostream &operator<< (ostream &s1, LISTA<T> &o1){
- EL<T> *temp=o1.head;
- while(temp!=NULL){
- s1 << temp->wartosc<<" ";
- temp=temp->next;
- }
- return s1;
- }
- template<class T>
- istream &operator>> (istream &s1, LISTA<T> &o1){
- auto *element = new EL<T>;
- s1 >> element->wartosc;
- if(o1.head==NULL) {
- o1.head = element;
- o1.head->next=NULL;
- return s1;
- }
- EL<T> *n=o1.head;
- while(n->next!=NULL){
- n = n->next;
- }
- n->next = element;
- element->next = NULL;
- return s1;
- }
- class MACIERZ{
- friend ostream & operator<< (ostream & s1, MACIERZ & o1);
- friend istream & operator>>(istream & s1, MACIERZ & o1);
- double *ws;
- int rozm;
- public:
- MACIERZ(){
- ws=NULL;
- }
- MACIERZ(int rozm, int wart){
- rozm = rozm;
- ws = new double[rozm*rozm];
- for ( int i = 0; i < rozm*rozm; i++){
- ws[i] = wart;
- }
- }
- MACIERZ(const MACIERZ &o1){
- rozm = o1.rozm;
- ws = new double[o1.rozm*o1.rozm];
- for ( int i = 0; i < rozm*rozm; i++){
- ws[i] = o1.ws[i];
- }
- }
- ~MACIERZ(){
- if (ws!=NULL){
- delete [] ws;
- ws=NULL;
- }
- }
- MACIERZ & operator=(const MACIERZ & o1){
- if (this == &o1) return *this;
- this->rozm = o1.rozm;
- if (this->ws!=NULL){
- delete [] this->ws;
- }
- if (o1.rozm>0){
- this->ws = new double[o1.rozm*o1.rozm];
- for (int i=0;i<(o1.rozm*o1.rozm);i++){
- this->ws[i] = o1.ws[i];
- }
- }
- return *this;
- }
- MACIERZ operator+ (const MACIERZ &o1){
- MACIERZ wynik(*this);
- if (this->rozm != o1.rozm){
- cout <<"Nie można dodać macierzy o różnych wymiarach!";
- return wynik;
- }
- else{
- for (int i=0;i< o1.rozm*o1.rozm; i++){
- wynik.ws[i] += o1.ws[i];
- }
- return wynik;
- }
- }
- MACIERZ operator- (const MACIERZ &o1){
- MACIERZ wynik= MACIERZ(*this);
- if (this->rozm != o1.rozm){
- cout <<"Nie można wykonać" <<endl;
- return wynik;
- }
- else{
- for (int i=0;i< o1.rozm*o1.rozm; i++){
- wynik.ws[i] -= o1.ws[i];
- }
- return wynik;
- }
- }
- MACIERZ operator*(const MACIERZ &o1){
- MACIERZ wynik= MACIERZ(*this);
- for (int k=0; k<o1.rozm*o1.rozm;k++){
- wynik.ws[k]=0;
- }
- if (this->rozm != o1.rozm){
- cout <<"Nie można mnozyc macierzy roznych rozmiarow!" <<endl;
- return wynik;
- }
- else{
- for (int i=0; i<o1.rozm*o1.rozm;i+=o1.rozm){
- for (int j=0; j<o1.rozm;j++){
- for (int k=0; k<o1.rozm;k++){
- wynik.ws[i+j]+=this->ws[k+i]*o1.ws[k*o1.rozm+j];
- }
- }
- }
- return wynik;
- }
- }
- int operator==(const MACIERZ &o1){
- if (this->rozm != o1.rozm){
- cout <<"Macierze różnych rozmów!" <<endl;
- return 0;
- }
- else{
- int flaga=0;
- for(int i=0;i< o1.rozm*o1.rozm; i++){
- if(this->ws[i] != o1.ws[i]) flaga+=1;
- }
- if(flaga==0) return 1;
- else{
- return 0;
- }
- }
- }
- int operator>=(const MACIERZ &o1){
- //operatory >= i <= porownuja sume elemntow macierzy
- if (this->rozm != o1.rozm){
- cout <<"Macierze różnych rozmiarów!" <<endl;
- return 0;
- }
- else{
- int s1=0;
- int s2=0;
- for(int i=0;i< this->rozm*this->rozm; i++){
- s1+=this->ws[i];
- }
- for(int i=0;i< o1.rozm*o1.rozm; i++){
- s2+=o1.ws[i];
- }
- if(s1>=s2) return 1;
- else{
- return 0;
- }
- }
- }
- int operator<=(const MACIERZ &o1){
- if (this->rozm != o1.rozm){
- cout <<"Macierze sa różnych rozmiarów!" <<endl;
- return 0;
- }
- else{
- int s1=0;
- int s2=0;
- for(int i=0;i< this->rozm*this->rozm; i++){
- s1+=this->ws[i];
- }
- for(int i=0;i< o1.rozm*o1.rozm; i++){
- s2+=o1.ws[i];
- }
- if(s1<=s2) return 1;
- else{
- return 0;
- }
- }
- }
- int operator!=(const MACIERZ &o1){
- if (this->rozm != o1.rozm){
- cout <<"Macierze sa różnych rozmiarów!" <<endl;
- return 0;
- }
- else{
- int flaga=0;
- for(int i=0;i< this->rozm*this->rozm; i++){
- if (this->ws[i] != o1.ws[i])flaga+=1;
- }
- if(flaga!=0) return 1;
- else{
- return 0;
- }
- }
- }
- //koniec klasy MACIERZ
- };
- ostream & operator<< (ostream & s1, MACIERZ & o1){
- if (o1.ws!=NULL){
- for(int i=0, j=1;i<(o1.rozm*o1.rozm);i++, j++){
- cout << o1.ws[i] <<" " ;
- if (j==o1.rozm){
- s1<<endl;
- j=0;
- }
- }
- }
- return s1;
- }
- istream & operator>>(istream & s1, MACIERZ & o1){
- s1>>o1.rozm;
- if (o1.ws!=NULL){
- delete [] o1.ws;
- o1.ws=NULL;
- }
- if (o1.rozm>0){
- o1.ws = new double[o1.rozm*o1.rozm];
- for (int i=0;i<(o1.rozm*o1.rozm);i++){
- cin >> o1.ws[i];
- }
- }
- return s1;
- }
- int main(){
- para<int> a,b,c;
- para<MACIERZ> d,e,f;
- cout<<"Wpisz pierwsza pare (main): ";
- cin>>d;
- cout<<"Wpisz druga pare (main): ";
- cin>>e;
- f=d+e;
- cout<<f;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement