Advertisement
Guest User

Untitled

a guest
May 16th, 2018
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.20 KB | None | 0 0
  1. #include <iostream>
  2. #include <conio.h>
  3. #include <cstdlib>
  4. using namespace std;
  5.  
  6. class vect {
  7. int *p;
  8. int size;
  9. public:
  10. int ub; // upper bound = size-1
  11. vect(int n = 10); // konstruktor z domniemanym argumentem
  12. vect(const vect &v); // konstruktor kopiuj¹cy
  13. ~vect() { delete p; } // destruktor
  14. vect& operator =(const vect &v);// operator podstawiania
  15. int operator ==(const vect &v) const;// porównanie wektorów
  16. int& operator[](int i); // operator indeksowy
  17. vect operator+(const vect &v) const;// dodawanie wektorów
  18. vect operator-(const vect &v) const;// odejmowanie wektorów
  19. int operator*(const vect &v) const; // iloczyn skalarny
  20. vect operator*(int a) const; // mno¿enie wektora przez skalar
  21. void operator !() const; // print
  22. vect operator+(int n);
  23. vect operator-(int n);
  24. friend vect operator+(int n, vect &s);
  25. friend vect operator-(int n, vect &s);
  26.  
  27. };
  28. vect vect::operator+(int n)
  29. {
  30. vect temp = (*this);
  31. return temp + n;
  32. }
  33. vect vect::operator-(int n)
  34. {
  35. vect temp = (*this);
  36. for (int i = 0; i <= ub; i++)
  37. {
  38. p[i] *= -1;
  39. }
  40. return temp + n;
  41.  
  42. }
  43. vect operator-(int n, vect &s)
  44. {
  45. n = -n;
  46. return s + n;
  47. }
  48. vect operator+(int n, vect &s)
  49. {
  50. vect temp(s.size);
  51. for (int i = 0; i <= s.ub; i++)
  52. {
  53. temp.p[i] = s.p[i] + n;
  54. }
  55. return temp;
  56. }
  57. vect::vect(int n)
  58. {
  59. if (n <= 0) exit(1);// "fatal error" mo¿na by powiedzieæ...
  60. size = n;
  61. p = new int[size];
  62. ub = size - 1; // Okreœlenie maksymalnej wartoœci indeksu
  63. }
  64.  
  65. vect::vect(const vect &v)
  66. {
  67. p = 0;
  68. *this = v;
  69. }
  70.  
  71. vect& vect::operator =(const vect &v)
  72. {
  73. *size = v.size;
  74. if (p) delete p;
  75. p = new int[ub];
  76. ub = size - 1; // Okreœlenie maksymalnej wartoœci indeksu
  77. for (int i = 0; i<=ub; i++)
  78. this->p[i] = v.p[i];
  79. return *this;
  80.  
  81. }
  82.  
  83. int vect::operator ==(const vect &v) const
  84. {
  85. if (size != v.size)
  86. return 0; // inny wymiar
  87. for (int i = 0; i<size; i++)
  88. if (p[i] != v.p[i])
  89. return 0;// ró¿ne elementy
  90. }
  91.  
  92. int& vect::operator[](int i)
  93. {
  94. if (i<0 || i>ub) exit(1);
  95. return(p[i]);
  96. }
  97.  
  98. vect vect::operator+(const vect &v) const
  99. {
  100. int s = size>v.size ? size : v.size;
  101. vect temp(s);
  102. for (int i = 0; i<s; i++)
  103. temp[i] = p[i] + v.p[i];
  104. return temp;
  105. }
  106.  
  107. vect vect::operator-(const vect &v) const
  108. {
  109. int s = size>v.size ? size : v.size;
  110. vect temp(s);
  111. for (int i = 0; i<s; i++)
  112. temp[i] = p[i] - v.p[i];
  113. return temp;
  114. }
  115.  
  116. int vect::operator*(const vect &v) const
  117. {
  118. int s = size<v.size ? size : v.size;
  119. int i_sk = 0;
  120. for (int i = 0; i<s; i++)
  121. i_sk += p[i] * v.p[i];
  122. return i_sk;
  123. }
  124.  
  125. vect vect::operator*(int a) const
  126. {
  127. vect temp(*this);
  128. for (int i = 0; i<size; i++)
  129. temp[i] *= a;
  130. return temp;
  131. }
  132.  
  133. vect operator*(int a, const vect &v)
  134. {
  135. return v * a;
  136. }
  137.  
  138. void vect::operator !() const
  139. {
  140. for (int i = 0; i<size; i++)
  141. cout << endl << p[i];
  142. }
  143.  
  144. int main()
  145. {
  146. const int n = 3;
  147. vect a(n), b(n), c(n);
  148. for (int i = 0; i<n; i++)
  149. a[i] = i * 2 + 3;
  150. for (int i = 0; i<n; i++)
  151. b[i] = 10 - i;
  152. c = 2 * a - b;
  153. cout << "\nWektor a:\n"; !a;
  154. cout << "\nWektor b:\n"; !b;
  155. cout << "\nWektor c=2*a-b:\n"; !c;
  156. cout << "\n\na*b = " << a * b;
  157. _getch();
  158. cout << endl << "=======";
  159. !a;
  160. c = a + 2;
  161. !c;
  162. _getch();
  163. cout << endl << "=======";
  164. !a;
  165. c = a - 2;
  166. !c;
  167. _getch();
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement