ssd5 网络拍卖 实验四

Listing.h

View Code
#ifndef LISTING_H
#define LISTING_H

#include
<iostream>
#include
<vector>
#include
<iterator>
#include
<algorithm>
#include
<functional>

#include
"Advertisement.h"

using namespace std;

class Listing;

class Listing {

protected:
typedef vector
<Advertisement*> Container;

public:
typedef Container::iterator iterator;

protected:
Container objects;

public:
virtual Advertisement* operator[](const int& number);

virtual void add(Advertisement* ptr);

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

// return a sorted copy of this Listing
virtual Listing sort(string field);

// return a filtered by keyword copy of this Listing
virtual Listing filter(string keyword);
};

#endif

Listing.cpp

View Code
#include"Listing.h"
#include
<algorithm>
bool cmp1(Advertisement *a,Advertisement *b)
{
return a->getQuantity()<b->getQuantity();
}
bool cmp2(Advertisement *a,Advertisement *b)
{
return a->getStart()<b->getStart();
}
bool cmp3(Advertisement *a,Advertisement *b)
{
return a->getClose()<b->getClose();
}
bool cmp4(Advertisement *a,Advertisement *b)
{
int n = a->getEmail().compare(b->getEmail());
if(n<0)return true;
else return false;
}

void Listing::add(Advertisement* ptr)
{
objects.push_back(ptr);
}
vector
<Advertisement*>::iterator Listing::begin()
{
return objects.begin();
}
vector
<Advertisement*>::iterator Listing::end()
{
return objects.end();
}
Advertisement
*Listing::operator[](const int& number)
{
vector
<Advertisement*>::iterator it = objects.begin();
for(;it!=objects.end();it++)
if((*it)->getNumber()==number)
return *it;
return NULL;
}
Listing Listing::sort(
string field)
{
Listing l;
l.objects
= this->objects;
if(field.compare("quantity")==0)
{
std::sort(l.objects.begin(),l.objects.end(),cmp1);
return l;
}
if(field.compare("email")==0)
{
std::sort(l.objects.begin(),l.objects.end(),cmp4);
return l;
}
if(field.compare("start")==0)
{
std::sort(l.objects.begin(),l.objects.end(),cmp2);
return l;
}
if(field.compare("close")==0)
{
std::sort(l.objects.begin(),l.objects.end(),cmp3);
return l;
}
}
struct f
{
f(
string keyword,Listing &l):key(keyword){l1=&l;}
void operator()(Advertisement* a)
{
if(strstr(a->getTitle().c_str(),key.c_str())!=NULL||strstr(a->getBody().c_str(),key.c_str())!=NULL)
{
l1
->add(a);
}
}
private:
string key;
Listing
*l1;
};
Listing Listing::filter(
string keyword)
{
Listing l;
if(keyword=="")
{
l.objects
= this->objects;
return l;
}
else
{
for_each(
this->begin(),this->end(),f(keyword,l));
return l;
}
}

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

导航