📄 sx.bak
字号:
if (dc=='D'&&uc=='-')
{
textcolor(MAGENTA);
cprintf("D");
textcolor(GREEN);
cprintf("-");
}
else
if (uc=='U'&&dc=='D')
{
textcolor(MAGENTA);
cprintf("D");
textcolor(RED);
cprintf("U");
}
else
if (uc=='-'||dc=='-') {textcolor(GREEN); cprintf("--");}
cout<<setw(3)<<np;
textcolor(WHITE);
}
void floors::SetUpButton(void) { if (floorNumber<MAXFLOORS-1) up=true;}
void floors::SetDownButton(void) { if (floorNumber>0) down=true;}
floorSet::floorSet()
{
for (int i=0;i<MAXFLOORS;i++)
fa[i].SetFloorNumber(i);
}
void floorSet::showFloors(perSet &persons)
{
for (int i=0;i<MAXFLOORS;i++)
fa[i].showFloor(persons);
}
void floorSet::reSetButton(direction dir,int floorNumber)
{
if (dir==UP)
fa[floorNumber].reSetUpButton();
else
if (dir==DOWN)
fa[floorNumber].reSetDownButton();
}
bool floorSet::signalUp(int floorNumber)
{
for (int i=MAXFLOORS-1;i>floorNumber;i--)
if (fa[i].upButton()||fa[i].downButton()) return true;
return false;
}
bool floorSet::signalDown(int floorNumber)
{
for (int i=0;i>floorNumber;i++)
if (fa[i].upButton()||fa[i].downButton()) return true;
return false;
}
bool floorSet::signalSameDir(direction dir,int floorNumber)
{
if (dir==UP)
return fa[floorNumber].upButton();
else
if (dir==DOWN)
return fa[floorNumber].downButton();
else
return false;
}
int floorSet::avgWaiting(void)
{
int total=0;
for (int i=0;i<MAXFLOORS;i++)
total+=fa[i].getNumWaiting();
return total/MAXFLOORS;
}
//# ifndef _ELEVATOR_H
//# define _ELEVATOR_H
//# include "elevsim.h"
//# include "floor.h"
//# include "person.h"
class elevator
{
int elevNumber;
int timeToAction;
int floorNumber;
bool stopped;
direction dir;
bool buttons[MAXFLOORS];
int passengers;
bool buttonUp(void);
bool buttonDown(void);
public:
elevator();
int getPassengers(void) {return passengers;}
void (SetelevNumber)(int n) {elevNumber=n;}
void showElevator(void);
void SetDirection(floorSet &floors);
bool elevStopping(floorSet &floors);
void action(floorSet &floors,perSet &persons);
};
class elevSet
{
elevator ea[MAXFLOORS];
public:
elevSet();
void showElevators(void);
void action(floorSet &floors,perSet &persons);
int avgRiding(void);
};
//# endif
//# include "elevator.h"
//# include <iostream.h>
//# include <iomanip.h>
# include <conio.h>
elevator::elevator()
{
elevNumber=-1;
timeToAction=ELEVWAIT;
floorNumber=0;
stopped=true;
dir=NODIRECTION;
passengers=0;
for (int i=0;i<MAXFLOORS;i++)
buttons[i]=false;
}
bool elevator::buttonUp(void)
{
for (int i=floorNumber+1;i<MAXFLOORS;i++)
if (buttons[i]) return true;
return false;
}
bool elevator::buttonDown(void)
{
for (int i=0;i<floorNumber;i++)
if (buttons[i]) return true;
return false;
}
void elevator::showElevator(void)
{
textcolor(BROWN);
gotoxy(13+elevNumber*7,1);
switch (floorNumber)
{
case 0: cprintf("1 "); break;
case 1: cprintf("2 "); break;
case 2: cprintf("3 "); break;
case 3: cprintf("4 "); break;
case 4: cprintf("5 "); break;
case 5: cprintf("6 "); break;
case 6: cprintf("7 "); break;
case 7: cprintf("8 "); break;
case 8: cprintf("9 "); break;
case 9: cprintf("10"); break;
}
Draw_window ((10+elevNumber*7),(20-floorNumber*2),(16+elevNumber*7),(22-floorNumber*2));
gotoxy((11+elevNumber*7),(21-floorNumber*2));
cout<<setw(3)<<passengers<<" ";
if (dir==UP) {textcolor(RED); cprintf("U");}
if (dir==DOWN) { textcolor(MAGENTA); cprintf("D");}
if (dir==NODIRECTION) { textcolor(GREEN); cprintf("-");}
textcolor(WHITE);
}
void elevator::SetDirection(floorSet &floors)
{
if (buttonUp())
dir=UP;
else
if (buttonDown())
dir=DOWN;
else
if (floors.signalUp(floorNumber))
dir=UP;
else
if (floors.signalDown(floorNumber))
dir=DOWN;
else
if (dir!=DOWN||floorNumber==0)
dir=NODIRECTION;
}
bool elevator::elevStopping(floorSet &floors)
{
if (buttons[floorNumber])
return true;
else
if (floors.signalSameDir(dir,floorNumber))
return true;
else
if (floorNumber==0)
return true;
else
if (floorNumber==MAXFLOORS-1)
return true;
else
if (dir==NODIRECTION&&floors.getnp(floorNumber)!=0)
{
dir=DOWN;
return true;
}
else
return false;
}
void elevator::action(floorSet &floors,perSet &persons)
{
int pdest;
int newdir;
if (stopped)
{
passengers-=persons.discharge(elevNumber,floorNumber);
if (dir==0&&passengers<CAPACITY)
{
if (persons.loadAny(elevNumber,floorNumber,pdest))
{
passengers++;
timeToAction++;
buttons[pdest]=true;
if (pdest>floorNumber)
dir=UP;
else
dir=DOWN;
}
}
if (dir!=0&&passengers<CAPACITY)
{
if (persons.loadOne(elevNumber,floorNumber,dir,pdest))
{
passengers++;
timeToAction++;
buttons[pdest]=true;
if (passengers>=CAPACITY) timeToAction=0;
}
}
if (timeToAction--<=0)
{
if (dir==0)
SetDirection(floors);
if (dir==0&&floorNumber>0)
dir=DOWN;
if (dir==0)
timeToAction=ELEVWAIT;
else
{
floors.reSetButton(dir,floorNumber);
stopped=false;
timeToAction=TRAVELTIME;
}
}
}
else
if (timeToAction--<=0)
{
if (dir==UP)
floorNumber++;
else
floorNumber--;
SetDirection(floors);
if (elevStopping(floors))
{
floors.reSetButton(dir,floorNumber);
stopped=true;
timeToAction=ELEVWAIT;
buttons[floorNumber]=false;
}
else
timeToAction=TRAVELTIME;
}
}
elevSet::elevSet()
{
for (int i=0;i<MAXELEVS;i++)
ea[i].SetelevNumber(i);
}
void elevSet::showElevators(void)
{
for (int k=2;k<=22;k++)
{
gotoxy(10,k);
clreol();
}
for (int i=0;i<MAXELEVS;i++)
ea[i].showElevator();
}
void elevSet::action(floorSet &floors,perSet &persons)
{
for (int i=0;i<MAXELEVS;i++)
ea[i].action(floors,persons);
}
int elevSet::avgRiding(void)
{
int total=0;
for (int i=0;i<MAXELEVS;i++)
total+=ea[i].getPassengers();
return total/MAXELEVS;
}
//# ifndef _BUILDING_H
//# define _BUILDING_H
//# include "simulation.h"
//# include "elevsim.h"
//# include "person.h"
//# include "floor.h"
//# include "elevator.h"
class building:public simulation
{
perSet Persons;
floorSet Floors;
elevSet Elevators;
int hours;
int minutes;
int seconds;
public:
building()
{
minutes=0;
hours=0;
seconds=0;
}
bool continues(void);
void perform(void);
void display(void);
};
//# endif
//# include "building.h"
//# include <conio.h>
# include <dos.h>
//# include <iostream.h>
//# include <iomanip.h>
bool building::continues(void)
{
if (kbhit())
if (getch()==ESCKEY)
SetTime(0);
return simulation::continues();
}
void building::perform(void)
{
Persons.action();
Elevators.action(Floors,Persons);
reduceTime(1);
totalTime++;
}
void building::display(void)
{
//clrscr();
_setcursortype (_NOCURSOR);
gotoxy(1,1);
textcolor(YELLOW);
cprintf("floors");
Floors.showFloors(Persons);
Elevators.showElevators();
avgwait=Floors.avgWaiting();
avgRide=Elevators.avgRiding();
gotoxy(1,23);
textcolor(11);
cprintf("allPeople inBuilding leftBuilding avgWait avgRide tookStair Floor Elevs ReTime");
textcolor(WHITE);
cout<<setw(6)<<totalPeople<<" "<<setw(9)<<inBuilding<<" "<<setw(11)
<<leftBuilding<<" "<<setw(9)<<avgwait<<" "<<setw(8)<<avgRide<<" "
<<setw(7)<<tookstair<<setw(8)<<MAXFLOORS<<setw(6)<<MAXELEVS<<" ";
if (getTime()/3600<10) cout<<"0";
cout<<getTime()/3600<<":";
if (getTime()%3600/60<10) cout<<"0";
cout<<getTime()%3600/60<<":";
if (getTime()%3600%60%60<10) cout<<"0";
cout<<getTime()%3600%60%60;
gotoxy(72,25);
seconds++;
if (seconds>=60) { minutes++; seconds=0; }
if (minutes>=60) { hours++; minutes=0; }
if (hours<10) cout<<"0";
cout<<hours<<":";
if (minutes<10) cout<<"0";
cout<<minutes<<":";
if (seconds<10) cout<<"0";
cout<<seconds;
//sleep(1);
delay(200);
}
//# include "elevsim.h"
//# include "building.h"
void initdisplay(void);
building buildsim;
void main()
{
time_t t;
srand((unsigned)time(&t));
textcolor(WHITE);
initdisplay();
starttime=time(NULL);
buildsim.display();
while (buildsim.continues())
{
buildsim.perform();
buildsim.display();
}
buildsim.results();
}
void initdisplay(void)
{
clrscr();
long time;
_setcursortype (_SOLIDCURSOR);
//textcolor(WHITE);
Draw_window (17,6,60,15);
textcolor(13);
gotoxy(18,7);
cprintf("------&**& ");
textbackground(5);
textcolor(WHITE+BLINK);
cprintf("Please settime start");
textbackground(BLACK);
textcolor(13);
cprintf(" &**&------");
textcolor(WHITE);
gotoxy(18,10);
cprintf("SetTime to programme:");
textbackground(6);
cprintf(" ");
textcolor(RED);
cout<<"secs";
textbackground(BLACK);
textcolor(RED);
gotoxy(39,10);
cin>>time;
buildsim.SetTime(time);
_setcursortype (_NOCURSOR);
textcolor(WHITE);
clrscr();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -