ssd5 网络拍卖 实验三

实验配置同实验二

Categeory.h

View Code
#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 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

Categeory.cpp

View Code
#include "Category.h"

Category::Category()
{
this->number=0;
this->parent=0;
this->name="";
}
Category::Category(
int parent,string name)
{
this->parent=parent;
this->name = name;
}
int Category::getNumber()const
{
return this->number;
}
int Category::getParent()const
{
return this->parent;
}
string Category::getName()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;
}
void Category::addSubCategory(Category* ca)
{
this->sub_categories.push_back(ca);
}
void Category::addItem(int item)
{
this->items.push_back(item);
}
vector
<int>::iterator Category::itemsBegin()
{
return this->items.begin();
}
vector
<int>::iterator Category::itemsEnd()
{
return this->items.end();
}
vector
<Category*>::iterator Category::subCategoriesBegin()
{
return this->sub_categories.begin();
}
vector
<Category*>::iterator Category::subCategoriesEnd()
{
return this->sub_categories.end();
}

void Category::findOfferings(Listing::iterator start,Listing::iterator finish,Listing &maches)
{
Listing::iterator it1;
vector
<int>::iterator it;
for(it1=start;it1!=finish;it1++)
{
for(it=items.begin();it!=items.end();it++)
if((*it)==(*it1)->getNumber())
maches.add((
*it1));
}
}
void Category::findOfferingsRecursive(Listing::iterator start,Listing::iterator finish,Listing &maches)
{
findOfferings(start,finish,maches);
vector
<Category*>::iterator it = this->subCategoriesBegin();
for(; it!=this->subCategoriesEnd();it++)
(
*it)->findOfferingsRecursive(start,finish,maches);
}

bool Category::operator==(const Category&rhs)
{
if(this->number = rhs.getNumber())return true;
else return false;
}
istream
&operator>>(istream &stream,Category &c)
{
int id;
string name;
stream
>>id>>name;
c.setParent(id);
c.setName(name);
return stream;
}

Categeories.h

View Code
#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 Category* operator[](const int& number);
virtual void add(Category* ptr);

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

};


#endif

Categeories.cpp

View Code
#include "Categories.h"
const int Categories::TOP_LEVEL=0;
const int Categories::NO_PARENT=0;

Category
* Categories::operator[](const int& number)
{
vector
<Category*>::iterator it=this->objects.begin();
for(; it!=this->objects.end();it++)
if((*it)->getNumber()==number)return *it;
return NULL;
}
void Categories::add(Category* ptr)
{
this->objects.push_back(ptr);
}
vector
<Category*>::iterator Categories::begin()
{
return this->objects.begin();
}
vector
<Category*>::iterator Categories::end()
{
return this->objects.end();
}

posted on 2011-09-01 22:09  NewPanderKing  阅读(373)  评论(0编辑  收藏  举报

导航