home *** CD-ROM | disk | FTP | other *** search
/ Oakland CPM Archive / oakcpm.iso / sigm / vol148 / tutor.prn < prev   
Encoding:
Text File  |  1984-04-29  |  7.9 KB  |  259 lines

  1. Lets select option New Student from the main menu.  When we add a
  2. new student we set the flag 'command' to N and call procedure ADD.
  3.  
  4. procedure ADD
  5.      if no space in file
  6.         print file full message
  7.         exit procedure
  8.      else add another record
  9.      increment record count
  10.      blank ( student )
  11.      ChangeAddress ( student, goodstatus )
  12.      display ( console, student )
  13.      if a valid key field then
  14.         insert key field into grades matrix
  15.         insert key and record pointer into index list
  16.         update name&address data file
  17.         set file updated flag to true
  18.         update total records on file.
  19.         give operator chance to change make changes
  20. end add
  21.  
  22.  
  23. It is most important when dealing with strings that they be
  24. initialized to some value even if that is a zero length string.  If
  25. strings are not initialized the result will be garbage on the screen
  26. and the strong possibility that your program will blow up.  Both ADD
  27. and MODIFY call ChangeAddress.
  28.  
  29. The basic concept of ChangeAddress is to display each field and
  30. allow any field to be changed.    If new data is input then it will
  31. replace the previous data.
  32.  
  33. It is very difficult to write a generalized procedure for data entry
  34. since the typing is so strict with Pascal.  I took the easy way out
  35. and wrote a data entry for each field in the record. As you can see
  36. this takes a lot of code and in addition makes this procedure very
  37. specific to this one program.
  38.  
  39.  
  40. procedure ChangeAddress
  41.      set returned status flag to true
  42.      print enter message if changing data
  43.      if adding new records
  44.          enter id number
  45.          search index list for match
  46.          if already on file
  47.              read student record from file
  48.              print error message
  49.              set returned status flag to false
  50.              exit procedure
  51.      else print id number
  52.      for each field
  53.          print title string
  54.          request for new inputs
  55.      set updated flag to true
  56. end changeaddress
  57.  
  58.  
  59. ChangeAddress will allow us to change both a blank record and one
  60. that is full.  The procedure input() displays a text message and the
  61. passed string then places the cursor on the next line to await any
  62. input.    If the string's length is zero then the cursor remains after
  63. the text message.
  64.  
  65.  
  66. ChangeGrades is very straight forward.
  67.  
  68. procedure ChangeGrades
  69.      write student's name
  70.      for each grade
  71.         print the grade and request change
  72.         if changed then store new grade
  73. end changegrades
  74.  
  75.  
  76. Notice when we say:
  77.  
  78.      for g:=exam1 to final do
  79.          write ( ord(g):3, grades [ R,g ]:4, ' ?' );
  80.  
  81. that we are printing the ordinal value of an enumerated type.  The
  82. enumerated type gradetype was setup so that the ordinal value of id
  83. = 0; exam1 = 1; exam2 = 2, ...    The ordinal value of exam1 is 1 and
  84. you will see a 1 printed in a field width of 3.  This is not a trick
  85. it is perfectly correct Pascal and should be taken advantage of at
  86. any time.
  87.  
  88.  
  89. Lets take a look at FIND and CHANGE.  The pseudo-code for FIND is:
  90.  
  91.      procedure FIND
  92.         input a key to search for
  93.         search index tree for key
  94.         if match then
  95.            read data file at record R
  96.            display record
  97.         else print no match found
  98.      end find
  99.  
  100.  
  101. and for CHANGE it is:
  102.  
  103.      procedure CHANGE
  104.         input a key to search for
  105.         search index tree for key
  106.         if match then
  107.            read data file at record R
  108.            display record
  109.            change 1) address
  110.               2) grades
  111.         else print no match found
  112.      end change
  113.  
  114.  
  115. The first 5 steps are exactly the same for both procedures and so
  116. can easily be combined into a single procedure.  We can call this
  117. procedure MODIFY.
  118.  
  119.      procedure MODIFY
  120.         input a key to search for
  121.         search index tree for key
  122.         if match then
  123.            read data file at record R
  124.            case activity flag of
  125.          C: change A(ddress G(rades
  126.               A: change address
  127.               G: change grades
  128.             display record
  129.             updata data file
  130.          F: display record
  131.            end case
  132.         else print no match found.
  133.      end modify
  134.  
  135.  
  136. We call upon MODIFY whenever we need to FIND or CHANGE a record.
  137. The procedure ADD calls MODIFY after inputting a new record in case
  138. you want to change the address or grades.  If called by ADD we set
  139. the flag command to 'C' so that changeaddress will operate on the
  140. current record.
  141.  
  142.  
  143. procedure list
  144.      direct output to console or printer
  145.      print the entire tree <recursively>
  146. end list
  147.  
  148. Procedure LIST prints the records in order by traversing the index
  149. list and returning the record pointer from procedure FREAD.  FREAD
  150. also reads the disk file record R into the record variable student
  151. which may be displayed on the console or printer.
  152.  
  153.  
  154. procedure display ( VAR output: TEXT; VAR student: sturec );
  155.     print student's name and address
  156.     print student's grades
  157. end display;
  158.  
  159.  
  160. Procedure display is very versatile.  We can print to any text
  161. device
  162.  
  163. 1).    rewrite ( 'LST:', output );
  164.     display ( output, student );
  165.  
  166. 2).    rewrite ( 'CON:', output );
  167.     display ( output, student );
  168.  
  169. 3).    rewrite ( 'A:STUDENT.LST', output );
  170.     display ( output, student );
  171.  
  172.  
  173. by opening the file as in 1) the output will be directed to the list
  174. device, as in 2) the output will be directed to the console, and as
  175. in 3) the output will go to the disk file A:STUDENT.LST.
  176.  
  177.  
  178. procedure fclose
  179.    OPEN 'STUDENT.NDX' for WRITE assign StuNdx
  180.    writeln( StuNdx, rof );
  181.    writeln( StuNdx, date );
  182.    OPEN 'STUDENT.GDS' for WRITE assign StuGrades
  183.    FOR R:=1 TO rof DO
  184.       write ( StuGrades, grades[R] )
  185. end fclose
  186.  
  187.  
  188. The procedure fclose writes out the total number of records and the
  189. date to file student.ndx and the array grades to file student.gds.
  190.  
  191. Notice that the procedures fclose, initialize, and list all have
  192. local file variables.  This allows us to close the files.  Pascal/Z
  193. has three ways that it can close files: 1) upon exit of a procedure
  194. in which the file variable was declared, 2) when a file variable is
  195. reused, and 3) upon termination of the program block.
  196.  
  197. We will wrap up with a look at the relationships between the file
  198. 'student.dat', the array grades and the index list.
  199.  
  200. STUDENT.DAT                  GRADES MATRIX          INDEX
  201. -----------                  -------------          -----
  202. RCD#  id,name,address,city,st,zip     ROW  id,exam1,exam2,    id,RCD#
  203. 1)    100,Jones,...              1)   100,10, ...          100,1
  204. 2)    101,Smith,...              2)   101,90, ...          101,2
  205. 3)    102,Wilson,...              3)   102,90, ...          102,3
  206. 4)    103,Edwards,...              4)   103,90, ...          103,4
  207.  
  208.  
  209. It can be seen from the above that student Edwards will be stored in
  210. record #4 in student.dat and row 4 in the array grades and that the
  211. index list will store the student.id and the record pointer.  Also,
  212. the student.id is stored in the array grades.  This can be used both
  213. for searching and for verification in that the student.id must equal
  214. grades[R,id].
  215.  
  216.  
  217. I hope that this short program has shown you that Pascal is very
  218. flexible.  We used records, enumerated types, data files, memory
  219. files, pointers, linked lists, strings, booleans, characters,
  220. external functions, functions, and procedures, etc.  By starting out
  221. writing pseudo-code in plain English one can almost translate
  222. directly into the Pascal language.
  223. If you enjoy short programs (ones we might fit into our news letter)
  224. please write and let us know what you would like to see and maybe we
  225. can make this a premanent feature in the newsletter.
  226.  
  227.  
  228.  
  229.  
  230. Ray Penley
  231.  
  232.  
  233. PS.  I have sent Charlie version 2 of STUDENT.PAS and it
  234. incorporates many additional features and more error handling.
  235.  
  236.  
  237. REFERENCES:
  238.  
  239. A. Excellent place to start.
  240. 1) Grogono, P., "Programming in Pascal", (Addison-Wesley, Reading,
  241. MA.)
  242.  
  243. 2) Findlay & Watt, "Pascal. An Introduction to Methodical
  244. Programming", (Computer Science Press, Potomac, MD.)
  245.  
  246. B. Where to go next.
  247. 1) Kernighan & Plauger, "Software Tools in Pascal", (Addison-Wesley,
  248. Reading, MA.)
  249.  
  250. 2) "Practical Pascal Programs", (OSBORNE/McGraw-Hill, Berkley, CA.)
  251.  
  252. C. HEAVY STUFF (If you can read and understand these then you should
  253. be writing another "WordStar", or "dBASE-II", or "MultiPlan").
  254. 1) Wirth, N., "Algorithms + Data Structures = Programs",
  255. (Prentice-Hall, Englewood Cliffs, NJ.)
  256.  
  257. 2) Tenenbaum & Augenstein, "Data Structures Using Pascal",
  258. (Prentice-Hall, Englewood Cliffs, NJ.)
  259.