home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ftp.barnyard.co.uk
/
2015.02.ftp.barnyard.co.uk.tar
/
ftp.barnyard.co.uk
/
cpm
/
walnut-creek-CDROM
/
MBUG
/
MBUG017.ARC
/
WORDC1.PAS
< prev
next >
Wrap
Pascal/Delphi Source File
|
1979-12-31
|
4KB
|
180 lines
Program Lettercount;
Const
Name_length = 14;
Bufsize = 250;
Bufbytesize = 32000;
maxwordlen = 20;
Type
upper = 'A'..'Z';
lower = 'a'..'z';
frequecies = 0..maxint;
ucase = array[upper] of frequecies;
lcase = array[lower] of frequecies;
Name= String[Name_length];
charindex = 1..maxwordlen;
counttype = 1..maxint;
wordtype = array[charindex] of char;
pointer = ^entrytype;
entrytype = record
left,right : pointer;
word : wordtype;
count : counttype;
end;
var
Filename :name;
Master_file : file;
New_file :text;
buffer :array[1..bufbytesize] of byte;
Rec_to_read,
Remaining,I : integer;
No_Error : Boolean;
ch:char;
unumber:ucase ;
lnumber:lcase;
Lettercount:integer;
wordtree : pointer;
nextword : wordtype;
letters : set of char;
alphabet : set of char;
uppercase: set of upper;
lowercase: set of lower;
Function Exist(Filename:name):boolean;
var
fil:file;
begin
assign(fil,filename);
{$I-} reset(fil) {$I+};
if IOresult <>0 then exist := false else exist:=true;
end;
Procedure Construct;
begin
for ch := 'A' to 'Z' do unumber[ch]:=0;
for ch := 'a' to 'z' do lnumber[ch]:=0;
lettercount:=0;
No_error:=true;
remaining:=filesize(Master_file);
while (remaining >0) and No_error do begin
if bufsize <= remaining then
rec_to_read := bufsize
else
rec_to_read := remaining;
{$I-} blockread(Master_file,buffer,rec_to_read) {$I+};
if IOresult <>0 then No_error :=false;
for i := 1 to Rec_to_read*128 do begin
ch := chr(buffer[i]);
if ch in ['A'..'Z'] then begin
unumber[ch] := unumber[ch]+1;
lettercount :=lettercount+1;
end;
if ch in ['a'..'z'] then begin
lnumber[ch] := lnumber[ch]+1;
lettercount := lettercount+1;
end;
end;
remaining := remaining - Rec_to_read;
end;
end;
Procedure Readword(var packedword : wordtype);
const
blank = ' ';
var
buffer :array[charindex] of char;
charcount:0..maxwordlen;
ch:char;
begin
if not eof(New_file) then
repeat
read(New_file,ch)
until eof(New_file) or (ch in letters);
if not eof(New_file) then begin
charcount:=0;
while ch in letters do begin
if charcount <maxwordlen then begin
charcount := charcount +1;
buffer[charcount] := ch;
end;{then}
if eof(New_file) then ch:=blank
else read(New_file,ch)
end;
for charcount := charcount +1 to maxwordlen do
buffer[charcount] := blank;
end;{while}
end;
Procedure printword(packedword : wordtype);
const
blank =' ';
var
buffer : array[charindex] of char;
charpos : 1..maxwordlen;
begin
for charpos :=1 to maxwordlen do
write(buffer[charpos]);
end;
Procedure makentry(var tree:pointer; entry: wordtype);
begin
if tree=nil then begin
new(tree);
with tree^ do begin
word := entry;
count :=1;
left := nil;
right := nil;
end;{with}
end
else with tree^ do
if entry < word then makentry(left,entry)
else if entry > word then makentry(right,entry)
else count := count +1;
end;
Procedure printtree(tree:pointer);
begin
if tree <> nil then
with tree^ do begin
printtree(left);
printword(word);
writeln(count);
printtree(right);
end;
end;
begin
clrscr;
alphabet := ['A'..'Z']+['a'..'z'];
letters :=['A'..'Z','a'..'z'];
writeln('This program will count and tell you how many different letters');
writeln('are in a passage of text. It will list the letters and how many');
writeln('they occurr. Please enter the name of file to be read. ');
repeat
write('File Name >');
readln(filename);
until exist(filename);
assign(Master_file,filename);
reset(Master_file);
construct;
i:=0;
for ch := 'a' to 'z' do begin
write(ch:10,'=',lnumber[ch]:4);
i:=i+1;
if i mod 4 = 0 then writeln;
end;
writeln;writeln;
i:=0;
for ch :='A' to 'Z' do begin
write(ch:10,'=',unumber[ch]:4);
i:=i+1;
if i mod 4 = 0 then writeln;
end;
writeln;writeln;
writeln('Total letters in ',filename,' =',lettercount:6);
end.