Вывести предложение с наименьшим количеством слов
Копируем весь текст, создаем новый проект, и заменяем весь текст Unit1 вот этим. Кидаем на форму 2 Memo (Memo1, Memo2) и кнопку Button1. Или качаем исходник внизу статьи.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Memo2: TMemo;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
Row, Column: Integer;
Symbol: Char;
implementation
{$R *.dfm}
function IsEndSentence(Simbol: Char): boolean;
begin
IsEndSentence := Simbol in [#10, '.', '!', '?'];
end;
function IsSymbolWord(Simbol: Char): boolean;
begin
IsSymbolWord := Simbol in ['a'..'z', 'A'..'Z', 'а'..'я', 'А'..'я']; end;
function GetSymbol: Char;
var
Line: String;
begin
if Form1.Memo1.Lines.Count > Row then
begin
Line := Form1.Memo1.Lines.Strings[Row];
if Length(Line) >= Column then
begin
Symbol := Line[Column];
inc(Column);
end
else
begin
inc(Row);
Column := 1;
Symbol := GetSymbol;
end;
end
else
Symbol := #10;
GetSymbol := Symbol;
end;
function GetSentence: String;
var
Sentence: String;
IsReadSentence: Boolean;
begin
Sentence := '';
repeat
Symbol := GetSymbol;
until ((Symbol <> ' ') or IsEndSentence(Symbol));
if not IsEndSentence(Symbol) then
Sentence := Sentence + Symbol;
repeat
Symbol := getSymbol;
Sentence := Sentence + Symbol;
until IsEndSentence(Symbol);
GetSentence := Sentence;
end;
function GetWordCount(Sentence: String): Integer;
var
i, wrdCount: Integer;
k: Integer;
IsWord: Boolean;
begin
IsWord := False;
wrdCount := 0;
k := 0;
for i := 1 to Length(Sentence) do
if IsSymbolWord(Sentence[i]) then
begin
if Not IsWord then
begin
wrdCount := wrdCount + 1;
IsWord := True;
end;
end
else
IsWord := False;
GetWordCount := wrdCount;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
IsFinish: Boolean;
FinishSentence, Sentence: String;
FinishWordCount, WordCount: Integer;
begin
IsFinish := False;
FinishWordCount := -1;
repeat
Sentence := GetSentence;
if Length(Sentence) = 1 then
if Sentence[1] = #10 then
IsFinish := True;
if not IsFinish then
begin
WordCount := GetWordCount(Sentence);
if (FinishWordCount = -1) or (FinishWordCount > WordCount) then
begin
FinishWordCount := WordCount;
FinishSentence := Sentence;
end;
end;
until IsFinish;
Form1.Memo2.Text := FinishSentence;
Row := 0;
Column := 1;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin Row := 0;
Column := 1;
end; end. |