home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
CP/M
/
CPM_CDROM.iso
/
mbug
/
mbug003.arc
/
PASCAL.005
< prev
next >
Wrap
Text File
|
1979-12-31
|
11KB
|
224 lines
PASCAL FOR BASIC PROGRAMMERS
Dixon Kenney
PART V
Iεá thi≤ episodσ oµ ou≥ excitinτ seria∞ ¿ o≥ maybσ it'≤ a≤ un-invitinτ ì
a≤ cerea∞ t∩ yo⌡ !⌐ wσ arσ leavinτ thσ Bankinτ program¼á anΣ movinτ oε int∩ ì
ne≈á ground«á Wσá arσ introducinτ thσ concep⌠ oµ ßá pro-cedure¼á thσá CAS┼ ì
statement¼á anΣ ß differen⌠ kinΣ oµ loop¼á onσ whicΦ i≤ unavailablσ iεá BE┼ ì
BASIC.
Here'≤á thσ codσ oµ thσ program¼á which¼á a≤ yo⌡ caε see¼á i≤ thσá be-ì
ginninτ oµ ß stock-contro∞ program«á Wσ won'⌠ ge⌠ to∩ fa≥ witΦ it¼á bu⌠ i⌠ ì
ma∙ givσ yo⌡ somσ gooΣ ideas.
Thi≤á codσá doe≤á no⌠ underlinσ reserveΣ words¼á a≤ wσá havσá donσá iε ì
previou≤ listings« The∙ arσ no⌠ normall∙ underlineΣ wheε codσ i≤ written.
1ááPrograφ Inventory;
3ááType
4áá St3░ ╜ String[30];
6ááConst
7áá MenuItem≤ ╜ 10;
9ááVar
10áá MenuLis⌠ ║ Arra∙ █ ░ .« 1╡ ▌ oµ St30;
11áá Iteφ ║ Integer;
12áá Loope≥ ║ Boolean;
14ááááááá√ **¬ Thi≤ i≤ thσ beginninτ oµ ß procedurσ **¬ }
16áááááááProcedurσ MenuWritσ ;
18áááááááVar
19ááááááá ╔ ║ Integer;
21áááááááBegin
22ááááááá ClrScr;
23ááááááá Writeln╗ Writeln;
24ááááááá Writeln(º ':20¼ MenuList[0]);
25ááááááá Writeln╗ Writeln;
26ááááááá Fo≥ ╔ :╜ ▒ t∩ MenuItem≤ do
27ááááááá Writeln(º ':10,MenuList[I]);
28áááááááEnd╗ √ Procedurσ MenuWritσ }
30ááááááá√ **¬ End of procedure for this program }
32ááBegin
34áá Loope≥ :╜ True;
36áá MenuList[0▌ :╜ 'MASTE╥ INVENTOR┘ MENU';
37áá MenuList[1▌ :╜ º 0║ Qui⌠ thσ Program';
38áá MenuList[2▌ :╜ º 1║ Ente≥ ß ne≈ iteφ t∩ thσ list';
39áá MenuList[3▌ :╜ º 2║ Deletσ aε iteφ froφ thσ list';
40áá MenuList[4▌ :╜ º 3║ Edi⌠ aε existinτ item';
41áá MenuList[5▌ :╜ º 4║ Quer∙ aε existinτ item';è42áá MenuList[6▌ :╜ º 5║ Lis⌠ item≤ belo≈ re-orde≥ level';
43áá MenuList[7▌ :╜ º 6║ Sale≤ transaction';
44áá MenuList[8▌ :╜ º 7║ Purchasσ transaction';
45áá MenuList[9▌ :╜ º 8:';
46áá MenuList[10]:╜ º 9║ Star⌠ ß ne≈ file';
48ááRepeat
50áá MenuWrite;
52áá Writeln;
53áá Write('Typσ iε numbe≥ oµ actioε desireΣ ║ ');
54áá Readln(Item);
56áá Casσ Iteφ of
57áá 0║ Halt;
58áá 1║ Begiε Writeln('Iteφ 1')╗ Delay(2000⌐ End;
59áá 2║ Begiε Writeln('Iteφ 2')╗ Delay(2000⌐ End;
60áá 3║ Begiε Writeln('Iteφ 3')╗ Delay(2000⌐ End;
61áá 4║ Begiε Writeln('Iteφ 4')╗ Delay(2000⌐ End;
62áá 5║ Begiε Writeln('Iteφ 5')╗ Delay(2000⌐ End;
63áá 6║ Begiε Writeln('Iteφ 6')╗ Delay(2000⌐ End;
64áá 7║ Begiε Writeln('Iteφ 7')╗ Delay(2000⌐ End;
65áá 8║ Delay(2000);
66áá 9║ Begiε Writeln('Iteφ 9')╗ Delay(2000⌐ End;
67áá Else
68áá Begin
69áá Writeln╗ Writeln;
70áá Writeln(º ':20,'N∩ actioε possiblσ fo≥ typeΣ input');
71áá Writeln(º ':30,'Typσ ß valiΣ number');
72áá Delay(2000)
73áá EnΣ √ Elsσ ..}
74áá End╗ √ Casσ Iteφ ..« }
76áá Unti∞ Loope≥ ╜ False;
78ááEnd.
Firs⌠ oµ all¼á noticσ line≤ │ anΣ 4« Iε previou≤ programs¼ we'vσ useΣ ì
thσ concep⌠ oµ ß strinτ declaratioε - eg¼ iε Par⌠ I╔ :
CustName := String[20];
wherσá thi≤á versioεá oµá Pasca∞ declare≤ ß strinτ oµ character≤á t∩á bσá ß ì
certaiε maximuφ length.
We'vσ alread∙ me⌠ datß type≤ sucΦ a≤ Integer¼ Booleaε anΣ Real« Thesσ ì
arσá PRE-DEFINE─ iε PASCAL¼á jus⌠ likσ numeriπ anΣ strinτ type≤á iεá BASIC« ì
Therσá i≤ anothe≥ pre-defineΣ typσ - CHA╥ (which¼á a≤ yo⌡ ma∙ guess¼á i≤á ß ì
"characteróá type)«á ┴á variablσ oµ typσ CHA╥ i≤ an∙ onσ oµ thσá printablσ ì
ASCI╔á character≤á whicΦá thσá compute≥ use≤ - i⌠á ma∙á bσá aεá alphabetiπ ì
character¼á ß numeriπ digit¼á ß punctuatioε mark«á Wheε i⌠ i≤ defined¼á i⌠ ì
look≤ somethinτ likσ thi≤ -
Const Dash = '-';
Var NextLetter : Char;
è Thσá constan⌠ declaratioε i≤ equivalen⌠ t∩á BASIC'≤á A1$="-"╗á noticσ ì
tha⌠á iεá PASCAL¼á singlσá quote≤ arσ useΣ arounΣá thσá character(s⌐á beinτ ì
assigneΣá t∩á thσ variablσ label«á However¼á thσ variablσá NextLette≥á caε ì
consis⌠á oµá onl∙ ON┼ character¼á whateve≥ i⌠ ma∙ be¼á sincσ typσá CHA╥á i≤ ì
defineΣ a≤ consistinτ oµ onσ character.
Mos⌠ version≤ oµ PASCA╠ founΣ oε micro≤ definσ ß STRING[▌ type¼á whicΦ ì
i≤á reall∙ aε arra∙ oµ characters«á Iε Turb∩ Pascal¼á ß STRING[▌ typσá caε ì
var∙á dynamicall∙ iε lengtΦ betweeε ░ anΣ thσ defineΣ uppe≥ limi⌠ - iε thi≤ ì
i⌠ differ≤ froφ aε array¼ iε whicΦ thσ numbe≥ oµ element≤ i≤ fixed« S∩ ou≥ ì
defineΣ strinτ "CustNameó abovσ caε havσ betweeε ░ anΣ 2░ character≤ iε it« ì
Thσ longes⌠ strinτ caε bσ 25╡ character≤ iε length¼ jus⌠ a≤ iε BASIC.
Iµ wσ wan⌠ t∩ usσ ß lo⌠ oµ string≤ oµ characters¼á wσ caε definσ ß ne≈ ì
datß type«á Herσ iε line≤ │ anΣ 4¼ thσ ne≈ typσ i≤ ß strinτ oµ characters¼ ì
whicΦ caε havσ ß maximuφ lengtΦ oµ 30«á We'vσ calleΣ thσ typσ St3░ ¿ whicΦ ì
mean≤á "Strinτ oµ 3░ characters"¼á naturall∙ )«á So¼á alongsidσá integers¼ ì
reals¼á boolean≤ anΣ chars¼ wσ no≈ havσ ß ne≈ type¼ defineΣ b∙ us¼ whicΦ i≤ ì
ß strinτ oµ 3░ characters«á Iε linσ 10¼ wσ caε se⌠ u≡ aε array¼ usinτ thi≤ ì
ne≈ type« Wσ mus⌠ definσ thσ arra∙ iε thi≤ way¼ rathe≥ thaε a≤ ARRA┘ .« O╞ ì
STRING[30]¼á sincσá thσá arra∙ mus⌠ bσ defineΣ a≤ ßá defineΣá TYPE¼á eithe≥ ì
predefineΣ b∙ TP¼ o≥ defineΣ b∙ u≤ a≤ wσ havσ donσ i⌠ here.
Thi≤ arra∙ i≤ defineΣ a≤ ß one-dimensiona∞ array¼ containinτ 1╢ items« ì
I⌠ i≤ equivalen⌠ t∩ ß BASI├ arra∙ dimensioneΣ a≤ A1$(16⌐ - althougΦá that'≤ ì
no⌠á quitσ ho≈ it'≤ dimensioneΣ iε Beσ BASIC¼á oµ course«á EacΦ elemen⌠ iε ì
thi≤ arra∙ i≤ ß strinτ oµ no⌠ morσ thaε 3░ characters.
Iε linσ 1▓ wσ definσ ß variablσ a≤ ß Booleaε datß type« Thi≤ typσ caε ì
takσ onl∙ thσ value≤ Truσ o≥ False¼á anΣ i≤ generall∙ useΣ fo≥ sucΦá thing≤ ì
a≤á settinτá flags«á Ofteεá iε BASIC¼á wσ equatσ ░ t∩ Falsσ anΣá -▒á ¿á o≥ ì
sometime≤á +▒á ⌐ t∩ True¼á anΣ se⌠ ß variablσ t∩ bσ onσ o≥ othe≥á oµá thesσ ì
value≤ t∩ ac⌠ a≤ ß flag«á Iε fact¼ wha⌠ wσ arσ doinτ iε BASI├ i≤ writinτ ß ì
numeriπá equivalen⌠ oµ ß PASCA╠ Booleaε variable«á Iµ yo⌡ looδ a⌠ line≤ 3┤ ì
anΣ 76¼ you'l∞ seσ thσ variablσ iε use.
Iε line≤ 14-3░ wσ definσ ß Procedure« ┴ Procedurσ iε Pasca∞ i≤ likσ ß ì
GOSU┬ subroutinσ iε BASIC«á Likσ sucΦ ß routinσ iε Beσ BASIC¼ yo⌡ caε pas≤ ì
parameter≤ betweeε thσ maiε bod∙ oµ codσ anΣ thσ procedurσ iµ yo⌡ wan⌠á to« ì
However¼á therσá i≤ onσ biτ difference¼á whicΦ yo⌡ mus⌠ gras≡ holΣ of«á Iε ì
PASCAL¼ iµ yo⌡ declarσ ß variablσ iε thσ procedure¼ sucΦ a≤ wσ havσ donσ iε ì
linσ 19¼á theε tha⌠ variablσ caε bσ useΣ ONL┘ INSID┼ TH┼ PROCEDUR┼ iε whicΦ ì
i⌠á i≤á declared«á I⌠ i≤ calleΣ ß LOCA╠ variable¼á iε distinctioεá t∩á an∙ ì
variablσ declareΣ iε thσ maiε bod∙ oµ thσ program¼á whicΦ arσ calleΣ GLOBA╠ ì
variables« Iε mos⌠ version≤ oµ BASIC¼ al∞ variable≤ arσ GLOBAL.
Iεá ou≥á procedure¼á wσ declarσ ß variablσ labelleΣ I«á Iµ wσá haΣá ß ì
simila≥á declaratioεá iε linσ 11¼á PASCA╠ woulΣ seσ thσ Globa∞á variablσá ╔ ì
declareΣá iεá linσ 1▒ anΣ thσ Loca∞ variablσ ╔ declareΣ iε linσ 1╣á a≤á tw∩ ì
totall∙ DIFFEREN╘ variables«á The∙ arσ helΣ a⌠ differen⌠ addresse≤ iεá thσ ì
computer'≤ memory¼ anΣ therσ i≤ n∩ wa∙ tha⌠ thσ compile≥ wil∞ confusσ them.
Procedure≤á iε PASCA╠ arσ declareΣ BEFOR┼ thσ maiε bod∙ oµ thσ prograφ ì
- therσá i≤á n∩ choicσ iε thσ matter¼á a≤ therσ i≤á iεá BASIC¼á wherσá sub-ì
routine≤ caε bσ writteε anywherσ yo⌡ fancy«á Thi≤ particula≥ procedurσ ha≤ ì
n∩ parameter≤ passeΣ t∩ it.
è Therσ i≤ n∩ equivalen⌠ t∩ ß READ/DAT┴ BASI├ statemen⌠ iε PASCAL«á Thσ ì
onl∙ wa∙ t∩ assigε value≤ t∩ aε array¼á sucΦ a≤ wσ wan⌠ t∩ d∩ here¼á i≤á t∩ ì
writσ i⌠ ou⌠ a≤ wσ havσ donσ iε line≤ 3╢ t∩ 46.
Iε line≤ 4╕ t∩ 7╢ wσ havσ ß loop«á Thi≤ i≤ ß REPEA╘ - UNTI╠ loop« I⌠ ì
i≤á differen⌠ froφ ß D╧ - WHIL┼ loop¼á iε tha⌠ thσ conditioε i≤ no⌠á testeΣ ì
unti∞á thσ UNTI╠ statemen⌠ i≤ reached«á Thi≤ mean≤ tha⌠ thσ loo≡ i≤ alway≤ ì
executeΣá once«á Iεá thi≤á case¼á i⌠ wil∞ bσ repeateΣá unti∞á thσá Booleaε ì
variablσá "Looperó i≤ se⌠ t∩ False«á Iε fact¼á thi≤ i≤ neve≥ donσ iεá thi≤ ì
program¼á anΣá wσ havσ createΣ aε endles≤ loop«á Normally¼á iε ßá program¼ ì
therσ woulΣ bσ aε elegan⌠ wa∙ t∩ breaδ ou⌠ oµ sucΦ ß loop¼ whereb∙ "Looperó ì
i≤ se⌠ t∩ True«á Here¼ i⌠ i≤ optioε "0ó oµ thσ menu« dBAS┼ I╔ enthusiast≤ ì
migh⌠ bσ remindeΣ herσ oµ thσ loo≡ "D╧ WHIL┼ ╘ .« ENDDO".
Noticσá iεá linσá 5░ ho≈ thσ Procedurσ i≤á calleΣá oεá fo≥á execution« ì
Simpl∙ b∙ usinτ thσ Procedure'≤ name¼ whicΦ thσ prograφ knows¼ sincσ i⌠ wa≤ ì
defineΣ beforσ i⌠ wa≤ called¼ tha⌠ Procedurσ i≤ executed« Somσ version≤ oµ ì
BASI├ arσ ablσ t∩ calleΣ sub-routine≤ b∙ name¼á rathe≥ thaε G╧ SUBbinτ t∩ ß ì
linσ number.
Iεá line≤á 5╢á t∩ 74¼á wσ havσ ß CAS┼ statement«á Thi≤á i≤á simpl∙á ß ì
differen⌠ wa∙ oµ writinτ multiplσ I╞ .«á THENs«á Wσ coulΣ havσ writteε thσ ì
codσ a≤ -
If Item = 0 then Halt
Else if Item = 1 then Begin .. End
Else if Item = 2 .... and so on.
Thi≤á i≤á ßá mucΦ easie≥ wa∙ t∩ writσ ßá multiplσá choicσá conditiona∞ ì
statement«á Noticσá tha⌠ iε TURB╧ PASCAL¼á Casσ statement≤ arσ alloweΣá aε ì
ELS┼ clause¼á whicΦ simpl∙ mean≤ ó ..«á iε an∙ othe≥ case¼á d∩ thi≤ ..«á "« ì
Notice¼á too¼á tha⌠ thσ variablσ Iteφ froφ linσ 5┤ i≤ useΣ iε linσ 5╢ afte≥ ì
thσ reserveΣ worΣ CASE«á Then¼ iε ß list¼ arσ thσ allow-ablσ value≤ i⌠ caε ì
takσ - ▒ througΦ ╣ - followeΣ b∙ thσ ELSE.
Thσá verΓá DELA┘á stop≤á executioε fo≥ thσ defineΣá numbe≥á oµá milli-ì
seconds« Thi≤ numbe≥ mus⌠ bσ aε integer« Iε Beσ BASIC¼ yo⌡ woulΣ probabl∙ ì
PLA┘ 0,1╢ t∩ gaiε aε equivalen⌠ effect« Thσ verΓ HALT¼ iε linσ 57¼ i≤ iε ì
fac⌠ ß predefineΣ procedure¼ equivalen⌠ t∩ BASIC'≤ EN─ verb.
Thi≤á i≤á aε eas∙ wa∙ t∩ develo≡ ß prograφ witΦ ßá menu«á Thσá actioε ì
takeε iε eacΦ casσ herσ i≤ simpl∙ t∩ prin⌠ ou⌠ oε thσ VD╘ thσ numbe≥ oµ thσ ì
iteφá chosen«á Thi≤ i≤ ß calleΣ ß men⌡ "stubó - wheε thσ routinσ fo≥á tha⌠ ì
actioε i≤ codeΣ anΣ debugged¼á it≤ procedurσ cal∞ wil∞ bσ sub-stituteΣá fo≥ ì
thσ stub¼á and¼ sincσ botΦ men⌡ anΣ procedurσ arσ correct¼ therσ wil∞ bσ n∩ ì
neeΣ fo≥ furthe≥ testing« You'l∞ notice¼ too¼ ho≈ ß sequencσ oµ statement≤ ì
caε bσ writteε oε onσ line¼á as¼á fo≥ example¼ iε linσ 58¼ jus⌠ a≤ iε BASI├ ì
wσ caε havσ ß multi-statemen⌠ line.
END OF PART FIVE.