Advertisement
Trawka011

Untitled

Mar 15th, 2023
735
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.32 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. class Figure {
  6. public:
  7.     virtual double area() = 0;
  8.     virtual double perimeter() = 0;    
  9. };
  10.  
  11. class Rectangle : public Figure{
  12. protected:
  13.     int w;              // Ширина
  14.     int h;              // Высота
  15.  
  16. public:
  17.     Rectangle(int w = 0, int h = 0) {
  18.         this->w = w;
  19.         this->h = h;
  20.     }
  21.  
  22.     int width() {
  23.         return w;
  24.     }
  25.     int height() {
  26.         return h;
  27.     }
  28.     double area() {
  29.         return w * h;
  30.     }
  31.     double perimeter() {
  32.         return (w + h) * 2;
  33.     }
  34.     friend istream& operator>> (istream&, Rectangle&);
  35.     friend ostream& operator<< (ostream&, const Rectangle&);
  36. };
  37.  
  38. istream& operator>>(istream& in, Rectangle& a) {
  39.     in >> a.w >> a.h;
  40.     return in;
  41. }
  42.  
  43. ostream& operator<<(ostream& on, const Rectangle& a) {
  44.     on << a.w << " " << a.h;
  45.     return on;
  46. }
  47.  
  48. class Circle : public Figure {
  49. protected:
  50.     const double p = 3.14159265;
  51.     int r;
  52. public:
  53.     Circle(int r = 0) {
  54.         this->r = r;
  55.     }
  56.     int radius() {
  57.         return r;
  58.     }
  59.     double area() {
  60.         return p * r * r;
  61.     }
  62.     double perimeter() {
  63.         return 2 * p * r ;
  64.     }
  65.  
  66.     friend istream& operator>> (istream&, Circle&);
  67.     friend ostream& operator<< (ostream&, const Circle&);
  68. };
  69.  
  70. istream& operator>>(istream& in, Circle& a) {
  71.     in >> a.r;
  72.     return in;
  73. }
  74.  
  75. ostream& operator<<(ostream& on, const Circle& a) {
  76.     on << a.r;
  77.     return on;
  78. }
  79.  
  80. double array_area(Figure** array, int n) {
  81.     double sum = 0;
  82.     for (int i = 0; i < n; i++) {
  83.         sum += array[i]->area();
  84.     }
  85.     return sum;
  86. }
  87. double array_perimeter(Figure** array, int n) {
  88.     double sum = 0;
  89.     for (int i = 0; i < n; i++) {
  90.         sum += array[i]->perimeter();
  91.     }
  92.     return sum;
  93. }
  94.  
  95. int main() {
  96.     int n;
  97.     cin >> n;
  98.     string tip;
  99.     Figure** a = new Figure * [n];
  100.     for (int i = 0; i < n; i++){
  101.         cin >> tip;
  102.         if (tip == "Rectangle"){
  103.             int w, h;
  104.             cin >> w >> h;
  105.             a[i] = new Rectangle(w,h);
  106.            
  107.         }
  108.         else{
  109.             int r;
  110.             cin >> r;
  111.             a[i] = new Circle(r);
  112.            
  113.         }
  114.     }
  115.     cout << array_area(a,n) << " " << array_perimeter(a,n);
  116.     return 0;
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement