Advertisement
Vladislav8653

laba_3_2_delphi

Dec 7th, 2022
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 4.36 KB | None | 0 0
  1. Program Project4;
  2.  
  3. {$APPTYPE CONSOLE}
  4.  
  5. Uses
  6.     System.SysUtils;
  7.  
  8. Type
  9.     TSet = Set Of Char;
  10.  
  11. Function Choose() : Boolean;
  12. Var
  13.     InputNumber : Integer;
  14.     IsCorrect : Boolean;
  15. Const
  16.     MIN_NUM = 0;
  17.     MAX_NUM = 1;
  18. Begin
  19.     InputNumber := 0;
  20.     Repeat
  21.         IsCorrect := True;
  22.         Try
  23.             Readln(InputNumber)
  24.         Except
  25.             Writeln('Please, enter a integer number:');
  26.             IsCorrect := False;
  27.         End;
  28.         If ((InputNumber < MIN_NUM) or (InputNumber > MAX_NUM)) then
  29.         Begin
  30.             Writeln('You are out of input range!');
  31.             IsCorrect := False;
  32.         End;
  33.  
  34.     Until IsCorrect;
  35.     If (InputNumber = 1) then
  36.         Choose := True
  37.     Else
  38.         Choose := False;
  39. End;
  40.  
  41. Function InputFilePath() : String;
  42. Var
  43.     IsCorrect : Boolean;
  44.     Path : String;
  45. Begin
  46.     Writeln('Input path to file: ');
  47.     Repeat
  48.         IsCorrect := True;
  49.         Readln(Path);
  50.         If(Not FileExists(Path)) Then
  51.         Begin
  52.             IsCorrect := False;
  53.             Writeln('Wrong way to file. Input correct path.');
  54.         End
  55.         Else If (ExtractFileExt(Path) <> '.txt') Then
  56.         Begin
  57.             Writeln('Must have .txt');
  58.             IsCorrect := False;
  59.         End;
  60.     Until IsCorrect;
  61.     InputFilePath := Path;
  62. End;
  63.  
  64. Function InputSequenceFromFile(Path : String): AnsiString;
  65.  
  66. Var
  67.     Sequence: AnsiString;
  68.     InputFile: TextFile;
  69.     IsCorrect : Boolean;
  70. Begin
  71.     AssignFile(InputFile, Path);
  72.     Reset(InputFile);
  73.     Repeat
  74.         IsCorrect := True;
  75.         Try
  76.             Readln(InputFile, Sequence);
  77.         Except
  78.             IsCorrect := False;
  79.             Writeln('Mistake of reading string from file.');
  80.         End;
  81.     Until IsCorrect;
  82.     CloseFile(InputFile);
  83.     InputSequenceFromFile := Sequence;
  84. End;
  85.  
  86. Function InputSequence(): AnsiString;
  87. Var
  88.     Sequence: AnsiString;
  89. Begin
  90.     Writeln('Enter Sequence');
  91.     Readln(Sequence);
  92.     InputSequence := Sequence;
  93. End;
  94.  
  95. Function MakeSet(Sequence: AnsiString): TSet;
  96. Var
  97.     I: Integer;
  98.     SetOfSymbols: TSet;
  99. Const
  100.     Symbols = ['/', '*', '-', '+', '%', '0' .. '9'];
  101. Begin
  102.     SetOfSymbols := [];
  103.     For I := 0 To Length(Sequence) Do
  104.         If Sequence[I] In Symbols Then
  105.             Include(SetOfSymbols, Sequence[I]);
  106.     MakeSet := SetOfSymbols;
  107. End;
  108.  
  109. Procedure ConsoleOutput(SetOfSymbols: TSet);
  110. Var
  111.     I: Integer;
  112. Begin
  113.     If SetOfSymbols = [] Then
  114.         Write('You have not entered any signs of arithmetic operations or digits.')
  115.     Else
  116.     Begin
  117.         Writeln('The resulting set');
  118.         For I := 0 To 255 Do
  119.             If chr(I) In SetOfSymbols Then
  120.                 Write(Chr(I), ' ');
  121.     End;
  122. End;
  123.  
  124. Procedure FileOutput(SetOfSymbols: TSet);
  125. Var
  126.     IsCorrect: Boolean;
  127.     OutputFile: TextFile;
  128.     Path: String;
  129.     I: Integer;
  130. Begin
  131.     Path := InputFilePath();
  132.     AssignFile(OutputFile, Path);
  133.     Repeat
  134.         IsCorrect := True;
  135.         Try
  136.             Rewrite(OutputFile);
  137.         Except
  138.             IsCorrect := False;
  139.             Writeln('Mistake with writing in file. Input another path.');
  140.             Path := InputFilePath();
  141.         End;
  142.     Until IsCorrect;
  143.     If SetOfSymbols = [] Then
  144.         Write(OutputFile, 'You have not entered any signs of arithmetic operations or digits.')
  145.     Else
  146.     Begin
  147.         Writeln(OutputFile, 'The resulting set');
  148.         For I := 0 To 255 Do
  149.             If chr(I) In SetOfSymbols Then
  150.                 Write(OutputFile, Chr(I), ' ');
  151.     End;
  152.     CloseFile(OutputFile);
  153.     Write('Output in file was successful.');
  154. End;
  155.  
  156. Var
  157.     SetOfSymbols : TSet;
  158.     Sequence: AnsiString;
  159.     Path: String;
  160.     Chose : Boolean;
  161. Begin
  162.     Writeln('The program builds and prints a set, the elements of which are the signs of arithmetic operations and numbers occurring in the sequence.');
  163.     Writeln('Type 1 - console input, type 0 - file input.');
  164.     Chose := Choose();
  165.     If (Chose) Then
  166.         Sequence := InputSequence
  167.     Else
  168.     Begin
  169.         Path := InputFilePath();
  170.         Sequence := InputSequenceFromFile(Path);
  171.     End;
  172.     SetOfSymbols := MakeSet(Sequence);
  173.     Writeln('Type 1 - console output, type 0 - file output.');
  174.     Chose := Choose();
  175.     If (Chose) Then
  176.         ConsoleOutput(SetOfSymbols)
  177.     Else
  178.         FileOutput(SetOfSymbols);
  179.     Readln;
  180. End.
  181.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement