home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 15 / CDACTUAL15.iso / cdactual / program / pascal / PAS_TUT.ZIP / CHAP16.TXT < prev    next >
Encoding:
Text File  |  1991-02-04  |  11.3 KB  |  295 lines

  1.  
  2.  
  3.  
  4.                                                        Chapter 16
  5.                                          COMPLETE SAMPLE PROGRAMS
  6.  
  7.  
  8. Prior to this point, this tutorial has given you many example
  9. programs illustrating a point of some kind, but these have all been
  10. "nonsense" programs as far as being useful.  It would be a
  11. disservice to you to simply quit with only tiny programs to study,
  12. so the following programs are offered to you as examples of good
  13. Pascal programming practice.  They are useful programs, but they
  14. are still short enough to easily grasp their meaning.  We will
  15. discuss them one at a time.
  16.  
  17.  
  18.  
  19. AMORTIZATION TABLE GENERATOR
  20. _________________________________________________________________
  21.  
  22. This is not one program, but five.  Each one is an improvement on
  23. the previous one, and the series is intended to give you an idea
  24. of program development.
  25.  
  26. AMORT1.PAS - This is the bare outline of the amortization program. 
  27.           Although it is an operating program, it doesn't do very
  28.           much. After some thought and planning, the main program
  29.           was written to allow for an initialization, then an
  30.           annual repeating loop. The annual loop would require a
  31.           header, a monthly calculation, and an annual balance.
  32.           Finally, a procedure was outlined for each of these
  33.           functions with a minimum of calculations in each
  34.           procedure. This program can be compiled and run to see
  35.           that it does do something for each month and for each
  36.           year. It has a major problem because it does not stop
  37.           when the loan is payed off but keeps going to the end of
  38.           that year. The primary structure is complete.
  39.  
  40. AMORT2.PAS - This is an improvement over AMORT1. The monthly
  41.           calculations are correct but the final payment is still
  42.           incorrectly done. Notice that for ease of testing, the
  43.           loan variables are simply defined as constants in the
  44.           initialize procedure. To make the procedures easier to
  45.           find, comments with asterisks were added. This program
  46.           is nearly usable.  Compile and run it.
  47.  
  48. AMORT3.PAS - Now we calculate the final payment correctly and we
  49.           have a correct annual header with column headings. We
  50.           have introduced a new variable to be used for an annual
  51.           interest accumulation. This is neat to have at income tax
  52.           time. This program can also be compiled and run.
  53.  
  54. AMORT4.PAS - This program does nearly everything we would like it
  55.           to do. All of the information needed to build the table
  56.           for any loan is now read in from the keyboard, greatly
  57.  
  58.                                                         Page 16-1
  59.  
  60.                             Chapter 16 - Complete Sample Programs
  61.  
  62.           adding to the flexibility. After the information is
  63.           available, the monthly payment is calculated in the newly
  64.           added procedure Calculate_Payment.  The annual  header
  65.           has a new line added to include the original loan amount
  66.           and the interest rate in the information. Compile and run
  67.           this program to see its operation.
  68.  
  69. AMORT5.PAS - The only additional feature in this program is the
  70.           addition of a printout of the results. Examining the
  71.           program, you will notice that many of the output
  72.           statements are duplicated with the Lst included for the
  73.           device selection. Compile and run this program, but be
  74.           sure to turn your printer on to get a printout of the
  75.           amortization table you ask for. If you are using TURBO
  76.           Pascal version 3.0, you will need to either comment out
  77.           line 3 or remove it altogether.
  78.  
  79.  
  80.  
  81. TOP DOWN PROGRAMMING
  82. _________________________________________________________________
  83.  
  84. The preceding example is an example of a top-down approach to
  85. programming.  This is where the overall task is outlined, and the
  86. details are added in whatever fashion makes sense to the designer. 
  87. The opposite is a bottom-up programming effort, in which the heart
  88. of the problem is defined and the rest of the program is built up
  89. around it.  In this case, the monthly payment schedule would
  90. probably be a starting point and the remainder of the program
  91. slowly built up around it.  Use whichever method works best for
  92. you.
  93.  
  94. The final program AMORT5.PAS is by no means a program which can
  95. never be improved upon.  Many improvements can be thought of. 
  96. These will be exercises for you if you so desire.
  97.  
  98.  
  99. 1.   In the data input section, ask if a printout is desired, and
  100.      only print if it was requested. This would involve defining
  101.      a new variable and if statements controlling all write
  102.      statements with Lst as a device selector.
  103.  
  104. 2.   Format the printout with a formfeed every three years to cause
  105.      a neater printout. The program presently prints data right
  106.      across the paper folds  with no regard to the top of page.
  107.  
  108. 3.   Modify the program to include semimonthly payments. Payments
  109.      twice a month are becoming popular, but this program cannot
  110.      handle them.
  111.  
  112. 4.   Instead of listing the months as numbers, put in a case
  113.      statement to cause the months to be printed out as three
  114.      letter names. You could also include the day of the month when
  115.      the payment is due.
  116.  
  117.                                                         Page 16-2
  118.  
  119.                             Chapter 16 - Complete Sample Programs
  120.  
  121.  
  122. 5.   Any other modification you can think up. The more you modify
  123.      this and other programs, the more experience and confidence
  124.      you will gain.
  125.  
  126.  
  127.  
  128. LIST.PAS, to list your Pascal programs
  129. _________________________________________________________________
  130.  
  131. LIST.PAS is a very useful program that you can use to list your
  132. Pascal programs on the printer.  It can only be compiled with TURBO
  133. Pascal because it uses a TURBO extension, the string type variable.
  134.  
  135. The method used in the Initialize procedure to read the command
  136. line parameter should be no problem for you to understand at this
  137. point.  To use this program to print out the last program, for
  138. example, you would enter the following at the DOS prompt LIST
  139. AMORT5.PAS.  This program reads in the AMORT5.PAS from the command
  140. line and uses it to define the input file.  It should be pointed
  141. out that this program cannot be run from a "compiled in memory"
  142. compilation with the TURBO Pascal compiler.  It must be compiled
  143. to a Disk file, and you must quit TURBO Pascal in order to run it
  144. from the DOS command level.
  145.  
  146. The parameter read from the command line, AMORT5.PAS, is stored at
  147. computer memory location 80(hexadecimal) referred to the present
  148. code segment.  If you didn't understand that, don't worry, you can
  149. still find the input parameter in any program using the method
  150. given in the initialize procedure for your version of TURBO Pascal.
  151.  
  152. If you are not using a TURBO Pascal compiler, but you are using
  153. MS-DOS or PC-DOS, you can still use this program because it is
  154. provided on your disk already compiled as LIST.EXE, and can be run
  155. like any other .COM or .EXE program.
  156.  
  157.  
  158.  
  159. TIMEDATE.PAS, to get today's time and date
  160. _________________________________________________________________
  161.  
  162. This is a very useful program as an example of using some of the
  163. extensions of TURBO Pascal.  It interrogates the inner workings of
  164. DOS and gets the present time and date for you, provided you
  165. entered them correctly when you turned your computer on.  The
  166. procedure Time_And_Date can be included in any TURBO Pascal program
  167. you write to give you the time and date for your listings.  As an
  168. exercise in programming, add the time and date to the program LIST
  169. to improve on its usefulness.  It turns out to be an almost trivial
  170. program but is still a good illustration of how to use some of the
  171. newer Borland extensions to Pascal.  The observant student will
  172. notice that the time and date procedures have already been added
  173. to LIST.PAS.
  174.  
  175.  
  176.                                                         Page 16-3
  177.  
  178.                             Chapter 16 - Complete Sample Programs
  179.  
  180.  
  181. SETTIME.PAS, a useful utility program 
  182. _________________________________________________________________
  183.  
  184. This program is very interesting in that it changes the date and
  185. time stamp on any file in the current directory.  It is the program
  186. used to set the time and date on all of the files on the
  187. distribution disk included with this tutorial.  It sets the time
  188. to 12:00:00 and the date to Feb 4, 1991 but you can use it to set
  189. any desired time.
  190.  
  191.  
  192.  
  193. OT.PAS, The OAKTREE directory program
  194. _________________________________________________________________
  195.  
  196. This program should be very useful to you, especially if you have
  197. a hard disk.  It will list the entire contents of your hard disk
  198. (or floppy) in a very easy to read and easy to use form.  The
  199. program is documented in the file named OT.DOC.  It uses many of
  200. the TURBO Pascal extensions and will probably not compile with any
  201. other Pascal compiler without extensive modifications.
  202.  
  203. This is a very useful program, so you should spend the time
  204. necessary to both understand it and modify it for your own needs.
  205.  
  206. You will find this program to be a good example of linked lists
  207. because it includes a sort routine using a dynamically allocated
  208. B-TREE and another sorting routine that uses a dynamically
  209. allocated linked list with a bubble sort.  These methods are
  210. completely defined in Niklaus Wirth's book, "Algorithms + Data
  211. Structures = Programs", a highly recommended book if you are
  212. interested in advanced programming techniques.
  213.  
  214. It might also be pointed out that OT.PAS makes use of recursive
  215. methods for both sorting and handling subdirectories.  It is
  216. definitely an example of advanced programming methods, and it would
  217. be a good vehicle for your personal study.
  218.  
  219.  
  220.  
  221. MOST IMPORTANT - Your own programs
  222. _________________________________________________________________
  223.  
  224. Having completed this tutorial on Pascal, you are well on your way
  225. to becoming a proficient Pascal programmer.  The best way you can
  226. improve your skills now is to actually write Pascal programs. 
  227. Another way to aid in your building of skill and confidence is to
  228. study other Pascal programs.  Many programming examples can be
  229. found in computing magazines and books.  There are many books
  230. available devoted entirely to TURBO Pascal and you would do well
  231. to visit your local bookstore and review a few of them.
  232.  
  233.  
  234.  
  235.                                                         Page 16-4
  236.  
  237.                             Chapter 16 - Complete Sample Programs
  238.  
  239. You already own one of the best books available for reference if
  240. you are using TURBO Pascal.  Although the TURBO Pascal reference
  241. manual is worth very little as a learning tool, it is excellent as
  242. a language reference manual.  Now that you have completed all 16
  243. chapters of this tutorial, you have a good grasp of the terminology
  244. of Pascal and should have little trouble reading and understanding
  245. your reference manual.  Your only limitation at this point is your
  246. own perseverance and imagination.
  247.  
  248. Whatever your programming level or needs may be, Pascal can fulfill
  249. them and do so in a very elegant way,
  250.  
  251. Happy Programming.
  252.  
  253.  
  254.  
  255.  
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268.  
  269.  
  270.  
  271.  
  272.  
  273.  
  274.  
  275.  
  276.  
  277.  
  278.  
  279.  
  280.  
  281.  
  282.  
  283.  
  284.  
  285.  
  286.  
  287.  
  288.  
  289.  
  290.  
  291.  
  292.  
  293.  
  294.                                                         Page 16-5
  295.