Advertisement
lithie_oce

lab 3.2 delphi

Dec 2nd, 2023
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 4.88 KB | Source Code | 0 0
  1. Program Lab3task2;
  2.  
  3. {$APPTYPE CONSOLE}
  4.  
  5. Uses
  6.   SysUtils;
  7. Type
  8.     TSet = Set Of Char;
  9. Function ChoiceCheck() : Integer;
  10. Var
  11.     Choice: Integer;
  12.     IsCorrect: Boolean;
  13. Begin
  14.     Repeat
  15.         IsCorrect:=True;
  16.         Try
  17.             Readln(Choice);
  18.         Except
  19.             Writeln('Error! Input a number');
  20.             IsCorrect := False;
  21.         End;
  22.         If (IsCorrect And ((Choice < 1) Or (Choice > 2))) Then
  23.         Begin
  24.             Writeln('Error! Input 1 or 2');
  25.             IsCorrect := False;
  26.         End;
  27.     Until  (IsCorrect);
  28.     ChoiceCheck := Choice;
  29. End;
  30.  
  31. Function InputCheck(): String;
  32. Var
  33.     Str: String;
  34. Begin
  35.         Writeln ('Input a string');
  36.         Readln(Str);
  37.     InputCheck := Str;
  38. End;
  39.  
  40. Function CheckInputFilePath(): String;
  41. Var
  42.     Path: String;
  43.     IsCorrect: Boolean;
  44.     InputFile: File;
  45. Begin
  46.     Repeat
  47.         Writeln('Input path to the file');
  48.         IsCorrect := True;
  49.         Readln(Path);
  50.         If (Not(FileExists(Path))) Then
  51.         Begin
  52.             IsCorrect := False;
  53.             Writeln('Could not find the file');
  54.         End;
  55.         If (IsCorrect) Then
  56.         Begin
  57.             Try
  58.                 AssignFile(InputFile, Path);
  59.                 ReSet(InputFile);
  60.             Except
  61.                 IsCorrect := False;
  62.                 Writeln('Could not open the file');
  63.             End;
  64.         End;
  65.     Until (IsCorrect);
  66.     CloseFile(InputFile);
  67.     CheckInputFilePath := Path;
  68. End;
  69.  
  70. Function FileCheckString() : String;
  71. Var
  72.     InputFile: TextFile;
  73.     Path, Str: String;
  74.     IsCorrect: Boolean;
  75. Begin
  76.     Repeat
  77.         Path := CheckInputFilePath();
  78.         IsCorrect := True;
  79.         AssignFile(InputFile, Path);
  80.         Reset(Inputfile);
  81.         Read(InputFile, Str);
  82.         If (IsCorrect And (Length(Str) < 1)) Then
  83.         Begin
  84.             IsCorrect := False;
  85.             Writeln ('The string is empty');
  86.         End;
  87.         If ((IsCorrect) And Not(EoF(InputFile))) Then
  88.         Begin
  89.             IsCorrect := False;
  90.             Writeln ('There should be only one string in the file');
  91.         End;
  92.     Until (IsCorrect);
  93.     FileCheckString := Str;
  94. End;
  95.  
  96. Function InputChoice (): String;
  97. Var
  98.     Str: String;
  99.     Choice: Integer;
  100. Begin
  101.     Writeln('Choose input option: ', #10, '1.Input from console', #10,
  102.             '2.Input from file');
  103.     Choice := ChoiceCheck();
  104.     If (Choice = 1)  Then
  105.     Str := InputCheck()
  106.     Else
  107.     Str:= FileCheckString();
  108.     InputChoice := Str;
  109. End;
  110.  
  111. Function CheckOutputFilePath(): String;
  112. Var
  113.     Path: String;
  114.     IsCorrect: Boolean;
  115.     OutputFile: File;
  116. Begin
  117.     Writeln('Input file path and the name of the file for', #10, 'example, C:\Projects\Number\FileName.txt. If the ', #10, 'file does not exist, then it will be created', #10,'automatically in the root folder of the program');
  118.     IsCorrect := True;
  119.     Readln(Path);
  120.     If (Not(FileExists(Path))) Then
  121.     Begin
  122.         IsCorrect := False;
  123.         Writeln('Could not find the file');
  124.     End;
  125.     If (IsCorrect)Then
  126.     Try
  127.         AssignFile(OutputFile, Path);
  128.         ReSet(OutputFile);
  129.     Except
  130.         IsCorrect := False;
  131.         Writeln('Could not open the file');
  132.     End;
  133.     If (Not(IsCorrect)) Then
  134.     Begin
  135.     Writeln ('File will be created in the root folder of the program');
  136.     Path := 'Result.txt';
  137.     End
  138.     Else
  139.     CloseFile(OutputFile);
  140.     CheckOutputFilePath := Path;
  141. End;
  142.  
  143. Function CreateSet(S: String; Set0: TSet): TSet;
  144. Var
  145.     Set1: Set of Char;
  146.     I: Integer;
  147. Begin
  148.     Set1 := [];
  149.     For I := 1 To Length(S) Do
  150.         If (S[I] In Set0) Then
  151.         Begin
  152.             Include (Set1, S[I]);
  153.             Exclude (Set0, S[I]);
  154.         End;
  155.     CreateSet := Set1;
  156. End;
  157.  
  158. Procedure OutputConsoleSet(Set1: TSet);
  159. Var
  160.     I: Integer;
  161. Begin
  162.     Writeln ('Here is the created set:');
  163.     For  I:= 32 To 255 Do
  164.         If (Chr(I) In Set1) Then
  165.             Writeln(Chr(I));
  166. End;
  167.  
  168. Procedure OutputFileSet(Set1: TSet);
  169. Var
  170.     I: Integer;
  171.     OutputFile: TextFile;
  172.     Path: String;
  173. Begin
  174.     Path:=CheckOutputFilePath();
  175.     AssignFile (OutputFile, Path);
  176.     Rewrite(OutputFile);
  177.     For  I:= 32 To 255 Do
  178.         If (Chr(I) In Set1) Then
  179.             Writeln(OutputFile, Chr(I));
  180.             Close(OutputFile);
  181.     Writeln('Successful output');
  182. End;
  183.  
  184. Procedure OutputChoice (Set1: TSet);
  185. Var
  186.     Choice: Integer;
  187. Begin
  188.     Writeln('Choose output option: ', #10, '1.Output through console', #10,
  189.               '2.Output through file');
  190.     Choice := ChoiceCheck();
  191.     If (Choice = 1) Then
  192.     OutputConsoleSet(Set1)
  193.     Else
  194.     OutputFileSet(Set1);
  195. End;
  196.  
  197. Var
  198.     Set0, Set1: Set Of Char;
  199.     S: String;
  200. Begin
  201.     Set0 := ['+', '-', '*', '/', '2', '4', '6', '8', '0', '(', ')', '[', ']', '{', '}'];
  202.     S := InputChoice();
  203.     Set1 := CreateSet(S, Set0);
  204.     OutputChoice(Set1);
  205.     Readln
  206. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement