HunteryS

Untitled

Nov 13th, 2024
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 10.67 KB | None | 0 0
  1. Program Lab3_1;
  2.  
  3. Uses
  4.     System.SysUtils;
  5.  
  6. Procedure PrintCondition();
  7. Begin
  8.     Writeln('Данная программа перекодирует текст на основе таблицы перекодировки');
  9.     Writeln('Каждый символ из строки St1 будет заменен на соответствующий символ из строки St2');
  10. End;
  11.  
  12. Function ReadNum(Min, Max: Integer): Integer;
  13. Var
  14.     Num: Integer;
  15.     IsCorrect: Boolean;
  16. Begin
  17.     Repeat
  18.         IsCorrect := True;
  19.         Try
  20.             Readln(Num);
  21.         Except
  22.             Write('Некорректный ввод! Введите значение еще раз: ');
  23.             IsCorrect := False;
  24.         End;
  25.         If (IsCorrect) And ((Num < Min) Or (Num > Max)) Then
  26.         Begin
  27.             Write('Недопустимое значение! Введите значение еще раз: ');
  28.             IsCorrect := False;
  29.         End;
  30.     Until (IsCorrect);
  31.     ReadNum := Num;
  32. End;
  33.  
  34. Function UniquenessOfSymbols(S : String) : Boolean;
  35. Var
  36.     I, J : Integer;
  37.     Uniquenes : Boolean;
  38. Begin
  39.     Uniquenes := True;
  40.     For I := 1 To Length(S)  Do
  41.     Begin
  42.         For J := I + 1 To Length(S) Do
  43.         Begin
  44.             If (S[I] = S[J]) Then
  45.             Begin
  46.                 Uniquenes := False;
  47.             End;
  48.         End;
  49.     End;
  50.     UniquenessOfSymbols := Uniquenes;
  51. End;
  52.  
  53. Function HasCommonCharacters(St1, St2: String): Boolean;
  54. Var
  55.     I, J : Integer;
  56.     Uniquenes : Boolean;
  57. Begin
  58.     Uniquenes := True;
  59.     For I := 1  To Length(St1)  Do
  60.     Begin
  61.         For J := 1 To Length(St2) Do
  62.         Begin
  63.             If (Uniquenes) And (St1[I] = St2[J]) Then
  64.             Begin
  65.                 Uniquenes := False;
  66.             End;
  67.         End;
  68.     End;
  69.     HasCommonCharacters := Uniquenes;
  70. End;
  71.  
  72. Function GetNewText(InputText, St1, St2: String): String;
  73. Var
  74.     I, J: Integer;
  75. Begin
  76.     For I := 1 To Length(InputText) Do
  77.         For J := 1 To Length(St1) Do
  78.             If InputText[I] = St1[J] Then
  79.             Begin
  80.                 InputText[I] := St2[J];
  81.             End;
  82.     GetNewText := InputText;
  83. End;
  84.  
  85.  
  86. Function CheckFile(Path : String): Boolean;
  87. Var
  88.     InputFile: TextFile;
  89.     Line, St1, St2, Text: String;
  90.     LineCount: Integer;
  91.     IsFileCorrect: Boolean;
  92. Begin
  93.     IsFileCorrect := True;
  94.     LineCount := 0;
  95.  
  96.     If FileExists(Path) Then
  97.     Begin
  98.         AssignFile(InputFile, Path);
  99.         If (ExtractFileExt(Path) = '.txt') Then
  100.         Begin
  101.             Try
  102.                 Reset(InputFile);
  103.  
  104.                 While Not Eof(InputFile) And (LineCount < 3) Do
  105.                 Begin
  106.                     Readln(InputFile, Line);
  107.                     LineCount := LineCount + 1;
  108.  
  109.                     If LineCount = 1 Then
  110.                         St1 := Line
  111.                     Else If LineCount = 2 Then
  112.                         St2 := Line
  113.                     Else
  114.                         Text := Text + Line + ' ';
  115.                 End;
  116.  
  117.                 If LineCount < 2 Then
  118.                 Begin
  119.                     Writeln('Ошибка: файл должен содержать хотя бы две строки для перекодировки.');
  120.                     IsFileCorrect := False;
  121.                 End;
  122.  
  123.                 If (IsFileCorrect) And ((Length(St1) = 0) Or Not UniquenessOfSymbols(St1)) Then
  124.                 Begin
  125.                     Writeln('Ошибка: строка St1 некорректна (пустая или с повторяющимися символами).');
  126.                     IsFileCorrect := False;
  127.                 End;
  128.  
  129.                 If (IsFileCorrect) And ((Length(St2) = 0) Or Not UniquenessOfSymbols(St2)) Then
  130.                 Begin
  131.                     Writeln('Ошибка: строка St2 некорректна (пустая или с повторяющимися символами).');
  132.                     IsFileCorrect := False;
  133.                 End;
  134.  
  135.                 If (IsFileCorrect) And Not HasCommonCharacters(St1, St2) Then
  136.                 Begin
  137.                     Writeln('Ошибка: строки St1 и St2 содержат общие символы.');
  138.                     IsFileCorrect := False;
  139.                 End;
  140.  
  141.                 If (IsFileCorrect) And (Length(St1) <> Length(St2)) Then
  142.                 Begin
  143.                     Writeln('Ошибка: строки St1 и St2 имеют разную длину.');
  144.                     IsFileCorrect := False;
  145.                 End;
  146.  
  147.                 CloseFile(InputFile);
  148.             Except
  149.                 Writeln('Не удалось открыть файл.');
  150.                 IsFileCorrect := False;
  151.             End;
  152.         End
  153.         Else
  154.         Begin
  155.             Writeln('Файл не типа txt!');
  156.             IsFileCorrect := False;
  157.         End;
  158.     End
  159.     Else
  160.     Begin
  161.         Writeln('Файл не существует!');
  162.         IsFileCorrect := False;
  163.     End;
  164.  
  165.  
  166.     CheckFile := IsFileCorrect;
  167. End;
  168.  
  169. Procedure InputSt(var St1, St2 : String);
  170. Var
  171.     IsCorrect: Boolean;
  172. Begin
  173.     Repeat
  174.         IsCorrect := True;
  175.         Write('Введите строку St1 (таблица перекодировки): ');
  176.         Try
  177.             Readln(St1);
  178.         Except
  179.             IsCorrect := False;
  180.         End;
  181.         If (IsCorrect) And (Length(st1) = 0) Or Not UniquenessOfSymbols(St1) Then
  182.         Begin
  183.             IsCorrect := False;
  184.             Writeln('Строка 1 некорректна(Она либо пуста, либо имеет одинаковые символы ) ');
  185.         End;
  186.  
  187.     Until IsCorrect;
  188.  
  189.     Repeat
  190.         IsCorrect := True;
  191.         Write('Введите строку St2 (таблица перекодировки): ');
  192.         Try
  193.             Readln(St2);
  194.         Except
  195.             IsCorrect := False;
  196.         End;
  197.         If (IsCorrect) And (Length(St2) = 0) Or Not UniquenessOfSymbols(St2) Or Not HasCommonCharacters(St1, St2) Then
  198.         Begin
  199.             IsCorrect := False;
  200.             Writeln('Строка 2 некорректна(Она либо пуста, либо имеет одинаковые символы ) ');
  201.         End;
  202.  
  203.         If (Length(St1) <> Length(St2)) Then
  204.         Begin
  205.             IsCorrect := False;
  206.             Writeln('Строки имеют разную длину!');
  207.         End;
  208.  
  209.     Until IsCorrect;
  210.  
  211. End;
  212.  
  213. Function ReadFile(Path: String): String;
  214. Var
  215.     Text, BufText: String;
  216.     InputFile: TextFile;
  217. Begin
  218.     AssignFile(InputFile, Path);
  219.     Reset(InputFile);
  220.  
  221.     Repeat
  222.         Readln(InputFile, BufText);
  223.         Text := Text + BufText + ' ';
  224.     Until Eof(InputFile);
  225.     Close(InputFile);
  226.  
  227.     ReadFile := Text;
  228. End;
  229.  
  230. Function InputDataFromFile(Var St1, St2 : String): String;
  231. Var
  232.     PathFile, Line, Text: String;
  233.     InputFile: TextFile;
  234.     LineCount: Integer;
  235. Begin
  236.     Writeln('Данные в файле должны содержать:'#10' 1) строка перекодировки St1'#10' 2) строка перекодировки St2'#10' 3) текст для перекодировки');
  237.  
  238.     Repeat
  239.         Write('Введите путь к файлу с его расширением: ');
  240.         Readln(PathFile);
  241.     Until CheckFile(PathFile);
  242.  
  243.     AssignFile(InputFile, PathFile);
  244.     Reset(InputFile);
  245.     LineCount := 0;
  246.     Text := '';
  247.  
  248.     While Not Eof(InputFile) Do
  249.     Begin
  250.         Readln(InputFile, Line);
  251.         LineCount := LineCount + 1;
  252.  
  253.         If LineCount = 1 Then
  254.             St1 := Line
  255.         Else If LineCount = 2 Then
  256.             St2 := Line
  257.         Else
  258.             Text := Text + Line + ' ';
  259.     End;
  260.  
  261.     CloseFile(InputFile);
  262.     InputDataFromFile := Text;
  263. End;
  264.  
  265. Function InputTextFromConsole(Var St1, St2 : String): String;
  266. Var
  267.     Text: String;
  268.     IsTextEmpty: Boolean;
  269. Begin
  270.     Repeat
  271.         Writeln('Введите текст для перекодировки:');
  272.         Readln(Text);
  273.         Text := Text + ' ';
  274.         IsTextEmpty := (High(Text) = 0);
  275.     Until Not IsTextEmpty;
  276.     InputSt(St1, St2);
  277.     InputTextFromConsole := Text;
  278. End;
  279.  
  280. Function InputText(Var St1, St2 : String): String;
  281. Var
  282.     Choice: Integer;
  283.     Text: String;
  284. Begin
  285.     Write('Выберите вариант ввода:', #10, '1. Ввод из консоли', #10, '2. Ввод из файла',#10, 'Использовать вариант: ');
  286.     Choice := ReadNum(1,2);
  287.     If Choice = 1 Then
  288.     Begin
  289.         Text := InputTextFromConsole(St1, St2);
  290.     End
  291.     Else
  292.         Text := InputDataFromFile(St1, St2);
  293.  
  294.     InputText := Text;
  295. End;
  296.  
  297. Procedure OutputTextToConsole(Text: String);
  298. Begin
  299.     Writeln(Text);
  300. End;
  301.  
  302. Procedure OutputTextToFile(Text: String);
  303. Var
  304.     IsPathCorrect: Boolean;
  305.     Path: String;
  306.     OutputFile: TextFile;
  307. Begin
  308.     Writeln('Для вывода введите путь к файлу и его имя c расширением. Если файл отсутствует то он будет создан автоматически по указанному пути или в корневой папке программы (по умолчанию)');
  309.     Repeat
  310.         IsPathCorrect := True;
  311.         Write('Введите путь: ');
  312.         Readln(Path);
  313.         If Path = '' Then
  314.             IsPathCorrect := false
  315.         Else
  316.  
  317.         Begin
  318.             AssignFile(OutputFile, Path);
  319.             If (ExtractFileExt(Path) = '.txt') Then
  320.             Begin
  321.                 Try
  322.                     Rewrite(OutputFile);
  323.                 Except
  324.                     Writeln('Не удалось вывести в Файл');
  325.                     IsPathCorrect := False;
  326.                 End;
  327.             End
  328.             Else
  329.             Begin
  330.                 IsPathCorrect := False;
  331.                 Writeln('Файл не типа txt!');
  332.             End;
  333.         End;
  334.     Until IsPathCorrect;
  335.  
  336.     Writeln(OutputFile, Text);
  337.     CloseFile(OutputFile);
  338.     Writeln('Вывод данных...  успешно!');
  339. End;
  340.  
  341.  
  342. Procedure OutputText(Text: String);
  343. Var
  344.     Choice: Integer;
  345. Begin
  346.     Write('Выберите вариант вывода:', #10, '1. Вывод в консоль', #10, '2. Вывод файл',
  347.            #10, 'Использовать вариант: ');
  348.     Choice := ReadNum(1,2);
  349.  
  350.     If Choice = 1 Then
  351.         OutputTextToConsole(Text)
  352.     Else
  353.         OutputTextToFile(Text);
  354. End;
  355. Var
  356.     St1, St2 : String;
  357.     TextIn, NewText: String;
  358. Begin
  359.     PrintCondition();
  360.     TextIn := InputText(St1, St2);
  361.     NewText := GetNewText(TextIn, St1, St2);
  362.     OutputText(NewText);
  363.     Readln;
  364. End.
Advertisement
Add Comment
Please, Sign In to add comment