Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Program lab3_1;
- {$APPTYPE CONSOLE}
- uses
- System.SysUtils;
- type
- TString = array of string;
- Function InputFileLocation(): string;
- Var
- IsCorrect: Boolean;
- Location: String;
- Begin
- Repeat
- IsCorrect := false;
- WriteLn('Enter file location:');
- ReadLn(Location);
- If FileExists(Location) then
- IsCorrect := true
- Else
- Begin
- WriteLn('Please enter the correct location');
- WriteLn('');
- End;
- Until IsCorrect;
- InputFileLocation := Location;
- End;
- Function ChooseInput(): integer;
- Var
- Line: String;
- IsCorrect: Boolean;
- Begin
- Repeat
- IsCorrect := true;
- WriteLn('Do you want to input from file? (y/n)');
- ReadLn(line);
- Line := Line.ToLower();
- If(Line <> '') and (Line <> 'y') and (Line <> 'n') then
- Begin
- IsCorrect := false;
- WriteLn('Enter valid answer');
- End;
- Until IsCorrect;
- If (Line = '') or (Line = 'y') then
- ChooseInput := 0
- Else
- ChooseInput := 1;
- End;
- Procedure GetLineFromFile(Str: String);
- var
- IsCorrect: Boolean;
- TFile: TextFile;
- begin
- repeat
- IsCorrect := true;
- AssignFile(TFile, InputFileLocation());
- Reset(TFile);
- ReadLn(TFile, Str);
- CloseFile(TFile);
- if (Length(Str) = 0) then
- begin
- WriteLn('One or both strings are empty. Enter another file location');
- IsCorrect := false;
- end;
- until IsCorrect;
- end;
- Procedure OutputToFile(ShortestWordPos: Integer);
- Var
- TxtFile: TextFile;
- Begin
- AssignFile(TxtFile, InputFileLocation());
- Rewrite(TxtFile);
- Writeln(TxtFile, ShortestWordPos);
- End;
- Function GetLineFromConsole(): String;
- var
- IsCorrect: Boolean;
- InputLine: String;
- begin
- repeat
- IsCorrect := true;
- ReadLn(InputLine);
- if Length(InputLine) = 0 then
- begin
- WriteLn('The line is empty, re-enter please');
- IsCorrect := false;
- end;
- until IsCorrect;
- GetLineFromConsole := InputLine;
- end;
- Function FindAmountOfWordsInSentence(Str: String): Integer;
- Var
- Amount, I: Integer;
- Begin
- Amount := 1;
- for I := 1 to High(Str) do
- if Str[I] = ' ' then
- Inc(Amount);
- FindAmountOfWordsInSentence := Amount;
- End;
- Function FindWords(Str: String; Amount: Integer): TString;
- Var
- Words: TString;
- I, J: Integer;
- Begin
- J := 1;
- SetLength(Words, Amount);
- for I := 0 to Amount - 1 do
- Begin
- while (Str[J] <> ' ') and (J <= length(Str) ) do
- Begin
- Words[I] := Words[I] + Str[J];
- Inc(J);
- End;
- Inc(J);
- End;
- FindWords := Words;
- End;
- Function FindShortestWord(Amount: Integer; Words: TString): String;
- Var
- I, MinLength: Integer;
- ShortestWord: String;
- Begin
- MinLength := Length(Words[0]);
- for I := 0 to Amount - 1 do
- if MinLength > Length(Words[I]) then
- Begin
- MinLength := Length(Words[I]);
- ShortestWord := Words[I];
- End;
- FindShortestWord := ShortestWord;
- End;
- Function FindShortestWordPos(Amount: Integer; Words: TString; ShortestWord, Str: String): Integer;
- var
- ShortestWordPos, I: Integer;
- Begin
- ShortestWordPos := Pos(ShortestWord, Str);
- FindShortestWordPos := ShortestWordPos;
- End;
- Procedure Main();
- Var
- ChosenInput, ShortestWordPos, Amount: Integer;
- Str, ShortestWord: String;
- Words : TString;
- Begin
- WriteLn('Программа находит самое короткое слово в предложении и указывает позицию , с которой но начинается.');
- ChosenInput := ChooseInput();
- If (ChosenInput = 0) then
- GetLineFromFile(Str)
- Else
- Begin
- Writeln('Введите предложение');
- Str := GetLineFromConsole();
- End;
- Amount := FindAmountOfWordsInSentence(Str);
- Words := FindWords(Str, Amount);
- ShortestWord := FindShortestWord(Amount, Words);
- ShortestWordPos := FindShortestWordPos(Amount, Words, ShortestWord, Str);
- writeln('Позиция самого короткого слова предложения');
- Writeln(ShortestWordPos);
- WriteLn;
- OutputToFile(ShortestWordPos);
- ReadLn;
- End;
- Begin
- Main();
- End.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement