home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Simtel MSDOS 1992 September
/
Simtel20_Sept92.cdr
/
msdos
/
pascal
/
pasctxt.arc
/
CHAP14.TXT
< prev
next >
Wrap
Text File
|
1988-01-15
|
16KB
|
389 lines
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 - 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 - 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 - 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 - 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,
greatly adding to the flexibility. After the
Page 90
CHAPTER 14 - Complete sample programs
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 - 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 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.
Page 91
CHAPTER 14 - Complete sample programs
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.
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, to list your Pascal programs
Since the differences between TURBO Pascal 3.0 and 4.0
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, rename LIST4.PAS to LIST.PAS
before continuing on to the next section.
LIST 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 13 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 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, 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.COM, and can be run
like any other .COM or .EXE program.
Page 92
CHAPTER 14 - Complete sample programs
TIMEDATE, 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 Pascal
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 using the means
of defining a DOS call as it has been revised for the newer
version. 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.
SETTIME, 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 Jan
15, 1988 but you can use it to set any desired time.
You could ask the operator for the desired time and
date or use the procedure to get the present date and set
the time to noon or whatever time you desire. Its
usefulness is limited only by your imagination.
SHAPES3, 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 compiler. You can study the structure and
imagine many ways a menu can be used to improve the
usefulness of your own programs.
OT, 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 OT.DOC, and
is precompiled for you in OT.COM in case you are not using
Page 93
CHAPTER 14 - Complete sample programs
TURBO Pascal. It uses many of the TURBO Pascal extensions
and will probably 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 version 4.0 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
over a year ago 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 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 version 4.0.
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.
You will find either program to be a good example of
linked lists because it includes 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.
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. One of the best books available is
Page 94
CHAPTER 14 - Complete sample programs
"Programming in Pascal" by Peter Grogono, and another is
"Oh! Pascal!" by Doug Cooper and Michael Clancy.
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.
Happy programming.
Page 95