Advertisement
venik2405

lab3_2_1

Nov 30th, 2020
306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 3.54 KB | None | 0 0
  1. Program lab3_2;
  2.  
  3. {$APPTYPE CONSOLE}
  4.  
  5. uses
  6.     System.SysUtils;
  7. type
  8.     TChar = set of char;
  9.  
  10. Function InputFileLocation(): string;
  11. Var
  12.     IsCorrect: Boolean;
  13.     Location: String;
  14. Begin
  15.     Repeat
  16.         IsCorrect := false;
  17.         WriteLn('Enter file location:');
  18.         ReadLn(Location);
  19.         If FileExists(Location) then
  20.             IsCorrect := true
  21.         Else
  22.         Begin
  23.             WriteLn('Please enter the correct location');
  24.             WriteLn('');
  25.         End;
  26.     Until IsCorrect;
  27.     InputFileLocation := Location;
  28. End;
  29.  
  30.  
  31. Function ChooseInput(): integer;
  32. Var
  33.     Line: String;
  34.     IsCorrect: Boolean;
  35. Begin
  36.     Repeat
  37.         IsCorrect := true;
  38.         WriteLn('Do you want to input from file? (y/n)');
  39.         ReadLn(line);
  40.         Line := Line.ToLower();
  41.         If(Line <> '') and (Line <> 'y') and (Line <> 'n') then
  42.         Begin
  43.             IsCorrect := false;
  44.             WriteLn('Enter valid answer');
  45.         End;
  46.     Until IsCorrect;
  47.     If (Line = '') or (Line = 'y') then
  48.         ChooseInput := 0
  49.     Else
  50.         ChooseInput := 1;
  51. End;
  52.  
  53. Procedure OutputToFile(Str: String);
  54. var
  55.     TFile: TextFile;
  56. begin
  57.     AssignFile(TFile, InputFileLocation());
  58.     Rewrite(TFile);
  59.     Write(TFile, Str);
  60.     CloseFile(TFile);
  61. end;
  62.  
  63. Function CheckUnique(Str: String; I: Integer; LetSet: TChar): Boolean;
  64. Var
  65.     IsNotUnique: Boolean;
  66. Begin
  67.     isNotUnique := false;
  68.     If Str[I] in LetSet then
  69.         isNotUnique := true
  70.     else
  71.         isNotUnique := false;
  72.     CheckUnique := isNotUnique;
  73. End;
  74.  
  75. Procedure ChangeSentence(var Str: String);
  76. var
  77.     Temp, I: Integer;
  78.     IsNotUnique: Boolean;
  79.     LetSet: TChar;
  80. Begin
  81.     LetSet := [];
  82.     I := 1;
  83.     Temp := High(Str);
  84.     while I <= Temp do
  85.     Begin
  86.         isNotUnique := true;
  87.         IsNotUnique := CheckUnique(Str, I, LetSet);
  88.         if IsNotUnique then
  89.         Begin
  90.             Delete(Str, I, 1);
  91.             Dec(Temp);
  92.         End
  93.         Else
  94.         Begin
  95.             LetSet := LetSet + [Str[I]];
  96.             Inc(I);
  97.         End;
  98.     End;
  99. End;
  100.  
  101. Function GetLineFromConsole(): String;
  102. var
  103.     IsCorrect: Boolean;
  104.     Str: String;
  105. begin
  106.      repeat
  107.         IsCorrect := true;
  108.         ReadLn(Str);
  109.         if Length(Str) = 0 then
  110.         begin
  111.             WriteLn('The line is empty, please, try to enter it one more time');
  112.             IsCorrect := false;
  113.         end;
  114.     until IsCorrect;
  115.     GetLineFromConsole := Str;
  116. end;
  117.  
  118. Procedure GetLineFromFile(Str: String);
  119. var IsCorrect: Boolean; TFile: TextFile;
  120. begin
  121.     repeat
  122.         IsCorrect := true;
  123.         AssignFile(TFile, InputFileLocation());
  124.         Reset(TFile);
  125.         ReadLn(TFile, Str);
  126.         CloseFile(TFile);
  127.         if (Length(Str) = 0) then
  128.         begin
  129.             WriteLn('String is empty. Enter another file location');
  130.             IsCorrect := false;
  131.         end;
  132.     until IsCorrect;
  133. end;
  134.  
  135. Procedure Main();
  136. Var
  137.     ChosenInput: Integer;
  138.     Str: String;
  139.     Size, I : Integer;
  140.     isNotUnique : Boolean;
  141.     LetSet : TChar;
  142. Begin
  143.     WriteLn('Программа удаляет одинаковые буквы в предложении.');
  144.     ChosenInput := ChooseInput();
  145.     If (ChosenInput = 0) then
  146.         GetLineFromFile(Str)
  147.     Else
  148.         Begin
  149.         writeln('Введите строку');
  150.         Str := GetLineFromConsole();
  151.         End;
  152.     ChangeSentence(Str);
  153.     writeln('Modified string');
  154.     writeln(Str);
  155.     WriteLn;
  156.     OutputToFile(Str);
  157.     ReadLn;
  158. End;
  159.  
  160. Begin
  161.     Main();
  162. End.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement