Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <stdio.h>
- #include <conio.h>
- #include <malloc.h>
- #include <memory.h>
- class Wektor
- {
- private:
- double *tab;
- int N;
- public:
- Wektor(); //konstruktor domyślny
- ~Wektor();
- Wektor::Wektor(Wektor &kopia); //konstruktor kopiujący
- Wektor(const int N, const double M); //konstruktor parametryczny
- const int Size() const; //metoda zwracająca liczbę elementów tablicy
- void Display(); //wyświetla tablicę [el1, el2, ..., elN]
- void Koniec();
- Wektor& operator ~ () //operator dokonujący odwracania kolejności
- {
- Wektor sort(N, 0);
- //sort.tab = (double*)malloc(N * sizeof(double));
- int size = N-1;
- for(int i = 0; i < N; i++)
- {
- sort.tab[i] = tab[size];
- size--;
- }
- return sort;
- }
- Wektor& operator += (double x) //operator pozwalający na dodawanie do tablicy kolejnych elementów
- {
- N++;
- double *buff;
- if (double *buff = (double*)realloc(tab, N * sizeof(double)) != NULL)
- tab = buff;
- tab[N-1] = x;
- return *this;
- }
- };
- Wektor::Wektor() //konstruktor domyślny
- {
- tab = NULL;
- N = 0;
- }
- Wektor::Wektor(Wektor &kopia)
- {
- this->N = kopia.N;
- this->tab = (double*)malloc(N * sizeof(double));
- memcpy(this->tab, kopia.tab, N * sizeof(double));
- }
- Wektor::Wektor(const int N, const double M)
- {
- this->N = N;
- tab = (double*)malloc(N * sizeof(double));
- for( int i = 0; i < N; i++ )
- tab[i] = M;
- }
- const int Wektor::Size() const
- {
- return N;
- }
- void Wektor::Display()
- {
- printf("[");
- for(int i = 0; i < N; i++)
- {
- printf("%.2f", tab[i]);
- if(i!=N-1) printf(", ");
- }
- printf("]");
- }
- Wektor::~Wektor()
- {
- free(tab);
- }
- int main()
- {
- Wektor arr1;
- Wektor arr2(5, 2.00);
- printf("arr1 {%2d} = ", arr1.Size());
- arr1.Display();
- printf("\narr2 {%2d} = ", arr2.Size());
- arr2.Display();
- Wektor arr3 = arr2;
- arr3 += 11.1;
- arr3 += 22.2;
- arr3 += 33.3;
- printf("\narr3 {%2d} = ", arr3.Size());
- arr3.Display();
- arr3;
- printf("\n~arr3 {%2d} = ", arr3.Size());
- (~arr3).Display();
- printf("\narr2 {%2d} = ", arr2.Size());
- arr2.Display();
- arr1.~Wektor();
- arr2.~Wektor();
- arr3.~Wektor();
- printf("\n\nKoniec.");
- _getch();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement