SSD5_Exercise 3分析

这题还是接着Exercise1和Exercise2来的,没什么难的,照着它给的头文件补充好函数就可以了

本题给了两个头文件  Category.h和Categories.h

看一下题目给的提示

Class Category

Class Category models a category. This class contains private data members for the category name, identification number, and parent identification number. A vector<int> stores the unique identification numbers of the advertisements that belong to this category. A vector<Category*> stores pointers to the sub-categories of each category.

  • Default constructor

Initializes the private data members to default values

  • Two parameter constructor

Accepts parameters for parent id and name

  • Accessors and mutators

Provides access to the data members of the class

  • virtual vector<int>::iterator itemsBegin();

Returns a begin iterator for the items vector

  • virtual vector<int>::iterator itemsEnd();

Returns an end iterator for the items vector

  • virtual vector<Category*>::iterator subCategoriesBegin();

Returns a begin iterator for the sub_categories vector

  • virtual vector<Category*>::iterator subCategoriesEnd();

Returns an end iterator for the sub_categories vector

  • virtual void addItem (int item);

Adds an advertisement number to the items vector

  • virtual void addSubCategory (Category*);

Adds a category to the sub_categories vector

  • virtual void findOfferings (Listing::iterator start, Listing::iterator finish, Listing &matches);

Fills the Listing matches with the advertisements that exist in the invoking category.

  • void findOfferingsRecursive (Listing::iterator start, Listing::iterator finish, Listing &matches);

This routine fills the Listing matches with the advertisements that exist in the invoking category and all of its sub-categories. This must be implemented as a recursive function.

  • istream &operator>>(istream &stream, Category &b);

Reads a Category object from an input stream. The category should input the following data members of the class in the form

parent_id\nname\n

Notice that each field is terminated by a newline character ('\n'). Make sure your implementation reads the ending newline character from the stream.

  • virtual bool operator== (const Category &rhs);

Compares the invoking Category object and the parameter Category object for equality. Categories are considered equal if their identification numbers are equal.

Class Categories

Class Categories models a collection of categories. This class contains a private data member of type vector<Category*>. This stores information regarding each category that appears in the collection of categories.

  • virtual Category* operator[](const int& number);

Returns a pointer to a Category object whose identification number matches the parameter.

  • virtual void add(Category* ptr);

This routine adds a Category pointer to the objects vector.

  • iterator begin();

Returns a Categories::iterator to the first entry in the objects vector

  • iterator end();

Returns a Categories::iterator one position past the last entry in the objects vector

结合Category.h如下:

#ifndef CATEGORY_H
#define CATEGORY_H

#include <string>
#include <vector>

#include "Listing.h"

using namespace std;

class Category;

istream &operator>>(istream &stream, Category &c);

class Category {

private:
    int number;
    int parent;
    string name;

    vector<Category*> sub_categories;
    vector<int> items;

public:

    Category(void);
    Category(int parent, string name);

    virtual ~Category() {};

    virtual int getNumber(void) const;
    virtual int getParent(void) const;
    virtual string getName(void) const;

    virtual void setNumber(int);
    virtual void setParent(int);
    virtual void setName(string);

    virtual void addSubCategory(Category*);
    virtual void addItem(int);

    virtual void findOfferings (Listing::iterator start,
                                Listing::iterator finish, Listing &matches);
    virtual void findOfferingsRecursive (Listing::iterator start,
                                         Listing::iterator finish, Listing &matches);

    virtual vector<int>::iterator itemsBegin();
    virtual vector<int>::iterator itemsEnd();
    virtual vector<Category*>::iterator subCategoriesBegin();
    virtual vector<Category*>::iterator subCategoriesEnd();

    virtual bool operator==(const Category& rhs);

};

#endif

则有Category.cpp

#include <Category.h>
using namespace std;
/*  int number;
    int parent;
    string name;
    vector<Category*> sub_categories;
    vector<int> items;
    Category(void);
    Category(int parent, string name);
    virtual ~Category() {};
    virtual int getNumber(void) const;
    virtual int getParent(void) const;
    virtual string getName(void) const;
    virtual void setNumber(int);
    virtual void setParent(int);
    virtual void setName(string);
    virtual void addSubCategory(Category*);    virtual void addItem(int);
    virtual void findOfferings (Listing::iterator start,Listing::iterator finish, Listing &matches);
    virtual void findOfferingsRecursive (Listing::iterator start,Listing::iterator finish, Listing &matches);
    virtual vector<int>::iterator itemsBegin();
    virtual vector<int>::iterator itemsEnd();
    virtual vector<Category*>::iterator subCategoriesBegin();
    virtual vector<Category*>::iterator subCategoriesEnd();
    virtual bool operator==(const Category& rhs);*/
//两个构造函数
Category::Category(){
    this->parent=0;
    this->name="";
    }
Category::Categroy(int parent, string name){
    this->parent=parent;
    this->name=name;
    }
//Getter和Setter
int Category::getNumber (void) const{
    return this->number;
    }
int Category::getParent(void) const {
    return this->parent;
}
string Category::getName(void) const {
    return this->name;
}
void Category::setNumber(int number) {
    this->number = number;
}
void Category::setParent(int parent) {
    this->parent = parent;
}
void Category::setName(string name) {
    this->name = name;
}
//STL中的vector和iterator
void Category::addSubCategory(Category* ptr) {
    sub_categories.push_back(ptr);
}
void Category::addItem(int item) {
    items.push_back(item);
}
vector<int>::iterator Category::itemsBegin() {
    return items.begin();
}
vector<int>::iterator Category::itemsEnd() {
    return items.end();
}
vector<Category*>::iterator Category::subCategoriesBegin() {
    return sub_categories.begin();
}
vector<Category*>::iterator Category::subCategoriesEnd() {
    return sub_categories.end();
}

//重载==
bool Category::operator==(const Category& rhs) {
    return (this->getNumber() == rhs.getNumber());
}

void Category::findOfferings (Listing::iterator start,Listing::iterator finish, Listing &matches) {
//循环匹配
    for (Listing::iterator it = start; it != finish; it++) {

        vector<int>::iterator search;
        search = find(this->itemsBegin(), this->itemsEnd(), (*it)->getNumber());
        if (search != this->itemsEnd()) {
            //匹配则加入matches
            matches.add(*it);
        }
    }

}

void Category::findOfferingsRecursive (Listing::iterator start,Listing::iterator finish, Listing &matches) {

    findOfferings(start, finish, matches);

    vector<Category*>::iterator it;
    for (it = this->subCategoriesBegin(); it != this->subCategoriesEnd(); it++) {
        //递归调用
        (*it)->findOfferingsRecursive(start, finish, matches);
    }
}
//重载流>>
istream &operator>>(istream &stream, Category &c) {

    char l[100];
    string name;

    stream.getline(l, 100);
    c.setParent(atoi(l));

    stream.getline(l, 100);
    c.setName(l);

    return stream;
}

结合Categories.h

#ifndef CATEGORIES_H
#define CATEGORIES_H

#include <string>
#include <vector>

#include "Category.h"
#include "Listing.h"

using namespace std;

class Categories {

protected:
    typedef vector<Category*> Container;

public:
    typedef Container::iterator iterator;

protected:
    Container objects;

public:
    static const int TOP_LEVEL;
    static const int NO_PARENT;

    virtual ~Categories() {};

    virtual Category* operator[](const int& number);
    virtual void add(Category* ptr);

    virtual iterator begin();
    virtual iterator end();

};


#endif


生成Categories.cpp

#include "Categories.h"

using namespace std;
/*

    typedef vector<Category*> Container;
    typedef Container::iterator iterator;
    Container objects;
    static const int TOP_LEVEL;
    static const int NO_PARENT;
    virtual ~Categories() {};
    virtual Category* operator[](const int& number);
    virtual void add(Category* ptr);
    virtual iterator begin();
    virtual iterator end();

*/
const int Categories::NO_PARENT = -1;
//重载[] 匹配number
Category* Categories::operator[](const int& number) {

    Categories::iterator it;

    for (it = this->begin(); it != this->end(); it++) {
        if ((*it)->getNumber() == number) {
            return *it;
        }
    }
    return NULL;
}

//写过好多次了

void Categories::add(Category* ptr) {
    objects.push_back (ptr);
}

Categories::iterator Categories::begin() {
    return objects.begin();
}

Categories::iterator Categories::end() {
    return objects.end();
}

提交两个CPP文件即可

 

 

posted @ 2013-05-03 14:43  强子~Developer  阅读(575)  评论(0编辑  收藏  举报