Uploaded by Maximus Alvarioso

cpp

advertisement
/****************************
**
TSZ-HIN, LO
**
**
Project 2
**
**
02/21/2016
**
** CIS 22C -- MW:3:30-5:30 **
*****************************/
#include<iostream>
#include<stdlib.h>
#include<time.h>
#include<conio.h>
using namespace std;
class Event {
public:
// constructor requires time of event
Event (unsigned int t) : time(t) { };
// time is a public data field
unsigned int time;
// execute event by invoking this method
virtual void processEvent() { };
};
class Priority_queue{
public:
Priority_queue()
{
rear = 0;
}
int less(int x,int y)
{
if (pq[x]->time < pq[y]->time)
return 1;
else
return 0;
}
void exch(int a,int b)
{
Event* x = pq[a];
pq[a] = pq[b];
pq[b] = x;
}
void swim(int k)
{
This study source was downloaded by 100000853833074 from CourseHero.com on 05-13-2024 18:16:46 GMT -05:00
https://www.coursehero.com/file/16899033/Barcpp/
while (k > 1 && less(k,k/2) == 1) { //min heap
exch(k, k/2); // parent of
k = k/2;
// node k is at k/2
}
}
void insert(Event * newevent)
{
pq[++rear] = newevent;
swim(rear);
}
int empty(){
if(rear<1)
return 0;
else
return 1;
}
Event* peak()
{
return pq[1];
}
void sink(int k, int N) {
while (2*k <= N) {
int j = 2*k;
if (j < N && less(j+1, j) == 1) j++;
if (less(j, k)== 0) break;
exch(k,j);
k = j;
}
}
void deleteMin()
{
if(rear >= 1)
{
exch(1,rear);
sink(1, rear-1);
--rear;
}
}
This study source was downloaded by 100000853833074 from CourseHero.com on 05-13-2024 18:16:46 GMT -05:00
https://www.coursehero.com/file/16899033/Barcpp/
private:
Event* pq[360];
int rear;
};
class randomInteger{
public:
unsigned int operator () (unsigned int);
} randomizer;
unsigned int randomInteger::operator () (unsigned int max)
{
// rand return random integer
// convert to unsigned to make positive
// take remainder to put in range
unsigned int rval = rand();
return rval % max;
}
unsigned int randBetween(int low, int high) {
return low + randomizer(high - low);
}
class SimulationFramework {
public:
SimulationFramework () : eventQueue(), currentTime(0) { }
void scheduleEvent (Event * newEvent)
{
// insert newEvent into eventQueue (Priority Queue)
// Priority Queue is based on MinHeap using newEvents time
eventQueue.insert(newEvent);
}
void run();
unsigned int currentTime;
protected:
//priority_queue <vector<Event *>, eventComparison> eventQueue;
int weightedProbability(unsigned int num1,unsigned int num2,unsigned int
num3);
Priority_queue eventQueue;
};
int SimulationFramework::weightedProbability(unsigned int num1,unsigned
int num2,unsigned int num3)
This study source was downloaded by 100000853833074 from CourseHero.com on 05-13-2024 18:16:46 GMT -05:00
https://www.coursehero.com/file/16899033/Barcpp/
{
int maxNum = num1+num2+num3;
//Generate a random number between 1 to max value
int ranNum = rand()% maxNum + 1;
if(ranNum < 15)
return 0;
else if (ranNum <= (num1+num2))
return 1;
else return 2;
}
void SimulationFramework::run()
{
// execute events until event queue becomes empty
while (eventQueue.empty()!= 0) {
// copy & remove min-priority element (min time) from eventQueue
Event *nextEvent = eventQueue.peak();
eventQueue.deleteMin();
// update simulation's current time
currentTime = nextEvent->time;
// process nextEvent
nextEvent->processEvent(); // what do you see here???
// cleanup nextEvent object
delete nextEvent;
}
}
class SoftwareGurusBar {
public:
// try with 50 chairs, then try with 40, 60, ...
// in order to find out optimal profit prospects
SoftwareGurusBar()
: freeChairs(50), profit(0.0),drink1(0.0),drink2(0.0),drink3(0.0){ }
int canSeat(unsigned int numberOfPeople);
void order(unsigned int beerType);
void leave(unsigned int numberOfPeople);
//
// Data fields.
//
unsigned int freeChairs;
double profit;
int drink1,drink2,drink3;
};
This study source was downloaded by 100000853833074 from CourseHero.com on 05-13-2024 18:16:46 GMT -05:00
https://www.coursehero.com/file/16899033/Barcpp/
SoftwareGurusBar theBar;
SimulationFramework theSimulation;
class ArriveEvent : public Event {
public:
ArriveEvent (unsigned int time, unsigned int gs)
: Event(time), groupSize(gs) { }
virtual void processEvent ();
protected:
unsigned int groupSize;
};
class OrderEvent : public Event {
public:
OrderEvent (unsigned int time, unsigned int gs)
: Event(time), groupSize(gs) { }
virtual void processEvent ();
protected:
unsigned int groupSize;
};
void ArriveEvent::processEvent()
{
if ( theBar.canSeat(groupSize) != 1 )
// place an order within 2 & 10 minutes
theSimulation.scheduleEvent(
new OrderEvent (theSimulation.currentTime + randBetween(2,10),
groupSize));
}
class LeaveEvent : public Event {
public:
LeaveEvent (unsigned int time, unsigned int gs)
: Event(time), groupSize(gs) { }
virtual void processEvent ();
protected:
unsigned int groupSize;
};
void OrderEvent::processEvent()
{
// each member of the group orders a beer (type 1,2,3)
for (int i = 0; i < groupSize; i++)
theBar.order(randBetween(1,4));
int t = theSimulation.currentTime + randBetween(30,60);
// schedule a LeaveEvent for this group of drinkers
// all the group leaves together
This study source was downloaded by 100000853833074 from CourseHero.com on 05-13-2024 18:16:46 GMT -05:00
https://www.coursehero.com/file/16899033/Barcpp/
// add your code here ...
theSimulation.scheduleEvent(new LeaveEvent(t, groupSize));
};
void LeaveEvent::processEvent ()
{
theBar.leave(groupSize);
}
int SoftwareGurusBar::canSeat (unsigned int numberOfPeople)
{
// if sufficient room, then seat customers
cout << "\nTime: " << theSimulation.currentTime <<" minutes"<< " || ";
cout << " Group of " << numberOfPeople << " customers arrives";
if (numberOfPeople < freeChairs) {
cout << " Group is seated" << endl;
freeChairs -= numberOfPeople;
return 0;
}
else {
cout << " No room, group leaves" << endl;
return 1;
}
}
void SoftwareGurusBar::order (unsigned int beerType)
{
// serve beer
cout << "\nTime: " << theSimulation.currentTime <<" minutes"<< " || ";
cout << " serviced order for " << beerType << endl;
// update profit based on this beerType
// add your code here ...
switch(beerType)
{
case 1:
profit += 2;break;
case 2:
profit += 3;break;
case 3:
profit += 4;break;
}
if(beerType==1)
drink1 += 1;
else if(beerType==2)
This study source was downloaded by 100000853833074 from CourseHero.com on 05-13-2024 18:16:46 GMT -05:00
https://www.coursehero.com/file/16899033/Barcpp/
drink2 += 1;
else drink3 += 1;
}
void SoftwareGurusBar::leave (unsigned int numberOfPeople)
{
cout << "\nTime: " << theSimulation.currentTime <<" minutes"<< " || ";
cout << " group of size " << numberOfPeople << " leaves" << endl;
freeChairs += numberOfPeople;
}
int main() {
cout<<"SoftwareGurus Bar Simulation\n";
// load priority queue with initial Arrive Events then run simulation
unsigned int t = 0;
// load 4 hours (240 minutes) of Arrive Events
while (t < 240) {
// new group every 2-5 minutes
t += randBetween(2,5);
// group size ranges from 1 to 5
theSimulation.scheduleEvent(new ArriveEvent(t, randBetween(1,5)));
}
// then run simulation and print profits
theSimulation.run();
cout << "Local beer sold: " << theBar.drink1 << endl;
cout << "Imported beer sold: " << theBar.drink2 << endl;
cout << "Special beer sold: " << theBar.drink3 << endl;
cout << "Total profits: " << theBar.profit << endl;
return 0;
}
This study source was downloaded by 100000853833074 from CourseHero.com on 05-13-2024 18:16:46 GMT -05:00
https://www.coursehero.com/file/16899033/Barcpp/
Powered by TCPDF (www.tcpdf.org)
Download