9月5日 C++

#include <iostream>
#include <cmath>
using namespace std;

class Graphical
{
    protected:
        float perm;
        float area;
    public:
        void virtual Perimeter()=0;
        void virtual extent()=0;
        virtual ~Graphical(){}
};
                                                                    
class Rectangle:public Graphical
{
    private:
        float length;
        float wide;
    public:
    Rectangle(){}
    Rectangle(float l,float w):length(l),wide(w)
    {}
        void Perimeter() override
    {
        perm=2*(length+wide);
        cout << "Rectangle Perimeter=" << perm << endl;
    }
        void extent() override
        {
            area=length*wide;
            cout << "Rectangle extent=" << area << endl;
        }
};
class Circle:public Graphical
{
    private:
        float radius;
    public:
        Circle(){}
        Circle(float r):radius(r)
        {}
        void Perimeter() override
    {
        perm=3.14*2*radius;
        cout << "Circle Perimeter=" << perm << endl;
    }
        void extent() override
        {
            area=3.41*(radius*radius);
            cout << "Circle extent=" << area << endl;
        }
};
class Triangle:public Graphical
{
    private:
        int a;
        int b;
        int c;
    public:
        Triangle(){}
        Triangle(int a,int b,int c):a(a),b(b),c(c)
        {}
        void Perimeter() override
    {
        perm=a+b+c;
        cout << "Triangle Perimeter=" << perm << endl;
    }
        void extent() override
        {
            float p;
            p=(a+b+c)/2;
            area=sqrt(p*(p-a)*(p-b)*(p-c));
                        cout << "Triangle extent=" << area << endl;
        }

};

void output(Graphical &p)
{
    p.Perimeter();
    p.extent();
}

int main(int argc, const char *argv[])
{
    cout << "1:矩形 2:圆形 3:三角形\n";
        int a,b,c,d;
        float length,wide;
        float radius;
    cin>>d;
    if(d==1)
    {
        cout << "输入矩形的长和宽\n";
        cin>>length;
        cin>>wide;
        Rectangle r(length,wide);
        output(r);
    }
    else if(d==2)
    {
        cout << "输入圆形的半径\n";
        cin>>radius;
        Circle e(radius);
        output(e);
    }
    else if(d==3)
    {
        cout << "输入三角形的长宽高\n";
        cin>>a;
        cin>>b;
        cin>>c;
        if( (a+b)>c && (a+c)>b && (b+c)>a )
        {
            Triangle t(a,b,c);
            output(t);
        }
        else
            cout << "不是三角形\n";
    }
    return 0;
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值