Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #include <vector>
- template<typename T, typename M>
- struct Pair{
- T first;
- M second;
- Pair() = default;
- Pair(T a, T b): first(a), second(b){}
- };
- template<typename T, typename M>
- inline std::ostream& operator<<(std::ostream& os, const Pair<T,M>& obj){ return os <<"x : "<< obj.first << " y : "<< obj.second; }
- template<typename T, typename M>
- inline std::istream& operator>>(std::istream& is, Pair<T,M>& obj){ return is >> obj.first >> obj.second; }
- class Shape{
- public:
- // virtual ~Shape() = default;
- virtual double area() const = 0;
- virtual Pair<double, double> center() const = 0;
- friend inline std::ostream& operator<<(std::ostream& os, const Shape& obj) { return obj.output(os); }
- friend inline std::istream& operator>>(std::istream& is, Shape& obj){ return obj.input(is); }
- protected:
- virtual std::istream& input(std::istream& is) = 0;
- virtual std::ostream& output(std::ostream& os) const = 0;
- 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));}
- 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);}
- };
- class Quadrangle: public Shape{
- public:
- // friend Square;
- double area() const {} // можно определить для общего случая четырехугольника
- Pair<double, double> center() const {} // можно определить для общего случая четырехугольника
- protected:
- std::istream& input(std::istream& is) { return is >> a[0] >> a[1] >> a[2] >> a[3]; }
- std::ostream& output(std::ostream& os) const{ return os << a[0] << " " << a[1] << " " << a[2] << " " << a[3] << "\n"; }
- Pair<double,double> a[4];
- };
- class Rhombus:public Quadrangle{
- public:
- // Rhombus() = default;
- double area() const { return (edge_length(a[0], a[2]) * edge_length(a[1], a[3]))/2; }
- Pair<double, double> center() const { return edge_center(a[0], a[2]); }
- };
- class Square: public Rhombus{
- public:
- double area() const { return std::pow(edge_length(a[0], a[1]), 2); }
- };
- class Trapezoid:public Quadrangle{
- public:
- double area() const {} //а тут нужно написать как вычислеть площадь и центр
- Pair<double, double> center() const {}
- };
- double sum(std::vector<Shape*> v){
- double res= 0;
- for (auto i = v.begin(); i!= v.end(); ++i){
- res += (*i)->area();
- }
- return res;
- }
- void remove_elem(int index, std::vector<Shape*>& v){
- v.erase(v.begin() + index);
- }
- void print(std::vector<Shape*>& v){
- for (auto i = v.begin(); i!= v.end(); ++i){
- std::cout << (**i) << (*i)->center()<< " " << (*i)->area()<< "\n";
- }
- }
- int main(){
- std::vector<Shape*> v;
- Rhombus* r1 = new Rhombus(), *r2 = new Rhombus();
- Square* s1 = new Square(), *s2 = new Square();
- std::cin >> (*r1) >> (*r2) >> (*s1) >> (*s2);
- v.push_back(r1);
- v.push_back(r2);
- v.push_back(s1);
- v.push_back(s2);
- print(v);
- std::cout << sum(v)<< "\n";
- remove_elem(1, v);
- print(v);
- return 0;
- }
- /*
- 2
- 4
- -3
- 7
- -6
- 6
- -1
- 3
- 2
- 4
- -3
- 7
- -6
- 6
- -1
- 3
- 2
- 4
- -3
- 7
- -6
- 6
- -1
- 3
- 2
- 4
- -3
- 7
- -6
- 6
- -1
- 3
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement