free pascal 单词出现的频率

来源:百度知道 编辑:UC知道 时间:2024/06/23 02:01:42
读入一英文句子,单词之间用空格或逗号隔开,统计其中单词个
数,并输出各个单词出现的频率。
我希望是正确无误,lizhaoyan1993 所答有错

我测了一下,好像正确。
英文句子总长不超过255的可以过。
如果数据比较大,单词个数多就要用另一种算法了。到时候我再写一个。
program words;
type
re=record
data:longint;
str:string;
end;
var
f:boolean;
str,str1:string;
l,i,j,k,sword:longint;
s:set of char;
a:array[0..10000] of re;
begin
readln(str);
l:=length(str);
for i:=1 to l do if str[i] in ['A'..'Z'] then str[i]:=chr(ord(str[i])+32);
i:=1;
k:=0;
s:=['a'..'z'];
sword:=0;
while i<=l do
begin
if str[i] in s then
begin
inc(sword);
str1:='';
while (i<=l) and (str[i] in s) do
begin
str1:=str1+str[i];
inc(i);
end;
f:=false;
for j:=1 to k do if a[j].str=str1 then begin inc(a[j].data);f:=true;end;
if not f then begin k:=k+1;a[k].data:=1;a[k].str:=str1;end;