卡码网编程基础课 | 图形的面积

问题描述

题目描述

考虑一个简单的图形类层次结构,包括基类 Shape 和两个派生类 Rectangle 和 Circle。每个类都有一个用于计算面积的方法。你的任务是编写一个程序,根据输入数据创建一个图形对象,然后计算并输出其面积。

输入描述

输入包括多行,每行包含一个图形的描述。 描述的第一个单词是图形类型("rectangle"或"circle"),然后是与该图形相关的参数。 对于矩形,参数是宽度和高度,对于圆形,参数是半径。输入以单词"end"结束。

输出描述

对于每个图形描述,输出其类型和面积。使用两位小数点精度输出面积。

输入示例
rectangle 5 3
circle 2
end
输出示例
Rectangle area: 15.00
Circle area: 12.56
提示信息

长方形面积的计算 = 长 * 宽

圆形面积的计算 = 3.14 * 半径 * 半径

python解法

# Shape类
class Shape:
    def __init__(self, shape_type):
        self.type = shape_type
        
    def calculate_area(self):
        pass
    
# Rectangle类,包含width和height,并计算面积的方法
class Rectangle(Shape):
    def __init__(self, width, height):
        super().__init__("Rectangle")
        self.width = width
        self.height = height
        
    def calculate_area(self):
        return self.width * self.height
        
# Circle类,包含radius,计算面积的方法
class Circle(Shape):
    def __init__(self, radius):
        super().__init__("Circle")
        self.radius = radius
        
    def calculate_area(self):
        return 3.14 * self.radius * self.radius
        
        
shapes = []

while True:
    data = input().split()
    shape_type = data[0]
    # 处理输出
    if shape_type == "end":
        break
    
    if shape_type == "rectangle":
        # 获取输入的width和height,将之转换成整数
        width, height = int(data[1]), int(data[2])
        # 新建一个对象,append到列表中
        shapes.append(Rectangle(width, height))
        
    if shape_type == "circle":
        # 获取输入的radius,将之转换成整数
        radius = int(data[1])
        # 新建一个对象,append到列表中
        shapes.append(Circle(radius))
    
for shape in shapes:
    # 不同类别的对象调用同一个方法,有不同的处理逻辑
    print("{} area: {:.2f}".format(shape.type, shape.calculate_area()))

C++解法

#include <iostream>
#include <vector>
#include <string>
// 引入iomanip库文件,用于控制输出格式
#include <iomanip>

// Shape类
class Shape{
public:
        // 定义计算面积和获取类型的纯虚函数
        virtual double CalculateArea() const = 0;
        virtual std::string GetType() const = 0;
};

// Rectangle类
class Rectangle : public Shape{
// 属性: 宽度和高度
private:
    int width;
    int height;

public:
    // 初始化参数列表
    Rectangle(int width, int height) : width(width), height(height){}
    // 计算长方形面积,将整数转换为浮点数 
    double CalculateArea() const override{
        return static_cast<double>(width*height);
    }
    // 获取图形的形状
    std::string GetType() const override{
        return "Rectangle";
    }
};


class Circle : public Shape{
// 属性: 半径
private:
    int radius;
    
public:
    // 初始化参数列表
    Circle(int radius) : radius(radius) {}
    // 计算圆的面积
    double CalculateArea() const override {
        return 3.14 * radius * radius;
    }
    // 获取图形的形状
    std::string GetType() const override{
        return "Circle";
    }
};


int main(){
    // 定义一个容器,容纳shape对象
    std::vector<Shape*> shapes;
    
    while (true){
        // 获取输入的type类型
        std::string type;
        std::cin >> type;
        
        if (type == "end"){
            break;
        }
        
        if (type == "rectangle"){
            // 获取输入的宽度和高度
            int width, height;
            std::cin >> width >> height;
            // 新建Rectangle对象
            shapes.push_back(new Rectangle(width, height));
        }
        else if (type == "circle"){
            // 获取输入的半径
            int radius;
            std::cin >> radius;
            // 新建Radius对象
            shapes.push_back(new Circle(radius));
        }
    }
    
    // 输出结果,控制小数位数为两位
    for (auto shape : shapes){
        // shape对象调用同一个方法,有不同的处理逻辑
        std::cout << shape->GetType() << " area: " <<std::fixed << std::setprecision(2) <<  shape->CalculateArea() << std::endl;
    }
    
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值