Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Program Lab3_1;
- Uses
- System.SysUtils;
- Procedure PrintCondition();
- Begin
- Writeln('Данная программа перекодирует текст на основе таблицы перекодировки');
- Writeln('Каждый символ из строки St1 будет заменен на соответствующий символ из строки St2');
- End;
- Function ReadNum(Min, Max: Integer): Integer;
- Var
- Num: Integer;
- IsCorrect: Boolean;
- Begin
- Repeat
- IsCorrect := True;
- Try
- Readln(Num);
- Except
- Write('Некорректный ввод! Введите значение еще раз: ');
- IsCorrect := False;
- End;
- If (IsCorrect) And ((Num < Min) Or (Num > Max)) Then
- Begin
- Write('Недопустимое значение! Введите значение еще раз: ');
- IsCorrect := False;
- End;
- Until (IsCorrect);
- ReadNum := Num;
- End;
- Function UniquenessOfSymbols(S : String) : Boolean;
- Var
- I, J : Integer;
- Uniquenes : Boolean;
- Begin
- Uniquenes := True;
- For I := 1 To Length(S) Do
- Begin
- For J := I + 1 To Length(S) Do
- Begin
- If (S[I] = S[J]) Then
- Begin
- Uniquenes := False;
- End;
- End;
- End;
- UniquenessOfSymbols := Uniquenes;
- End;
- Function HasCommonCharacters(St1, St2: String): Boolean;
- Var
- I, J : Integer;
- Uniquenes : Boolean;
- Begin
- Uniquenes := True;
- For I := 1 To Length(St1) Do
- Begin
- For J := 1 To Length(St2) Do
- Begin
- If (Uniquenes) And (St1[I] = St2[J]) Then
- Begin
- Uniquenes := False;
- End;
- End;
- End;
- HasCommonCharacters := Uniquenes;
- End;
- Function GetNewText(InputText, St1, St2: String): String;
- Var
- I, J: Integer;
- Begin
- For I := 1 To Length(InputText) Do
- For J := 1 To Length(St1) Do
- If InputText[I] = St1[J] Then
- Begin
- InputText[I] := St2[J];
- End;
- GetNewText := InputText;
- End;
- Function CheckFile(Path : String): Boolean;
- Var
- InputFile: TextFile;
- Line, St1, St2, Text: String;
- LineCount: Integer;
- IsFileCorrect: Boolean;
- Begin
- IsFileCorrect := True;
- LineCount := 0;
- // Проверка на существование файла и его расширение
- If FileExists(Path) Then
- Begin
- AssignFile(InputFile, Path);
- If (ExtractFileExt(Path) = '.txt') Then
- Begin
- Try
- Reset(InputFile);
- // Чтение строк из файла
- While Not Eof(InputFile) And (LineCount < 3) Do
- Begin
- Readln(InputFile, Line);
- LineCount := LineCount + 1;
- // Строка 1 - St1
- If LineCount = 1 Then
- St1 := Line
- // Строка 2 - St2
- Else If LineCount = 2 Then
- St2 := Line
- // Остальной текст (для перекодировки)
- Else
- Text := Text + Line + ' ';
- End;
- // Проверки на корректность содержимого файла
- If LineCount < 2 Then
- Begin
- Writeln('Ошибка: файл должен содержать хотя бы две строки для перекодировки.');
- IsFileCorrect := False;
- End;
- // Проверка строки St1
- If (IsFileCorrect) And ((Length(St1) = 0) Or Not UniquenessOfSymbols(St1)) Then
- Begin
- Writeln('Ошибка: строка St1 некорректна (пустая или с повторяющимися символами).');
- IsFileCorrect := False;
- End;
- // Проверка строки St2
- If (IsFileCorrect) And ((Length(St2) = 0) Or Not UniquenessOfSymbols(St2)) Then
- Begin
- Writeln('Ошибка: строка St2 некорректна (пустая или с повторяющимися символами).');
- IsFileCorrect := False;
- End;
- // Проверка на общие символы между St1 и St2
- If (IsFileCorrect) And Not HasCommonCharacters(St1, St2) Then
- Begin
- Writeln('Ошибка: строки St1 и St2 содержат общие символы.');
- IsFileCorrect := False;
- End;
- // Проверка на одинаковую длину строк St1 и St2
- If (IsFileCorrect) And (Length(St1) <> Length(St2)) Then
- Begin
- Writeln('Ошибка: строки St1 и St2 имеют разную длину.');
- IsFileCorrect := False;
- End;
- // Закрытие файла
- CloseFile(InputFile);
- Except
- Writeln('Не удалось открыть файл.');
- IsFileCorrect := False;
- End;
- End
- Else
- Begin
- Writeln('Файл не типа txt!');
- IsFileCorrect := False;
- End;
- End
- Else
- Begin
- Writeln('Файл не существует!');
- IsFileCorrect := False;
- End;
- CheckFile := IsFileCorrect;
- End;
- Procedure InputSt(var St1, St2 : String);
- Var
- IsCorrect: Boolean;
- Begin
- Repeat
- IsCorrect := True;
- Write('Введите строку St1 (таблица перекодировки): ');
- Try
- Readln(St1);
- Except
- IsCorrect := False;
- End;
- If (IsCorrect) And (Length(st1) = 0) Or Not UniquenessOfSymbols(St1) Then
- Begin
- IsCorrect := False;
- Writeln('Строка 1 некорректна(Она либо пуста, либо имеет одинаковые символы ) ');
- End;
- Until IsCorrect;
- Repeat
- IsCorrect := True;
- Write('Введите строку St2 (таблица перекодировки): ');
- Try
- Readln(St2);
- Except
- IsCorrect := False;
- End;
- If (IsCorrect) And (Length(St2) = 0) Or Not UniquenessOfSymbols(St2) Or Not HasCommonCharacters(St1, St2) Then
- Begin
- IsCorrect := False;
- Writeln('Строка 2 некорректна(Она либо пуста, либо имеет одинаковые символы ) ');
- End;
- If (Length(St1) <> Length(St2)) Then
- Begin
- IsCorrect := False;
- Writeln('Строки имеют разную длину!');
- End;
- Until IsCorrect;
- End;
- Function ReadFile(Path: String): String;
- Var
- Text, BufText: String;
- InputFile: TextFile;
- Begin
- AssignFile(InputFile, Path);
- Reset(InputFile);
- Repeat
- Readln(InputFile, BufText);
- Text := Text + BufText + ' ';
- Until Eof(InputFile);
- Close(InputFile);
- ReadFile := Text;
- End;
- Function InputDataFromFile(Var St1, St2 : String): String;
- Var
- PathFile, Line, Text: String;
- InputFile: TextFile;
- LineCount: Integer;
- Begin
- Writeln('Данные в файле должны содержать:'#10' 1) строка перекодировки St1'#10' 2) строка перекодировки St2'#10' 3) текст для перекодировки');
- Repeat
- Write('Введите путь к файлу с его расширением: ');
- Readln(PathFile);
- Until CheckFile(PathFile);
- AssignFile(InputFile, PathFile);
- Reset(InputFile);
- LineCount := 0;
- Text := '';
- While Not Eof(InputFile) Do
- Begin
- Readln(InputFile, Line);
- LineCount := LineCount + 1;
- If LineCount = 1 Then
- St1 := Line
- Else If LineCount = 2 Then
- St2 := Line
- Else
- Text := Text + Line + ' ';
- End;
- CloseFile(InputFile);
- InputDataFromFile := Text;
- End;
- Function InputTextFromConsole(Var St1, St2 : String): String;
- Var
- Text: String;
- IsTextEmpty: Boolean;
- Begin
- Repeat
- Writeln('Введите текст для перекодировки:');
- Readln(Text);
- Text := Text + ' ';
- IsTextEmpty := (High(Text) = 0);
- Until Not IsTextEmpty;
- InputSt(St1, St2);
- InputTextFromConsole := Text;
- End;
- Function InputText(Var St1, St2 : String): String;
- Var
- Choice: Integer;
- Text: String;
- Begin
- Write('Выберите вариант ввода:', #10, '1. Ввод из консоли', #10, '2. Ввод из файла',#10, 'Использовать вариант: ');
- Choice := ReadNum(1,2);
- If Choice = 1 Then
- Begin
- Text := InputTextFromConsole(St1, St2);
- End
- Else
- Text := InputDataFromFile(St1, St2);
- InputText := Text;
- End;
- Procedure OutputTextToConsole(Text: String);
- Begin
- Writeln(Text);
- End;
- Procedure OutputTextToFile(Text: String);
- Var
- IsPathCorrect: Boolean;
- Path: String;
- OutputFile: TextFile;
- Begin
- Writeln('Для вывода введите путь к файлу и его имя c расширением. Если файл отсутствует то он будет создан автоматически по указанному пути или в корневой папке программы (по умолчанию)');
- Repeat
- IsPathCorrect := True;
- Write('Введите путь: ');
- Readln(Path);
- If Path = '' Then
- IsPathCorrect := false
- Else
- Begin
- AssignFile(OutputFile, Path);
- If (ExtractFileExt(Path) = '.txt') Then
- Begin
- Try
- Rewrite(OutputFile);
- Except
- Writeln('Не удалось вывести в Файл');
- IsPathCorrect := False;
- End;
- End
- Else
- Begin
- IsPathCorrect := False;
- Writeln('Файл не типа txt!');
- End;
- End;
- Until IsPathCorrect;
- Writeln(OutputFile, Text);
- CloseFile(OutputFile);
- Writeln('Вывод данных... успешно!');
- End;
- Procedure OutputText(Text: String);
- Var
- Choice: Integer;
- Begin
- Write('Выберите вариант вывода:', #10, '1. Вывод в консоль', #10, '2. Вывод файл',
- #10, 'Использовать вариант: ');
- Choice := ReadNum(1,2);
- If Choice = 1 Then
- OutputTextToConsole(Text)
- Else
- OutputTextToFile(Text);
- End;
- Var
- St1, St2 : String;
- TextIn, NewText: String;
- Begin
- PrintCondition();
- TextIn := InputText(St1, St2);
- NewText := GetNewText(TextIn, St1, St2);
- OutputText(NewText);
- Readln;
- End.
Advertisement
Add Comment
Please, Sign In to add comment