home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload / ShartewareOverload.cdr / progm / pascal1.zip / CHAP14.TXT < prev    next >
Text File  |  1988-01-15  |  16KB  |  389 lines

  1.  
  2.                   CHAPTER 14 - Complete sample programs
  3.  
  4.  
  5.              Prior  to this point, this tutorial has given you  many
  6.         example  programs  illustrating a point of  some  kind,  but
  7.         these  have  all been "nonsense" programs as  far  as  being
  8.         useful.  It would be a disservice to you to simply quit with
  9.         only  tiny programs to study so the following  programs  are
  10.         offered  to  you  as examples  of  good  Pascal  programming
  11.         practice.   They  are useful programs, but  they  are  still
  12.         short enough to easily grasp their meaning.  We will discuss
  13.         them one at a time.
  14.  
  15.                        AMORTIZATION TABLE GENERATOR
  16.  
  17.              This  is  not one program, but five.  Each  one  is  an
  18.         improvement on the previous one, and the series is  intended
  19.         to give you an idea of program development.
  20.  
  21.         AMORT1  - This  is  the  bare outline  of  the  amortization
  22.                 program.  Although  it is an operating  program,  it
  23.                 doesn't  do  very  much.   After  some  thought  and
  24.                 planning,  the main program was written to allow for
  25.                 an  initialization,  then an annual repeating  loop.
  26.                 The  annual loop would require a header,  a  monthly
  27.                 calculation,  and  an  annual  balance.  Finally,  a
  28.                 procedure  was outlined for each of these  functions
  29.                 with  a  minimum of calculations in each  procedure.
  30.                 This program can be compiled and run to see that  it
  31.                 does  do something for each month and for each year.
  32.                 It has a major problem because it does not stop when
  33.                 the loan is payed off but keeps going to the end  of
  34.                 that year. The primary structure is complete.
  35.  
  36.         AMORT2  - This  is an improvement over AMORT1.  The  monthly
  37.                 calculations  are  correct but the final payment  is
  38.                 still  incorrectly  done.  Notice that for  ease  of
  39.                 testing,  the  loan variables are simply defined  as
  40.                 constants in the initialize procedure.  To make  the
  41.                 procedures  easier to find,  comments with asterisks
  42.                 were added.  This program is nearly usable.  Compile
  43.                 and run it.
  44.  
  45.         AMORT3 - Now we calculate the final payment correctly and we
  46.                 have  a correct annual header with column  headings.
  47.                 We have introduced a new variable to be used for  an
  48.                 annual  interest accumulation.  This is neat to have
  49.                 at  income  tax  time.  This  program  can  also  be
  50.                 compiled and run.
  51.  
  52.         AMORT4  - This program does nearly everything we would  like
  53.                 it to do. All of the information needed to build the
  54.                 table for any loan is now read in from the keyboard,
  55.                 greatly   adding  to  the  flexibility.   After  the
  56.  
  57.  
  58.                                  Page 90
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.                   CHAPTER 14 - Complete sample programs
  69.  
  70.  
  71.                 information  is available,  the monthly  payment  is
  72.                 calculated    in    the   newly   added    procedure
  73.                 Calculate_Payment.   The  annual  header has  a  new
  74.                 line  added to include the original loan amount  and
  75.                 the  interest rate in the information.  Compile  and
  76.                 run this program to see its operation.
  77.  
  78.         AMORT5 - The  only additional feature in this program is the
  79.                 addition of a printout of the results. Examining the
  80.                 program,  you  will notice that many of  the  output
  81.                 statements  are  duplicated with the "Lst"  included
  82.                 for  the  device selection.  Compile  and  run  this
  83.                 program, but be sure to turn your printer on to  get
  84.                 a printout of the amortization table you ask for. If
  85.                 you  are  using TURBO Pascal version 3.0,  you  will
  86.                 need  to  either  comment out line 3  or  remove  it
  87.                 altogether.
  88.  
  89.                              TOP DOWN PROGRAMMING
  90.  
  91.              The  preceding  example  is an example  of  a  top-down
  92.         approach to programming.  This is where the overall task  is
  93.         outlined,  and  the details are added  in  whatever  fashion
  94.         makes  sense to the designer.  The opposite is  a  bottom-up
  95.         programming  effort,  in which the heart of the  problem  is
  96.         defined  and the rest of the program is built up around  it.
  97.         In this case, the monthly payment schedule would probably be
  98.         a  starting  point and the remainder of the  program  slowly
  99.         built  up  around it.  Use whichever method works  best  for
  100.         you.
  101.  
  102.              The final program AMORT5 is by no means a program which
  103.         can  never  be  improved upon.   Many  improvements  can  be
  104.         thought  of.   These  will be exercises for you  if  you  so
  105.         desire.
  106.  
  107.         1.  In the data input section, ask if a printout is desired,
  108.             and  only print if it was requested.  This would involve
  109.             defining  a new variable and "if" statements controlling
  110.             all write statements with "Lst" as a device selector.
  111.  
  112.         2.  Format the printout with a formfeed every three years to
  113.             cause  a neater printout.  The program presently  prints
  114.             data right across the paper folds  with no regard to the
  115.             top of page.
  116.  
  117.         3.  Modify  the  program  to include  semimonthly  payments.
  118.             Payments  twice a month are becoming popular,  but  this
  119.             program cannot handle them.
  120.  
  121.  
  122.  
  123.  
  124.                                  Page 91
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.                   CHAPTER 14 - Complete sample programs
  135.  
  136.  
  137.         4.  Instead of listing the months as numbers,  put in a case
  138.             statement to cause the months to be printed out as three
  139.             letter  names.  You could also include the  day  of  the
  140.             month when the payment is due.
  141.  
  142.         5.  Any  other modification you can think up.  The more  you
  143.             modify this and other programs,  the more experience and
  144.             confidence you will gain.
  145.  
  146.                     LIST, to list your Pascal programs
  147.  
  148.              Since the differences between TURBO Pascal 3.0 and  4.0
  149.         are  significant, two files are included here.  If  you  are
  150.         using TURBO Pascal 3.0, rename LIST3.PAS to LIST.PAS, and if
  151.         you are using TURBO Pascal 4.0, rename LIST4.PAS to LIST.PAS
  152.         before continuing on to the next section.
  153.  
  154.              LIST is a very useful program that you can use to  list
  155.         your  Pascal  programs  on  the printer.   It  can  only  be
  156.         compiled with TURBO Pascal because it uses TURBO extensions.
  157.         The two extensions it uses are the string type variable  and
  158.         (in the case of TURBO Pascal version 3.0), the absolute type
  159.         variable.   The  absolute type variable in line 13  and  the
  160.         coding in the Initialize procedure is an example of how  you
  161.         can read in the parameters given on the command line.
  162.  
  163.              If  you  are  using  TURBO  Pascal  4.0  a   completely
  164.         different  method is used in the Initialize procedure  which
  165.         should be no problem for you to understand at this point.
  166.  
  167.              To use this program to print out the last program,  for
  168.         example,  you  would enter the following at the  DOS  prompt
  169.         LIST AMORT5.PAS.  This program reads in the AMORT5.PAS  from
  170.         the  command line and uses it to define the input file.   It
  171.         should be pointed out that this program cannot be run from a
  172.         "compiled  in  memory"  compilation with  the  TURBO  Pascal
  173.         compiler.  It must be compiled to a Disk file, and you  must
  174.         quit  TURBO Pascal in order to run it from the  DOS  command
  175.         level.
  176.  
  177.              The parameter, AMORT5.PAS, is stored at computer memory
  178.         location  80(hexadecimal)  referred  to  the  present   code
  179.         segment.   If you didn't understand that, don't  worry,  you
  180.         can still find the input parameter in any program using  the
  181.         method given in the initialize procedure for your version of
  182.         TURBO Pascal.
  183.  
  184.              If you do not have TURBO Pascal, but you are using  MS-
  185.         DOS or PC-DOS, you can still use this program because it  is
  186.         on  your disk already compiled as LIST.COM, and can  be  run
  187.         like any other .COM or .EXE program.
  188.  
  189.  
  190.                                  Page 92
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.                   CHAPTER 14 - Complete sample programs
  201.  
  202.  
  203.  
  204.                   TIMEDATE, to get today's time and date
  205.  
  206.              This  is a very useful program as an example  of  using
  207.         some  of  the extensions of TURBO Pascal if  you  are  using
  208.         TURBO Pascal 3.0.  It interrogates the inner workings of DOS
  209.         and  gets  the present time and date for you,  provided  you
  210.         entered  them  correctly when you turned your  computer  on.
  211.         The  procedure Time_And_Date can be included in  any  Pascal
  212.         program  you  write to give you the time and date  for  your
  213.         listings.   As an exercise in programming, add the time  and
  214.         date to the program LIST to improve on its usefulness.
  215.  
  216.              The  program named TIMEDAT4.PAS does the same thing  as
  217.         the last, but it works with TURBO Pascal 4.0 using the means
  218.         of defining a DOS call as it has been revised for the  newer
  219.         version.   It turns out to be an almost trivial program  but
  220.         is still a good illustration of how to use some of the newer
  221.         Borland extensions to Pascal.
  222.  
  223.                      SETTIME, a useful utility program
  224.  
  225.              This program is very interesting in that it changes the
  226.         date  and time stamp on any file in the  current  directory.
  227.         It  is the program used to set the time and date on  all  of
  228.         the  files  on  the distribution disks  included  with  this
  229.         tutorial.  It sets the time to 12:00:00 and the date to  Jan
  230.         15, 1988 but you can use it to set any desired time.
  231.  
  232.              You  could  ask the operator for the desired  time  and
  233.         date  or use the procedure to get the present date  and  set
  234.         the  time  to  noon  or  whatever  time  you  desire.    Its
  235.         usefulness is limited only by your imagination.
  236.  
  237.                         SHAPES3, an example of menus
  238.  
  239.              This program is not very useful, but it illustrates one
  240.         way to handle menus in a Pascal program, but only if you are
  241.         using version 3.0 of TURBO Pascal.  Chapter 13 included  the
  242.         identical program done slightly differently for use with the
  243.         TURBO Pascal 4.0 compiler.  You can study the structure  and
  244.         imagine  many  ways  a  menu can  be  used  to  improve  the
  245.         usefulness of your own programs.
  246.  
  247.                      OT, The OAKTREE directory program
  248.  
  249.              This  program should be very useful to you,  especially
  250.         if  you have a hard disk.  It will list the entire  contents
  251.         of  your  hard disk (or floppy) in a very easy to  read  and
  252.         easy to use form.  The program is documented in OT.DOC,  and
  253.         is  precompiled for you in OT.COM in case you are not  using
  254.  
  255.  
  256.                                  Page 93
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.                   CHAPTER 14 - Complete sample programs
  267.  
  268.  
  269.         TURBO  Pascal.  It uses many of the TURBO Pascal  extensions
  270.         and will probably not compile with any other Pascal compiler
  271.         without extensive modifications.
  272.  
  273.              You will find two versions of the source code for  this
  274.         program, one named OT3.PAS for use with TURBO Pascal version
  275.         3.0,  and another named OT4.PAS for use with version 4.0  of
  276.         the  TURBO Pascal compiler.  You should rename one  of  them
  277.         OT.PAS for use with your particular compiler.
  278.  
  279.              The  two  versions are different in a number  of  ways.
  280.         The  first version was written for TURBO Pascal version  3.0
  281.         over a year ago and was only slightly modified for this  new
  282.         version  of the tutorial.  The newer version,  OT4.PAS,  was
  283.         modified extensively to use some of the procedures  provided
  284.         by   Borland  such  as  GetDate,  GetTime,  FindFirst,   and
  285.         FindNext.   The program for version 4.0 is somewhat  smaller
  286.         since  the  predefined procedures use  fewer  characters  to
  287.         perform  a  given job, and the executable version  shows  an
  288.         even greater reduction in size.  Apparently Borland has done
  289.         a very good job in code size reduction with the introduction
  290.         of version 4.0.
  291.  
  292.              It would benefit you greatly to study the two  versions
  293.         of OT.PAS side by side and compare the benefits of using the
  294.         predefined procedures.
  295.  
  296.              You  will find either program to be a good  example  of
  297.         linked  lists  because it includes a sort  routine  using  a
  298.         dynamically  allocated  B-TREE and another  sorting  routine
  299.         that  uses  a  dynamically  allocated  linked  list  with  a
  300.         bubble_sort.   These  methods  are  completely  defined   in
  301.         Niklaus  Wirth's  book,  "Algorithms  +  Data  Structures  =
  302.         Programs",  a highly recommended book if you are  interested
  303.         in advanced programming techniques.
  304.  
  305.              It  might  also be pointed out that  both  OT3.PAS  and
  306.         OT4.PAS also makes use of recursive methods for both sorting
  307.         and handling subdirectories.  It is definitely an example of
  308.         advanced programming methods, and it would be a good vehicle
  309.         for your personal study.
  310.  
  311.                      Most Important - Your own programs
  312.  
  313.              Having completed this tutorial on Pascal, you are  well
  314.         on your way to becoming a proficient Pascal programmer.  The
  315.         best  way  you can improve your skills now  is  to  actually
  316.         write Pascal programs.  Another way to aid in your  building
  317.         of  skill and confidence is to study other Pascal  programs.
  318.         Many   programming  examples  can  be  found  in   computing
  319.         magazines  and  books.  One of the best books  available  is
  320.  
  321.  
  322.                                  Page 94
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.  
  332.                   CHAPTER 14 - Complete sample programs
  333.  
  334.  
  335.         "Programming  in  Pascal" by Peter Grogono, and  another  is
  336.         "Oh! Pascal!" by Doug Cooper and Michael Clancy.
  337.  
  338.              You  already  own one of the best books  available  for
  339.         reference if you are using TURBO Pascal.  Although the TURBO
  340.         Pascal  reference manual is worth very little as a  learning
  341.         tool,  it is excellent as a language reference manual.   Now
  342.         that  you have completed all 14 chapters of  this  tutorial,
  343.         you  have  a  good grasp of the terminology  of  Pascal  and
  344.         should  have little trouble reading and  understanding  your
  345.         reference  manual.   Your only limitation at this  point  is
  346.         your own perseverance and imagination.
  347.  
  348.              Happy programming.
  349.  
  350.  
  351.  
  352.  
  353.  
  354.  
  355.  
  356.  
  357.  
  358.  
  359.  
  360.  
  361.  
  362.  
  363.  
  364.  
  365.  
  366.  
  367.  
  368.  
  369.  
  370.  
  371.  
  372.  
  373.  
  374.  
  375.  
  376.  
  377.  
  378.  
  379.  
  380.  
  381.  
  382.  
  383.  
  384.  
  385.  
  386.  
  387.  
  388.                                  Page 95
  389.