Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <conio.h>
- #include <cstdlib>
- using namespace std;
- class vect {
- int *p;
- int size;
- public:
- int ub; // upper bound = size-1
- vect(int n = 10); // konstruktor z domniemanym argumentem
- vect(const vect &v); // konstruktor kopiuj¹cy
- ~vect() { delete p; } // destruktor
- vect& operator =(const vect &v);// operator podstawiania
- int operator ==(const vect &v) const;// porównanie wektorów
- int& operator[](int i); // operator indeksowy
- vect operator+(const vect &v) const;// dodawanie wektorów
- vect operator-(const vect &v) const;// odejmowanie wektorów
- int operator*(const vect &v) const; // iloczyn skalarny
- vect operator*(int a) const; // mno¿enie wektora przez skalar
- void operator !() const; // print
- vect operator+(int n);
- vect operator-(int n);
- friend vect operator+(int n, vect &s);
- friend vect operator-(int n, vect &s);
- };
- vect vect::operator+(int n)
- {
- vect temp = (*this);
- return temp + n;
- }
- vect vect::operator-(int n)
- {
- vect temp = (*this);
- for (int i = 0; i <= ub; i++)
- {
- p[i] *= -1;
- }
- return temp + n;
- }
- vect operator-(int n, vect &s)
- {
- n = -n;
- return s + n;
- }
- vect operator+(int n, vect &s)
- {
- vect temp(s.size);
- for (int i = 0; i <= s.ub; i++)
- {
- temp.p[i] = s.p[i] + n;
- }
- return temp;
- }
- vect::vect(int n)
- {
- if (n <= 0) exit(1);// "fatal error" mo¿na by powiedzieæ...
- size = n;
- p = new int[size];
- ub = size - 1; // Okreœlenie maksymalnej wartoœci indeksu
- }
- vect::vect(const vect &v)
- {
- p = 0;
- *this = v;
- }
- vect& vect::operator =(const vect &v)
- {
- *size = v.size;
- if (p) delete p;
- p = new int[ub];
- ub = size - 1; // Okreœlenie maksymalnej wartoœci indeksu
- for (int i = 0; i<=ub; i++)
- this->p[i] = v.p[i];
- return *this;
- }
- int vect::operator ==(const vect &v) const
- {
- if (size != v.size)
- return 0; // inny wymiar
- for (int i = 0; i<size; i++)
- if (p[i] != v.p[i])
- return 0;// ró¿ne elementy
- }
- int& vect::operator[](int i)
- {
- if (i<0 || i>ub) exit(1);
- return(p[i]);
- }
- vect vect::operator+(const vect &v) const
- {
- int s = size>v.size ? size : v.size;
- vect temp(s);
- for (int i = 0; i<s; i++)
- temp[i] = p[i] + v.p[i];
- return temp;
- }
- vect vect::operator-(const vect &v) const
- {
- int s = size>v.size ? size : v.size;
- vect temp(s);
- for (int i = 0; i<s; i++)
- temp[i] = p[i] - v.p[i];
- return temp;
- }
- int vect::operator*(const vect &v) const
- {
- int s = size<v.size ? size : v.size;
- int i_sk = 0;
- for (int i = 0; i<s; i++)
- i_sk += p[i] * v.p[i];
- return i_sk;
- }
- vect vect::operator*(int a) const
- {
- vect temp(*this);
- for (int i = 0; i<size; i++)
- temp[i] *= a;
- return temp;
- }
- vect operator*(int a, const vect &v)
- {
- return v * a;
- }
- void vect::operator !() const
- {
- for (int i = 0; i<size; i++)
- cout << endl << p[i];
- }
- int main()
- {
- const int n = 3;
- vect a(n), b(n), c(n);
- for (int i = 0; i<n; i++)
- a[i] = i * 2 + 3;
- for (int i = 0; i<n; i++)
- b[i] = 10 - i;
- c = 2 * a - b;
- cout << "\nWektor a:\n"; !a;
- cout << "\nWektor b:\n"; !b;
- cout << "\nWektor c=2*a-b:\n"; !c;
- cout << "\n\na*b = " << a * b;
- _getch();
- cout << endl << "=======";
- !a;
- c = a + 2;
- !c;
- _getch();
- cout << endl << "=======";
- !a;
- c = a - 2;
- !c;
- _getch();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement