Подсчитать количество символов слов и предложений в текстовом файле — Pascal(Паскаль)/Delphi(Делфи)

Язык программирования Pascal

procedure Counter(var symbols, words, sentences: integer);
var
  f: text;
  letter: char;
  end_of_word, end_of_sentence: boolean;
begin
  symbols := 0;
  words := 0;
  sentences := 0;
  end_of_word := false;
  end_of_sentence := false;
  assign(f, 'file.txt');
  reset(f);
  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;
  close(f);
end;

Язык программирования 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;

Leave a Comment

64 + = 74