Advertisement
Guest User

Untitled

a guest
May 23rd, 2019
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.34 KB | None | 0 0
  1. #include <iostream>
  2. #include "CirclePoints.h"
  3.  
  4. int main() {
  5. CirclePoints circlePoints1(3, 4); //окружность. радиус равен 3, кол-во точек 4
  6. std::cout << "Points (1): ";
  7. for (auto a : circlePoints1) { //прямой итератор
  8. std::cout << "(" << a.x << "; " << a.y << "), "; //вывод координат точек в научной записи
  9. }
  10. std::cout << std::endl << "Reversed points (1): ";
  11. for (auto a = circlePoints1.end()--; a != circlePoints1.begin(); a--) { //обратный итератор
  12. std::cout << "(" << (*a).x << "; " << (*a).y << "), ";
  13. }
  14. CirclePoints circlePoints2(2, 8); //окружность. радиус равен 2, кол-во точек 8
  15. std::cout << std::endl << "Points (2): ";
  16. for (auto a : circlePoints2) { //прямой итератор
  17. std::cout << "(" << a.x << "; " << a.y << "), ";
  18. }
  19. return 0;
  20. }
  21.  
  22.  
  23.  
  24.  
  25.  
  26.  
  27.  
  28.  
  29.  
  30. #include <iterator>
  31. #include <math.h>
  32.  
  33. struct Point {
  34. double x, y;
  35. };
  36.  
  37. class CirclePoints {
  38. private:
  39. double radius;
  40. int k; //количество точек, равномерно распределённых вдоль окружности
  41. public:
  42. CirclePoints(double radius, int k): radius(radius), k(k) {
  43. }
  44. double getRadius() {
  45. return radius;
  46. }
  47. void setRadius(double radius) {
  48. this->radius = radius;
  49. }
  50. int getK() {
  51. return k;
  52. }
  53. void setK(int k) {
  54. this->k = k;
  55. }
  56. class PointsIterator: public std::iterator<std::bidirectional_iterator_tag, int, ptrdiff_t, Point*, Point> {
  57. private:
  58. bool isDefault = true;
  59. CirclePoints& circlePoints;
  60. int currentPoint; //номер данной точки [0; k)
  61. void next() {
  62. isDefault = ++currentPoint == circlePoints.k;
  63. }
  64. void previous() {
  65. isDefault = --currentPoint == -1;
  66. }
  67. public:
  68. PointsIterator(CirclePoints& circlePoints, bool isDefault): isDefault(false), circlePoints(circlePoints) {
  69. currentPoint = isDefault ? circlePoints.k : -1;
  70. }
  71. bool operator == (PointsIterator other) {
  72. return (isDefault && other.isDefault) || (currentPoint == other.currentPoint);
  73. }
  74. bool operator != (PointsIterator other) {
  75. return !(*this == other);
  76. }
  77. Point operator * () {
  78. if (isDefault) {
  79. throw "not initialized iterator";
  80. }
  81. if (currentPoint == -1) {
  82. next();
  83. } else if (currentPoint == circlePoints.k) {
  84. previous();
  85. }
  86. double alpha = 360.0 * (double) currentPoint / (double) circlePoints.k * std::acos(-1) / 180.0; //угол относительно начальной точки (перевод в радианы)
  87. Point point;
  88. point.x = circlePoints.radius * std::cos(alpha);
  89. point.y = circlePoints.radius * std::sin(alpha);
  90. return point;
  91. }
  92. PointsIterator& operator ++ () {
  93. if (isDefault) {
  94. throw "not initialized iterator";
  95. }
  96. next();
  97. return *this;
  98. }
  99. PointsIterator& operator ++ (int) {
  100. PointsIterator& tmp = *this;
  101. operator++();
  102. return tmp;
  103. }
  104. PointsIterator& operator -- () {
  105. if (isDefault) {
  106. throw "not initialized iterator";
  107. }
  108. previous();
  109. return *this;
  110. }
  111. PointsIterator& operator -- (int) {
  112. PointsIterator& tmp = *this;
  113. operator--();
  114. return tmp;
  115. }
  116. };
  117. PointsIterator begin() {
  118. return PointsIterator(*this, false);
  119. }
  120. PointsIterator end() {
  121. return PointsIterator(*this, true);
  122. }
  123. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement