长方形,圆与三角形

#include<iostream>

#include<cmath>

#include<iomanip>

using namespace std;

class Shape

{

public:

virtual double area() = 0;

virtual double perimeter() = 0;

};

class Rect:public Shape

{

private:

int chang,kuan;

public:

Rect(int x,int y):chang(x),kuan(y){}

double area()

{

return chang*kuan;

}

double perimeter()

{

return 2*(chang+kuan);

}

};

class Triangle:public Shape

{

private:

double X,Y,Z;

public:

Triangle(double x,double y,double z):X(x),Y(y),Z(z)

{}

double area()

{

double p=(X+Y+Z)/2;

return sqrt(p)*(sqrt(p-X)*sqrt(p-Y)*sqrt(p-Z));

}

double perimeter()

{

return X+Y+Z;

}

};

class Circle:public Shape

{

private:

int R;

public:

Circle(int r):R(r){}

double area()

{

return 3.1415926*R*R;

}

double perimeter()

{

return 2*3.1415926*R;

}

};

int main()

{

int n,i,x1,x2,x3,x4,x5,x6;

double y1,y2,y3;

string a;

cin>>n;

for(i=0;i<n;i++)

{

cin>>a;

if(a=="Rect"){cin>>x1>>x2>>x3>>x4;y1=(x3-x1<0)?x1-x3:x3-x1;y2=(x4-x2<0)?x2-x4:x4-x2;

Rect x(y1,y2);

cout<<fixed<<setprecision(5)<<x.area()<<" "<<x.perimeter()<<'\n';}

else if(a=="Circle"){cin>>x1>>x2>>x3;

Circle x(x3);

cout<<fixed<<setprecision(5)<<x.area()<<" "<<x.perimeter()<<'\n';}

else if(a=="Triangle"){cin>>x1>>x2>>x3>>x4>>x5>>x6;

y1=sqrt((x3-x1)*(x3-x1)+(x4-x2)*(x4-x2)),y2=sqrt((x5-x3)*(x5-x3)+(x6-x4)*(x6-x4)),y3=sqrt((x5-x1)*(x5-x1)+(x6-x2)*(x6-x2));

Triangle x(y1,y2,y3);

cout<<fixed<<setprecision(5)<<x.area()<<" "<<x.perimeter()<<'\n';}

}

}

全部评论

相关推荐

华勤技术 结构工程师 11K×15薪 本科211
看看我说不定有惊喜:上海这个钱不好生存啊
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务