Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.31 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <vector>
  4.  
  5. template<typename T, typename M>
  6. struct Pair{
  7. T first;
  8. M second;
  9. Pair() = default;
  10. Pair(T a, T b): first(a), second(b){}
  11. };
  12.  
  13. template<typename T, typename M>
  14. inline std::ostream& operator<<(std::ostream& os, const Pair<T,M>& obj){ return os <<"x : "<< obj.first << " y : "<< obj.second; }
  15. template<typename T, typename M>
  16. inline std::istream& operator>>(std::istream& is, Pair<T,M>& obj){ return is >> obj.first >> obj.second; }
  17.  
  18. class Shape{
  19. public:
  20. // virtual ~Shape() = default;
  21. virtual double area() const = 0;
  22. virtual Pair<double, double> center() const = 0;
  23. friend inline std::ostream& operator<<(std::ostream& os, const Shape& obj) { return obj.output(os); }
  24. friend inline std::istream& operator>>(std::istream& is, Shape& obj){ return obj.input(is); }
  25.  
  26. protected:
  27. virtual std::istream& input(std::istream& is) = 0;
  28. virtual std::ostream& output(std::ostream& os) const = 0;
  29. double edge_length(const Pair<double, double>& a, const Pair<double, double>& b) const { return std::sqrt(std::pow(b.first - a.first, 2) + std::pow(b.second - a.second, 2));}
  30. Pair<double, double> edge_center(const Pair<double, double>& a, const Pair<double, double>& b) const { return Pair<double, double>((a.first + b.first)/2, (a.second + b.second)/2);}
  31. };
  32.  
  33. class Quadrangle: public Shape{
  34. public:
  35. // friend Square;
  36. double area() const {} // можно определить для общего случая четырехугольника
  37. Pair<double, double> center() const {} // можно определить для общего случая четырехугольника
  38.  
  39. protected:
  40. std::istream& input(std::istream& is) { return is >> a[0] >> a[1] >> a[2] >> a[3]; }
  41. std::ostream& output(std::ostream& os) const{ return os << a[0] << " " << a[1] << " " << a[2] << " " << a[3] << "\n"; }
  42. Pair<double,double> a[4];
  43.  
  44. };
  45.  
  46. class Rhombus:public Quadrangle{
  47. public:
  48. // Rhombus() = default;
  49. double area() const { return (edge_length(a[0], a[2]) * edge_length(a[1], a[3]))/2; }
  50. Pair<double, double> center() const { return edge_center(a[0], a[2]); }
  51. };
  52.  
  53. class Square: public Rhombus{
  54. public:
  55. double area() const { return std::pow(edge_length(a[0], a[1]), 2); }
  56. };
  57.  
  58. class Trapezoid:public Quadrangle{
  59. public:
  60. double area() const {} //а тут нужно написать как вычислеть площадь и центр
  61. Pair<double, double> center() const {}
  62. };
  63.  
  64. double sum(std::vector<Shape*> v){
  65. double res= 0;
  66. for (auto i = v.begin(); i!= v.end(); ++i){
  67. res += (*i)->area();
  68. }
  69. return res;
  70. }
  71.  
  72. void remove_elem(int index, std::vector<Shape*>& v){
  73. v.erase(v.begin() + index);
  74. }
  75.  
  76. void print(std::vector<Shape*>& v){
  77. for (auto i = v.begin(); i!= v.end(); ++i){
  78. std::cout << (**i) << (*i)->center()<< " " << (*i)->area()<< "\n";
  79. }
  80. }
  81.  
  82. int main(){
  83. std::vector<Shape*> v;
  84.  
  85. Rhombus* r1 = new Rhombus(), *r2 = new Rhombus();
  86. Square* s1 = new Square(), *s2 = new Square();
  87. std::cin >> (*r1) >> (*r2) >> (*s1) >> (*s2);
  88. v.push_back(r1);
  89. v.push_back(r2);
  90. v.push_back(s1);
  91. v.push_back(s2);
  92.  
  93. print(v);
  94.  
  95. std::cout << sum(v)<< "\n";
  96.  
  97. remove_elem(1, v);
  98.  
  99. print(v);
  100.  
  101. return 0;
  102. }
  103.  
  104. /*
  105. 2
  106. 4
  107. -3
  108. 7
  109. -6
  110. 6
  111. -1
  112. 3
  113. 2
  114. 4
  115. -3
  116. 7
  117. -6
  118. 6
  119. -1
  120. 3
  121. 2
  122. 4
  123. -3
  124. 7
  125. -6
  126. 6
  127. -1
  128. 3
  129. 2
  130. 4
  131. -3
  132. 7
  133. -6
  134. 6
  135. -1
  136. 3
  137. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement