Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Program Lab3_1;
- Uses
- System.SysUtils;
- type
- InputType = (FromConsole, FromFile);
- Var
- Path: String;
- Sentence: String;
- Order: Integer;
- function GetInputType(): InputType;
- var
- Ret: InputType;
- Input: String;
- IsCorrect: Boolean;
- begin
- IsCorrect := False;
- repeat
- Writeln('Выберите способ ввода предложения файл/консоль (ф/к)');
- Readln(Input);
- if (Input = 'ф') or (Input = 'Ф') or (Input = 'Файл')
- or (Input = 'файл')
- then
- begin
- Ret := InputType.FromFile;
- IsCorrect := True;
- end
- else if (Input = 'к') or (Input = 'К') or (Input =
- 'Консоль') or (Input = 'консоль') then
- begin
- Ret := InputType.FromConsole;
- IsCorrect := True;
- end;
- until IsCorrect;
- Result := Ret;
- end;
- function GetSentenceFromConsole(Sentence:String): String;
- begin
- Writeln('Введите исходное предложение');
- Readln(Sentence);
- Result := Sentence;
- end;
- Function GetFilePath(): String;
- Var
- Path: String;
- IsCorrect: Boolean;
- Begin
- Repeat
- Writeln('Введите абсолютный путь к файлу ');
- Readln(Path);
- IsCorrect := False;
- If FileExists(Path) then
- Begin
- Writeln('Файл успешно открыт');
- IsCorrect := true
- End
- Else
- Writeln('Файл не найден');
- Until IsCorrect;
- Result := Path;
- End;
- Function GetSentenceFromFile(Sentence:String): String;
- Var
- SenFile: TextFile;
- Begin
- Path := GetFilePath();
- AssignFile(SenFile, Path);
- Reset(SenFile);
- Read(SenFile, Sentence);
- CloseFile(SenFile);
- Result := Sentence;
- End;
- Procedure PrintSentenceBeforeEditing(Sentence: String);
- Begin
- Writeln;
- Writeln('Исходное предложение: ');
- Write(Sentence);
- Writeln;
- End;
- function GetSentence(): String;
- var
- SelectedInputType: InputType;
- begin
- SelectedInputType := GetInputType();
- if (SelectedInputType = InputType.FromConsole) then
- begin
- Sentence := GetSentenceFromConsole(Sentence)
- end
- else if (SelectedInputType = InputType.FromFile) then
- begin
- Sentence := GetSentenceFromFile(Sentence);
- end;
- PrintSentenceBeforeEditing(Sentence);
- Result := Sentence;
- end;
- Function Output(): String;
- Var
- Patho: String;
- IsCorrect: Boolean;
- Begin
- IsCorrect := False;
- Repeat
- Writeln('Введите директорию, в которую хотите сохранить результат');
- Readln(Patho);
- If DirectoryExists(Patho) then
- IsCorrect := True
- Else
- Writeln('Такой директории не существует. Попробуйте ещё раз');
- Until IsCorrect;
- Result := Patho;
- End;
- Procedure SaveSentenceToFile(Sentence: String);
- Var
- I, J: Integer;
- OutputFile: TextFile;
- Directory: String;
- Begin
- Directory := Output();
- AssignFile(OutputFile, Directory + '\output.txt');
- Rewrite(OutputFile);
- Write(OutputFile,Sentence);
- Writeln(OutputFile);
- Writeln('Результат сохранён по указанному пути');
- CloseFile(OutputFile);
- Write;
- End;
- Procedure PrintSentenceAfterEditing(var Sentence: String);
- Begin
- Writeln;
- if Length(Sentence) < 1 then
- begin
- Sentence := 'Ни один символ не прошёл проверку! От вашей строки ничего не осталось!';
- end
- else
- Writeln('Исправленное предложение:');
- Writeln(Sentence);
- Writeln;
- End;
- Function Editing(Sentence: String): String;
- Var
- I:Integer;
- Begin
- I := 1;
- while (I <= Length(Sentence)) do
- begin
- if (NOT(Sentence[I] in ['0'..'9', 'a'..'z' , 'A'..'Z', ' '])) then
- begin
- Delete(Sentence, I, 1);
- Dec(I);
- end;
- Inc(I)
- end;
- Result := Sentence;
- End;
- Begin
- Sentence := GetSentence();
- Sentence := Editing(Sentence);
- PrintSentenceAfterEditing(Sentence);
- SaveSentenceToFile(Sentence);
- Readln;
- End.
Advertisement
Add Comment
Please, Sign In to add comment