Problem D: 儿童绘画游戏

#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;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值