Professor:
Manuel Leonardo Mosquera Palomino
National University of Colombia
Faculty of Engineering
Department of Systems and Industrial Engineering
Bogotá D.C., 2016
This document presents a program that uses various concepts learned during the Computer Programming course. The program simulates an elevator and was created using the C++ language with the Dev C++ program. This program includes the miniwin.h library, which was developed to print figures in the console and interact with them minimally.
The reason we chose to work on this project is that it represents a significant challenge that tests our ability to apply programming skills to real-world problems. In this case, we chose an elevator, which required us to design the logical structure for its operation, as well as the graphical part that represents the elevator's physical structure.
The program simulates the movement of an elevator. Two sets of control panels are used to guide its movement: one is located next to the building, where users can call the elevator, and another shows the floor numbers. First, a button is pressed to request the elevator, causing it to move to the requested floor. Then, from the second panel, the desired destination floor is selected, and the elevator moves accordingly.
In order to run the program, you must have some c++ compiler installed on your computer. We recommend using MinGW for Windows (After installing MinGW, you must add the path to the bin folder to the system's PATH variable), and g++ for Linux (usually already installed). Once you have the compiler installed, you can compile the program by following these steps:
-
Open a terminal.
-
Compile the miniwin library by running the following command:
cd lib g++ -c miniwin.cpp -o miniwin.o # Compile the miniwin library into an object file without linking (-c flag) ar -rcs libminiwin.a miniwin.o # Create a static library from the object file
-
Compile the program by running the following command:
cd .. g++ main.cpp -L ./lib -l miniwin
- The
-L
flag is used to specify the directory where the library is located - The
-l
flag is used to specify the library to link to the program. In this case, the library is calledminiwin
.
[!IMPORTANT]
- If you are using Windows, you need to add the
-l gdi32
flag to the compilation command to link the GDI library. - If you are using Linux, you need to add the
-l X11
flag to link the X11 library.
[!NOTE] IF you have problems with
pthread
library, you can add the-l pthread
flag to the compilation command. - The
-
These commands will generate an executable file called
a.exe
if you are using Windows ora.out
if you are using Linux. You can run the program by executing the following command:./a.exe # If you are using Windows (or just double-click the executable file) ./a.out # If you are using Linux
-
Include libraries:
- Include
miniwin.h
andcstdlib
.
- Include
-
Use namespace:
- Use the
miniwin
namespace.
- Use the
-
Create constant for scaling.
-
Create
person
function (parameters: position, gender):- Assign skin color and draw head, neck, and hands.
- Assign any color and draw two rectangles for the shirt (one for the sleeves, the other for the rest).
- If
gender == 'm'
, draw small circles for hair.
-
Create
brick
function (input: coordinate):- Draw a rectangle starting at the given coordinate.
- The endpoint is the starting coordinate plus the scale.
-
Create
eraser
function (no parameters):- Draw a black rectangle the size of the elevator.
- Draw a smaller filled rectangle to erase what is passed without causing errors.
-
Create
building
function (parameters: location, number of floors):- Inside a
while
loop nested in afor
loop, draw the floors at each level. - In the
for
loop:- Draw planters, floors, and gates for each floor with different colors.
- Draw columns, the roof, and the building's edges with different colors using
color_rgb
. - Use the
text
function to print the building's name. - Call
refresh
to display the drawings.
- Inside a
-
Create
elevator
function (parameters: current floor, destination floor, total floors):- Reassign variables based on the total floors to prevent the elevator from moving in the wrong direction.
- Draw a brick at the position of the current floor.
- Add a condition to restrict upward or downward movement.
- Create a loop:
- Initialize a counter at the current floor.
- Move by
+0.001
or-0.001
until reaching the destination.
- Constantly draw and erase the elevator to simulate movement, calling
wait
with a 10 ms delay. - Call
refresh
to update the screen. - Draw the elevator at the destination floor.
-
Create
controlPanel
function:- Assign color
0128128
. - Create variables for the control panel's initial position.
- Use a
for
loop to draw three rectangles per row. - Reassign positioning variables.
- Draw numbers and the STOP option according to the grid.
- Call
refresh
to update the drawings.
- Assign color
-
Create
miniPanels
function:- Use a
for
loop with a counter up to the total number of floors. - Draw a blue rectangle and lines to simulate an arrow.
- Use a
-
Create
grid
function:- Use two loops: one for the X-axis, another for the Y-axis.
-
Create
main
function:- Declare variables
x
,rangeX
, andrangeY
. - Set the window size using
resize(950, 600)
. - Call the
building
function. - Create a loop for selecting multiple floors.
- Call the
miniPanels
function. - Call the
controlPanel
function. - Create an infinite loop to update the mouse's coordinates:
- If the coordinates are valid:
- Call
mouse_left_button
and limit the click usingrangeX
andrangeY
. - Limit clicks on different sections of the panel, simulating a matrix.
- If the STOP button is clicked:
- Exit the program and trigger an emergency call.
- If the mouse is outside the matrix:
- Assign
k
to the user variable.
- Assign
- Call
- If the coordinates are valid:
- Refresh all drawings.
- Declare variables
-
End of the program.
flowchart TD
A[Start] --> B[Include libraries: miniwin.h, cstdlib]
B --> C[Use namespace: miniwin]
C --> D[Create constant for scaling]
D --> E[Create 'person' function]
E --> E1[Assign skin color, draw head, neck, hands]
E1 --> E2[Assign color for shirt, draw two rectangles]
E2 --> E3[If gender == 'm', draw hair]
D --> F[Create 'brick' function]
F --> F1[Draw rectangle starting at input coordinate]
F --> G[Create 'eraser' function]
G --> G1[Draw black rectangle, erase previous drawings]
G --> H[Create 'building' function]
H --> H1[Draw floors using a nested loop]
H1 --> H2[Draw planters, gates with different colors]
H2 --> H3[Draw columns, roof, edges, and building name]
H3 --> H4[Call refresh to display drawings]
H --> I[Create 'elevator' function]
I --> I1[Reassign variables based on total floors]
I1 --> I2[Draw brick at current floor]
I2 --> I3[Condition for upward/downward movement]
I3 --> I4[Loop: Move elevator to destination]
I4 --> I5[Call wait for 10 ms delay, simulate movement]
I5 --> I6[Call refresh to update screen]
I6 --> I7[Draw elevator at destination floor]
I --> J[Create 'controlPanel' function]
J --> J1[Assign color]
J1 --> J2[Create variables for panel position]
J2 --> J3[Draw three rectangles per row, reassign variables]
J3 --> J4[Draw numbers, STOP option, call refresh]
J --> K[Create 'miniPanels' function]
K --> K1[Loop: Draw blue rectangle, lines for arrows]
K --> L[Create 'grid' function]
L --> L1[Two loops: X and Y axes for positioning]
L --> M[Create 'main' function]
M --> M1[Declare variables x, rangeX, rangeY]
M1 --> M2[Set window size: resize 950x600]
M2 --> M3[Call 'building' function]
M3 --> M4[Loop: Select multiple floors]
M4 --> M5[Call 'miniPanels' function]
M5 --> M6[Call 'controlPanel' function]
M6 --> M7[Infinite loop to update mouse coordinates]
M7 --> M8[Check valid coordinates, limit clicks]
M8 --> M9[If STOP clicked, exit program]
M9 --> M10[Refresh all drawings]
M10 --> Z[End]
By developing the elevator simulation program, we learned to use many features of the Dev C++ program, as well as the tools in the C++ language, and integrate them in a structured way to perform logical operations that solve both simple and complex problems.
We also learned how to create simpler programs in terms of the number of lines of code, while ensuring they perform efficiently, as seen when running the programs.
- (2011, January). Functions. MiniWin. Retrieved November 2016, from MiniWin Documentation