home *** CD-ROM | disk | FTP | other *** search
/ CP/M / CPM_CDROM.iso / mbug / mbug003.arc / PASCAL.006 < prev    next >
Text File  |  1979-12-31  |  12KB  |  277 lines

  1.                     PASCAL  FOR   BASIC   PROGRAMMERS.
  2.  
  3.                                Dixon Kenney
  4.  
  5.                                  PART VI.          
  6.  
  7.      Iεá thi≤á nex⌠á par⌠ oµ thσ series¼á wσ continuσá witΦá thσá Inventor∙ ì
  8. prograφ wσ begaε iε thσ las⌠ part«  Iε tha⌠ part¼ wσ haΣ ß procedurσ calleΣ ì
  9. Menuwrite«á  Iε thσ codσ fo≥ thi≤ part¼á wσ don'⌠ includσ thσ procedure¼ iε ì
  10. orde≥ t∩ savσ space╗á however¼á wσ d∩ includσ thσ Includσ functioε call¼ t∩ ì
  11. indicatσ wherσ i⌠ woulΣ come«á  Iε thi≤ extendeΣ version¼ wσ creatσ ß file¼ ì
  12. checkinτ firs⌠ t∩ seσ tha⌠ i⌠ i≤ no⌠ alread∙ open«  Herσ i≤ thσ code.
  13.  
  14.  
  15.    1ááPrograφ Inventory_2;
  16.  
  17.    4ááConst
  18. ááá5áá  MenuItem≤   ╜ 10;
  19. ááá6áá  NameOfFilσ  ╜ 'INVENT.DAT';
  20. ááá7áá  DriveNamσ   ╜ 'B:';
  21.  
  22. ááá9ááType
  23. áá10áá  FileSpeπ ╜ String[14];
  24. áá11áá  St2░     ╜ String[20];
  25. áá12áá  St4░     ╜ String[40];
  26.  
  27. á 14áá  PartReπ ╜ Record
  28. áá15áá    PartNuφ      ║ 100░ .« 9999;
  29. áá16áá    Descriptioε  ║ St20;
  30. áá17áá    Cos⌠         ║ Real;
  31. áá18áá    Retai∞       ║ Real;
  32. áá19áá    MinO╚        ║ Integer;
  33. áá20áá    MaxReorde≥   ║ Integer;
  34. áá21áá    CurrentO╚    ║ Integer;
  35. áá22áá  End;
  36.  
  37.   24ááVar
  38. áá25áá  PartFilσ   ║ Filσ oµ PartRec;
  39. áá26áá  Par⌠       ║ PartRec;
  40. áá27áá  MenuLis⌠   ║ Arra∙ █ ░ .« 1╡ ▌ oµ St40;
  41. áá28áá  Iteφ       ║ Integer;
  42. áá29áá  Loope≥     ║ Boolean;
  43. áá30áá  FileNamσ   ║ FileSpec;
  44.  
  45.   33 {$╔ EXISTS.FN├ }
  46.  
  47. ááá1IááááFunctioε Exist≤ (Namσ ║ FileSpec⌐ ║ Booleaε ;
  48.  
  49.    3IááááVar
  50. ááá4Iáááá  ╞ ║ File;
  51.  
  52. ááá6IááááBegin
  53. ááá7Iáááá  Assign(F¼ Name);
  54. ááá8Iáááá{$I-}
  55. ááá9Iáááá  Reset(F);
  56. áá10Iáááá{$I+}
  57. áá11Iáááá  Exist≤ :╜ (IOResul⌠ ╜ 0)
  58. áá12IááááEnd╗  √ Functioε Exist≤ ² 
  59.   34 {$╔ CREATEFL.PR╧ }èááá1JááááProcedurσ CreateFile;
  60.  
  61. ááá3JááááBegin
  62. ááá4Jáááá  Iµ No⌠ Exists(FileName⌐ then
  63. ááá5Jáááá    Begin
  64. ááá6Jáááá      Assign(PartFile¼ FileName);
  65. ááá7Jáááá      Rewrite(PartFile);
  66. ááá8Jáááá      Close(PartFile);
  67. ááá9Jáááá      GotoXY(10¼ 20);
  68. áá10Jáááá      Writeln('Thσ filσ ',FileName,º i≤ no≈ open')
  69. áá11Jáááá    End
  70. áá12Jáááá  Else
  71. áá13Jáááá    Begin
  72. áá14Jáááá      GotoXY(10¼ 20);
  73. áá15Jáááá      Writeln('Thσ filσ ',FileName,º i≤ alread∙ open')
  74. áá16Jáááá    End
  75. áá17JááááEnd╗   √ Procedurσ OpenFilσ }
  76.  
  77.  35 {$╔ MENU.PR╧ }
  78.  
  79.  37ááá√ ¬ Thi≤ i≤ thσ beginninτ oµ thσ Maiε Sectioε oµ thσ Prograφ * }
  80.  
  81.  39áááBegin
  82.  
  83.  41ááá  FileNamσ  :╜ Concat(DriveName¼ NameOfFile);
  84.  
  85. á43ááá  Loope≥      :╜ True;
  86.  
  87.  45ááá  MenuList[0▌ :╜ 'MASTE╥ INVENTOR┘ MENU';
  88.  
  89. [ Lines have been omitted here - they are in the previous part's code ]
  90.  
  91.  65ááá    Casσ Iteφ of
  92.  
  93. █ Furthe≥ line≤ omitteΣ herσ - they'rσ iε thσ previou≤ codσáals∩ ]
  94.  
  95. á75ááá      9║ CreateFile;
  96. á76ááá    Else
  97. á77ááá      Begin
  98. á78ááá        Writeln╗ Writeln;
  99. á79ááá        Writeln(º ':20,'N∩ actioε possiblσ fo≥ typeΣ input')
  100. á80ááá      EnΣ      √ Elsσ ..}
  101. á81ááá    End╗   √ Casσ Iteφ ..« }
  102.  
  103.  83ááá  Delay(2000);
  104.  
  105.  85ááá  Unti∞ Loope≥ ╜ False;
  106.  
  107.  87áááEnd.
  108.  
  109.      Iε line≤ 6¼á 7¼ 1░ anΣ 30¼ wσ arσ defininτ somσ extrß items«  Thσ bes⌠ ì
  110. wa∙ t∩ d∩ thi≤ wheε yo⌡ arσ writinτ ß prograφ i≤ t∩ finΣ ou⌠ wherσ yo⌡ wan⌠ ì
  111. ß ne≈ iteφ first¼á anΣ theε writσ it≤ definitioε iε thσ correc⌠ areß later«  ì
  112. However¼á alway≤á remembe≥á tha⌠á thesσ item≤ mus⌠ bσ defineΣá firs⌠á iεá ß ì
  113. listinτá beforσá thσá prograφá caεá usσ theφ iε thσ codσá whicΦá i≤á t∩á bσ ì
  114. executed.
  115.  
  116.  
  117. è     Line≤ 14-2▓ definσ wha⌠ i≤ calleΣ ß RECORD«á  ┴ recorΣ i≤ simila≥ t∩ ß ì
  118. ╢ ° ┤ filinτ card¼á useΣ b∙ doctors¼á dentists¼á etπ t∩ kee≡ thei≥á patien⌠ ì
  119. records¼á o≥ ß housewifσ t∩ kee≡ ß recipe¼á o≥ s∩ forth«  EacΦ "cardó deal≤ ì
  120. witΦ onσ se⌠ oµ informatioε - onσ patient¼á onσ cake¼ or¼ a≤ here¼ onσ iteφ ì
  121. oµá stock«á  Thσá recorΣá i≤ divideΣ int∩ ß numbe≥ oµá FIELDS¼á whicΦá givσ ì
  122. differen⌠ piece≤ oµ informatioε abou⌠ thσ iteφ the∙ describσ - here¼ ß par⌠ ì
  123. number¼ ß description¼ thσ cos⌠ t∩ us¼ thσ retai∞ price¼ thσ minimuφ numbe≥ ì
  124. wσ neeΣ t∩ kee≡ oε hand¼á thσ maximuφ numbe≥ wσ woulΣ havσ afte≥ ß ful∞ re-ì
  125. order¼á anΣ thσ curren⌠ numbe≥ oµ unit≤ oµ thi≤ iteφ oε hand«á  Therσ arσ ╖ ì
  126. FIELD╙ iε thi≤ ▒ RECORD¼ anΣ eacΦ recorΣ ha≤ thσ samσ numbe≥ oµ fields.
  127.  
  128.      User≤ oµ dBasσ I╔ ough⌠ t∩ bσ familia≥ witΦ thesσ terms¼á anΣ the∙ wa∙ ì
  129. the∙á fi⌠ together«á Thσ file≤ whicΦ wσ creatσ iε thi≤ wa∙ arσá simila≥á t∩ ì
  130. Randoφá file≤á iε ß BASI├ system«á  Thesσ file≤ caε bσá accesseΣá randomly¼ ì
  131. sincσ eacΦ recorΣ i≤ oµ equa∞ length«á  So¼ b∙ "numberingó them¼ thσ systeφ ì
  132. caεá ge⌠ t∩ an∙ particula≥ recorΣ  immediately¼á  knowinτ wherσ eacΦ recorΣ ì
  133. begin≤ anΣ ends.
  134.  
  135.      Wσ caε als∩ havσ file≤ simila≥ t∩ ß BASI├ sequent- ia∞ file¼á a≤á wel∞ ì
  136. a≤ TEX╘ files¼ whicΦ arσ simpl∙ considereΣ t∩ bσ line≤ oµ text«  Sequentia∞ ì
  137. file≤ havσ t∩ bσ reaΣ sequentially¼ sincσ eacΦ recorΣ ma∙ bσ oµ ß differinτ ì
  138. length¼á anΣá thσá compute≥ can-no⌠ computσ jus⌠ wherσ ß recorΣ begin≤á anΣ ì
  139. ends.
  140.  
  141. Note the way in which a record is defined here -
  142.  
  143.                <record_label> = RECORD
  144.                <field_label>  :  <type> ..
  145.                END
  146.  
  147.      Linσá 1╡ i≤ ß sub-rangσ type«á  Thσ sub-rangσ oµ ß typσ specifie≤á thσ ì
  148. minimuφá anΣá maximuφ limit≤ oµ thσ field«á  Here¼á ß PartNuφá i≤á oµá typσ ì
  149. integer¼ bu⌠ thσ limit≤ arσ defineΣ - PartNuφ mus⌠ bσ ß fou≥ dig-i⌠ intege≥ ì
  150. number«  Sub-range≤ caε onl∙ bσ oµ scala≥ type≤ - integer¼ boolean¼ char¼ ß ì
  151. declareΣá scala≥ ¿ defineΣ b∙ thσ programme≥ )¼á strinτ o≥ ß pointe≥ typσ ¿ ì
  152. wσá haven'⌠á deal⌠ witΦ al∞ oµ thesσ type≤ ye⌠ )«á  Real≤ arσá als∩á scala≥ ì
  153. types¼ bu⌠ canno⌠ bσ usσ witΦ sub-ranges«  ┴ programme≥ defineΣ scala≥ typσ ì
  154. coulΣ bσ onσ sucΦ a≤ thi≤ -
  155.  
  156.      Day = (Mon, Tue, Wed, Thu, Fri, Sat, Sun);
  157.  
  158. The sub-range defined for this programmer-defined type might be -
  159.  
  160.      WorkDay : ( Mon .. Fri )
  161.  
  162.      Linσ 2╡ define≤ thσ labe∞ oµ thσ filσ (box⌐ oµ record≤ (cards⌐ oµá thσ ì
  163. typσá defineΣ iε line≤ 14-22«á  Linσ 2╢ define≤ ß variablσ labe∞ fo≥ usσ iε ì
  164. describinτ onσ oµ thσ record≤ oµ typσ PartRec«á  T∩ jum≡ fo≥ anothe≥á brieµ ì
  165. reference¼á linσá 7╡ fill≤ iε thσ stuΓ fo≥ choicσ ╣ - "Creatσ ß ne≈á file"«  ì
  166. I⌠ call≤ fo≥ thσ procedurσ CreateFile«  Thus¼ ß maiε prograφ is¼ t∩ ß largσ ì
  167. extent¼á ß serie≤ oµ call≤ t∩ procedures¼ o≥ t∩ onσ o≥ morσ sub-menu≤ whicΦ ì
  168. iεá turεá cal∞ procedures«á  Iµ wσ describσ thσ procedure≤ witΦá meaningfu∞ ì
  169. labels¼á theε thσ maiε prograφ i≤ aε easil∙ reaΣ descriptioε oµ wha⌠ wσ arσ ì
  170. doinτ iε thσ prograφ a≤ ß whole.
  171.  
  172.  
  173.  
  174.  
  175. è     Becausσ thσ Turb∩ Pasca∞ edito≥ onl∙ edit≤ tex⌠ whicΦ caε bσ containeΣ ì
  176. iε memory¼á it'≤ ofteε wise≥ t∩ dividσ thσ codσ int∩ separatσ pieces¼ whicΦ ì
  177. caεá bσ assembleΣ int∩ onσ piecσ wheε thσ sourcσ codσ prograφ i≤á compiled«  ì
  178. Thi≤á mean≤á tha⌠ piece≤ oµ codσ sucΦ a≤ proced-ure≤ anΣ function≤á caεá bσ ì
  179. testeΣá anΣ debugged¼á theε fileΣ oε disδ a≤ separatσ programs¼á unti∞ the∙ ì
  180. arσ needed«  Iε thi≤ way¼ ß "libraryó oµ routine≤ caε bσ buil⌠ up.
  181.      
  182.      A⌠ compilσ time¼á thσ compile≥ need≤ t∩ bσ tolΣ t∩ includσ thesσ file≤ ì
  183. a⌠á thσá correc⌠á placσ iε thσ sourcσ code«á  Thi≤ i≤á donσá b∙á usinτá thσ ì
  184. directive
  185.  
  186.           {$I filename.ext }   -   e.g  {$I MENU.PRO }
  187.  
  188.      Lowe≥á casσá letter≤ wil∞ bσ translateΣ int∩ uppe≥ case¼á and¼á iµá n∩ ì
  189. extensioε i≤ provided¼ ß .PA╙ extensioε i≤ assumed«  However¼ includeΣ filσ ì
  190. call≤ canno⌠ bσ nested«  Thσ MenuWritσ procedurσ oµ thσ previou≤ versioε i≤ ì
  191. herσá representeΣ simpl∙ b∙ thσ Includσ cal∞ - linσ 35«á  Thσ codσ fo≥á thσ ì
  192. othe≥á procedurσ anΣ functioε arσ includeΣ here¼á a≤ the∙ arσ useΣ fo≥á thσ ì
  193. firs⌠ time¼á anΣ thσ Includσ call≤ arσ printeΣ a≤ wel∞ (line≤ 33¼ 1╔ - 12I¼ ì
  194. 34¼ 1╩ - 17J).
  195.  
  196.      Thσá Procedurσá CreateFilσ simpl∙ create≤ ßá ne≈á file«á  Firstly¼á i⌠ ì
  197. check≤á t∩á seσá iµá thσ filσ alread∙ exist≤á ¿á linσá 4J)¼á usinτá anothe≥ ì
  198. function¼á calleΣ Exists«  Iµ thσ filσ doe≤ exist¼ thσ procedurσ exit≤ bacδ ì
  199. t∩ thσ Maiε Menu¼ witΦ ß messagσ sayinτ tha⌠ thσ filσ alread∙ exists.
  200.  
  201.      If the file doesn't exist, the procedure does three things -
  202.  
  203. i⌐á linσ 6╩ assign≤ thσ namσ oµ thσ filσ t∩ ß filσ variable¼á b∙ whicΦá thσ ì
  204. filσ i≤ referenceΣ froφ no≈ on«á  Thi≤ variablσ labe∞ wa≤ declareΣ iεá linσ ì
  205. 25«  Thσ actua∞ filσ namσ wa≤ createΣ iε linσ 41.
  206.  
  207. CONCAT(A,B⌐á i≤á equivalen⌠ t∩ BASIC'≤ A1ñ ╜ B1$+B2ñ i.e.¼á tw∩á (o≥á more⌐ ì
  208. string≤ arσ joineΣ together¼á o≥ concatenated«á  Thσ DriveNamσ anΣ FileNamσ ì
  209. content≤ werσ declareΣ iε line≤ ╢ anΣ 7).
  210.  
  211.      Iε thσ BASI├ statemen⌠  OPE╬ "I"¼á 6¼á "INVENT.DAT"¼ thσ filσ variablσ ì
  212. i≤ "6"¼ anΣ wσ usσ thi≤ numbe≥ t∩ refe≥ t∩ thσ filσ subsequentl∙ - eg
  213.  
  214. ááááááááá      PRINT #6        GET #1
  215.  
  216. áááááááááPartFile functions in precisely the same way.
  217.  
  218. ii⌐ linσ 7╩ - Rewritσ actuall∙ create≤ thσ ne≈ filσ oε disk¼ usinτ thσ namσ ì
  219. assigneΣ t∩ thσ filσ variablσ label¼á anΣ thσ filσ recorΣ pointe≥ i≤ se⌠ t∩ ì
  220. thσ beginninτ oµ thσ file¼á tha⌠ is¼á t∩ recorΣ numbe≥ 0«  Iµ yo⌡ Rewritσ ß ì
  221. filσ whicΦ alread∙ exists¼á theε thσ origina∞ filσ i≤ erased¼ anΣ ß ne≈ onσ ì
  222. i≤ created«  S∩ takσ carσ witΦ thi≤ command.
  223.  
  224. iii⌐á linσ 8╩ - thσ filσ i≤ closeΣ here¼á a≤ wσ don'⌠ wan⌠ t∩ d∩á any-thinτ ì
  225. furthe≥ witΦ it¼ anΣ thσ disδ director∙ i≤ updated«  Wheε thσ timσ come≤ t∩ ì
  226. usσ i⌠ again¼ wσ caε re-opeε it¼ usinτ ß differen⌠ statement«  
  227.  
  228. Noticσá tha⌠ we'vσ useΣ thσ extensioε .PR╧ t∩ sho≈ tha⌠ thi≤ piecσ oµá codσ ì
  229. i≤ ß Procedure.
  230.  
  231.      Thσá joΓá oµ thσ Functioε Exist≤ ¿ line≤ 1╔ - 12╔ ⌐ i≤ t∩ checδá iµá ß ì
  232. certaiε filσ exist≤ oε thσ disk¼ o≥ not«  ┴ Functioε i≤ useΣ iε ß statemen⌠ ì
  233. t∩ cal∞ ß pre-determineΣ operatioε whicΦ i≤ ac⌠ upoε thσ operand.è     Iε BASIC¼á  INT(X1⌐ anΣ VAL(D3$⌐ arσ functions¼á thσ operanΣ beinτ thσ ì
  234. variablσ - X1¼á D3ñ - iε eacΦ case«á  Therσ are¼á oµ course¼á man∙ oµ thesσ ì
  235. functions.
  236.  
  237.      Iεá thσ declaratioε oµ thi≤ functioε ¿ linσ 1╔ )¼á thσ necessar∙ para-ì
  238. meter≤á whicΦ arσ beinτ passeΣ t∩ i⌠ arσ declareΣ ¿ labe∞ anΣá typσá - herσ ì
  239. (Namσá ║á FileSpec⌐ )¼á anΣ theε thσ Typσ oµ thσ resul⌠ beinτ ob-taineΣá i≤ ì
  240. added╗á ßá coloεá separate≤á thσá tw∩ parts«á  Thσ variablσá labe∞á iεá thσ ì
  241. parenthese≤á doe≤ no⌠ havσ t∩ bσ thσ samσ a≤ thσ variablσ labe∞á useΣá wheε ì
  242. thσá functioεá i≤ calleΣ - comparσ 1╔ anΣ 4J«á  Here¼á wσ arσá passinτá thσ ì
  243. content≤ oµ FileNamσ t∩ anothe≥ variablσ calleΣ Namσ iε thσ Function«á  Thσ ì
  244. resul⌠ wil∞ bσ Booleaε - "I≤ i⌠ truσ tha⌠ thσ filσ exists¼ o≥ i≤ i⌠ false?"
  245.  
  246.      Thσá variablσ F¼á declareΣ iε linσ 4I¼á fulfill≤ thσ samσ functioεá a≤ ì
  247. PartFile¼á declareΣá iεá linσ 25«á  Wσ refe≥ t∩ thσ filenamσ b∙ ╞á iεá thi≤ ì
  248. block«á  Linσá 7╔á assign≤á thσ content≤ oµ Name¼á passeΣ t∩á i⌠á wheεá thσ ì
  249. functioεá wa≤ called¼á t∩ thσ variablσ F«á  Linσ 9╔ theε trie≤ t∩ opeεá thσ ì
  250. file«á  I⌠á canno⌠á d∩ s∩ iµ thσ filσ doesn'⌠ alread∙ exist«á  Iµ thσá filσ ì
  251. doesn'⌠á exist¼áá ßá run-timσá I/╧á erro≥á i≤á created¼á anΣá unde≥á norma∞ ì
  252. circumstance≤ thσ executioε oµ thσ prograφ stops.
  253.  
  254.      B∙á usinτ thσ compile≥ directivσ iε linσ 8I¼á thσ I/╧á run-timσá erro≥ ì
  255. checke≥á i≤ turneΣ off¼á s∩ tha⌠ prograφ executioε wil∞ no⌠ bσ halteΣ iµ aε ì
  256. erro≥ ha≤ occurred«á  However¼á furthe≥ I/╧ operation≤ wil∞ no⌠ takσá placσ ì
  257. unti∞á thσá standarΣ functioε IOResul⌠ i≤ called«á  Iµ IOResul⌠ contain≤á ß ì
  258. zero¼á thσ operatioε wa≤ successfu∞ - anythinτ elsσ signifie≤ aε error«  S∩ ì
  259. here¼ iµ thσ filσ alread∙ existed¼ IOResul⌠ woulΣ contaiε ß non-zer∩ resul⌠ ì
  260. ¿ thσ actua∞ valuσ i≤ immateria∞ )«á  Thσ run-timσ I/╧ checke≥ i≤ turneΣ oε ì
  261. agaiε iε linσ 10I.
  262.  
  263.      Withiεá thσá function¼á therσ mus⌠ bσ ß leas⌠ onσá statemen⌠á wherσá ß ì
  264. resul⌠á i≤á assigneΣ t∩ thσ functioε name¼á anΣ thi≤ i≤ donσ iεá linσá 11I« ì
  265. Firstly¼ ß Booleaε conditioε i≤ assesseΣ - doe≤ IOResul⌠ contaiε ░ ┐  Iµ i⌠ ì
  266. doe≤ ¿ thσ functioε operatioε wa≤ successfu∞ )¼á theε Exist≤ wil∞ bσ se⌠ t∩ ì
  267. Truσá - i⌠ i≤ truσ tha⌠ thσ filσ exists«á  Iµ IOResul⌠ <╛ 0¼á theε thσ filσ ì
  268. doe≤ no⌠ exist¼á anΣ Exist≤ wil∞ bσ se⌠ t∩ false«á  S∩ then¼ iε linσ 4J¼ iµ ì
  269. Exist≤ i≤ false¼á tha⌠ is¼á NO╘ EXISTS¼ theε wσ wil∞ creatσ ß file«  Eithe≥ ì
  270. way¼á ßá messagσ t∩ sa∙ wha⌠ ha≤ happeneΣ i≤ printeΣ oε thσ screeεá b∙á thσ ì
  271. procedurσ CreateFile.
  272.  
  273.      Iεá linσá 9I¼á wσ refe≥ t∩ thσ functioε Reset«á Iε line≤ 9╩á anΣá 14J¼ ì
  274. anothe≥á functioεá - GotoX┘á - i≤ referenced«á  Wσ wil∞á talδá abou⌠á thesσ ì
  275. function≤ iε thσ nex⌠ par⌠ oµ thσ series«            
  276.  
  277.                              END OF PART SIX.