实验12:Problem J: 动物爱好者

  1. #define null ""是用来将字符串清空的
  2. #define none -1是用来当不存在这种动物时,返回-1。

其实这种做法有点多余,不过好理解一些。

 

Home Web Board ProblemSet Standing Status Statistics
 
Problem J: 动物爱好者

Problem J: 动物爱好者

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 509  Solved: 376
[Submit][Status][Web Board]

Description

某人是一个狂热的动物爱好者,豢养了大量的各种动物。现在请定义两个类:

1. Animal类:

(1)string name和int  num属性表示该种动物的名称和数量。

(2)无参构造函数。

(3)void setAnimal(string,int)方法,用于设置一个动物的相关属性。

(4)int getNum() const和string getName() const方法用于获得该动物的数量和名称。

(5)重载的赋值运算符=。

2. AnimalList类:

(1)Animal *animalList和int numOfAnimal属性,用于表示该人豢养的所有动物的列表以及动物的种类数。

(2)构造函数AnimalList(Animal *animals, int n)。

(3)重载的下标运算符[],int operator[](string name),用于返回参数name指定名称的动物的数量,当不存在这种动物时,返回-1。

 

Input

第一行M>0表示有M种动物,之后有M行,每行第一个字符串表示动物名称,第二个整数是该种动物的数量。

之后一个N>0表示有N个测试用的动物名称,之后又有N行,每行是一个动物名。

 

Output

输出共N行,格式见样例。

 

Sample Input

5 Dog 5 Bird 10 Cat 11 Duck 1 Sparrow 66 6 Dog Bird Cat Duck Sparrow Bull

Sample Output

There are 5 Dogs. There are 10 Birds. There are 11 Cats. There are 1 Ducks. There are 66 Sparrows. There is none Bull.

HINT

 

Append Code

[Submit][Status][Web Board]
#include<iostream>
#include<cstring>
#define null ""
#define none -1
using namespace std;
class Animal{
public:
    string name;
    int  num;
    Animal(string N=null,int n=0):name(N),num(n){}
    void setAnimal(string N,int n){name=N;num=n;}
    int getNum() const{return num;}
    string getName() const{return name;}
    Animal &operator=(const Animal &a)
    {
        name=a.name;
        num=a.num;
        return *this;
    }
};
class AnimalList{
    friend class Animal;
public:
    Animal *animalList;
    int numOfAnimal;
    AnimalList(Animal *animals, int n):animalList(animals),numOfAnimal(n){}
    int operator[](const string s)
    {
        for(int i=0;i<numOfAnimal;i++)
        {
            if(animalList[i].name==s)
                return animalList[i].num;

        }
        return none;
    }
};
int main()
{
    int cases;
    string name;
    int num;
    cin>>cases;
    Animal animals[cases];
    for (int i = 0; i < cases; i++)
    {
        cin>>name>>num;
        animals[i].setAnimal(name, num);
    }
    AnimalList animalList(animals, cases);

    cin>>cases;
    for (int i = 0; i < cases; i++)
    {
        cin>>name;
        if (animalList[name] != -1)
            cout<<"There are "<<animalList[name]<<" "<<name<<"s."<<endl;
        else
            cout<<"There is none "<<name<<"."<<endl;
    }
    return 0;
}

 

posted @ 2016-05-22 12:22  一一一匹马赛克儿  阅读(583)  评论(0编辑  收藏  举报