Chapter 9 EXAMPLE PROGRAMS WHY THIS CHAPTER? ______________________________________________________________ The programs included in this chapter are intended to be illustrations to you in how to write a complete program. The programs are meant to be useful to you either as an example of how to do some operation or as utility programs for your own personal use. TIMEDATE - Get Time and Date ______________________________________________________________ This program calls one of the DOS ================ functions to get the current time and TIMEDATE.MOD date. The various components of the time ================ and date are input as variables and can be printed or displayed in any format you desire. Your particular compiler may use a different format because there is no standard in Modula-2. This is one of those areas that will probably deviate from compiler to compiler. If you study your documentation that came with your compiler, you will find many other calls of this type described. This program is meant to be an illustration of how to use this particular call. Note that this program is especially written to use the MS-DOS calls available on an IBM-PC microcomputer and will probably have no resemblance to the calls on some other system. AREAS - Calculate Areas ______________________________________________________________ This program is presented as an =============== illustration of how to build up a larger AREAS.MOD program than any we have examined up to =============== this point. Notice that the main program is simply one CASE statement that calls all of the other procedures. It would be very simple to include the code from each procedure right in the CASE statement and have no procedure calls, but it would make the program a little more difficult to understand. The way this example is formatted, the code is very easy to understand. After you understand the main program, it is a simple matter to visit each procedure to see exactly what they do. Observe carefully how the menu works in this program. It reads one keystroke and responds immediately making it a very simple program to use. 9-1 Chapter 9 - Example Programs PC - Printer Control ______________________________________________________________ This is a very useful program that you can ================ use to control your printer. It is PC.MOD specifically set up for an Epson RX-80, ================ but you can modify the control characters to set up your printer to whatever mode you desire. To use it, you call the program by typing its name on the DOS command line, and supply a single letter according to the displayed menu. The program will send the requested control sequences to the printer to select the enhanced, compressed, or whatever mode you desire. If your printer is located physically remote from you, you can use this program to send a formfeed to the printer by selecting the F option. If you have some longer control sequences to send, you may want to store the values in a string and use a loop to output the data until you come to an 0C character. LIST - List Program File ______________________________________________________________ If you ran the batch file named LISTALL as ================ suggested at the beginning of this LIST.MOD tutorial to print out all of the source ================ files, you have already used a program similar to this one. This program will list any ASCII file, adding line numbers, page numbers, and the date and time, on the printer. It is specifically designed to be a program listing utility. The operation is very simple, and you should have no trouble in understanding this program or what it does. Feel free to modify this program for your own personal use. The LIST.EXE file on the distribution disk is not a result of compiling this source file but was written and compiled in TURBO Pascal. This was done in order to alleviate the possibility of any legal problem, because Borland International does not require a distribution fee or a run- time license for compiled programs. The Modula-2 program is nearly identical to the distributed executable program, and allows you to modify the source code to meet your own personal needs. Additional programs will be given at the end of Part III for your information. You will no doubt find additional example programs in various books and periodicals and it would be to your advantage to spend some time studying them as illustrations of both good and bad practices. 9-2 Chapter 9 - Example Programs PROGRAMMING EXERCISE ______________________________________________________________ 1. Study the source code for LIST.EXE for a period of time, then compile it with your compiler. You may need a few modifications to get it to compile properly because all Modula-2 compilers are not identical. After successfully compiling and executing this program, put it aside. Starting with a blank sheet of paper, write and debug your own version of a program lister. This is a fairly major programming assignment, but if you get stuck, you have the existing program to help you over the rough spots. If you complete this assignment, you will have gained a good bit of experience in writing Modula-2, and you will have your own customized program lister that you thoroughly understand. 9-3