-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTPDEMO.PAS
228 lines (198 loc) · 5.2 KB
/
TPDEMO.PAS
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
{$N+,E+}
{ File: TPDEMO.PAS
Turbo Pascal Demonstration program to show off Turbo Debugger
Reads words from standard input, analyzes letter and word frequency.
Uses linked list to store commandline parameters on heap.
Uses the following data types:
Boolean,
Char, Byte,
Integer, Word,
LongInt,
Extended (8087 type, links in emulator)
String,
Array,
Record,
Set,
Pointer
}
program TPDemo;
const
BufSize = 128; { length of line buffer }
MaxWordLen = 10; { maximum word length allowed }
type
BufferStr = String[BufSize];
LInfoRec = record
Count : Word; { number of occurrences of this letter }
FirstLetter : Word; { number of times as first letter of a }
end;
var
NumLines, NumWords : Word; { counters }
NumLetters : LongInt;
WordLenTable : array [1..MaxWordLen] of Word; { info for each word }
LetterTable : array['A'..'Z'] of LInfoRec; { info for each letter }
Buffer : BufferStr;
procedure ShowResults;
procedure ShowLetterInfo(FromLet, ToLet : Char);
{ Dump letter information }
var
ch : Char;
begin
Writeln;
Write('Letter: ');
for ch := FromLet to ToLet do { column titles }
Write(ch:5);
Writeln;
Write('Frequency: ');
for ch := FromLet to ToLet do { letter count }
Write(LetterTable[ch].Count:5);
Writeln;
Write('Word starts:');
for ch := FromLet to ToLet do { first letter count }
Write(LetterTable[ch].FirstLetter:5);
Writeln;
end; { ShowLetterInfo }
var
i : Integer;
AvgWords : Extended;
begin { ShowResults }
if NumLines <> 0 then
AvgWords := NumWords / NumLines
else
AvgWords := 0;
Writeln;
Writeln(NumLetters, ' char(s) in ',
NumWords, ' word(s) in ',
NumLines, ' line(s)');
Writeln('Average of ', AvgWords:0:2, ' words per line');
Writeln;
{ Dump word count }
Write('Word length:');
for i := 1 to MaxWordLen do
Write(i:4);
Writeln;
Write('Frequency: ');
for i := 1 to MaxWordLen do
Write(WordLenTable[i]:4);
Writeln;
{ Dump letter counts }
ShowLetterInfo('A', 'M');
ShowLetterInfo('N', 'Z');
end; { ShowResults }
procedure Init;
begin
NumLines := 0; NumWords := 0; NumLetters := 0;
FillChar(LetterTable, SizeOf(LetterTable), 0);
FillChar(WordLenTable, SizeOf(WordLenTable), 0);
Writeln('Enter a string to process, an empty string quits.');
end; { Init }
procedure ProcessLine(var S : BufferStr);
function IsLetter(ch : Char) : Boolean;
begin
IsLetter := UpCase(ch) in ['A'..'Z'];
end; { IsLetter }
var
i : Integer;
WordLen : Word;
begin { ProcessLine }
Inc(NumLines);
i := 1;
while i <= Length(S) do
begin
{ Skip non-letters }
while (i <= Length(S)) and not IsLetter(S[i]) do
Inc(i);
{ Find end of word, bump letter & word counters }
WordLen := 0;
while (i <= Length(S)) and IsLetter(S[i]) do
begin
Inc(NumLetters);
Inc(LetterTable[UpCase(S[i])].Count);
if WordLen = 0 then { bump counter }
Inc(LetterTable[UpCase(S[i])].FirstLetter);
Inc(i);
Inc(WordLen);
end;
{ Bump word count info }
if WordLen > 0 then
begin
Inc(NumWords);
if WordLen <= MaxWordLen then
Inc(WordLenTable[WordLen]);
end;
end; { while }
end; { ProcessLine }
function GetLine : BufferStr;
var
S : BufferStr;
begin
Write('String: ');
Readln(S);
GetLine := S;
end;
procedure ParmsOnHeap;
{ Builds a linked list of commandline parameters on the heap.
Note that the zero'th parameter, ParamStr(0), returns the
Exec name of the program on Dos 3.xx only.
}
type
ParmRecPtr = ^ParmRec;
ParmRec = record
Parm : ^String;
Next : ParmRecPtr;
end;
var
Head, Tail, Temp : ParmRecPtr;
i : Integer;
s : String;
begin
Head := nil;
for i := 0 to ParamCount do
begin
{ Get next commandline parameter }
s := ParamStr(i);
if MaxAvail < SizeOf(ParmRec) + Length(s) + 1 then { room on heap? }
begin
Writeln('Heap full, procedure aborting...');
Exit;
end;
{ Add to linked list }
New(Temp); { another Parm record }
with Temp^ do
begin
GetMem(Parm, Length(s) + 1); { string + length byte }
Parm^ := s;
Next := nil;
end;
if Head = nil then { initialize list pointer }
Head := Temp
else
Tail^.Next := Temp; { add to end }
Tail := Temp; { update tail pointer }
end; { for }
{ Dump list }
Writeln;
with Head^ do
if Parm^ <> '' then
Writeln('Program name: ', Parm^);
Write('Command line parameters: ');
Tail := Head^.Next;
while Tail <> nil do
with Tail^ do
begin
Write(Parm^, ' ');
Tail := Next;
end;
Writeln;
end; { ParmsOnHeap }
begin { program }
Init;
Buffer := GetLine;
while Buffer <> '' do
begin
ProcessLine(Buffer);
Buffer := GetLine;
end;
ShowResults;
ParmsOnHeap;
end.