Advertisement
Guest User

Untitled

a guest
Apr 16th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.28 KB | None | 0 0
  1. // przeciazanie1.cpp : Defines the entry point for the console application.
  2. //
  3. #include <iostream>
  4. #include "stdafx.h"
  5. #include <conio.h>
  6. #include <cstdlib>
  7. #include <vector>
  8. #include <algorithm>
  9. class Wektor
  10. {
  11. private:
  12. float* b;
  13. int length;
  14. public:
  15. Wektor(int n, int m);
  16. Wektor();
  17. ~Wektor();
  18. Wektor(const Wektor &wektor);
  19. void Wyswietl() const;
  20. int Dlugosc() const;
  21. void operator+=(float n);
  22. void operator~();
  23. Wektor operator<<(int x);
  24. Wektor operator>>(int x);
  25. };
  26. Wektor::Wektor(int n, int m)
  27. {
  28. this->length = n;
  29. b = new float[n];
  30. for (int i = 0;i < n;i++)
  31. {
  32. b[i] = m;
  33. }
  34. }
  35. Wektor::Wektor()
  36. {
  37. this->length = 0;
  38. b = new float[length];
  39. }
  40. Wektor::Wektor(const Wektor &wektor)
  41. {
  42. this->length = wektor.length;
  43. b = new float[wektor.length];
  44. for (int i = 0;i < wektor.length;i++)
  45. {
  46. b[i] = wektor.b[i];
  47. }
  48. }
  49. void Wektor::Wyswietl() const
  50. {
  51. printf("[");
  52. for (int i = 0;i < length;i++)
  53. {
  54. printf("%0.2f,", b[i]);
  55. }
  56. printf("]");
  57. }
  58. int Wektor::Dlugosc() const
  59. {
  60. return length;
  61. }
  62. Wektor::~Wektor()
  63. {
  64. delete[] b;
  65. }
  66. void Wektor::operator~()
  67. {
  68. int pom = length - 1;
  69. for (int i = 0;i <length/2;i++)
  70. {
  71. std::swap(b[i], b[pom]);
  72. pom--;
  73. }
  74. }
  75. void Wektor::operator+=(float n)
  76. {
  77. float *a;
  78. a = new float[length+1];
  79. for (int i = 0;i < length;i++)
  80. {
  81. a[i] = b[i];
  82. }
  83. a[length] = n;
  84. this->length = length + 1;
  85. free(b);
  86. b = a;
  87. }
  88. Wektor Wektor::operator<<(int x)
  89. {
  90. float a_tmp = 0;
  91. for (int i = 0;i < length;i++)
  92. {
  93. if (i == x)break;
  94. a_tmp = b[i];
  95. b[i] = b[length - i - 1];
  96. b[length - i - 1] = a_tmp;
  97. }
  98. return *this;
  99. }
  100. Wektor Wektor::operator>>(int x)
  101. {
  102. float a_tmp = 0;
  103. for (int i = 0;i < length;i++)
  104. {
  105. if (i == x)break;
  106. a_tmp = b[length-i-1];
  107. b[length - i - 1] = b[i];
  108. b[i] = a_tmp;
  109. }
  110. return *this;
  111. }
  112. int main()
  113. {
  114. Wektor w1;
  115. Wektor w2(3, 10);
  116. printf("{%d} W1: ", w1.Dlugosc());
  117. w1.Wyswietl();
  118. printf("\n{%d} W2: ", w2.Dlugosc());
  119. w2.Wyswietl();
  120. Wektor w3 = w2;
  121. w3 += 1.0;
  122. w3 += 2.10;
  123. w3 += 2.11;
  124. w3 += 2.13;
  125. w3 += 5.18;
  126. printf("\n{%d} W3: ", w3.Dlugosc());
  127. w3.Wyswietl();
  128. w3 >> 3;
  129. printf("\n{%d} W3: <<", w3.Dlugosc());
  130. w3.Wyswietl();
  131. w3 << 3;
  132. printf("\n{%d} W3 >>: ", w3.Dlugosc());
  133. w3.Wyswietl();
  134. _getch();
  135. return 0;
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement