資源簡介
完整的六層電梯程序,已經試驗過,可用,希望對各位有用,有什么建議,歡迎提出

代碼片段和文件信息
#include?
#include?
#include?
#include?“lift.h“
/*?some?global?stats?*/
int?total_waiting?=?0;
int?no_of_waits?=?0;
int?max_wait?=?0;
int?min_wait?=?10000000;?
/*
?*?Initialize?the?floors
?*/
void?init_floors(struct?floor_s*?floors)?
{
??int?i?=?0;
??for(i?=?0;?i?????floors[i].floor_number?=?i;
????floors[i].num_waiting?=?0;
????floors[i].num_not_waiting?=?0;
??}
}
/*
?*?arrive_on_floor?is?called?when?a?person?arrives?at?a?new?floor.
?*/
void?arrive_on_floor(int?time?struct?floor_s*?floor?struct?person_s*?person)
{
????person->target_floor?=?-1;??/*?just?arrived?don‘t?want?to?go
???anywhere?yet?*/
????person->floor?=?floor->floor_number;
????/*?store?them?in?the?first?free?slot?in?the?array?*/
????floor->people_not_waiting[floor->num_not_waiting]?=?person;
????floor->num_not_waiting++;
????if?(person->start_waiting?!=?-1)?{
??????total_waiting?+=?time?-?person->start_waiting;
??????printf(“Their?journey?time?was?%d\n“?time?-?person->start_waiting);
??????if?(time?-?person->start_waiting?>?max_wait)?{
max_wait?=?time?-?person->start_waiting;
??????}
??????if?(time?-?person->start_waiting? min_wait?=?time?-?person->start_waiting;
??????}
??????no_of_waits++;
??????person->start_waiting?=?-1;
????}
}
/*?
?*?init_people?initialized?the?people?to?start?from?the?ground?floor?
?*/
void?init_people(struct?floor_s*?floors?struct?person_s*?people)?
{
??int?i?=?0;
??for(i?=?0;?i?????people[i].name[0]?=?‘A‘?+?i;
????people[i].name[1]?=?‘\0‘;
????people[i].start_waiting?=?-1;
????arrive_on_floor(0?&floors[0]?&people[i]);
??}
}
/*?
?*?init_lists?initializes?the?lifts?to?start?from?the?ground?floor
?*/
void?init_lifts(struct?lift_s*?lifts)
{
??int?i;
??for?(i?=?0;?i?????lifts[i].lift_id?=?i;
????lifts[i].occupants?=?0;
????lifts[i].position?=?0;
????lifts[i].speed?=?0;
????lifts[i].last_floor?=?0;
????lifts[i].mode?=?MODE_IDLE;
??}
}
/*
?*?queue_for_list?is?called?to?add?a?person?who?was?not?previously
?*?queuing?for?a?lift?to?the?lift?queue?on?their?current?floor
?*/
void?queue_for_lift(struct?person_s*?person?struct?floor_s*?floor)
{
??int?i?j?=?-1;
??/*?find?the?person?and?remoove?them?from?the?pool?of?people?not
?????queuing?*/
??for?(i?=?0;?i?num_not_waiting;?i++)?{
????if?(floor->people_not_waiting[i]?==?person)?{
??????for?(j?=?i;?j?num_not_waiting-1;?j++)?{
??????
floor->people_not_waiting[j]?=?floor->people_not_waiting[j+1];
??????}
??????break;
????}
??}
??assert(j?!=?-1);
??floor->num_not_waiting--;
??floor->people_not_waiting[floor->num_not_waiting]?=?NULL;
??/*?add?them?to?the?queue?of?people?waiting?for?the?lift?*/
??floor->people_waiting[floor->num_waiting]?=?person;
??floor->num_waiting++;
}
/*
?*?decide_action?is?called?to?allow?a?person?to?randomly?decide?if
?*?they?want?to?go?to?an
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件??????33792??2009-03-19?11:15??LIFT\Debug\vc60.idb
?????文件??????28672??2009-03-19?11:15??LIFT\Debug\vc60.pdb
?????文件???????4260??2009-03-19?11:15??LIFT\LIFT.dsp
?????文件????????533??2009-03-19?11:15??LIFT\LIFT.dsw
?????文件??????33792??2009-03-19?11:15??LIFT\LIFT.ncb
?????文件??????48640??2009-03-19?11:15??LIFT\LIFT.opt
?????文件????????771??2009-03-19?11:15??LIFT\LIFT.plg
?????文件??????14893??2009-03-19?11:15??LIFT\main.cpp
?????目錄??????????0??2009-03-19?11:15??LIFT\Debug
?????目錄??????????0??2009-03-19?11:15??LIFT
-----------?---------??----------?-----??----
???????????????165353????????????????????10
評論
共有 條評論