Advertisement
NickAndNick

Классы геометрических фигур

Jun 7th, 2023 (edited)
1,190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.39 KB | Software | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. double input(istream& inp, const char* msg) {
  6.     double value{};
  7.     bool flag{};
  8.     do {
  9.         flag = false;
  10.         cout << msg;
  11.         inp >> value;
  12.         if (inp.fail()) {
  13.             inp.clear();
  14.             flag = !flag;
  15.         }
  16.         inp.ignore(0x1000, '\n');
  17.     } while (flag);
  18.     return value;
  19. }
  20.  
  21. struct Shape {
  22.     virtual ~Shape() {}
  23.     virtual double area()const = 0;
  24. protected:
  25.     inline static const auto pi = 3.1415926535897932;
  26. };
  27.  
  28. struct Shape2D : virtual public Shape {
  29.     virtual ~Shape2D() {}
  30.     virtual double perimeter()const = 0;
  31. };
  32.  
  33. struct Shape3D : virtual public Shape {
  34.     virtual ~Shape3D() {}
  35.     virtual double volume()const = 0;
  36. };
  37.  
  38. class Square : virtual public Shape2D {
  39. public:
  40.     Square() : a(0) {}
  41.     double area()const override {
  42.         return a * a;
  43.     }
  44.     double perimeter()const override {
  45.         return 4.0 * a;
  46.     }
  47. protected:
  48.     double a;
  49. private:
  50.     friend istream& operator>>(istream& inp, Square& square) {
  51.         square.a = input(inp, "Сторона квадрата: ");
  52.         return inp;
  53.     }
  54.     friend ostream& operator<<(ostream& out, const Square& square) {
  55.         out << "Площадь: " << square.area() << '\n'
  56.             << "Периметор: " << square.perimeter();
  57.         return out;
  58.     }
  59. };
  60.  
  61. class Rectangle : virtual public Square {
  62. public:
  63.     Rectangle() : Square(), b(0) {}
  64.     double area()const override {
  65.         return a * b;
  66.     }
  67.     double perimeter()const override {
  68.         return 2.0 * (a + b);
  69.     }
  70. private:
  71.     double b;
  72.     friend istream& operator>>(istream& inp, Rectangle& rectangle) {
  73.         rectangle.a = input(inp, "Первая сторона прямоугольника: ");
  74.         rectangle.b = input(inp, "Вторая сторона прямоугольника: ");
  75.         return inp;
  76.     }
  77.     friend ostream& operator<<(ostream& out, const Rectangle& rectangle) {
  78.         out << "Площадь: " << rectangle.area() << '\n'
  79.             << "Периметор: " << rectangle.perimeter();
  80.         return out;
  81.     }
  82. };
  83.  
  84. class Circle : virtual public Shape2D {
  85. public:
  86.     Circle() : r(0) {}
  87.     double area()const override {
  88.         return pi * r * r;
  89.     }
  90.     double perimeter()const override {
  91.         return 2.0 * pi * r;
  92.     }
  93. protected:
  94.     double r;
  95.     friend istream& operator>>(istream& inp, Circle& circle) {
  96.         circle.r = input(inp, "Радиус окружности: ");
  97.         return inp;
  98.     }
  99.     friend ostream& operator<<(ostream& out, const Circle& circle) {
  100.         out << "Площадь: " << circle.area() << '\n'
  101.             << "Периметор: " << circle.perimeter();
  102.         return out;
  103.     }
  104. };
  105.  
  106. class Cube : virtual public Shape3D {
  107. public:
  108.     Cube() : a(0) {}
  109.     double area()const override {
  110.         return 6.0 * a * a;
  111.     }
  112.     double volume()const override {
  113.         return a * a * a;
  114.     }
  115. protected:
  116.     double a;
  117.     friend istream& operator>>(istream& inp, Cube& сube) {
  118.         сube.a = input(inp, "Сторона куба: ");
  119.         return inp;
  120.     }
  121.     friend ostream& operator<<(ostream& out, const Cube& сube) {
  122.         out << "Площадь: " << сube.area() << '\n'
  123.             << "Объём: " << сube.volume();
  124.         return out;
  125.     }
  126. };
  127.  
  128. class Сuboid : virtual public Cube {
  129. public:
  130.     Сuboid() : Cube(), b(0), c(0) {}
  131.     double area()const override {
  132.         return 2.0 * (a * b + b * c + c * a);
  133.     }
  134.     double volume()const override {
  135.         return a * b * c;
  136.     }
  137. private:
  138.     double b;
  139.     double c;
  140.     friend istream& operator>>(istream& inp, Сuboid& сuboid) {
  141.         сuboid.a = input(inp, "Первая сторона прямоугольного параллелепипеда: ");
  142.         сuboid.b = input(inp, "Вторая сторона прямоугольного параллелепипеда: ");
  143.         сuboid.c = input(inp, "Третья сторона прямоугольного параллелепипеда: ");
  144.         return inp;
  145.     }
  146.     friend ostream& operator<<(ostream& out, const Сuboid& сuboid) {
  147.         out << "Площадь: " << сuboid.area() << '\n'
  148.             << "Объём: " << сuboid.volume();
  149.         return out;
  150.     }
  151. };
  152.  
  153. class Ball : virtual public Shape3D {
  154. public:
  155.     Ball() : r(0) {}
  156.     double area()const override {
  157.         return 4.0 * pi * r * r;
  158.     }
  159.     double volume()const override {
  160.         return 4.0 / 3.0 * pi * r * r *r;
  161.     }
  162. private:
  163.     double r;
  164.     friend istream& operator>>(istream& inp, Ball& ball) {
  165.         ball.r = input(inp, "Радиус шара: ");
  166.         return inp;
  167.     }
  168.     friend ostream& operator<<(ostream& out, const Ball& ball) {
  169.         out << "Площадь: " << ball.area() << '\n'
  170.             << "Объём: " << ball.volume();
  171.         return out;
  172.     }
  173. };
  174.  
  175. class Program {
  176. public:
  177.     Program() = default;
  178.     void start() {
  179.         while (true) {
  180.             title();
  181.             auto choice = cin.get();
  182.             cin.ignore(0x1000, '\n');
  183.             switch (Choice(choice)) {
  184.                 case Choice::Square: execute<Square>(); break;
  185.                 case Choice::Rectangle: execute<Rectangle>(); break;
  186.                 case Choice::Circle: execute<Circle>(); break;
  187.                 case Choice::Cube: execute<Cube>(); break;
  188.                 case Choice::Сuboid: execute<Сuboid>(); break;
  189.                 case Choice::Ball: execute<Ball>(); break;
  190.                 default: puts("Ошибка ввода!");
  191.             }
  192.             puts("");
  193.         }
  194.     }
  195. private:
  196.     enum class Choice { Square = int('1'), Rectangle, Circle, Cube, Сuboid, Ball };
  197.     void title()const {
  198.         puts("\t\tВыберите тип фигуры:");
  199.         puts("\t1. Квадрат");
  200.         puts("\t2. Прямоугольник");
  201.         puts("\t3. Круг");
  202.         puts("\t4. Куб");
  203.         puts("\t5. Прямоугольный параллелепипед");
  204.         puts("\t6. Шар");
  205.         cout << ">>> ";
  206.     }
  207.     template<typename T>
  208.     void execute()const {
  209.         T value{};
  210.         cin >> value;
  211.         cout << value;
  212.     }
  213. };
  214.  
  215. void config(const streamsize w) {
  216.     system("chcp 1251 > nul");
  217.     cout.setf(ios::fixed);
  218.     cout.precision(w);
  219. }
  220.  
  221. int main() {
  222.     config(3);
  223.     Program program;
  224.     program.start();
  225. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement