#include <iostream>
using namespace std;
class Shape
{
private:
public:
virtual~Shape() {}
virtual double Area()=0;
virtual void Show()=0;
friend bool operator==(Shape&A,Shape&B)
{
return A.Area()==B.Area();
}
friend bool operator>(Shape&A,Shape&B)
{
return A.Area()>B.Area();
}
friend bool operator<(Shape&A,Shape&B)
{
return A.Area()<B.Area();
}
protected:
double area;
};
class Rectangle:public Shape
{
private:
public:
Rectangle(double width,double height):area(width*height),rectWidth(width),rectHeight(height) {}
double Area()
{
return area;
}
void Show()
{
cout<<"W: "<<rectWidth<<"; H:"<<rectHeight<<"; Area: "<<Area()<<endl;
}
protected:
double Shape::area;
double rectWidth,rectHeight;
};
class Ellipse:public Shape
{
private:
static const double PI=3.1415926;
public:
Ellipse(double width,double height):area(width*height/4*PI),rectWidth(width),rectHeight(height)
{
}
double Area()
{
return area;
}
void Show()
{
cout<<"W: "<<rectWidth<<"; H:"<<rectHeight<<"; Area: "<<Area()<<endl;
}
protected:
double Shape::area;
double rectWidth,rectHeight;
};
int split(Shape*p[],int l,int h)
{
Shape*t=p[l];
while(l<h)
if(p[h]->Area()>t->Area())
{
p[l++]=p[h];
while(l<h)
if(t->Area()>p[l]->Area())
{
p[h--]=p[l];
break;
}
else l++;
}
else h--;
p[h]=t;
return h;
}
void Sort(Shape*p[],int l,int h)
{
if(l<h)
{
int m=split(p,l,h);
Sort(p,l,m-1);
Sort(p,m+1,h);
}
}
int main()
{
int n;
double w,h;
Rectangle t(w,h);
char ShapeType;
cin >> n;
Shape*p[n];
for(int i=0; i<n; ++i)
{
cin >> ShapeType >> w >> h;
if(ShapeType=='R') p[i]=new Rectangle(w,h);
else p[i]=new Ellipse(w,h);
}
//排序前
for(int i=0; i<n; ++i)
p[i]->Show();
//面积相等
for(int i=0; i<n; ++i)
{
for(int j=i+1; j<n; ++j)
{
if(p[i]->Area()==p[j]->Area())
cout << "Area of Shape[" <<i<< "] is equal to Shape[" <<j<< "]" <<endl;
}
}
Sort(p,0,n-1);
//排序后
for(int i=0; i<n; ++i)
p[i]->Show();
for(int i=0; i<n; ++i)
delete p[i];
return 0;
}
评论0