Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- double input(istream& inp, const char* msg) {
- double value{};
- bool flag{};
- do {
- flag = false;
- cout << msg;
- inp >> value;
- if (inp.fail()) {
- inp.clear();
- flag = !flag;
- }
- inp.ignore(0x1000, '\n');
- } while (flag);
- return value;
- }
- struct Shape {
- virtual ~Shape() {}
- virtual double area()const = 0;
- protected:
- inline static const auto pi = 3.1415926535897932;
- };
- struct Shape2D : virtual public Shape {
- virtual ~Shape2D() {}
- virtual double perimeter()const = 0;
- };
- struct Shape3D : virtual public Shape {
- virtual ~Shape3D() {}
- virtual double volume()const = 0;
- };
- class Square : virtual public Shape2D {
- public:
- Square() : a(0) {}
- double area()const override {
- return a * a;
- }
- double perimeter()const override {
- return 4.0 * a;
- }
- protected:
- double a;
- private:
- friend istream& operator>>(istream& inp, Square& square) {
- square.a = input(inp, "Сторона квадрата: ");
- return inp;
- }
- friend ostream& operator<<(ostream& out, const Square& square) {
- out << "Площадь: " << square.area() << '\n'
- << "Периметор: " << square.perimeter();
- return out;
- }
- };
- class Rectangle : virtual public Square {
- public:
- Rectangle() : Square(), b(0) {}
- double area()const override {
- return a * b;
- }
- double perimeter()const override {
- return 2.0 * (a + b);
- }
- private:
- double b;
- friend istream& operator>>(istream& inp, Rectangle& rectangle) {
- rectangle.a = input(inp, "Первая сторона прямоугольника: ");
- rectangle.b = input(inp, "Вторая сторона прямоугольника: ");
- return inp;
- }
- friend ostream& operator<<(ostream& out, const Rectangle& rectangle) {
- out << "Площадь: " << rectangle.area() << '\n'
- << "Периметор: " << rectangle.perimeter();
- return out;
- }
- };
- class Circle : virtual public Shape2D {
- public:
- Circle() : r(0) {}
- double area()const override {
- return pi * r * r;
- }
- double perimeter()const override {
- return 2.0 * pi * r;
- }
- protected:
- double r;
- friend istream& operator>>(istream& inp, Circle& circle) {
- circle.r = input(inp, "Радиус окружности: ");
- return inp;
- }
- friend ostream& operator<<(ostream& out, const Circle& circle) {
- out << "Площадь: " << circle.area() << '\n'
- << "Периметор: " << circle.perimeter();
- return out;
- }
- };
- class Cube : virtual public Shape3D {
- public:
- Cube() : a(0) {}
- double area()const override {
- return 6.0 * a * a;
- }
- double volume()const override {
- return a * a * a;
- }
- protected:
- double a;
- friend istream& operator>>(istream& inp, Cube& сube) {
- сube.a = input(inp, "Сторона куба: ");
- return inp;
- }
- friend ostream& operator<<(ostream& out, const Cube& сube) {
- out << "Площадь: " << сube.area() << '\n'
- << "Объём: " << сube.volume();
- return out;
- }
- };
- class Сuboid : virtual public Cube {
- public:
- Сuboid() : Cube(), b(0), c(0) {}
- double area()const override {
- return 2.0 * (a * b + b * c + c * a);
- }
- double volume()const override {
- return a * b * c;
- }
- private:
- double b;
- double c;
- friend istream& operator>>(istream& inp, Сuboid& сuboid) {
- сuboid.a = input(inp, "Первая сторона прямоугольного параллелепипеда: ");
- сuboid.b = input(inp, "Вторая сторона прямоугольного параллелепипеда: ");
- сuboid.c = input(inp, "Третья сторона прямоугольного параллелепипеда: ");
- return inp;
- }
- friend ostream& operator<<(ostream& out, const Сuboid& сuboid) {
- out << "Площадь: " << сuboid.area() << '\n'
- << "Объём: " << сuboid.volume();
- return out;
- }
- };
- class Ball : virtual public Shape3D {
- public:
- Ball() : r(0) {}
- double area()const override {
- return 4.0 * pi * r * r;
- }
- double volume()const override {
- return 4.0 / 3.0 * pi * r * r *r;
- }
- private:
- double r;
- friend istream& operator>>(istream& inp, Ball& ball) {
- ball.r = input(inp, "Радиус шара: ");
- return inp;
- }
- friend ostream& operator<<(ostream& out, const Ball& ball) {
- out << "Площадь: " << ball.area() << '\n'
- << "Объём: " << ball.volume();
- return out;
- }
- };
- class Program {
- public:
- Program() = default;
- void start() {
- while (true) {
- title();
- auto choice = cin.get();
- cin.ignore(0x1000, '\n');
- switch (Choice(choice)) {
- case Choice::Square: execute<Square>(); break;
- case Choice::Rectangle: execute<Rectangle>(); break;
- case Choice::Circle: execute<Circle>(); break;
- case Choice::Cube: execute<Cube>(); break;
- case Choice::Сuboid: execute<Сuboid>(); break;
- case Choice::Ball: execute<Ball>(); break;
- default: puts("Ошибка ввода!");
- }
- puts("");
- }
- }
- private:
- enum class Choice { Square = int('1'), Rectangle, Circle, Cube, Сuboid, Ball };
- void title()const {
- puts("\t\tВыберите тип фигуры:");
- puts("\t1. Квадрат");
- puts("\t2. Прямоугольник");
- puts("\t3. Круг");
- puts("\t4. Куб");
- puts("\t5. Прямоугольный параллелепипед");
- puts("\t6. Шар");
- cout << ">>> ";
- }
- template<typename T>
- void execute()const {
- T value{};
- cin >> value;
- cout << value;
- }
- };
- void config(const streamsize w) {
- system("chcp 1251 > nul");
- cout.setf(ios::fixed);
- cout.precision(w);
- }
- int main() {
- config(3);
- Program program;
- program.start();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement