小九久久 2021-12-31 01:17 采纳率: 100%
浏览 16
已结题

#不知道是什么问题,这怎么改

img


#include <iostream>
using namespace std;

class Shape 
{
public:
    virtual float getArea() = 0;
};

class Circle : public Shape
{
public:
    Circle(float r) { radius = r; }
    void setRadius() 
    { 
        cout << "请输入圆的半径=" << endl;
        cin >> radius;
    }
    float getArea()
    {
        return 3.14 * radius * radius;
    }

private:
    float radius;
};

class Rectangle : public Shape
{
public:
    Rectangle(float l,float w) { length = l, width = w; }
    void setLength() 
    {
        cout << "请输入长方形的长=" << endl;
        cin >> length;
            cout << "请输入长方形的宽=" << endl;
        cin >> width;
    }
    float getArea()
    {
        return length*width;
    }
private:
    float length;
    float width;
};

class Tringle : public Shape
{
    Tringle(float a, float b,float c); 
public:
    void setA()
    {
        cout << "请输入三角形的边长a=" << endl;
        cin >> a;
        cout << "请输入三角形的边长b=" << endl;
        cin >> b;
        cout << "请输入三角形的边长c=" << endl;
        cin >> c;
    }
    float getArea()
    {
        return sqrt(0.5 * (a + b + c) * (0.5 * (a + b + c) - a) * (0.5 * (a + b + c) - b) * (0.5 * (a + b + c) - c));
    }
private:
    float a;
    float b;
    float c;
};

int main()
{
    Shape* s[3]; // 定义一个指针数组

    s[0] = new Circle();
    s[1] = new Rectangle();
    s[2] = new Tringle();

    int i;
    for (i = 0; i < 3; i++) 
    {
        s[i]->getArea();
    }

    for (i = 0; i < 3; i++) // 用完后要释放堆上创建的对象
    {
        delete s[i];
    }
    return 0;
}
  • 写回答

1条回答 默认 最新

  • _GX_ 2021-12-31 04:22
    关注

    帮你修改好了

    #define _USE_MATH_DEFINES
    
    #include <iostream>
    #include <cmath>
    #include <stdexcept>
    
    class Shape
    {
    public:
        virtual ~Shape() {}
        virtual float getArea() const = 0;
    };
    
    class Circle : public Shape
    {
    public:
        Circle(float radius)
        {
            setRadius(radius);
        }
    
        float getRadius() const { return _radius; }
    
        void setRadius(float radius)
        {
            if (radius <= 0.0f)
                throw std::invalid_argument("invalid circle");
            _radius = radius;
        }
    
        float getArea() const
        {
            return M_PI * _radius * _radius;
        }
    
    private:
        float _radius;
    };
    
    class Rectangle : public Shape
    {
    public:
        Rectangle(float length, float width)
        {
            setLength(length);
            setWidth(width);
        }
    
        float getLength() const { return _length; }
    
        void setLength(float length)
        {
            if (length <= 0.0f)
                throw std::invalid_argument("invalid rectangle");
            _length = length;
        }
    
        float getWidth() const { return _width; }
    
        void setWidth(float width)
        {
            if (width <= 0.0f)
                throw std::invalid_argument("invalid rectangle");
            _width = width;
        }
    
        float getArea() const
        {
            return _length * _width;
        }
    
    private:
        float _length;
        float _width;
    };
    
    class Tringle : public Shape
    {
    public:
        Tringle(float a, float b, float c)
        {
            set(a, b, c);
        }
    
        float getA() const { return _a; }
        float getB() const { return _b; }
        float getC() const { return _c; }
    
        void set(float a, float b, float c)
        {
            if (a <= 0.0f || b <= 0.0f || c <= 0.0f || (a + b) <= c || (a + c) <= b || (b + c) <= a)
                throw std::invalid_argument("invalid triangle");
            _a = a;
            _b = b;
            _c = c;
        }
    
        float getArea() const
        {
            float s = (_a + _b + _c) / 2.0f;
            return sqrt(s * (s - _a) * (s - _b) * (s - _c));
        }
    
    private:
        float _a;
        float _b;
        float _c;
    };
    
    int main()
    {
        try
        {
            Shape *s[3];
    
            float r;
            std::cout << "请输入圆的半径=";
            std::cin >> r;
            s[0] = new Circle(r);
    
            float l, w;
            std::cout << "请输入长方形的长=";
            std::cin >> l;
            std::cout << "请输入长方形的宽=";
            std::cin >> w;
            s[1] = new Rectangle(l, w);
    
            float a, b, c;
            std::cout << "请输入三角形的边长a=";
            std::cin >> a;
            std::cout << "请输入三角形的边长b=";
            std::cin >> b;
            std::cout << "请输入三角形的边长c=";
            std::cin >> c;
            s[2] = new Tringle(a, b, c);
    
            for (int i = 0; i < 3; i++)
                std::cout << "图形" << i + 1 << "的面积=" << s[i]->getArea() << '\n';
    
            for (int i = 0; i < 3; i++)
                delete s[i];
        }
        catch (const std::exception &e)
        {
            std::cerr << e.what();
        }
        return 0;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 1月8日
  • 已采纳回答 12月31日
  • 创建了问题 12月31日

悬赏问题

  • ¥200 如何使用postGis实现最短领规划?
  • ¥15 pyinstaller打包错误
  • ¥20 cesm的气溶胶排放文件
  • ¥15 逐月累计,月份不连续,补齐月份
  • ¥15 应用简单的Python代码完成一个学生成绩管理系统
  • ¥15 用matlab求微分方程初值问题
  • ¥15 vscode下编写第三方库opencv与pcl代码时没有代码提示
  • ¥15 能够跑通不报错,如何解决?(标签-matlab)
  • ¥15 MOS在RDS较大,频率高时开关波形异常
  • ¥15 SCENIC分析报错求解答