-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelevsim.cpp
132 lines (101 loc) · 4.53 KB
/
elevsim.cpp
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// elevsim.cpp -- Elevator simulation in C++
#include <iostream>
#include <stdlib.h>
#include <time.h>
#ifdef _WIN32
#include <Windows.h>
#else
#include <unistd.h>
#endif
#include "tsargs.h"
void initDisplay(void);
#include "disp.h"
#include "building.h"
int MAXELEVS = DFLT_MAXELEVS; // Number of elevators (1 to 10 only)
int MAXFLOORS = DFLT_MAXFLOORS; // Number of floors (2 to 10 only)
int MAXPERSONS = DFLT_MAXPERSONS; // Maximum people in building
int ELEVWAIT = DFLT_ELEVWAIT; // Min. seconds to wait at floors
int CAPACITY = DFLT_CAPACITY; // Maximum people in an elevator
int TRAVELTIME = DFLT_TRAVELTIME; // Seconds to travel between floors
int TICDELAY = DFLT_TICDELAY; // Simultation iteration delay (0.1s) ;
unsigned SIMTIME = 600;
/* -- Function prototype used only by main() */
simWindow wndSim;
/* -- Global variables. These are the only global variables used by
the simulation. Except for the main building object (theAction), the
variables hold the statistics displayed on the bottom line. */
unsigned totalPeople; // Number of people handled
unsigned inBuilding; // People in building now
unsigned leftBuilding; // People who left building
unsigned avgWait; // Average no. people waiting
unsigned avgRide; // Average no. people in elevators
unsigned tookStair; // Number people who walked
unsigned totalTime; // Seconds simulation has run
building theAction; // Building simulation object
/* -- Note: Enable one of the optional sleep() or msleep() statements
below to slow the simulation on a fast system. */
int main(int argc, char **argv)
{
int ch;
if( TSargs::init(argc,argv) == -1 ) exit( -1 );
theAction.setTime( SIMTIME );
srand(time(NULL)); // Randomize random-number generator
initDisplay(); // Initialize the display
theAction.display(); // Display the elevators and labels
/* -- This while-loop handles the entire simulation. It cycles
while the building object (named theAction) returns true through
its `continues()' function. */
while (theAction.continues()) {
if( TICDELAY > 0 )
usleep(TICDELAY * 100000 );
// 1 real second == 1 simulated second
// msleep(250); // 1/4 real second = 1 simulated second
// msleep(125); // 1/8 real second = 1 simulated second
theAction.perform(); // Perform all simulation actions
theAction.display(); // Update the display
}
/* -- Perform an orderly exit. */
// disp_move(24, 0); // Position cursor on last line
// disp_showcursor(); // Make cursor visible
// disp_close(); // Close display package
return(0); // Exit with no errors to report
}
/* -- Initialize the display, showing various labels that remain
unchanged throughout the simulation. Also initialize the Zortech
display package for fast screen writes. */
void initDisplay(void)
{
int i;
wndSim.disp_open(); // Initialize display package
wndSim.disp_move(0, 0); // Move to "home" position
wndSim.disp_eeop(); // Clear the display
wndSim.disp_hidecursor(); // Make cursor invisible
wndSim.disp_move(21, 30); // Display title line
wndSim.disp_puts( "Elevator Simulation from Learning C++ by Tom Swan");
wndSim.disp_startstand(); // Begin reversed-video display
wndSim.disp_move(0, 0);
wndSim.disp_puts("Floors ");
for (i = 0; i < 10; i++) // Show reversed video floor
wndSim.disp_puts(" 0 "); // numbers at top of screen
wndSim.disp_move(22, 0);
wndSim.disp_puts(" Total In Left Avg # Avg # ");
wndSim.disp_puts("Took Num Num Seconds Elapsed");
wndSim.disp_move(23, 0);
wndSim.disp_puts("People Bldg Bldg Waiting Riding ");
wndSim.disp_puts("Stair Elv Flr Left Time");
wndSim.disp_endstand(); // End reversed-video display
wndSim.disp_move(1, 0);
for (i = 0; i < 10; i++) // Display horzontal divider line
wndSim.disp_puts("--------");
;
/* -- Display elevator cables. These are redrawn by the elevator
class as needed while elevators move. */
for (int row = 2; row < 18; row++) {
for (int col = 0; col < MAXELEVS; col++) {
wndSim.disp_move(row, 12 + (col * 7));
wndSim.disp_putc(':');
}
}
}
// Copyright (c) 1990 by Tom Swan. All rights reserved
// Revision 1.00 Date: 09/18/1990 Time: 09:16 am