Читайте также:
|
|
BidSniperAnalysis.cpp
/* Begin Student Code */
#pragma warning (disable: 4786)
#include "BidSniperAnalysis.h"
#include <numeric>
#include <functional>
#include <algorithm>
using namespace std;
bool operator<(const CategoryStats& cs1, const CategoryStats& cs2) {
if (cs1.percentDiff > cs2.percentDiff)
return true;
else
return false;
}
BidSniperAnalysis::BidSniperAnalysis(Listing& listing, Categories& categories) {
convertRepresentation(listing, categories);
calculatePercentDiff();
}
void BidSniperAnalysis::convertRepresentation (Listing& listing, Categories& categories) {
Categories::iterator it;
for (it = categories.begin(); it!= categories.end(); it++) {
vector<int>::iterator items;
for (items = (*it)->itemsBegin(); items!= (*it)->itemsEnd(); items++) {
Advertisement* ad = listing[*items];
if (ad->getBids().size() > 1) {
if (isSniped(ad)) {
stats[*it].numberSniped++;
stats[*it].priceSniped += ad->getBids().top().getAmount();
} else {
stats[*it].numberNonSniped++;
stats[*it].priceNonSniped += ad->getBids().top().getAmount();
}
}
}
}
}
void BidSniperAnalysis::calculatePercentDiff(void) {
map<Category*, CategoryStats>::iterator it;
for (it = stats.begin(); it!= stats.end(); it++) {
if (it->second.numberNonSniped > 0 &&
it->second.numberSniped > 0) {
double snipedAverage, nonSnipedAverage;
snipedAverage = it->second.priceSniped / it->second.numberSniped;
nonSnipedAverage = it->second.priceNonSniped / it->second.numberNonSniped;
it->second.percentDiff = ((nonSnipedAverage - snipedAverage) / nonSnipedAverage) * 100;
it->second.cat = it->first;
filtered.push_back(it->second);
}
}
}
bool BidSniperAnalysis::isSniped(Advertisement* ad) {
const int SECONDS_IN_FIVE_MINUTES = 60 * 5;
const int MAX_SNIPE_TIME = SECONDS_IN_FIVE_MINUTES;
priority_queue<Bid> bids = ad->getBids();
Bid winning = bids.top();
bids.pop();
double time = ad->getClose() - winning.getDate();
if (time < MAX_SNIPE_TIME) {
while (!bids.empty()) {
if (bids.top().getEmail() == winning.getEmail()) {
return false;
}
bids.pop();
}
// the winning bidder placed only one bid
// and it was less than the MAX_SNIPE_TIME within
// the close of the auction
return true;
} else {
return false;
}
}
void BidSniperAnalysis::showSnipedStatistics(void) {
sort(filtered.begin(), filtered.end());
vector<CategoryStats>::iterator it;
for (it = filtered.begin(); it!= filtered.end(); it++) {
cout << it->cat->getName() << ": " << it->numberNonSniped
<< "\t" << it->numberSniped
<< "\t" << it->percentDiff << endl;
}
}
/* End Student Code */
Дата добавления: 2015-11-14; просмотров: 45 | Нарушение авторских прав
<== предыдущая страница | | | следующая страница ==> |
BidAnalysis.h | | | BiddingWarAnalysis.cpp |