home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / TURBOPAS / PASTUTOR.ZIP / CHAP13.TXT < prev    next >
Encoding:
Text File  |  1986-07-14  |  11.8 KB  |  257 lines

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