#include <bits/stdc++.h>
using namespace std;
class Shape
{
public:
static int cnt;
Shape(){cnt++;}
static int getCntOfShapes(){return cnt;}
virtual double getArea()=0;
virtual void print()=0;
};
int Shape ::cnt=0;
class Rectangle :public Shape{
public:
double L,W;
static int cnt;
Rectangle(double a=0,double b=0):L(a),W(b){cnt++;}
static int getCntOfRects(){return cnt;}
double getArea(){return L*W;}
void print(){
cout<<"rect("<<L<<","<<W<<")";
}
};
int Rectangle::cnt=0;
class Circle:public Shape
{
public:
double r;
static int cnt;
Circle(double a=0):r(a){cnt++;}
static int getCntOfCircles(){return cnt;}
double getArea(){return 3.14*r*r;}
void print(){
cout<<"circle("<<r<<")";
}
};
int Circle::cnt=0;
class User
{
public:
string name;
Shape *shape;//一个普通的图形
vector<Shape *> dict1;//图形的列表
static int cnt;
//*****************************
User(string s):name(s){cnt++;}
static int getCntOfUsers(){return cnt;}
void addAShape(int t,double a,double b){
if(t==1){
shape=new Rectangle(a,b);
dict1.push_back(shape);
}
}
void addAShape(int t,double a){
if(t==2){
shape=new Circle (a);
dict1.push_back(shape);
}
}
double getArea(){
double sum=0;
for(int i=0;i<dict1.size();i++)
sum+=dict1[i]->getArea();
return sum;
}
};
int User::cnt=0;
class UserList
{
friend class Shape;
friend class Rectangle;
friend class Circle ;
friend class User;
public:
vector <User *> dict2;
User *user;
Shape *shape;
//******************************
void showTotalArea(string s){
bool ok=false;
for(int i=0;i<dict2.size();i++){
if(dict2[i]->name==s) {
cout<<dict2[i]->getArea()<<endl;ok=true;
}
}
if(ok==false) cout<<"The user "<<s<<" doesn't exist."<<endl;
}
friend istream& operator >>(istream & is,UserList & p){
int n;
is>>n;
for(int i=0;i<n;i++){
int M,t;string s;
is>>s>>M;
p.user = new User(s);//创建一个用户
for(int j=0;j<M;j++){
is>>t;
if(t==1){
double a,b;
is>>a>>b;
p.shape=new Rectangle(a,b);
}
else {
double a;
is>>a;
p.shape=new Circle(a);
}
p.user->dict1.push_back(p.shape);
}
p.dict2.push_back(p.user);
}
return is;
}
friend ostream &operator<<(ostream & os,UserList p)
{
for(int i=0;i<p.dict2.size();i++){
User* U=p.dict2[i];//取出一个用户 U
cout<<U->name<<" : ";//输出用户的名字
vector <Shape *> S=U->dict1;//取出用户的图形列表
for(int j=0;j<S.size();j++){
if(j!=0) cout<<",";
Shape *SS=S[j];//取出一个图形
SS->print();
}
cout<<endl;
}
return os;
}
};
int main()
{
cout<<"In beginning, "<<User::getCntOfUsers()<<" users,";
cout<<Shape::getCntOfShapes()<<" shapes."<<endl;
User tmp("C++");
tmp.addAShape(1, 1, 2);
tmp.addAShape(2, 3);
cout<<tmp.getArea()<<endl;
UserList lst;
string s1;
cin>>lst;
cout<<"Now, "<<User::getCntOfUsers()<<" users,";
cout<<Shape::getCntOfShapes()<<" shapes, including ";
cout<<Circle::getCntOfCircles()<<" circles, and ";
cout<<Rectangle::getCntOfRects()<<" rectangles."<<endl;
cout<<lst;
while(cin>>s1)
{
lst.showTotalArea(s1);
}
return 0;
}
Problem D: 儿童绘画游戏
最新推荐文章于 2020-11-02 19:48:50 发布