今年ebiz的冬天来的特别早# Money - 海外理财
h*o
1 楼
我觉得和cc150 8.2 design a call center 类似。
bank/callcenter 收到一个request,分给一个空闲的elevator/employee
如果没闲人,把request入queue. 一旦有人闲下来,就来queue里找任务去处理。bank/
callcenter就好像server, elevator/employee 就好象process/thread/task.
passanger 是 request 中的 subrequest.
贴一个我的码吧。 欢迎指正。网上有些code有铃啊,门的。我不明白。就不加入我的
码了。
test_evelatorSystem(){
mg = Manager::getInstance();
for (i = 0; i < 4; i++){
//sb. press button UP at floor 5
Request* req = new Request(floor=5, direction=UP);
mg.dispatchRequest(req);
}
}
class Manager{ //singleton
private:
vector elevator_list[3];
queue req_queue;
public:
static Manager* getInstance();
dispatchRequest(Request& req);
Request* getNextReq(){return the next req of req_queue};
}
Manager::dispatchRequest(Request& req);{
//or using goodSchedule() to find a best elevator.
find an idle evelator E from elevator_list,
E.startReq(req).
if no idle evelator, put req in req_queue
}
Manager::goodSchedule(){ //TBD
if available pick a standing elevator for this floor.
else pick an elevator moving to this floor.
else pick a standing elevator on an other floor.
else pick the elevator with the lowest load.
}
//A request is created when sb. press button "UP" or "DOWN" at a floor
class Request{
floor; //in which floor the button is pressed
direction; //UP/DOWN
}
//passanger press target_floor in the elevator
class Passanger{
name;
target_floor;
};
class Elevator{
public:
Elevator(Manager& m, max_size):_m(m), _capacity(capacity){};
startReq(req); //call by manager
private:
int passanger_num;
vector plist;
int id;
int _capacity;
status; //busy/idle
current_floor;
moveTo(destination);
load(vector pass)
unLoad(vector pass);
completeReq();
vector stop_list;
}
Elevator::startReq(req){
status = busy ;
moveTo(req.floor);
pass=getPassangers(NULL);
load(pass);
schedule(req.direction);
for each target_floor of stop_list:
moveTo(target_floor);
pass=getPassangers(plist)
unLoad(pass);
stop_list.pop(target_floor);
completeReq();
}
Elevator::moveTo(destination){
sleep some time;
current_floor = destination;
}
vector& Elevator::getPassangers(vector plist){
if plist exist; randomly create some passangers from plist;
else randomly create some passangers
return passangers;
}
Elevator::load(vector pass){
if pass overload, dump some of them.
add pass to plist
// passamger_num = plist.size();
sleep some time;
}
Elevator::unLoad(pass){
remove pass from plist;
// passamger_num = plist.size();
sleep some time;
}
//resort the task according to the passanger's target_floors and execute
step by step.
Elevator::schedule(req.direction){
go over all passanger P in plist{
//only handle P matching the requestion direction.
if (P->direction == req.direction){
if (req.direction == UP){
if (P.target_floor>cur_floor) stop_list.push(P
.target_floor);
}
else if (req.direction == DOWN){
if (P.target_floor .target_floor);
}
}
sort stop_list
}
}
completeReq(){
status = idle;
if (req = manager.getNextReq()){
startReq(req);
}
else //cout << "I am idle"
}
bank/callcenter 收到一个request,分给一个空闲的elevator/employee
如果没闲人,把request入queue. 一旦有人闲下来,就来queue里找任务去处理。bank/
callcenter就好像server, elevator/employee 就好象process/thread/task.
passanger 是 request 中的 subrequest.
贴一个我的码吧。 欢迎指正。网上有些code有铃啊,门的。我不明白。就不加入我的
码了。
test_evelatorSystem(){
mg = Manager::getInstance();
for (i = 0; i < 4; i++){
//sb. press button UP at floor 5
Request* req = new Request(floor=5, direction=UP);
mg.dispatchRequest(req);
}
}
class Manager{ //singleton
private:
vector
queue
public:
static Manager* getInstance();
dispatchRequest(Request& req);
Request* getNextReq(){return the next req of req_queue};
}
Manager::dispatchRequest(Request& req);{
//or using goodSchedule() to find a best elevator.
find an idle evelator E from elevator_list,
E.startReq(req).
if no idle evelator, put req in req_queue
}
Manager::goodSchedule(){ //TBD
if available pick a standing elevator for this floor.
else pick an elevator moving to this floor.
else pick a standing elevator on an other floor.
else pick the elevator with the lowest load.
}
//A request is created when sb. press button "UP" or "DOWN" at a floor
class Request{
floor; //in which floor the button is pressed
direction; //UP/DOWN
}
//passanger press target_floor in the elevator
class Passanger{
name;
target_floor;
};
class Elevator{
public:
Elevator(Manager& m, max_size):_m(m), _capacity(capacity){};
startReq(req); //call by manager
private:
int passanger_num;
vector
int id;
int _capacity;
status; //busy/idle
current_floor;
moveTo(destination);
load(vector
unLoad(vector
completeReq();
vector
}
Elevator::startReq(req){
status = busy ;
moveTo(req.floor);
pass=getPassangers(NULL);
load(pass);
schedule(req.direction);
for each target_floor of stop_list:
moveTo(target_floor);
pass=getPassangers(plist)
unLoad(pass);
stop_list.pop(target_floor);
completeReq();
}
Elevator::moveTo(destination){
sleep some time;
current_floor = destination;
}
vector
if plist exist; randomly create some passangers from plist;
else randomly create some passangers
return passangers;
}
Elevator::load(vector
if pass overload, dump some of them.
add pass to plist
// passamger_num = plist.size();
sleep some time;
}
Elevator::unLoad(pass){
remove pass from plist;
// passamger_num = plist.size();
sleep some time;
}
//resort the task according to the passanger's target_floors and execute
step by step.
Elevator::schedule(req.direction){
go over all passanger P in plist{
//only handle P matching the requestion direction.
if (P->direction == req.direction){
if (req.direction == UP){
if (P.target_floor>cur_floor) stop_list.push(P
.target_floor);
}
else if (req.direction == DOWN){
if (P.target_floor
}
}
sort stop_list
}
}
completeReq(){
status = idle;
if (req = manager.getNextReq()){
startReq(req);
}
else //cout << "I am idle"
}