Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdio.h>
- using namespace std;
- // OBIEKT LISTA DONE
- // operator wejscia dodaje 1 element, wyjscia wypisuje wszystkie el DONE
- // konstruktor, destruktor, konstruktor kopiujący DONE
- // + do listy dopisuje na końcu drógą DONE
- // =, ==, != DONE
- template<typename T>
- class List;
- template<typename T>
- class Element;
- class Macierze{
- friend ostream & operator<< (ostream & s1, Macierze & o1);
- friend istream & operator>>(istream & s1, Macierze & o1);
- double *ws;
- int rozmiar;
- public:
- Macierze(){
- ws=NULL;
- }
- Macierze(int rozm, int wart){
- rozmiar = rozm;
- ws = new double[rozm*rozm];
- for ( int i = 0; i < rozmiar*rozmiar; i++){
- ws[i] = wart;
- }
- }
- Macierze(const Macierze &o1){ // kopiujacy konstruktor, potrzebny przy dodawaniu
- rozmiar = o1.rozmiar;
- ws = new double[o1.rozmiar*o1.rozmiar];
- for ( int i = 0; i < rozmiar*rozmiar; i++){
- ws[i] = o1.ws[i];
- }
- }
- ~Macierze(){
- if (ws!=NULL){
- delete [] ws;
- ws=NULL;
- }
- }
- Macierze & operator=(const Macierze & o1){
- //zwolnić pamięć dla lewej str
- //zarezerwować pamieć
- //przepisać pamieć
- if (this == &o1) return *this;
- if (this->ws!=NULL){
- delete [] this->ws;
- }
- this->rozmiar = o1.rozmiar;
- if (o1.rozmiar>0){
- this->ws = new double[o1.rozmiar*o1.rozmiar];
- for (int i=0;i<(o1.rozmiar*o1.rozmiar);i++){
- this->ws[i] = o1.ws[i];
- }
- }
- return *this;
- }
- Macierze operator+(const Macierze &o1){
- Macierze wynik= Macierze(*this);
- if (this->rozmiar != o1.rozmiar){
- cout <<"Nie można dodać różnych macierzy!";
- return wynik;
- }
- else{
- for (int i=0;i< o1.rozmiar*o1.rozmiar; i++){
- wynik.ws[i] += o1.ws[i];
- }
- return wynik;
- }
- }
- Macierze operator-(const Macierze &o1){
- Macierze wynik= Macierze(*this);
- if (this->rozmiar != o1.rozmiar){
- cout <<"nie można wykonać" <<endl;
- return wynik;
- }
- else{
- for (int i=0;i< o1.rozmiar*o1.rozmiar; i++){
- wynik.ws[i] -= o1.ws[i];
- }
- return wynik;
- }
- }
- Macierze operator*(const Macierze &o1){
- //działa tylko dla pierwszego wiersza
- Macierze wynik= Macierze(o1.rozmiar, 0);
- cout <<wynik <<endl;
- if (this->rozmiar != o1.rozmiar){
- cout <<"nie można wykonać" <<endl;
- return wynik;
- }
- else{
- int n=o1.rozmiar;
- for (int i=0; i<n*n;i+=n){
- for (int j=0; j<n;j++){
- for (int k=0; k<n;k++){
- cout<<i <<"=i " <<i+j << "=i+j XX ";
- wynik.ws[i+j]+=this->ws[k+i]*o1.ws[k*n+j];
- }
- cout <<endl;
- }
- }
- return wynik;
- }
- }
- int operator==(const Macierze &o1){
- if (this->rozmiar != o1.rozmiar){
- cout <<"Macierze różnych rozmiarów!" <<endl;
- return 0;
- }
- else{
- int flaga=0;
- for(int i=0;i< o1.rozmiar*o1.rozmiar; i++){
- if(this->ws[i] == o1.ws[i]) flaga+=1;
- }
- if(flaga==0) return 1;
- else{
- return 0;
- }
- }
- }
- int operator>=(const Macierze &o1){
- //operatory >= <= działają na zasadzie: zlicza sumę wszystkich elementów jednego i drugiego, a później porównuje te liczby
- if (this->rozmiar != o1.rozmiar){
- cout <<"Macierze różnych rozmiarów! Nie porównam" <<endl;
- return 0;
- }
- else{
- int suma1=0;
- int suma2=0;
- for(int i=0;i< this->rozmiar*this->rozmiar; i++){
- suma1+=this->ws[i];
- }
- for(int i=0;i< o1.rozmiar*o1.rozmiar; i++){
- suma2+=o1.ws[i];
- }
- if(suma1>=suma2) return 1;
- else{
- return 0;
- }
- }
- }
- int operator<=(const Macierze &o1){
- if (this->rozmiar != o1.rozmiar){
- cout <<"Macierze różnych rozmiarów! Nie porównam" <<endl;
- return 0;
- }
- else{
- int suma1=0;
- int suma2=0;
- for(int i=0;i< this->rozmiar*this->rozmiar; i++){
- suma1+=this->ws[i];
- }
- for(int i=0;i< o1.rozmiar*o1.rozmiar; i++){
- suma2+=o1.ws[i];
- }
- if(suma1<=suma2) return 1;
- else{
- return 0;
- }
- }
- }
- int operator!=(const Macierze &o1){
- if (this->rozmiar != o1.rozmiar){
- cout <<"Macierze różnych rozmiarów! Nie porównam" <<endl;
- return 0;
- }
- else{
- int roznica=0;
- for(int i=0;i< this->rozmiar*this->rozmiar; i++){
- if (this->ws[i] != o1.ws[i])roznica+=1;
- }
- if(roznica!=0) return 1;
- else{
- return 0;
- }
- }
- }
- //koniec klasy macierze
- };
- ostream & operator<< (ostream & s1, Macierze & o1){
- if (o1.ws!=NULL){
- for(int i=0, j=1;i<(o1.rozmiar*o1.rozmiar);i++, j++){
- cout << o1.ws[i] <<" " ;
- if (j==o1.rozmiar){
- s1<<endl;
- j=0;
- }
- }
- }
- return s1;
- }
- istream & operator>>(istream & s1, Macierze & o1){
- s1>>o1.rozmiar;
- if (o1.rozmiar>0){
- if (o1.ws!=NULL){
- delete [] o1.ws;
- }
- o1.ws = new double[o1.rozmiar*o1.rozmiar];
- for (int i=0;i<(o1.rozmiar*o1.rozmiar);i++){
- cin >> o1.ws[i];
- }
- }
- return s1;
- }
- template<class T>
- class Element{
- friend class List<T>;
- template<typename S>
- friend ostream & operator << (ostream & s1, List<S> & o1);
- template<typename S>
- friend istream & operator >> (istream & s1, List<S> & o1);
- public:
- T war;
- class Element<T> *next;
- };
- template<class T>
- class List{
- template<typename S>
- friend ostream & operator << (ostream & s1, List<S> & o1);
- template<typename S>
- friend istream & operator >> (istream & s1, List<S> & o1);
- class Element<T> *pierwszy;
- public:
- List<T>(){
- pierwszy=NULL;
- }
- List<T> (const class List<T> &o1){
- if (o1.pierwszy==NULL){
- pierwszy=NULL;
- return;
- }
- class Element<T> *nowy = new Element<T>;
- nowy->war=o1.pierwszy->war;
- Element<T> *i =o1.pierwszy;
- Element<T> *poprz = nowy;
- pierwszy=nowy;
- i=i->next;
- while (i!=NULL){
- class Element<T> *elem = new Element<T>;
- elem->war=i->war;
- poprz->next=elem;
- poprz=elem;
- i=i->next;
- }
- poprz->next=NULL;
- }
- ~List<T>(){
- while (pierwszy!=NULL){
- class Element<T> *toDelete;
- toDelete = pierwszy;
- pierwszy = pierwszy->next;
- delete toDelete;
- }
- pierwszy=NULL;
- }
- List<T> & operator = (const class List<T> &o1){
- if (this==&o1){
- return *this;
- }
- if (o1.pierwszy==NULL && this->pierwszy == NULL){
- return *this;
- }
- if(o1.pierwszy==NULL){
- this->~List();
- return *this;
- }
- if(this->pierwszy != NULL) this->~List();
- Element<T> *n_element = new Element<T>;
- n_element->war = o1.pierwszy->war;
- Element<T> *i = o1.pierwszy;
- Element<T> *prev = n_element;
- this->pierwszy = n_element;
- i = i->next;
- while(i != NULL){
- Element<T> *_element = new Element<T>;
- _element->war = i->war;
- prev->next = _element;
- prev = _element;
- i = i->next;
- }
- prev->next = NULL;
- return *this;
- }
- List<T> operator+(const class List<T> &o1){
- if(this->pierwszy == NULL && o1.pierwszy == NULL) return *this;
- if(this->pierwszy != NULL && o1.pierwszy == NULL){
- List<T> new_list;
- if (this->pierwszy==NULL){
- new_list.pierwszy=NULL;
- }
- else{
- class Element<T> *nowy = new Element<T>;
- nowy->war=this->pierwszy->war;
- Element<T> *i =this->pierwszy;
- Element<T> *poprz = nowy;
- new_list.pierwszy=nowy;
- i=i->next;
- while (i!=NULL){
- class Element<T> *elem = new Element<T>;
- elem->war=i->war;
- poprz->next=elem;
- poprz=elem;
- i=i->next;
- }
- poprz->next=NULL;
- }
- return new_list;
- }
- if(this->pierwszy == NULL && o1.pierwszy != NULL){
- List<T> new_list(o1);
- return new_list;
- }
- else {
- List<T> new_list;
- if (this->pierwszy==NULL){
- new_list.pierwszy=NULL;
- }
- else{
- class Element<T> *nowy = new Element<T>;
- nowy->war=this->pierwszy->war;
- Element<T> *i =this->pierwszy;
- Element<T> *poprz = nowy;
- new_list.pierwszy=nowy;
- i=i->next;
- while (i!=NULL){
- class Element<T> *elem = new Element<T>;
- elem->war=i->war;
- poprz->next=elem;
- poprz=elem;
- i=i->next;
- }
- poprz->next=NULL;
- }
- Element<T> *i = new_list.pierwszy;
- while (i->next) i = i->next;
- Element<T> *j = o1.pierwszy;
- Element<T> *prev = new Element<T>;
- prev->war = o1.pierwszy->war;
- i->next = prev;
- j = j->next;
- while (j) {
- Element<T> *new_el = new Element<T>;
- new_el->war = j->war;
- prev->next = new_el;
- prev = new_el;
- j = j->next;
- }
- prev->next = NULL;
- return new_list;
- }
- }
- bool operator == (List<T> &o1){
- if (this->pierwszy ==NULL && o1.pierwszy==NULL){
- return true;
- }
- if (this->pierwszy ==NULL && o1.pierwszy!=NULL){
- return false;
- }
- if (this->pierwszy !=NULL && o1.pierwszy==NULL){
- return false;
- }
- if (o1.pierwszy!=NULL){
- Element<T> *head=o1.pierwszy;
- Element<T> *headthis=this->pierwszy;
- while (head->next!=NULL){
- if (head->war!=headthis->war)
- return false;
- head=head->next;
- headthis=headthis->next;
- }
- return true;
- }
- return false;
- }
- bool operator != (List<T> &o1){
- if (this->pierwszy==NULL && o1.pierwszy==NULL){
- return false;
- }
- else if (this->pierwszy ==NULL && o1.pierwszy!=NULL){
- return true;
- }
- else if (this->pierwszy !=NULL && o1.pierwszy==NULL){
- return true;
- }
- else{
- bool wynik;
- Element<T> *head=o1.pierwszy;
- Element<T> *headthis=this->pierwszy;
- while (head->next!=NULL && headthis->next!=NULL){
- if (head->war!=headthis->war)
- wynik=true;
- else{
- wynik=false;
- break;
- }
- head=head->next;
- headthis=headthis->next;
- }
- if (head->next==NULL && headthis ->next!=NULL){
- wynik=false;
- }
- return wynik;
- }
- }
- };
- template<typename S>
- ostream & operator<< (ostream &s1, class List<S> &o1){
- Element<S> *n = o1.pierwszy;
- while(n){
- s1 << n->war << " " ;
- n = n->next;
- }
- return s1;
- }
- template<typename S>
- istream & operator >>(istream &s1, class List<S> &o1){
- class Element<S> *element = new Element<S>;
- s1 >> element->war;
- if (o1.pierwszy==NULL){
- o1.pierwszy=element;
- o1.pierwszy->next=NULL;
- }
- else{
- class Element<S> *wsk =o1.pierwszy;
- while(wsk->next != NULL){
- wsk=wsk->next;
- }
- wsk->next = element;
- element->next = NULL;
- }
- return s1;
- }
- int main(){
- List<Macierze> z1, z2, z3;
- cin >> z1;
- cin >> z1;
- cout << "Lista macierzy 1: " <<endl << z1;
- cin >> z2;
- z3 = z2+z1;
- z3 = z2+z1;
- cout <<"Suma list: "<< endl << z3;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement