home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
CP/M
/
CPM_CDROM.iso
/
mbug
/
mbug003.arc
/
PASCAL.006
< prev
next >
Wrap
Text File
|
1979-12-31
|
12KB
|
277 lines
PASCAL FOR BASIC PROGRAMMERS.
Dixon Kenney
PART VI.
Iεá thi≤á nex⌠á par⌠ oµ thσ series¼á wσ continuσá witΦá thσá Inventor∙ ì
prograφ wσ begaε iε thσ las⌠ part« Iε tha⌠ part¼ wσ haΣ ß procedurσ calleΣ ì
Menuwrite«á Iε thσ codσ fo≥ thi≤ part¼á wσ don'⌠ includσ thσ procedure¼ iε ì
orde≥ t∩ savσ space╗á however¼á wσ d∩ includσ thσ Includσ functioε call¼ t∩ ì
indicatσ wherσ i⌠ woulΣ come«á Iε thi≤ extendeΣ version¼ wσ creatσ ß file¼ ì
checkinτ firs⌠ t∩ seσ tha⌠ i⌠ i≤ no⌠ alread∙ open« Herσ i≤ thσ code.
1ááPrograφ Inventory_2;
4ááConst
ááá5áá MenuItem≤ ╜ 10;
ááá6áá NameOfFilσ ╜ 'INVENT.DAT';
ááá7áá DriveNamσ ╜ 'B:';
ááá9ááType
áá10áá FileSpeπ ╜ String[14];
áá11áá St2░ ╜ String[20];
áá12áá St4░ ╜ String[40];
á 14áá PartReπ ╜ Record
áá15áá PartNuφ ║ 100░ .« 9999;
áá16áá Descriptioε ║ St20;
áá17áá Cos⌠ ║ Real;
áá18áá Retai∞ ║ Real;
áá19áá MinO╚ ║ Integer;
áá20áá MaxReorde≥ ║ Integer;
áá21áá CurrentO╚ ║ Integer;
áá22áá End;
24ááVar
áá25áá PartFilσ ║ Filσ oµ PartRec;
áá26áá Par⌠ ║ PartRec;
áá27áá MenuLis⌠ ║ Arra∙ █ ░ .« 1╡ ▌ oµ St40;
áá28áá Iteφ ║ Integer;
áá29áá Loope≥ ║ Boolean;
áá30áá FileNamσ ║ FileSpec;
33 {$╔ EXISTS.FN├ }
ááá1IááááFunctioε Exist≤ (Namσ ║ FileSpec⌐ ║ Booleaε ;
3IááááVar
ááá4Iáááá ╞ ║ File;
ááá6IááááBegin
ááá7Iáááá Assign(F¼ Name);
ááá8Iáááá{$I-}
ááá9Iáááá Reset(F);
áá10Iáááá{$I+}
áá11Iáááá Exist≤ :╜ (IOResul⌠ ╜ 0)
áá12IááááEnd╗ √ Functioε Exist≤ ²
34 {$╔ CREATEFL.PR╧ }èááá1JááááProcedurσ CreateFile;
ááá3JááááBegin
ááá4Jáááá Iµ No⌠ Exists(FileName⌐ then
ááá5Jáááá Begin
ááá6Jáááá Assign(PartFile¼ FileName);
ááá7Jáááá Rewrite(PartFile);
ááá8Jáááá Close(PartFile);
ááá9Jáááá GotoXY(10¼ 20);
áá10Jáááá Writeln('Thσ filσ ',FileName,º i≤ no≈ open')
áá11Jáááá End
áá12Jáááá Else
áá13Jáááá Begin
áá14Jáááá GotoXY(10¼ 20);
áá15Jáááá Writeln('Thσ filσ ',FileName,º i≤ alread∙ open')
áá16Jáááá End
áá17JááááEnd╗ √ Procedurσ OpenFilσ }
35 {$╔ MENU.PR╧ }
37ááá√ ¬ Thi≤ i≤ thσ beginninτ oµ thσ Maiε Sectioε oµ thσ Prograφ * }
39áááBegin
41ááá FileNamσ :╜ Concat(DriveName¼ NameOfFile);
á43ááá Loope≥ :╜ True;
45ááá MenuList[0▌ :╜ 'MASTE╥ INVENTOR┘ MENU';
[ Lines have been omitted here - they are in the previous part's code ]
65ááá Casσ Iteφ of
█ Furthe≥ line≤ omitteΣ herσ - they'rσ iε thσ previou≤ codσáals∩ ]
á75ááá 9║ CreateFile;
á76ááá Else
á77ááá Begin
á78ááá Writeln╗ Writeln;
á79ááá Writeln(º ':20,'N∩ actioε possiblσ fo≥ typeΣ input')
á80ááá EnΣ √ Elsσ ..}
á81ááá End╗ √ Casσ Iteφ ..« }
83ááá Delay(2000);
85ááá Unti∞ Loope≥ ╜ False;
87áááEnd.
Iε line≤ 6¼á 7¼ 1░ anΣ 30¼ wσ arσ defininτ somσ extrß items« Thσ bes⌠ ì
wa∙ t∩ d∩ thi≤ wheε yo⌡ arσ writinτ ß prograφ i≤ t∩ finΣ ou⌠ wherσ yo⌡ wan⌠ ì
ß ne≈ iteφ first¼á anΣ theε writσ it≤ definitioε iε thσ correc⌠ areß later« ì
However¼á alway≤á remembe≥á tha⌠á thesσ item≤ mus⌠ bσ defineΣá firs⌠á iεá ß ì
listinτá beforσá thσá prograφá caεá usσ theφ iε thσ codσá whicΦá i≤á t∩á bσ ì
executed.
è Line≤ 14-2▓ definσ wha⌠ i≤ calleΣ ß RECORD«á ┴ recorΣ i≤ simila≥ t∩ ß ì
╢ ° ┤ filinτ card¼á useΣ b∙ doctors¼á dentists¼á etπ t∩ kee≡ thei≥á patien⌠ ì
records¼á o≥ ß housewifσ t∩ kee≡ ß recipe¼á o≥ s∩ forth« EacΦ "cardó deal≤ ì
witΦ onσ se⌠ oµ informatioε - onσ patient¼á onσ cake¼ or¼ a≤ here¼ onσ iteφ ì
oµá stock«á Thσá recorΣá i≤ divideΣ int∩ ß numbe≥ oµá FIELDS¼á whicΦá givσ ì
differen⌠ piece≤ oµ informatioε abou⌠ thσ iteφ the∙ describσ - here¼ ß par⌠ ì
number¼ ß description¼ thσ cos⌠ t∩ us¼ thσ retai∞ price¼ thσ minimuφ numbe≥ ì
wσ neeΣ t∩ kee≡ oε hand¼á thσ maximuφ numbe≥ wσ woulΣ havσ afte≥ ß ful∞ re-ì
order¼á anΣ thσ curren⌠ numbe≥ oµ unit≤ oµ thi≤ iteφ oε hand«á Therσ arσ ╖ ì
FIELD╙ iε thi≤ ▒ RECORD¼ anΣ eacΦ recorΣ ha≤ thσ samσ numbe≥ oµ fields.
User≤ oµ dBasσ I╔ ough⌠ t∩ bσ familia≥ witΦ thesσ terms¼á anΣ the∙ wa∙ ì
the∙á fi⌠ together«á Thσ file≤ whicΦ wσ creatσ iε thi≤ wa∙ arσá simila≥á t∩ ì
Randoφá file≤á iε ß BASI├ system«á Thesσ file≤ caε bσá accesseΣá randomly¼ ì
sincσ eacΦ recorΣ i≤ oµ equa∞ length«á So¼ b∙ "numberingó them¼ thσ systeφ ì
caεá ge⌠ t∩ an∙ particula≥ recorΣ immediately¼á knowinτ wherσ eacΦ recorΣ ì
begin≤ anΣ ends.
Wσ caε als∩ havσ file≤ simila≥ t∩ ß BASI├ sequent- ia∞ file¼á a≤á wel∞ ì
a≤ TEX╘ files¼ whicΦ arσ simpl∙ considereΣ t∩ bσ line≤ oµ text« Sequentia∞ ì
file≤ havσ t∩ bσ reaΣ sequentially¼ sincσ eacΦ recorΣ ma∙ bσ oµ ß differinτ ì
length¼á anΣá thσá compute≥ can-no⌠ computσ jus⌠ wherσ ß recorΣ begin≤á anΣ ì
ends.
Note the way in which a record is defined here -
<record_label> = RECORD
<field_label> : <type> ..
END
Linσá 1╡ i≤ ß sub-rangσ type«á Thσ sub-rangσ oµ ß typσ specifie≤á thσ ì
minimuφá anΣá maximuφ limit≤ oµ thσ field«á Here¼á ß PartNuφá i≤á oµá typσ ì
integer¼ bu⌠ thσ limit≤ arσ defineΣ - PartNuφ mus⌠ bσ ß fou≥ dig-i⌠ intege≥ ì
number« Sub-range≤ caε onl∙ bσ oµ scala≥ type≤ - integer¼ boolean¼ char¼ ß ì
declareΣá scala≥ ¿ defineΣ b∙ thσ programme≥ )¼á strinτ o≥ ß pointe≥ typσ ¿ ì
wσá haven'⌠á deal⌠ witΦ al∞ oµ thesσ type≤ ye⌠ )«á Real≤ arσá als∩á scala≥ ì
types¼ bu⌠ canno⌠ bσ usσ witΦ sub-ranges« ┴ programme≥ defineΣ scala≥ typσ ì
coulΣ bσ onσ sucΦ a≤ thi≤ -
Day = (Mon, Tue, Wed, Thu, Fri, Sat, Sun);
The sub-range defined for this programmer-defined type might be -
WorkDay : ( Mon .. Fri )
Linσ 2╡ define≤ thσ labe∞ oµ thσ filσ (box⌐ oµ record≤ (cards⌐ oµá thσ ì
typσá defineΣ iε line≤ 14-22«á Linσ 2╢ define≤ ß variablσ labe∞ fo≥ usσ iε ì
describinτ onσ oµ thσ record≤ oµ typσ PartRec«á T∩ jum≡ fo≥ anothe≥á brieµ ì
reference¼á linσá 7╡ fill≤ iε thσ stuΓ fo≥ choicσ ╣ - "Creatσ ß ne≈á file"« ì
I⌠ call≤ fo≥ thσ procedurσ CreateFile« Thus¼ ß maiε prograφ is¼ t∩ ß largσ ì
extent¼á ß serie≤ oµ call≤ t∩ procedures¼ o≥ t∩ onσ o≥ morσ sub-menu≤ whicΦ ì
iεá turεá cal∞ procedures«á Iµ wσ describσ thσ procedure≤ witΦá meaningfu∞ ì
labels¼á theε thσ maiε prograφ i≤ aε easil∙ reaΣ descriptioε oµ wha⌠ wσ arσ ì
doinτ iε thσ prograφ a≤ ß whole.
è Becausσ thσ Turb∩ Pasca∞ edito≥ onl∙ edit≤ tex⌠ whicΦ caε bσ containeΣ ì
iε memory¼á it'≤ ofteε wise≥ t∩ dividσ thσ codσ int∩ separatσ pieces¼ whicΦ ì
caεá bσ assembleΣ int∩ onσ piecσ wheε thσ sourcσ codσ prograφ i≤á compiled« ì
Thi≤á mean≤á tha⌠ piece≤ oµ codσ sucΦ a≤ proced-ure≤ anΣ function≤á caεá bσ ì
testeΣá anΣ debugged¼á theε fileΣ oε disδ a≤ separatσ programs¼á unti∞ the∙ ì
arσ needed« Iε thi≤ way¼ ß "libraryó oµ routine≤ caε bσ buil⌠ up.
A⌠ compilσ time¼á thσ compile≥ need≤ t∩ bσ tolΣ t∩ includσ thesσ file≤ ì
a⌠á thσá correc⌠á placσ iε thσ sourcσ code«á Thi≤ i≤á donσá b∙á usinτá thσ ì
directive
{$I filename.ext } - e.g {$I MENU.PRO }
Lowe≥á casσá letter≤ wil∞ bσ translateΣ int∩ uppe≥ case¼á and¼á iµá n∩ ì
extensioε i≤ provided¼ ß .PA╙ extensioε i≤ assumed« However¼ includeΣ filσ ì
call≤ canno⌠ bσ nested« Thσ MenuWritσ procedurσ oµ thσ previou≤ versioε i≤ ì
herσá representeΣ simpl∙ b∙ thσ Includσ cal∞ - linσ 35«á Thσ codσ fo≥á thσ ì
othe≥á procedurσ anΣ functioε arσ includeΣ here¼á a≤ the∙ arσ useΣ fo≥á thσ ì
firs⌠ time¼á anΣ thσ Includσ call≤ arσ printeΣ a≤ wel∞ (line≤ 33¼ 1╔ - 12I¼ ì
34¼ 1╩ - 17J).
Thσá Procedurσá CreateFilσ simpl∙ create≤ ßá ne≈á file«á Firstly¼á i⌠ ì
check≤á t∩á seσá iµá thσ filσ alread∙ exist≤á ¿á linσá 4J)¼á usinτá anothe≥ ì
function¼á calleΣ Exists« Iµ thσ filσ doe≤ exist¼ thσ procedurσ exit≤ bacδ ì
t∩ thσ Maiε Menu¼ witΦ ß messagσ sayinτ tha⌠ thσ filσ alread∙ exists.
If the file doesn't exist, the procedure does three things -
i⌐á linσ 6╩ assign≤ thσ namσ oµ thσ filσ t∩ ß filσ variable¼á b∙ whicΦá thσ ì
filσ i≤ referenceΣ froφ no≈ on«á Thi≤ variablσ labe∞ wa≤ declareΣ iεá linσ ì
25« Thσ actua∞ filσ namσ wa≤ createΣ iε linσ 41.
CONCAT(A,B⌐á i≤á equivalen⌠ t∩ BASIC'≤ A1ñ ╜ B1$+B2ñ i.e.¼á tw∩á (o≥á more⌐ ì
string≤ arσ joineΣ together¼á o≥ concatenated«á Thσ DriveNamσ anΣ FileNamσ ì
content≤ werσ declareΣ iε line≤ ╢ anΣ 7).
Iε thσ BASI├ statemen⌠ OPE╬ "I"¼á 6¼á "INVENT.DAT"¼ thσ filσ variablσ ì
i≤ "6"¼ anΣ wσ usσ thi≤ numbe≥ t∩ refe≥ t∩ thσ filσ subsequentl∙ - eg
ááááááááá PRINT #6 GET #1
áááááááááPartFile functions in precisely the same way.
ii⌐ linσ 7╩ - Rewritσ actuall∙ create≤ thσ ne≈ filσ oε disk¼ usinτ thσ namσ ì
assigneΣ t∩ thσ filσ variablσ label¼á anΣ thσ filσ recorΣ pointe≥ i≤ se⌠ t∩ ì
thσ beginninτ oµ thσ file¼á tha⌠ is¼á t∩ recorΣ numbe≥ 0« Iµ yo⌡ Rewritσ ß ì
filσ whicΦ alread∙ exists¼á theε thσ origina∞ filσ i≤ erased¼ anΣ ß ne≈ onσ ì
i≤ created« S∩ takσ carσ witΦ thi≤ command.
iii⌐á linσ 8╩ - thσ filσ i≤ closeΣ here¼á a≤ wσ don'⌠ wan⌠ t∩ d∩á any-thinτ ì
furthe≥ witΦ it¼ anΣ thσ disδ director∙ i≤ updated« Wheε thσ timσ come≤ t∩ ì
usσ i⌠ again¼ wσ caε re-opeε it¼ usinτ ß differen⌠ statement«
Noticσá tha⌠ we'vσ useΣ thσ extensioε .PR╧ t∩ sho≈ tha⌠ thi≤ piecσ oµá codσ ì
i≤ ß Procedure.
Thσá joΓá oµ thσ Functioε Exist≤ ¿ line≤ 1╔ - 12╔ ⌐ i≤ t∩ checδá iµá ß ì
certaiε filσ exist≤ oε thσ disk¼ o≥ not« ┴ Functioε i≤ useΣ iε ß statemen⌠ ì
t∩ cal∞ ß pre-determineΣ operatioε whicΦ i≤ ac⌠ upoε thσ operand.è Iε BASIC¼á INT(X1⌐ anΣ VAL(D3$⌐ arσ functions¼á thσ operanΣ beinτ thσ ì
variablσ - X1¼á D3ñ - iε eacΦ case«á Therσ are¼á oµ course¼á man∙ oµ thesσ ì
functions.
Iεá thσ declaratioε oµ thi≤ functioε ¿ linσ 1╔ )¼á thσ necessar∙ para-ì
meter≤á whicΦ arσ beinτ passeΣ t∩ i⌠ arσ declareΣ ¿ labe∞ anΣá typσá - herσ ì
(Namσá ║á FileSpec⌐ )¼á anΣ theε thσ Typσ oµ thσ resul⌠ beinτ ob-taineΣá i≤ ì
added╗á ßá coloεá separate≤á thσá tw∩ parts«á Thσ variablσá labe∞á iεá thσ ì
parenthese≤á doe≤ no⌠ havσ t∩ bσ thσ samσ a≤ thσ variablσ labe∞á useΣá wheε ì
thσá functioεá i≤ calleΣ - comparσ 1╔ anΣ 4J«á Here¼á wσ arσá passinτá thσ ì
content≤ oµ FileNamσ t∩ anothe≥ variablσ calleΣ Namσ iε thσ Function«á Thσ ì
resul⌠ wil∞ bσ Booleaε - "I≤ i⌠ truσ tha⌠ thσ filσ exists¼ o≥ i≤ i⌠ false?"
Thσá variablσ F¼á declareΣ iε linσ 4I¼á fulfill≤ thσ samσ functioεá a≤ ì
PartFile¼á declareΣá iεá linσ 25«á Wσ refe≥ t∩ thσ filenamσ b∙ ╞á iεá thi≤ ì
block«á Linσá 7╔á assign≤á thσ content≤ oµ Name¼á passeΣ t∩á i⌠á wheεá thσ ì
functioεá wa≤ called¼á t∩ thσ variablσ F«á Linσ 9╔ theε trie≤ t∩ opeεá thσ ì
file«á I⌠á canno⌠á d∩ s∩ iµ thσ filσ doesn'⌠ alread∙ exist«á Iµ thσá filσ ì
doesn'⌠á exist¼áá ßá run-timσá I/╧á erro≥á i≤á created¼á anΣá unde≥á norma∞ ì
circumstance≤ thσ executioε oµ thσ prograφ stops.
B∙á usinτ thσ compile≥ directivσ iε linσ 8I¼á thσ I/╧á run-timσá erro≥ ì
checke≥á i≤ turneΣ off¼á s∩ tha⌠ prograφ executioε wil∞ no⌠ bσ halteΣ iµ aε ì
erro≥ ha≤ occurred«á However¼á furthe≥ I/╧ operation≤ wil∞ no⌠ takσá placσ ì
unti∞á thσá standarΣ functioε IOResul⌠ i≤ called«á Iµ IOResul⌠ contain≤á ß ì
zero¼á thσ operatioε wa≤ successfu∞ - anythinτ elsσ signifie≤ aε error« S∩ ì
here¼ iµ thσ filσ alread∙ existed¼ IOResul⌠ woulΣ contaiε ß non-zer∩ resul⌠ ì
¿ thσ actua∞ valuσ i≤ immateria∞ )«á Thσ run-timσ I/╧ checke≥ i≤ turneΣ oε ì
agaiε iε linσ 10I.
Withiεá thσá function¼á therσ mus⌠ bσ ß leas⌠ onσá statemen⌠á wherσá ß ì
resul⌠á i≤á assigneΣ t∩ thσ functioε name¼á anΣ thi≤ i≤ donσ iεá linσá 11I« ì
Firstly¼ ß Booleaε conditioε i≤ assesseΣ - doe≤ IOResul⌠ contaiε ░ ┐ Iµ i⌠ ì
doe≤ ¿ thσ functioε operatioε wa≤ successfu∞ )¼á theε Exist≤ wil∞ bσ se⌠ t∩ ì
Truσá - i⌠ i≤ truσ tha⌠ thσ filσ exists«á Iµ IOResul⌠ <╛ 0¼á theε thσ filσ ì
doe≤ no⌠ exist¼á anΣ Exist≤ wil∞ bσ se⌠ t∩ false«á S∩ then¼ iε linσ 4J¼ iµ ì
Exist≤ i≤ false¼á tha⌠ is¼á NO╘ EXISTS¼ theε wσ wil∞ creatσ ß file« Eithe≥ ì
way¼á ßá messagσ t∩ sa∙ wha⌠ ha≤ happeneΣ i≤ printeΣ oε thσ screeεá b∙á thσ ì
procedurσ CreateFile.
Iεá linσá 9I¼á wσ refe≥ t∩ thσ functioε Reset«á Iε line≤ 9╩á anΣá 14J¼ ì
anothe≥á functioεá - GotoX┘á - i≤ referenced«á Wσ wil∞á talδá abou⌠á thesσ ì
function≤ iε thσ nex⌠ par⌠ oµ thσ series«
END OF PART SIX.