代码如下
#include <iostream>
#include <cstdlib>
#include <math.h>
using namespace std;
class Shape{
public:
virtual double area() = 0;
virtual void Show() = 0;
virtual ~Shape() { }
};
class R:public Shape{
protected:
double rw;
double rh;
public:
R(double i,double j):rw(i),rh(j) {}
void Show();
double area();
~R() { }
};
class E:public Shape{
protected:
double rw,rh;
public:
E(double i,double j):rw(i),rh(j) { }
void Show();
double area();
~E() { }
};
void R::Show()
{
cout<<"W: "<<rw<<"; H:"<<rh<<"; Area: "<<area()<<endl;
}
void E::Show()
{
cout<<"W: "<<rw<<"; H:"<<rh<<"; Area: "<<area()<<endl;
}
double R::area()
{
return rw*rh;
}
double E::area()
{
return 0.25*3.1415926*rw*rh;
}
void f(Shape *r)
{
r->Show();
}
double g(Shape *r)
{
return r->area() ;
}
void swap(Shape **a,int i,int j)
{
Shape *p=a[i];
a[i]=a[j];
a[j]=p;
}
int main()
{
int n,i,j,k;
char ch;
cin>>n;
Shape *a[n];
for(i = 0;i < n;i++)
{
cin>>ch>>j>>k;
if(ch == 'R')
{
a[i] = new R(j,k);
}
else if(ch == 'E')
{
a[i] = new E(j,k);
}
f(a[i]);
}//输入部分
for(i = 0;i < n - 1;i++)
{
for(j = i + 1;j < n;j++)
{
if(fabs(g(a[i]) - g(a[j])) < 1e-6)
cout<<"Area of Shape["<<i<<"] is equal to Shape["<<j<<"]"<<endl;
}
}//输出相同的
for(i = 0;i < n-1;i++)
{
for(j = 0;j < n - 1 - i;j++)
{
if(g(a[j])<g(a[j+1]))
{
Shape *p=a[j];
a[j]=a[j+1];
a[j+1]=p;
}
}
}//排序
for(i = 0;i<n-1;i++)
{
if(fabs(g(a[i]) - g(a[i+1])) < 1e-6)
{
swap(a,i,i+1);
}
}//调整结果
for(i = 0;i < n;i++)
{
f(a[i]);
}//输出结果
for(i = 0;i < n;i++)
delete a[i];
return 0;
}