In this chapter we intend to implement most of the concepts analyzed in previous chapters and for this we will program an application, the management of a small library.
The first decision we are interested in is the registration, that is, the information we keep about each book. Think that the following information will be saved: book code, title, writer, year and book type code. Since this record will be used in most functions, in the libru.h file the registry definition will be made (see program 1).
With this record two basic operations will be performed: read the information from the keyboard and write it on screen. We will compile these functions separately. Therefore they will be located in a file, lib_basi.c in our case, as seen in program 2.
You can improve screen handling. To do this, the new systems use a windows system that allows the introduction of data both through the keyboard and mouse. Unfortunately, libraries to program these systems are not standard and that is why we have not explained them here.
Let's now think about what kind of operations can be performed in our book database. In principle there should be at least the following operations: book entry, book listing and book consultation. As for the consultation of books, the following criteria should be taken into account: code (thus obtaining the information of the single book), writer, year (accepting intervals between dates) and type. These queries can be done quickly, on the one hand, taking into account that the database of books is small and will be brought to mind at first, to expedite queries, and on the other hand, build indexes as the database is being built in other files, and when consulting, read the database using these indexes.
This second is more general and that is why it is used in database systems, but it is outside the objectives of this series of articles, since it is a very complex set of programs. Therefore, we will begin with the first. However, we consider that the book code is sequential. In this way the query by code will be immediate, since sequential reading, in memory or in file, will not be necessary. Therefore, the main program will consist of opening a database file, bringing the file to memory, displaying a screen menu with different options, reading an option and calling a specialized function depending on the option. In program 3 we have a program that does it.
Note that entries, list, kon_cod and other functions are not defined in the main program, but we have defined them as external. The definition of these functions can be performed in 4 different files, 4. As explained in program 5 and 6, we will not explain the list function. The garbi_screen function has also been configured as extern, since the lib.base is in the file. Therefore it should not be defined. It should not be included in this file either, since the linker will do the link.
Error handling could be made much more complex, but we have made it as brief as possible to give importance to major actions.
We therefore have 6 modules: libru.h, lib.oinarri i.c, nagus.c, introduction.c, kon_kod.c and kon_beste.c. The listatu.c module does not appear as the write function is similar but the printer is handled and controlled by a for structure.
The hierarchical dependence of these modules is shown in Figure 1. It does not appear because the libro.h module is a title file and is therefore inserted into other modules by # include sasiord.
To compile first the four main modules are compiled one by one, obtaining the corresponding object modules .0. The main program is then compiled and linked, for the latter the names of the object modules obtained above are mentioned. As a result of the link, cross-references between modules are resolved and a single executable program is obtained.
Note: To view the photos correctly, go to the pdf.
Saioa hasi iruzkinak uzteko.