1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
|
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cstdlib>
#include <iomanip>
using namespace std;
const int NUMSELLER = 1;
const int NUMBUYER = 1;
const int NUMBIDS = 20;
const int MINQUANTITY = 1;
const int MAXQUANTITY = 30;
const int MINPRICE =100;
const int MAXPRICE = 150;
int s=0;
int trdId;
// Bid, simple container for values
struct Bid {
int bidId, trdId, qty, price;
char type;
// for sort and find.
bool operator<(const Bid &other) const { return price < other.price; }
bool operator==(int bidId) const { return this->bidId == bidId; }
};
// alias to the list, make type consistent
typedef vector<Bid> BidList;
// this class generates bids!
class Trader {
private:
int nextBidId;
public:
Trader();
Bid getNextBid();
Bid getNextBid(char type);
// generate a number of bids
void loadRange(BidList &, int size);
void loadRange(BidList &, char type, int size);
void setVector();
};
Trader::Trader() : nextBidId(1) {}
#define RAND_RANGE(min, max) ((rand() % (max-min+1)) + min)
Bid Trader::getNextBid() {
char type = RAND_RANGE('A','B');
return getNextBid(type);
}
Bid Trader::getNextBid(char type) {
for(int i = 0; i < NUMSELLER+NUMBUYER; i++)
{
// int trdId = RAND_RANGE(1,9);
if (s<10){trdId=0;type='A';}
else {trdId=1;type='B';}
s++;
int qty = RAND_RANGE(MINQUANTITY, MAXQUANTITY);
int price = RAND_RANGE(MINPRICE, MAXPRICE);
Bid bid = {nextBidId++, trdId, qty, price, type};
return bid;
}
}
//void Trader::loadRange(BidList &list, int size) {
// for (int i=0; i<size; i++) { list.push_back(getNextBid()); }
//}
//
//void Trader::loadRange(BidList &list, char type, int size) {
// for (int i=0; i<size; i++) { list.push_back(getNextBid(type)); }
//}
//---------------------------AUCTIONEER-------------------------------------------
class Auctioneer {
vector<Auctioneer> List;
Trader trader;
vector<Bid> list;
public:
Auctioneer(){};
void accept_bids(const BidList& bid);
};
typedef vector<Auctioneer> bidlist;
void Auctioneer::accept_bids(const BidList& bid){
BidList list;
BidList list2;
BidList::const_iterator iter;
copy (BidList.begin(),BidList.end(), back_inserter(list2));
//copy (BidList.begin(),BidList.end(),list2);
for(iter=list2.begin(); iter != list2.end(); iter++)//{
cout << iter << endl<<"\n";
}
//all the happy display commands
void show(const Bid &bid) {
cout << "\tBid\t(" << setw(3) << bid.bidId << "\t " << setw(3) << bid.trdId << "\t "
<< setw(3) << bid.type <<"\t " << setw(3) << bid.qty <<"\t " << setw(3) << bid.price <<")\t\n " ;
}
void show(const BidList &list) {
cout << "\t\tBidID | TradID | Type | Qty | Price \n\n";
for(BidList::const_iterator itr=list.begin(); itr != list.end(); ++itr) {
//cout <<"\t\t";
show(*itr);
cout << endl;
}
cout << endl;
}
//search now checks for failure
void show(const char *msg, const BidList &list) {
cout << msg << endl;
show(list);
}
void searchTest(BidList &list, int bidId) {
cout << "Searching for Bid " << bidId << endl;
BidList::const_iterator itr = find(list.begin(), list.end(), bidId);
if (itr==list.end()) {
cout << "Bid not found.";
} else {
cout << "Bid has been found. Its : ";
show(*itr);
}
cout << endl;
}
//comparator function for price: returns true when x belongs before y
bool compareBidList(Bid one, Bid two) {
if (one.type == 'A' && two.type == 'B')
return (one.price < two.price);
return false;
}
void sort(BidList &bidlist) { sort(bidlist.begin(), bidlist.end(), compareBidList); }
int main(int argc, char **argv) {
Trader trader;
BidList bidlist;
Auctioneer auctioneer;
//bidlist list;
auctioneer.accept_bids(bidlist);
//trader.loadRange(bidlist, NUMBIDS);
show("Bids before sort:", bidlist);
sort(bidlist);
show("Bids after sort:", bidlist);
system("pause");
return 0;
}
|