Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Program laba1;
- Uses
- Windows, System.SysUtils;
- Var
- IsCorrect : Boolean;
- Procedure PrintCondition();
- Begin
- Writeln('Эта программа выводит самое короткое слово в предложении и его позицию.', #13, #10);
- End;
- Function CheckPath() : String;
- Var
- Path : String;
- IsCorrect : Boolean;
- Begin
- Writeln('Введите путь к файлу:');
- Repeat
- IsCorrect := True;
- Readln(Path);
- If Not FileExists(Path) Then
- Begin
- IsCorrect := False;
- Writeln('Такого файла не существует. Повторите ввод:');
- End
- Else If Not(ExtractFileExt(Path) = '.txt') Then
- Begin
- IsCorrect := False;
- Writeln('Неправильное расширение файла. Повторите ввод:');
- End;
- Until IsCorrect;
- CheckPath := Path;
- End;
- Function PathForRead() : String;
- Var
- MyFile : TextFile;
- Path : String;
- IsCorrect : Boolean;
- Begin
- Repeat
- IsCorrect := True;
- Path := CheckPath();
- Try
- AssignFile(MyFile, Path);
- Reset(MyFile);
- Except
- IsCorrect := False;
- Writeln('Нет доступа к файлу. Измените настройки и повторите ввод.');
- End;
- CloseFile(MyFile);
- Until IsCorrect;
- PathForRead := Path;
- End;
- Function CheckPermission(Path : String) : Boolean;
- Var
- MyFile : TextFile;
- IsCorrect : Boolean;
- Begin
- IsCorrect := True;
- AssignFile(MyFile, Path);
- Try
- Rewrite(MyFile);
- Except
- Writeln('Файл закрыт для записи.');
- IsCorrect := False;
- Readln;
- End;
- CloseFile(MyFile);
- CheckPermission := IsCorrect;
- End;
- Function PathForWrite() : String;
- Var
- Path : String;
- MyFile : TextFile;
- IsCorrect : Boolean;
- Begin
- Repeat
- IsCorrect := True;
- Path := CheckPath();
- If Not (CheckPermission(Path)) Then
- IsCorrect := False
- Until IsCorrect;
- PathForWrite := Path;
- End;
- Function Input(Var IsCorrect : Boolean) : String;
- Var
- Sentence : String;
- Begin
- Readln(Sentence);
- If Sentence = '' Then
- Begin
- IsCorrect := False;
- Writeln('Ошибка. Вы ничего не ввели.');
- End;
- Input := Sentence;
- End;
- Function ReadSentenceConsole() : String;
- Var
- Text : String;
- Begin
- Repeat
- IsCorrect := True;
- Writeln('Введите предложение:');
- Text := Input(IsCorrect);
- Until IsCorrect;
- ReadSentenceConsole := Text;
- End;
- Function IsSpaceCharacter(Ch: Char): Boolean;
- Begin
- IsSpaceCharacter := (Ch = ' ') Or (Ch = #9);
- End;
- Function ExtractWords(Const Sentence: String): TArray<string>;
- Var
- Words: TArray<string>;
- CurrentWord: string;
- CurrentWordStartIndex: Integer;
- I: Integer;
- Begin
- SetLength(Words, 0);
- CurrentWord := '';
- CurrentWordStartIndex := 1;
- I := 1;
- While I <= Length(Sentence) Do
- Begin
- While (I <= Length(Sentence)) And IsSpaceCharacter(Sentence[I]) Do
- Inc(I);
- CurrentWordStartIndex := I;
- While (I <= Length(Sentence)) And Not IsSpaceCharacter(Sentence[I]) Do
- Inc(I);
- CurrentWord := Copy(Sentence, CurrentWordStartIndex, I - CurrentWordStartIndex);
- If CurrentWord <> '' Then
- Begin
- SetLength(Words, Length(Words) + 1);
- Words[Length(Words) - 1] := CurrentWord;
- End;
- End;
- ExtractWords := Words;
- End;
- Function FindShortestWord(Const Words: TArray<String>): String;
- var
- ShortestWord: String;
- I: Integer;
- Begin
- ShortestWord := '';
- For I := 0 To Length(Words) - 1 Do
- Begin
- If (ShortestWord = '') Or (Length(Words[I]) < Length(ShortestWord)) Then
- ShortestWord := Words[I];
- End;
- FindShortestWord := ShortestWord;
- End;
- Function GetWordStartPosition(Const Sentence: String; Const Word: String): Integer;
- Var
- I, J: Integer;
- WordLength: Integer;
- Found: Boolean;
- begin
- WordLength := Length(Word);
- I := 1;
- Found := False;
- While (Not Found) And (I <= Length(Sentence) - WordLength + 1) Do
- Begin
- J := 1;
- While (J <= WordLength) And (Sentence[I + J - 1] = Word[J]) Do
- Inc(J);
- If J > WordLength then
- Found := True
- Else
- Inc(I);
- End;
- If Found Then
- GetWordStartPosition := I
- Else
- GetWordStartPosition := -1;
- End;
- Function NumberToString(Number: Integer): String;
- Var
- CurrentDigit: Integer;
- Begin
- NumberToString := '';
- Repeat
- CurrentDigit := Number Mod 10;
- Number := Number Div 10;
- NumberToString := Chr(Ord('0') + CurrentDigit) + Result;
- Until Number = 0;
- End;
- Function FormatShortestWordPosition(Const ShortestWord: String; ShortestWordStartIndex: Integer): String;
- Begin
- If ShortestWordStartIndex <> -1 Then
- FormatShortestWordPosition := 'Самое короткое слово: ' + ShortestWord +
- ', начинается с позиции: ' + NumberToString(ShortestWordStartIndex)
- Else
- FormatShortestWordPosition := 'В предложении нет слов.';
- End;
- Procedure PrintShortestWordPosition(Const ShortestWord: String; ShortestWordStartIndex: Integer);
- Var
- ResultMessage: String;
- Begin
- ResultMessage := FormatShortestWordPosition(ShortestWord, ShortestWordStartIndex);
- Writeln(ResultMessage);
- End;
- Function ReadSentenceFromFile(Var MyFile : TextFile; Var IsCorrect : Boolean) : String;
- Var
- Sentence : String;
- Begin
- Readln(MyFile, Sentence);
- If Sentence = '' Then
- Begin
- IsCorrect := False;
- Writeln('Ошибка. Недостаточно данных в файле.');
- End;
- ReadSentenceFromFile := Sentence;
- End;
- Function ReadFile() : String;
- Var
- Path : String;
- MyFile : TextFile;
- Sentence : String;
- IsCorrect : Boolean;
- Words: TArray<string>;
- ShortestWord: string;
- ShortestWordStartIndex: Integer;
- Begin
- Repeat
- IsCorrect := True;
- Path := PathForRead();
- AssignFile(MyFile, Path);
- ReSet(MyFile);
- Sentence := ReadSentenceFromFile(MyFile, IsCorrect);
- If IsCorrect Then
- Begin
- IsCorrect := False;
- Writeln('Ошибка. Недостаточно данных в файле.');
- End;
- CloseFile(MyFile);
- Until IsCorrect;
- Words := ExtractWords(Sentence);
- ShortestWord := FindShortestWord(Words);
- ShortestWordStartIndex := GetWordStartPosition(Sentence, ShortestWord);
- ReadFile := Sentence;
- End;
- Function ChooseAction() : String;
- Var
- Input: String;
- IsCorrect : Boolean;
- Begin
- Repeat
- IsCorrect := True;
- Readln(Input);
- Input := LowerCase(Input);
- If (Input <> 'console') And (Input <> 'file') Then
- Begin
- Writeln('Ошибка. Введите "console" или "file".');
- IsCorrect := False;
- End;
- Until IsCorrect;
- ChooseAction := Input;
- End;
- Function ChooseInput() : String;
- Var
- Option : String;
- MyFile : TextFile;
- Sentence : String;
- Words: TArray<string>;
- ShortestWord: string;
- ShortestWordStartIndex: Integer;
- Begin
- Writeln('Выберите способ ввода данных.', #13#10,
- 'Введите "console", если хотите ввести данные через консоль', #13#10,
- 'Введите "file", если хотите передать данные из файла');
- Option := ChooseAction();
- If Option = 'console' Then
- Begin
- Sentence := ReadSentenceConsole();
- Words := ExtractWords(Sentence);
- ShortestWord := FindShortestWord(Words);
- ShortestWordStartIndex := GetWordStartPosition(Sentence, ShortestWord);
- End
- Else
- Sentence := ReadFile();
- ChooseInput := Sentence;
- End;
- Procedure WriteConsole(Const ShortestWord: string; ShortestWordStartIndex: Integer);
- Var
- ResultMessage: String;
- Begin
- ResultMessage := FormatShortestWordPosition(ShortestWord, ShortestWordStartIndex);
- Writeln(ResultMessage);
- End;
- Procedure WriteFile(Const ShortestWord: string; ShortestWordStartIndex: Integer);
- Var
- MyFile : TextFile;
- Path : String;
- ResultMessage : String;
- Begin
- Path := PathForWrite();
- AssignFile(MyFile, Path);
- Rewrite(MyFile);
- ResultMessage := FormatShortestWordPosition(ShortestWord, ShortestWordStartIndex);
- Writeln(MyFile, ResultMessage);
- CloseFile(MyFile);
- Writeln('Результат в файле.');
- End;
- Procedure ChooseOutput(ShortestWord : String; ShortestWordStartIndex : Integer);
- Var
- Option : String;
- Begin
- Writeln('Выберите способ вывода результата.', #13#10,
- 'Введите "console", если хотите вывести результат через консоль', #13#10,
- 'Введите "file", если хотите передать результат в файл');
- Option := ChooseAction;
- If Option = 'console' Then
- WriteConsole(ShortestWord, ShortestWordStartIndex)
- Else
- WriteFile(ShortestWord, ShortestWordStartIndex);
- End;
- Var
- Sentence : String;
- ShortestWord : String;
- ResultMessage : String;
- ShortestWordStartIndex : Integer;
- Begin
- SetConsoleCP(1251);
- SetConsoleOutputCP(1251);
- PrintCondition();
- Sentence := ChooseInput();
- ChooseOutput(ShortestWord, ShortestWordStartIndex);
- Readln
- End.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement