category.cpp
来自「本程序给出了SSD5实验课中Exercise6的参考程序 内附有详细的注释 下载」· C++ 代码 · 共 121 行
CPP
121 行
#pragma warning (disable:4786)
#pragma warning (disable:4503)
#include <iostream>
#include "Category.h"
using namespace std;
//Constructor without parameter
Category::Category(void): parent(-1), name("null") {
}
//Constructor with two parameters
Category::Category(int parent, string name):number(-1), parent(parent), name(name) {
}
//Return id
int Category::getNumber(void) const {
return number;
}
//Return parent's id
int Category::getParent(void) const {
return parent;
}
//Return name
string Category::getName(void) const {
return name;
}
//Set id
void Category::setNumber(int number) {
this->number = number;
}
//Set parent id
void Category::setParent(int parent) {
this->parent = parent;
}
//Set name
void Category::setName(string name) {
this->name = name;
}
//add a category pointer
void Category::addSubCategory(Category * category) {
sub_categories.insert(category);
}
//Add a item into items
void Category::addItem(int item) {
items.insert(item);
}
//Return the front iterator of the vector items
set<int>::iterator Category::itemsBegin() {
return items.begin();
}
//Return the back iterator of the vector items
set<int>::iterator Category::itemsEnd() {
return items.end();
}
//Return the start iterator of the vector sub_categories
set<Category*>::iterator Category::subCategoriesBegin() {
return sub_categories.begin();
}
//Return the end iterator of the vector sub_categories
set<Category*>::iterator Category::subCategoriesEnd() {
return sub_categories.end();
}
//Fills the Listing matches with the advertisements that exist in the invoking category.
void Category::findOfferings (Listing::iterator start,
Listing::iterator finish, Listing &matches) {
for (set<int>::iterator i = items.begin() ; i != items.end(); i++) {
for(Listing::iterator itr = start; itr != finish; itr++) {
if (*i == (*itr)->getNumber()) {
matches.add(*itr);
break;
}
}
}
}
//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.
void Category::findOfferingsRecursive (Listing::iterator start,
Listing::iterator finish, Listing &matches) {
findOfferings(start, finish, matches);
for (set<Category*>::iterator itre_sub_cat = sub_categories.begin(); itre_sub_cat != sub_categories.end(); itre_sub_cat++) {
if (start != finish) {
(*itre_sub_cat)->findOfferingsRecursive(start, finish, matches);
}
}
}
//Reads a Category object from an input stream. The category should input the following data
//members of the class in the form parent_id \n name \n
istream &operator>>(istream &stream, Category &cat) {
int id;
string name;
stream >> id >> name;
cat.setParent(id);
cat.setName(name);
return stream;
}
//Compares the invoking Category object and the parameter Category object for equality.
//Categories are considered equal if their identification numbers are equal.
bool Category::operator== (const Category &rhs) {
return this->getNumber() == rhs.getNumber();
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?