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
|
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cstdlib>
using namespace std;
const int NUMSELLER = 1;
const int NUMBUYER = 1;
const int NUMBIDS = 10;
const int MINQUANTITY = 1;
const int MAXQUANTITY = 30;
const int MINPRICE =50;
const int MAXPRICE = 100;
struct Bid {
int bidId, trdId, qty, price;
int type;
};
class Simulator {
private:
static int nextBidId;
int bidId, trdId,qty, price;
int type;
int currentBid;
public:
Simulator(){};
Simulator(int n, char o, int p ,int q) : trdId(n), type(o), qty(p), price(q){bidId =Simulator::nextBidId++;}
Simulator( getNextBid());
int get_bidId() const { return bidId; }
int get_trdId() const { return trdId; }
int get_type() const { return type; }
int get_qty() const { return qty; }
int get_price() const { return price;}
// for sort on price. It's already going to be in bidId order.
bool operator<(const Bid &other) const { return price < other.price; }
bool operator==(const Bid &other) const { return bidId == other.bidId; }
bool operator==(int bidId) const { return this->bidId == bidId; }
void show(ostream &out) const {
out << bidId << '\t' << trdId << '\t' << type << '\t' << qty << '\t' << price;
}
};
int Simulator::nextBidId = 1;
Simulator getRandomBid() {
int trdId = (rand() % 9) + 1;
int type = (rand() % 9) + 1;
int qty = (rand() % 9) + 1;
int price = (rand() % 50) + 50;
return Simulator(trdId, type, qty, price);
}
//void showForType(const vector<Bid> &vect, char type) {
// for(vector<Bid>::const_iterator itr=vect.begin(); itr != vect.end(); ++itr) {
// if (itr->getType()== type) {
// // do something
// }
// }
//}
void initList(vector<Simulator> &list) {
for (int i=0;i<20; i++)
{
list.push_back(getRandomBid());
}
}
void show(const char *msg, const vector<Simulator> &vect) {
cout << msg << endl;
cout <<"\t\tBidID | TradID | Type | Qty | Price \n\n";
for(vector<Simulator>::const_iterator itr=vect.begin(); itr != vect.end(); ++itr) {
cout <<"\t\t ( ";
itr->show(cout);
cout <<" )";
cout << endl;
}
cout << endl;
}
void searchTest(vector<Simulator> &list, int bidId) {
cout << "Searching for a particular Bid\n";
vector<Simulator>::iterator itr = find(list.begin(), list.end(), bidId);
cout << "Bid has been found. Its : ";
itr->show(cout);
cout << endl;
}
int main() {
vector<Similator> bidlist;
initList(bidlist);
show("Bids before sort:", bidlist);
sort(bidlist.begin(), bidlist.end());
show("Bids after sort:", bidlist);
searchTest(bidlist, 3);
system("pause");
return 0;
}
| |