SSD5 网络拍卖 实验六

完整版实验六

Advertisement.h

View Code
#ifndef ADVERTISEMENT_H
#define ADVERTISEMENT_H

#include
<queue>
#include
<iostream>
#include
<string>
#include
"Date.h"
#include
"Bid.h"

using namespace std;

class Advertisement;

istream
&operator>>(istream &stream, Advertisement &a);

class Advertisement {

private:

int number;
int quantity;
string title;
string seller_email;
string body;

Date start;
Date close;
priority_queue
<Bid> bids;

public:

Advertisement(
void);
Advertisement(
const Advertisement &a);
Advertisement (
string title, string seller_email, string body,
Date start, Date close,
int quantity);

virtual void setStart (const Date &start);
virtual void setClose (const Date &close);
virtual void setTitle (string title);
virtual void setBody (string body);
virtual void setNumber (int number);
virtual void setEmail (string email);
virtual void setQuantity (int quantity);

virtual Date getStart () const;
virtual Date getClose () const;
virtual string getTitle() const;
virtual string getBody() const;
virtual string getEmail() const;
virtual int getNumber () const;
virtual int getQuantity() const;

virtual bool operator==(const Advertisement&) const;

virtual priority_queue<Bid>& getBids(void);
virtual vector<Bid> getTopDutchBids (void) const;

};

#endif

Advertisement.cpp

View Code
#include <iostream>
#include
"Advertisement.h"
using namespace std;

Advertisement::Advertisement()
{
this->number = 0;
this->quantity=0;
this->seller_email=this->title=this->body="";
this->start=Date();
this->close=Date();
}
Advertisement::Advertisement(
const Advertisement &a)
{
this->quantity=a.getQuantity();
this->title = a.getTitle();
this->seller_email=a.getEmail();
this->body=a.getBody();
this->start=a.getStart();
this->close=a.getClose();
this->number = a.getNumber();
}
Advertisement::Advertisement(std::
string title, std::string seller_email, std::string body, Date start, Date close, int quantity)
{
this->quantity=quantity;
this->title = title;
this->seller_email=seller_email;
this->body=body;
this->start=start;
this->close=close;
this->number = 0;
}
void Advertisement::setStart(const Date &start)
{
this->start = start;
}
void Advertisement::setClose(const Date &close)
{
this->close = close;
}
void Advertisement::setBody(std::string body)
{
this->body = body;
}
void Advertisement::setEmail(std::string email)
{
this->seller_email = email;
}
void Advertisement::setNumber(int number)
{
this->number = number;
}
void Advertisement::setQuantity(int quantity)
{
this->quantity = quantity;
}
void Advertisement::setTitle(std::string title)
{
this->title = title;
}

string Advertisement::getBody() const
{
return this->body;
}
string Advertisement::getEmail() const
{
return this->seller_email;
}
string Advertisement::getTitle() const
{
return this->title;
}
Date Advertisement::getStart()
const
{
return this->start;
}
Date Advertisement::getClose()
const
{
return this->close;
}
int Advertisement::getNumber()const
{
return this->number;
}
int Advertisement::getQuantity()const
{
return this->quantity;
}
bool Advertisement::operator==(const Advertisement& a)const
{
if(this->number==a.getNumber())
return true;
else return false;
}
istream
&operator>>(istream &stream, Advertisement &a)
{
string title,email,body;
int quantity;
Date start,close;
stream
>>title>>email>>quantity>>start>>close>>body;
a.setTitle(title);
a.setBody(body);
a.setEmail(email);
a.setStart(start);
a.setClose(close);
a.setQuantity(quantity);
return stream;
}
priority_queue
<Bid>& Advertisement::getBids(void)
{
return this->bids;
}
vector
<Bid> Advertisement::getTopDutchBids(void) const
{
priority_queue
<Bid> pb = this->bids;//把所有的竞标放在一个优先权队列中
vector<Bid> b; //成功的竞标者放在这个vector中
Bid bid;
int quantity = this->quantity;//记录Advertisement的数量
while(!pb.empty()&&quantity>0)//判断竞标的人数是否还有,广告的数量是否还有剩余
{
bid
= pb.top();
quantity
-=bid.getQuantity();
if(quantity<0)
{
bid.setWinbid(quantity
+bid.getQuantity());//竞标者能够成功的竞标的数量
b.push_back(bid);
quantity
= 0;//此时设置quantity为0.说明广告的数量已经没有了
}
else
{
bid.setWinbid(bid.getQuantity());
b.push_back(bid);
pb.pop();
}

}
return b;//返回能够成功的竞标者
}

Bid.h

View Code
#ifndef BID_H
#define BID_H

#include
<iostream>
#include
<string>
#include
"Date.h"

using namespace std;

class Bid;

istream
&operator>>(istream &stream, Bid &b);

class Bid {

private:

string email;
float amount;
int quantity;
Date date;

int winbid;
public:

Bid(
void);
Bid(
const Bid &b);
Bid (
string email, float amount, int quantity, Date date);

virtual string getEmail () const ;
virtual float getAmount () const;
virtual int getQuantity () const;
virtual Date getDate () const;
////////////
virtual int getWinbid() const;
////////////
virtual void setEmail(const string&);
virtual void setAmount(const float&);
virtual void setQuantity(const int&);
virtual void setDate(const Date&);
//////
virtual void setWinbid(const int&);
/////
virtual bool operator< (const Bid &rhs) const;
virtual bool operator== (const Bid &rhs) const;
};

#endif

Bid.cpp

View Code
#include "Bid.h"

Bid::Bid(
void)
{
this->email="";
this->amount=0;
this->quantity=0;
this->date=Date();
}
Bid::Bid(
const Bid&b)
{
this->email = b.getEmail();
this->amount= b.getAmount();
this->quantity=b.getQuantity();
this->date = b.getDate();
/////
this->winbid = b.getWinbid();
/////
}
Bid::Bid(
string email,float amount, int quantity,Date date)
{
this->amount = amount;
this->email = email;
this->quantity = quantity;
this->date = date;
}
string Bid::getEmail()const
{
return this->email;
}
float Bid::getAmount()const
{
return this->amount;
}
int Bid::getQuantity()const
{
return this->quantity;
}
Date Bid::getDate()
const
{
return this->date;
}
///////////

int Bid::getWinbid()const
{
return this->winbid;
}
void Bid::setWinbid(const int& w)
{
this->winbid = w;
}

///////////
void Bid::setEmail(const string&e)
{
this->email = e;
}
void Bid::setAmount(const float&a)
{
this->amount=a;
}
void Bid::setQuantity(const int&q)
{
this->quantity = q;
}
void Bid::setDate(const Date&d)
{
this->date = d;
}
bool Bid::operator <(const Bid&rhs)const
{
if(this->amount<rhs.getAmount())return true;
else return false;
}
bool Bid::operator ==(const Bid&rhs)const
{
if(this->amount==rhs.getAmount())return true;
else return false;
}
istream
&operator>>(istream &stream,Bid &b)
{
int q;
float a;
string e;
Date d;
stream
>>e>>a>>q>>d;
b.setAmount(a);
b.setDate(d);
b.setEmail(e);
b.setQuantity(q);
return stream;
}

bidhistory.h

View Code
#ifndef BIDHISTORY_H
#define BIDHISTORY_H

#include
<sstream>

using namespace std;

void displayBidHistory(ostringstream &oss, Advertisement* ad);

#endif

buildbidpage.h

View Code
#ifndef BUILDBIDPAGE_H
#define BUILDBIDPAGE_H

#include
<sstream>

using namespace std;

void buildbidpage (ostringstream &oss, int port, int number);
void displayBidForm(ostringstream &oss, struct in_addr ip, int port, int number);

#define CLASSIFIED_CGI "classified.cgi"

#endif

buildbidpage.cpp

View Code
#include <sstream>
#include
<winsock2.h>
#include
<ws2tcpip.h>
#include
<signal.h>
#include
<cstdio>

#include
"main.h"
#include
"buildpage.h"
#include
"buildbidpage.h"
#include
"bidhistory.h"
#include
"Client.h"
#include
"Advertisement.h"
#include
"Listing.h"
#include
"Group.h"

using namespace std;

void buildbidpage (ostringstream &oss, int port, int number) {

in_addr ip;
ip
= getIP();

displayPageHeader(oss);

Advertisement
*ad = NULL;
ad
= advertisements[number];

oss
<< "<table border=0 width=100%>" << endl;
oss
<< "<tr><td valign=top width=50%>" << endl;
oss
<< "<b>" << ad->getTitle() << "</b><br>" << endl;
displayBidHistory(oss, ad);

oss
<< "<td valign=top>" << endl;
displayBidForm(oss, ip, port, number);
oss
<< "</tr></table>" << endl;

oss
<< "</body>" << endl;
oss
<< "</html>" << endl;
}

void displayBidForm(ostringstream &oss, struct in_addr ip, int port, int number) {

Advertisement
*ad = NULL;
ad
= advertisements[number];

oss
<< "<FORM ACTION=" << CLASSIFIED_CGI << " METHOD=POST>" << endl;
oss
<< "<INPUT NAME=PORT TYPE=hidden VALUE=" << port << ">" << endl;
oss
<< "<INPUT NAME=IP TYPE=hidden VALUE=" << inet_ntoa(ip) << ">" << endl;
oss
<< "<INPUT NAME=email TYPE=hidden VALUE=" << active_user << ">" << endl;
oss
<< "<INPUT NAME=number TYPE=hidden VALUE=" << number << ">" << endl;

oss
<< "<b><center>Place Bid</center></b><br>" << endl;
oss
<< "<table border=0 width=100%>" << endl;
oss
<< "<tr><td align=right>Bid amount:" << endl;

float high_bid = 0;
if (ad->getBids().size() > 0) {
high_bid
= ad->getBids().top().getAmount();
}
oss
<< "<td><INPUT NAME=amount TYPE=text SIZE=5 VALUE="
<< high_bid + 1 << "></tr>" << endl;

oss
<< "<tr><td align=right>Quantity:" << endl;
if (ad->getQuantity() > 1) {
oss
<< "<td><INPUT NAME=quantity SIZE=5 TYPE=text VALUE=1></tr>" << endl;
}
else {
oss
<< "<td><INPUT NAME=quantity TYPE=hidden VALUE=1>1</tr>" << endl;
}

oss
<< "<tr><td align=center colspan=2>" << endl;
oss
<< "<INPUT NAME=COMMAND TYPE=submit VALUE=\"Submit bid\">" << endl;
oss
<< "</FORM>" << "</tr></table>" << endl;
}
void displayBidHistory(ostringstream &oss, Advertisement* ad)
{
Client
* client = NULL;
client
= users[ad->getEmail()];
int bid_number = 0;
vector
<Bid> bids = ad->getTopDutchBids();

oss
<< "<table border=0 width=100%><tr><td align=center width=15%>" << endl;
oss
<< "<tr>" << endl;
oss
<< "<B>" << "AD Title: </B>" << ad->getTitle() << "<br>" <<endl;
oss
<< "<B>" << "LastName: </B>" << client->getLname() << "<br>" <<endl;
oss
<< "<B>" << "FirstName: </B>" << client->getFname() << "<br>" <<endl;
oss
<< "<B>" << "Posted: </B>" << ad->getStart() << "<br>" <<endl;
oss
<< "<B>" << "Closes: </B>" << ad->getClose() << "<br>" <<endl;
oss
<< "<B>" << "Quantity: </B>" << ad->getQuantity() << "<br>" <<endl;
oss
<< "<B>" << "Total number of bids: </B>" <<ad->getBids().size()
<< "<br><br><br>" <<endl;
oss
<< "<B>" << "The highest bid list</B><br><br>" << endl;
for(vector<Bid>::iterator it = bids.begin(); it!=bids.end(); it++)
{
oss
<< "NO.<B>" << ++bid_number << "</B><br>"<<endl;
oss
<< "Bidder's email: <B>" << it->getEmail() << "</B><br>" <<endl;
oss
<< "The dollar amount of the bid: <B>" << it->getAmount()
<< "$</B><br>" << endl;
oss
<< "The number of items bid on: <B>" << it->getQuantity()
<< "</B><br>" << endl;
oss
<< "The number of items that the bid is currently winning: <B>"
<< it->getWinbid() << "</B><br>" <<endl;
oss
<< "Auction Date: <B>" << it->getDate() << "</B><br><br>" <<endl;
}
oss
<< "<br></tr></table>" <<endl;
}

buildpage.h

View Code
#ifndef BUILDPAGE_H
#define BUILDPAGE_H

#include
<sstream>
#include
"Listing.h"

using namespace std;

in_addr getIP(
void);
void displayPageHeader(ostringstream &oss);

void displaySortFind (ostringstream &oss, struct in_addr ip, int port);
void displayCategoryFilter(ostringstream &oss, struct in_addr ip, int port);
void displayPostItem (ostringstream &oss, struct in_addr ip, int port);
void displayRegister (ostringstream &oss, struct in_addr ip, int port);
void displayLogin (ostringstream &oss, struct in_addr ip, int port);
void displayLogout (ostringstream &oss, struct in_addr ip, int port);
void displayControlPanel (ostringstream &oss, struct in_addr ip, int port);
void displayAdvertisement (ostringstream &oss, Advertisement* ad, struct in_addr ip,
int port);
void displayBidButton(ostringstream &oss, Advertisement* ad, struct in_addr ip,
int port);

void buildpage (ostringstream &oss, int port, Listing::iterator start,
Listing::iterator finish);

#define CLASSIFIED_CGI "classified.cgi"

#endif

buildpage.cpp

内容不变就是实验材料里的cpp文件

Category.h

View Code
#ifndef CATEGORY_H
#define CATEGORY_H

#include
<string>
#include
<vector>
//#include <set>

using namespace std;

class Category;

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

class Category {

private:
int number;
int parent;
string name;

vector
<int> 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(int);
virtual void addItem(int);

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

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

};

#endif

Category.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(int 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
<int>::iterator Category::subCategoriesBegin()
{
return this->sub_categories.begin();
}
vector
<int>::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 parent;
string name;
stream
>> parent >> name;
c.setParent(parent);
c.setName(name);
return stream;
}
/*
istream &operator >> (istream &stream,Category &c)
{
int parent;
string name;
stream >> parent;
stream >> name;
c.setParent(parent);
c.setName(name);
return stream;
}
*/

Categories.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 void findOfferings (int category, Listing::iterator start,
Listing::iterator finish, Listing
&matches);
virtual void findOfferingsRecursive (int category, Listing::iterator start,
Listing::iterator finish, Listing
&matches);

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

virtual void add(Category* ptr);

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

};


#endif

Categories.cpp

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

void Categories::findOfferings(int category, Listing::iterator start,
Listing::iterator finish, Listing
&matches)
{
Listing::iterator it1;
vector
<int>::iterator it;
Category
* c = this->operator[](category);
for(it1=start;it1!=finish;it1++)
{
for(it=c->itemsBegin();it!=c->itemsEnd();it++)
if((*it)==(*it1)->getNumber())
matches.add((
*it1));
}
}
void Categories::findOfferingsRecursive(int category, Listing::iterator start,
Listing::iterator finish, Listing
&matches)
{
findOfferings(category,start,finish,matches);
Category
* c = this->objects[category];
vector
<int>::iterator it = c->subCategoriesBegin();
for(; it != c->subCategoriesEnd();it++)
findOfferingsRecursive((
*it),start,finish,matches);
}
/*
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);
}
*/
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();
}

Client.h

View Code
#ifndef CLIENT_H
#define CLIENT_H

#include
<string>
#include
<vector>
#include
"Date.h"

using namespace std;

class Client;

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

class Client {

private:

string fname;
string lname;
string email;
string passwd;
vector
<int> offerings;
vector
<int> bids;

public:

Client(
void);
Client(Client
const &c);
Client (
string &fname, string &lname, string &email, string &passwd);

virtual void setFname(const string&);
virtual void setLname(const string&);
virtual void setEmail(const string&);
virtual void setPasswd(const string&);

virtual string getFname () const;
virtual string getLname () const;
virtual string getEmail () const;
virtual string getPasswd () const;

virtual vector<int>::iterator beginOfferings();
virtual vector<int>::iterator endOfferings();
virtual vector<int>::iterator beginBids();
virtual vector<int>::iterator endBids();

virtual void addBid (int item);
virtual void addOffering (int item);
virtual bool verifyPasswd(string passwd);

};

#endif

Client.cpp

View Code
#include <iostream>
#include
<string.h>
#include
"Client.h"
using namespace std;

Client::Client()
{
this->fname = "";
this->email = "";
this->lname = "";
this->passwd= "";
}
Client::Client(
const Client &c)
{
this->fname = c.getFname();
this->lname = c.getLname();
this->email = c.getEmail();
this->passwd = c.getPasswd();
}
Client::Client(std::
string &fname, std::string &lname, std::string &email, std::string &passwd)
{
this->fname = fname;
this->lname = lname;
this->email = email;
this->passwd = passwd;
}

void Client::setEmail(const std::string &email)
{
this->email = email;
}
void Client::setFname(const std::string &fname)
{
this->fname = fname;
}
void Client::setPasswd(const std::string &passwd)
{
this->passwd = passwd;
}
void Client::setLname(const std::string &lname)
{
this->lname = lname;
}

string Client::getFname() const
{
return this->fname;
}
string Client::getLname() const
{
return this->lname;
}
string Client::getEmail() const
{
return this->email;
}
string Client::getPasswd() const
{
return this->passwd;
}
bool Client::verifyPasswd(std::string passwd)
{
if(this->passwd == passwd)return true;
else return false;
}

istream
&operator>>(istream &stream, Client &c)
{
string fname,lname,email,passwd;
stream
>>fname>>lname>>email>>passwd;
c.setFname(fname);
c.setLname(lname);
c.setEmail(email);
c.setPasswd(passwd);
return stream;
}
/////////////////
vector<int>::iterator Client::beginBids()
{
return this->bids.begin();
}
vector
<int>::iterator Client::endBids()
{
return this->bids.end();
}
vector
<int>::iterator Client::beginOfferings()
{
return this->offerings.begin();
}
vector
<int>::iterator Client::endOfferings()
{
return this->offerings.end();
}
void Client::addBid(int item)
{
this->bids.push_back(item);
}
void Client::addOffering(int item)
{
this->offerings.push_back(item);
}

Date.h

View Code
#ifndef DATE_H
#define DATE_H

#include
<iostream>

using namespace std;

class Date;

ostream
&operator<<(ostream&, const Date&);
istream
&operator>>(istream&, Date&);

class Date {

private:
int month;
int day;
int year;
int hour;
int minute;
int second;

public:

Date(
void);
Date (
int month, int day, int year, int hour, int minute, int second);

virtual void setMonth(int&);
virtual void setDay(int&);
virtual void setYear(int&);
virtual void setHour(int&);
virtual void setMinute(int&);
virtual void setSecond(int&);

virtual int getMonth(void) const;
virtual int getDay(void) const;
virtual int getYear(void) const;
virtual int getHour(void) const;
virtual int getMinute(void) const;
virtual int getSecond(void) const;

virtual bool operator== (const Date &rhs);
virtual bool operator< (const Date &left);

};

#endif

Date.cpp

View Code
#include <iostream>
#include
<string>
#include
"Date.h"
using namespace std;

Date::Date()
{
this->day = 0;
this->month=0;
this->year = 0;
this->minute = 0;
this->hour = 0;
this->second =0;
}
Date::Date(
int month,int day,int year,int hour,int minute,int second)
{
this->month = month;
this->day = day;
this->year = year;
this->hour = hour;
this->minute = minute;
this->second = second;
}

void Date::setMonth(int &month)
{
this->month = month;
}
void Date::setDay(int &day)
{
this->day = day;
}
void Date::setYear(int &year)
{
this->year = year;
}
void Date::setMinute(int &minute)
{
this->minute = minute;
}
void Date::setSecond(int &second)
{
this->second = second;
}
void Date::setHour(int &hour)
{
this->hour = hour;
}
int Date::getDay() const
{
return this->day;
}
int Date::getHour() const
{
return this->hour;
}
int Date::getMinute() const
{
return this->minute;
}
int Date::getYear() const
{
return this->year;
}
int Date::getSecond() const
{
return this->second;
}
int Date::getMonth() const
{
return this->month;
}
bool Date::operator==(const Date &rhs)
{
return (this->day==rhs.day)&&(this->year==rhs.year)&&
(
this->month==rhs.month)&&(this->hour==rhs.hour)&&
(
this->minute == rhs.minute)&&(this->second == rhs.second);
}
bool Date::operator <(const Date &rhs)
{
if(this->year>rhs.year)return false;
else if(this->year<rhs.year)return true;
else if(this->month>rhs.month)return false;
else if(this->month<rhs.month)return true;
else if(this->day>rhs.day)return false;
else if(this->day<rhs.day)return true;
else if(this->hour>rhs.hour)return false;
else if(this->hour<rhs.hour)return true;
else if(this->minute>rhs.minute)return false;
else if(this->minute<rhs.minute)return true;
else if(this->second>rhs.second)return false;
else if(this->second<rhs.second)return true;
else return false;
}
ostream
&operator<<(ostream& os,const Date& date)
{
return os<<date.getMonth()<<"/"<<date.getDay()<<"/"<<date.getYear()<<" "
<<date.getHour()<<":"<<date.getMinute()<<":"<<date.getSecond()<<" "<<endl;
}

istream
&operator>>(istream& is, Date& date)
{
string s;
is>>s;
int year=0,month=0,day=0,hour=0,second=0,minute=0,i;
int len = s.length(),k=0;
for(i = 0; i<len ; i++)
{
if(s[i]=='/')
k
++;
else
{
if(k==0) month =month*10+s[i]-'0';
else if(k==1) day =day*10+s[i]-'0';
else if(k==2) year =year*10+s[i]-'0';
}
}
is>>s;
len
= s.length();
k
= 0;
for(i = 0; i<len ; i++)
{
if(s[i]==':') k++;
else
{
if(k==0)hour = hour*10+s[i]-'0';
else if(k==1)minute =minute*10+s[i]-'0';
else if(k==2)second =second*10+s[i]-'0';
}
}
date.setYear(year);
date.setMonth(month);
date.setDay(day);
date.setHour(hour);
date.setMinute(minute);
date.setSecond(second);
return is;
}

Group.h

View Code
#ifndef GROUP_H
#define GROUP_H

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

#include
"Client.h"

using namespace std;

class Group;

class Group {

protected:
//typedef vector<Client*> Container;
typedef map<string,Client*> Container;
public:
typedef Container::iterator iterator;

protected:
Container objects;

public:
Client
*operator[](const string& email);

virtual void add(Client* ptr);

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

};

#endif

Group.cpp

View Code
#include "Group.h"

void Group::add(Client* ptr)
{
this->objects.insert(pair<string,Client*>(ptr->getEmail(),ptr));
//objects.push_back(ptr);
}
Group::iterator Group::begin()
{
return objects.begin();
}
Group::iterator Group::end()
{
return objects.end();
}
Client
*Group::operator[](const string& email)
{
if(this->objects.find(email)!=this->objects.end())
{
return this->objects[email];
}
else return NULL;
/*
vector<Client*>::iterator it = objects.begin();
for(;it!=objects.end(); it++)
if((*it)->getEmail()==email)
return *it;
return NULL;
*/
}

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
<string.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;
}
bool cmp5(Advertisement *a,Advertisement *b)
{
Bid c , d;
if(!a->getBids().empty())
{
c
= a->getBids().top();
}
if(!b->getBids().empty())
{
d
= b->getBids().top();
}
return c.getAmount() > d.getAmount();
}
bool cmp6(Advertisement *a,Advertisement *b)
{
priority_queue
<Bid> c = a->getBids();
priority_queue
<Bid> d = b->getBids();
Bid e,f;
while(!c.empty())
{
e
= c.top();
c.pop();
}
while(!d.empty())
{
f
= d.top();
d.pop();
}
return e.getAmount() < f.getAmount();//>从大到小 <从小到大
}

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;
}
if(field.compare("highest")==0)
{
std::sort(l.objects.begin(),l.objects.end(),cmp5);
return l;
}
if(field.compare("lowest")==0)
{
std::sort(l.objects.begin(),l.objects.end(),cmp6);
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);
}
}
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;
}
}

main.h

内容不变同材料中给的一样

main.cpp

内容不变同材料中给的一样

 

processrequest.h

内容不变同材料中给的一样

prodessrequest.cpp

内容不变同材料中给的一样

posted on 2011-09-13 18:29  NewPanderKing  阅读(1909)  评论(0编辑  收藏  举报

导航