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
|
#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);
};
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(){cout<<"heloo\n";};
void accept_bids(const BidList& bid);
};
typedef vector<Auctioneer> bidlist;
void Auctioneer::accept_bids(const BidList& bidlist)
{
for (BidList::const_iterator iter = bidlist.begin();
iter != bidlist.end();
++iter)
{
const Bid& bid = *iter; // Get a reference to the Bid object that the iterator points to
cout << "Bid id : " << bid.bidId << endl;
cout << "Trd id : " << bid.trdId << endl;
cout << "Quantity: " << bid.qty << endl;
cout << "Price : " << bid.price << endl;
cout << "Type : " << bid.type << endl;
}
}
void Simulator::run()
{
auctioneer.accept_bids(trader.loadRange(bidlist));//receiver
//auctioneer.displayBids(); // print from receiver
}
Simulator::accept_bids(bid_vector::const_iterator begin, bid_vector::const_iterator end))
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 " ;
}
int main(int argc, char **argv) {
Trader trader;
BidList list;
Auctioneer auctioneer;
//bidlist list;
auctioneer.accept_bids(list);
system("pause");
return 0;
}
|