Chapter 14 COMPLETE SAMPLE PROGRAMS Prior to this point, this tutorial has given you many example programs illustrating a point of some kind, but these have all been "nonsense" programs as far as being useful. It would be a disservice to you to simply quit with only tiny programs to study, so the following programs are offered to you as examples of good Pascal programming practice. They are useful programs, but they are still short enough to easily grasp their meaning. We will discuss them one at a time. AMORTIZATION TABLE GENERATOR ____________________________________________________________ This is not one program, but five. Each one is an improvement on the previous one, and the series is intended to give you an idea of program development. AMORT1.PAS - This is the bare outline of the amortization program. Although it is an operating program, it doesn't do very much. After some thought and planning, the main program was written to allow for an initialization, then an annual repeating loop. The annual loop would require a header, a monthly calculation, and an annual balance. Finally, a procedure was outlined for each of these functions with a minimum of calculations in each procedure. This program can be compiled and run to see that it does do something for each month and for each year. It has a major problem because it does not stop when the loan is payed off but keeps going to the end of that year. The primary structure is complete. AMORT2.PAS - This is an improvement over AMORT1. The monthly calculations are correct but the final payment is still incorrectly done. Notice that for ease of testing, the loan variables are simply defined as constants in the initialize procedure. To make the procedures easier to find, comments with asterisks were added. This program is nearly usable. Compile and run it. AMORT3.PAS - Now we calculate the final payment correctly and we have a correct annual header with column headings. We have introduced a new variable to be used for an annual interest accumulation. This is neat to have at income tax time. This program can also be compiled and run. AMORT4.PAS - This program does nearly everything we would like it to do. All of the information needed to build the table for any loan is now read in from the keyboard, Page 14-1 Complete Sample Programs greatly adding to the flexibility. After the information is available, the monthly payment is calculated in the newly added procedure Calculate_Payment. The annual header has a new line added to include the original loan amount and the interest rate in the information. Compile and run this program to see its operation. AMORT5.PAS - The only additional feature in this program is the addition of a printout of the results. Examining the program, you will notice that many of the output statements are duplicated with the Lst included for the device selection. Compile and run this program, but be sure to turn your printer on to get a printout of the amortization table you ask for. If you are using TURBO Pascal version 3.0, you will need to either comment out line 3 or remove it altogether. TOP DOWN PROGRAMMING ____________________________________________________________ The preceding example is an example of a top-down approach to programming. This is where the overall task is outlined, and the details are added in whatever fashion makes sense to the designer. The opposite is a bottom-up programming effort, in which the heart of the problem is defined and the rest of the program is built up around it. In this case, the monthly payment schedule would probably be a starting point and the remainder of the program slowly built up around it. Use whichever method works best for you. The final program AMORT5.PAS is by no means a program which can never be improved upon. Many improvements can be thought of. These will be exercises for you if you so desire. 1. In the data input section, ask if a printout is desired, and only print if it was requested. This would involve defining a new variable and if statements controlling all write statements with Lst as a device selector. 2. Format the printout with a formfeed every three years to cause a neater printout. The program presently prints data right across the paper folds with no regard to the top of page. 3. Modify the program to include semimonthly payments. Payments twice a month are becoming popular, but this program cannot handle them. 4. Instead of listing the months as numbers, put in a case statement to cause the months to be printed out as three letter names. You could also include the day of the month when the payment is due. Page 14-2 Complete Sample Programs 5. Any other modification you can think up. The more you modify this and other programs, the more experience and confidence you will gain. LIST.PAS, to list your Pascal programs ____________________________________________________________ Since the differences between TURBO Pascal versions are significant, two files are included here. If you are using TURBO Pascal 3.0, rename LIST3.PAS to LIST.PAS, and if you are using TURBO Pascal 4.0 or 5.x, rename LIST4.PAS to LIST.PAS before continuing on to the next section. LIST.PAS is a very useful program that you can use to list your Pascal programs on the printer. It can only be compiled with TURBO Pascal because it uses TURBO extensions. The two extensions it uses are the string type variable and (in the case of TURBO Pascal version 3.0), the absolute type variable. The absolute type variable in line 14 and the coding in the Initialize procedure is an example of how you can read in the parameters given on the command line. If you are using TURBO Pascal 4.0 or 5.x, a completely different method is used in the Initialize procedure which should be no problem for you to understand at this point. To use this program to print out the last program, for example, you would enter the following at the DOS prompt LIST AMORT5.PAS. This program reads in the AMORT5.PAS from the command line and uses it to define the input file. It should be pointed out that this program cannot be run from a "compiled in memory" compilation with the TURBO Pascal compiler. It must be compiled to a Disk file, and you must quit TURBO Pascal in order to run it from the DOS command level. The parameter read from the command line, AMORT5.PAS, is stored at computer memory location 80(hexadecimal) referred to the present code segment. If you didn't understand that, don't worry, you can still find the input parameter in any program using the method given in the initialize procedure for your version of TURBO Pascal. If you do not have TURBO Pascal, but you are using MS-DOS or PC-DOS, you can still use this program because it is on your disk already compiled as LIST.EXE, and can be run like any other .COM or .EXE program. Note that LIST4.PAS is the version that is compiled and copied on the distribution disk. Page 14-3 Complete Sample Programs TIMEDATE.PAS, to get today's time and date ____________________________________________________________ This is a very useful program as an example of using some of the extensions of TURBO Pascal if you are using TURBO Pascal 3.0. It interrogates the inner workings of DOS and gets the present time and date for you, provided you entered them correctly when you turned your computer on. The procedure Time_And_Date can be included in any TURBO Pascal version 3.0 program you write to give you the time and date for your listings. As an exercise in programming, add the time and date to the program LIST to improve on its usefulness. The program named TIMEDAT4.PAS does the same thing as the last, but it works with TURBO Pascal 4.0 or 5.x using the means of defining a DOS call as it has been revised for the newer versions. It turns out to be an almost trivial program but is still a good illustration of how to use some of the newer Borland extensions to Pascal. The observant student will notice that the time and date procedures have already been added to LIST4.PAS. SETTIME.PAS, a useful utility program ____________________________________________________________ This program is very interesting in that it changes the date and time stamp on any file in the current directory. It is the program used to set the time and date on all of the files on the distribution disks included with this tutorial. It sets the time to 12:00:00 and the date to Dec 1, 1989 but you can use it to set any desired time. SHAPES3.PAS, an example of menus ____________________________________________________________ This program is not very useful, but it illustrates one way to handle menus in a Pascal program, but only if you are using version 3.0 of TURBO Pascal. Chapter 13 included the identical program done slightly differently for use with the TURBO Pascal 4.0 or 5.x compilers. You can study the structure and imagine many ways a menu can be used to improve the usefulness of your own programs. OT.PAS, The OAKTREE directory program ____________________________________________________________ This program should be very useful to you, especially if you have a hard disk. It will list the entire contents of your hard disk (or floppy) in a very easy to read and easy to use form. The program is documented in the file named OT.DOC. It uses many of the TURBO Pascal extensions and will probably Page 14-4 Complete Sample Programs not compile with any other Pascal compiler without extensive modifications. You will find two versions of the source code for this program, one named OT3.PAS for use with TURBO Pascal version 3.0, and another named OT4.PAS for use with versions 4.0 or 5.x of the TURBO Pascal compiler. You should rename one of them OT.PAS for use with your particular compiler. The two versions are different in a number of ways. The first version was written for TURBO Pascal version 3.0 and was only slightly modified for this new version of the tutorial. The newer version, OT4.PAS, was modified extensively to use some of the procedures provided by Borland such as GetDate, GetTime, FindFirst, and FindNext. The program for version 4.0 or 5.x is somewhat smaller since the predefined procedures use fewer characters to perform a given job, and the executable version shows an even greater reduction in size. Apparently Borland has done a very good job in code size reduction with the introduction of each new version of TURBO Pascal. It would benefit you greatly to study the two versions of OT.PAS side by side and compare the benefits of using the predefined procedures. Examine the procedure named Count_Print_Lines in each of the OT.PAS programs, and you will see a marked difference between them. OT3.PAS counts the lines of output, and when it detects that 55 lines have been output, it makes 11 Writeln calls to space the paper up to 66 lines. This works fine for a printer using continuous form paper, but it doesn't work with a laser printer that happens to be set to some number other than 66 lines. For that reason, OT4.PAS uses a form feed after it detects that 55 lines have been output. You can choose the proper OT.PAS program for your compiler and use whichever paper advance method suits you best. This is a very useful program, so you should spend the time necessary to both understand it and modify it for your own needs. You will find either program to be a good example of linked lists because they include a sort routine using a dynamically allocated B-TREE and another sorting routine that uses a dynamically allocated linked list with a bubble sort. These methods are completely defined in Niklaus Wirth's book, "Algorithms + Data Structures = Programs", a highly recommended book if you are interested in advanced programming techniques. It might also be pointed out that both OT3.PAS and OT4.PAS also makes use of recursive methods for both sorting and handling subdirectories. It is definitely an example of advanced programming methods, and it would be a good vehicle for your personal study. Page 14-5 Complete Sample Programs MOST IMPORTANT - Your own programs ____________________________________________________________ Having completed this tutorial on Pascal, you are well on your way to becoming a proficient Pascal programmer. The best way you can improve your skills now is to actually write Pascal programs. Another way to aid in your building of skill and confidence is to study other Pascal programs. Many programming examples can be found in computing magazines and books. There are many books available devoted entirely to TURBO Pascal and you would do well to visit your local bookstore and review a few of them. You already own one of the best books available for reference if you are using TURBO Pascal. Although the TURBO Pascal reference manual is worth very little as a learning tool, it is excellent as a language reference manual. Now that you have completed all 14 chapters of this tutorial, you have a good grasp of the terminology of Pascal and should have little trouble reading and understanding your reference manual. Your only limitation at this point is your own perseverance and imagination. OBJECT ORIENTED PROGRAMMING ____________________________________________________________ If you are relatively new to programming, it would be good for you to completely ignore the last two chapters of this tutorial until you gain some experience with standard Pascal. Object oriented programming is a relatively new development, and although it is a meaningful study, it is totally unnecessary for small programming projects. Once your programs grow to the point where you feel comfortable with a 1000 line Pascal program, you are ready for a study of this new packaging technique. Until then, it may only serve to confuse you. Whatever your programming level or needs may be, Pascal can fulfill them and do so in a very elegant way, Happy Programming. Page 14-6