1

Нужно переписать код так, чтобы на выходе было то же самое, но реализовано через классы. 3 Класса : IN(IN) / CALC(Square(), Rectangle(), Tringle()) / OUT(Printer()) Как-то так

import math


def IN():
    x = input('Enter the sides of the figures:\n').split(' ')
    for i, elem in enumerate(x):
        x[i] = int(elem)
    if len(x) == 1:
        Square(x)
    elif len(x) == 2:
        Rectangle(x)
    elif len(x) == 3:
        Tringle(x)
    else:
        print('WTF!')


def Square(x):
    P = 2 * (x[0] * 2)
    S = x[0] ** 2
    print('P: {}\nS: {}'.format(P, S))


def Rectangle(x):
    P = 2 * (x[0] + x[1])
    S = x[0] * x[1]
    print('P: {}\nS: {}'.format(P, S))


def Tringle(x):
    P = x[0] + x[1] + x[2]
    p = P / 2
    S = float((p * (p - x[0]) * (p - x[1]) * (p - x[2])) ** 0.5)
    S = math.ceil(S)
    print('P: {}\nS: {}'.format(P, S))




IN()

1 ответ 1

2

Вот так, например:

import math


class Geometry:

    def __init__(self, sides):
        self.sides = sides

    def perimeter(self):
        raise NotImplementedError()

    def area(self):
        raise NotImplementedError()

    def __str__(self):
        return 'P: {}\nS: {}'.format(self.perimeter(), self.area())


class Square(Geometry):

    def perimeter(self):
        return self.sides[0] * 4

    def area(self):
        return self.sides[0] * self.sides[0]


class Rectangle(Geometry):

    def perimeter(self):
        return (self.sides[0] + self.sides[1]) * 2

    def area(self):
        return self.sides[0] * self.sides[1]


class Triangle(Geometry):

    def perimeter(self):
        return self.sides[0] + self.sides[1] + self.sides[2]

    def area(self):
        hp = self.perimeter() / 2
        area = math.sqrt(hp * (hp - self.sides[0]) * (hp - self.sides[1]) * (hp - self.sides[2]))
        return math.ceil(area)


if __name__ == '__main__':
    x = input('Enter the sides of the figures:\n').split(' ')
    for i, elem in enumerate(x):
        x[i] = int(elem)
    if len(x) == 1:
        print(Square(x))
    elif len(x) == 2:
        print(Rectangle(x))
    elif len(x) == 3:
        print(Triangle(x))
    else:
        print('WTF!')
1
  • Спасибо, хорошее решение ! 8 ноя 2018 в 19:17

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.