Подсчитать количество символов слов и предложений в текстовом файле на языке delphi
Категория: Delphi/Pascal
2011-08-28 22:47:23
code: #delphi
- procedure Counter(var symbols,words,sentences:integer);
- var f:textfile;
- letter:char;
- end_of_word, end_of_sentence:boolean;
- begin
- symbols:=0;
- words:=0;
- sentences:=0;
- end_of_word:=false;
- end_of_sentence:=false;
- assignfile(f,'file.txt');
- reset(f);
- if IOResult<>0 then ShowMessage('Файла нет') else
- begin
- while not eof(f) do
- begin
- Read(f,letter);
- symbols:=symbols+1;
- if letter in [' ' , '.' , ',' , ';' , ':' , '-', #10, #13, #9] then
- begin
- if not end_of_word then
- words:=words+1;
- end_of_word:=true;
- if not end_of_sentence and (letter='.') then
- begin
- end_of_sentence:=true;
- sentences:=sentences+1
- end;
- end
- else
- begin
- end_of_word:=false;
- end_of_sentence=false
- end;
- end;
- if not end_of_word then
- words:=words+1;
- if not end_of_sentence then
- sentence:=sentences+1;
- end;
- closefile(f);
- end;
- procedure TForm1.Button1Click(Sender:TObject);
- var symbs,words,sents:integer;
- begin
- Counter(symbs,words,sents);
- Label1.Caption:='Символов '+IntToStr(symbs);
- Label2.Caption:='Слов '+IntToStr(words);
- Label3.Caption:='Предложений '+IntToStr(sents);
- end;
Поделиться: