Advertisement
LisunovaMaryna

lab3.2 delphi

Nov 25th, 2023
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 12.98 KB | None | 0 0
  1. Unit MySet;
  2.  
  3. Interface
  4.  
  5. Uses
  6.     Classes, Sysutils;
  7.  
  8. Type
  9.     TSetChar = Set Of Char;
  10.     TSetByte = Set Of Byte;
  11.  
  12. Type
  13.     TMySet = Class
  14.     Private
  15.         SymbolsPrivate: TSetChar;
  16.         NumbersPrivate: TSetByte;
  17.         Function IsDigit(Symbol: Char): Boolean;
  18.     Public
  19.         Constructor Create;
  20.         Procedure AddSymbol(Symbol: Char);
  21.         Procedure AddNumber(Number: Byte);
  22.  
  23.         Property Symbols: TSetChar Read SymbolsPrivate Write SymbolsPrivate;
  24.         Property Numbers: TSetByte Read NumbersPrivate Write NumbersPrivate;
  25.  
  26.         Procedure PrintSet(Caption: String);
  27.         Function ToString(): String;
  28.         Function Union(SetB: TMySet): TMySet;
  29.         Function IsEmpty(): Boolean;
  30.         Function GetLength(): Integer;
  31.  
  32.     End;
  33.  
  34. Implementation
  35.  
  36. Constructor TMySet.Create;
  37. Begin
  38.  
  39. End;
  40.  
  41. Function TMySet.GetLength(): Integer;
  42. Var
  43.     Index: Integer;
  44.     Count: Integer;
  45. Begin
  46.     Count := 0;
  47.     For Index := 0 To 255 Do
  48.     Begin
  49.         If Chr(Index) In SymbolsPrivate Then
  50.         Begin
  51.             Inc(Count);
  52.         End;
  53.         If Index In NumbersPrivate Then
  54.         Begin
  55.             Inc(Count);
  56.         End;
  57.     End;
  58.     GetLength := Count;
  59. End;
  60.  
  61. Function TMySet.IsEmpty(): Boolean;
  62. Begin
  63.     IsEmpty := GetLength() = 0;
  64. End;
  65.  
  66. Function TMySet.IsDigit(Symbol: Char): Boolean;
  67. Begin
  68.     IsDigit := ((Symbol > '0') And (Symbol < '9')) Or (Symbol = '0') Or
  69.       (Symbol = '9');
  70. End;
  71.  
  72. Procedure TMySet.AddSymbol(Symbol: Char);
  73. Begin
  74.     If IsDigit(Symbol) Then
  75.         AddNumber(Byte(Symbol) - Byte('0'))
  76.     Else
  77.         Include(SymbolsPrivate, AnsiChar(Symbol));
  78. End;
  79.  
  80. Procedure TMySet.AddNumber(Number: Byte);
  81. Begin
  82.     Include(NumbersPrivate, Number);
  83. End;
  84.  
  85. Procedure TMySet.PrintSet(Caption: String);
  86. Begin
  87.     Write(Caption);
  88.     Write(ToString());
  89. End;
  90.  
  91. Function TMySet.ToString(): String;
  92. Var
  93.     Index: Integer;
  94.     First: Boolean;
  95.     Line: String;
  96. Begin
  97.     First := True;
  98.     For Index := 0 To 255 Do
  99.     Begin
  100.         If Chr(Index) In SymbolsPrivate Then
  101.         Begin
  102.             If Not First Then
  103.                 Line := Line + ', ';
  104.             Line := Line + Chr(Index);
  105.             First := False;
  106.         End;
  107.         If Index In NumbersPrivate Then
  108.         Begin
  109.             If Not First Then
  110.                 Line := Line + ', ';
  111.             Line := Line + InttoStr(Index);
  112.             First := False;
  113.         End;
  114.     End;
  115.     Line := Line + #10;
  116.     ToString := Line;
  117. End;
  118.  
  119. Function TMySet.Union(SetB: TMySet): TMySet;
  120. Var
  121.     ResultSet: TMySet;
  122. Begin
  123.     ResultSet := TMySet.Create();
  124.     ResultSet.Symbols := Symbols + SetB.Symbols;
  125.     ResultSet.Numbers := Numbers + SetB.Numbers;
  126.     Union := ResultSet;
  127. End;
  128.  
  129. End.
  130.  
  131.  
  132. Program ab3_2_delphi;
  133.  
  134. {$APPTYPE CONSOLE}
  135. {$R *.res}
  136.  
  137. Uses
  138.     System.SysUtils,
  139.     MySet In 'MySet.pas';
  140.  
  141. Type
  142.     TArrOfTMySet = Array Of TMySet;
  143.  
  144. Procedure PrintCondition();
  145. Begin
  146.     Writeln('Generate the set Y={X1 X2 X3} and select from it a subset Y1 that represents all the digits included in Y that are divisible by 4.');
  147. End;
  148.  
  149. Function IsOneSymbol(Line: String): Boolean;
  150. Begin
  151.     IsOneSymbol := Line.Length = 1;
  152. End;
  153.  
  154. Function IsDigit(Symbol: Char): Boolean;
  155. Begin
  156.     IsDigit := ((Symbol > '0') And (Symbol < '9')) Or (Symbol = '0') Or
  157.       (Symbol = '9');
  158. End;
  159.  
  160. Function IsNumber(Line: String): Boolean;
  161. Var
  162.     Index: Integer;
  163.     AllDigits: Boolean;
  164. Begin
  165.     If Line.Length = 0 Then
  166.         IsNumber := False
  167.     Else
  168.     Begin
  169.         AllDigits := True;
  170.         Index := 1;
  171.         While (Index < Line.Length + 1) And (AllDigits) Do
  172.         Begin
  173.             If Not IsDigit(Line[Index]) Then
  174.                 AllDigits := False;
  175.             Inc(Index);
  176.         End;
  177.         IsNumber := AllDigits;
  178.     End;
  179. End;
  180.  
  181. Function TryAddElement(DataSet: TMySet; Line: String): Boolean;
  182. Var
  183.     Number: Integer;
  184.     IsCorrect: Boolean;
  185. Begin
  186.     IsCorrect := True;
  187.     If IsOneSymbol(Line) Then
  188.         DataSet.AddSymbol(Line[1])
  189.     Else If IsNumber(Line) Then
  190.     Begin
  191.         Try
  192.             Number := Strtoint(Line);
  193.             If Number > 255 Then
  194.             Begin
  195.                 Writeln('Max allowed number is 255.');
  196.                 IsCorrect := False;
  197.             End
  198.             Else
  199.                 DataSet.AddNumber(Number);
  200.         Except
  201.             Writeln('Failed to parse number.');
  202.             IsCorrect := False;
  203.         End;
  204.     End
  205.     Else
  206.     Begin
  207.         Writeln('Only one symbol is allowed.');
  208.         IsCorrect := False;
  209.     End;
  210.  
  211.     TryAddElement := IsCorrect;
  212. End;
  213.  
  214. Function CheckFileAccess(Path: String): Boolean;
  215. Var
  216.     MyFile: TextFile;
  217. Begin
  218.     Try
  219.         AssignFile(MyFile, Path);
  220.         Reset(MyFile);
  221.         CheckFileAccess := True;
  222.     Except
  223.         CheckFileAccess := False;
  224.         Writeln('Cannot open file.');
  225.     End;
  226.     CloseFile(MyFile);
  227. End;
  228.  
  229. Function InputFilePath(CheckExistence: Boolean): String;
  230. Var
  231.     IsCorrect: Boolean;
  232.     Path: String;
  233. Begin
  234.     Writeln('Input file path (*.txt): ');
  235.     Repeat
  236.         IsCorrect := True;
  237.         Try
  238.             Readln(Path);
  239.             If Not(ExtractFileExt(Path) = '.txt') Then
  240.             Begin
  241.                 IsCorrect := False;
  242.                 Writeln('Incorrect file extension. Repeat: ');
  243.             End
  244.             Else If CheckExistence And Not FileExists(Path) Then
  245.             Begin
  246.                 IsCorrect := False;
  247.                 Writeln('File is not exist. Repeat: ');
  248.             End;
  249.  
  250.             If CheckExistence And IsCorrect Then
  251.                 IsCorrect := CheckFileAccess(Path);
  252.         Except
  253.             IsCorrect := False;
  254.             Writeln('Something wrong happened. Repeat:');
  255.         End;
  256.  
  257.     Until IsCorrect;
  258.     InputFilePath := Path;
  259. End;
  260.  
  261. Function IsDivider(Symbol: Char): Boolean;
  262. Begin
  263.     If (Symbol = ' ') Or (Symbol = #9) Or (Symbol = #10) Or (Symbol = #13) Then
  264.         IsDivider := True
  265.     Else
  266.         IsDivider := False;
  267. End;
  268.  
  269. Function ReadWord(Text: String; CurrentIndex: Integer): String;
  270. Var
  271.     IsCompleted: Boolean;
  272.     Word: String;
  273.     Symbol: Char;
  274. Begin
  275.     IsCompleted := False;
  276.     While (Not IsCompleted And (CurrentIndex < Length(Text) + 1)) Do
  277.     Begin
  278.         Symbol := Text[CurrentIndex];
  279.         If Not IsDivider(Symbol) Then
  280.         Begin
  281.             Word := Word + Symbol;
  282.             Inc(CurrentIndex);
  283.         End
  284.         Else
  285.             IsCompleted := True;
  286.     End;
  287.  
  288.     ReadWord := Word;
  289. End;
  290.  
  291. Function ReadDividerBlock(Text: String; CurrentIndex: Integer): String;
  292. Var
  293.     IsCompleted: Boolean;
  294.     DividerBlock: String;
  295.     Symbol: Char;
  296. Begin
  297.     IsCompleted := False;
  298.     While (Not IsCompleted And (CurrentIndex < Length(Text) + 1)) Do
  299.     Begin
  300.         Symbol := Text[CurrentIndex];
  301.         If IsDivider(Symbol) Then
  302.         Begin
  303.             DividerBlock := DividerBlock + Symbol;
  304.             Inc(CurrentIndex);
  305.         End
  306.         Else
  307.             IsCompleted := True;
  308.     End;
  309.  
  310.     ReadDividerBlock := DividerBlock;
  311. End;
  312.  
  313. Function TryFillSet(DataSet: TMySet; Text: String): Boolean;
  314. Var
  315.     Word, DividerBlock: String;
  316.     CurrentIndex: Integer;
  317.     IsCorrect: Boolean;
  318. Begin
  319.     CurrentIndex := 1;
  320.  
  321.     IsCorrect := True;
  322.  
  323.     While (IsCorrect And (CurrentIndex < Length(Text) + 1)) Do
  324.     Begin
  325.         DividerBlock := ReadDividerBlock(Text, CurrentIndex);
  326.  
  327.         CurrentIndex := CurrentIndex + Length(DividerBlock);
  328.  
  329.         Word := ReadWord(Text, CurrentIndex);
  330.  
  331.         IsCorrect := TryAddElement(DataSet, Word);
  332.         CurrentIndex := CurrentIndex + Length(Word);
  333.  
  334.     End;
  335.  
  336.     TryFillSet := IsCorrect;
  337. End;
  338.  
  339. Function ReadSet(Var MyFile: TextFile; Var IsCorrect: Boolean): TMySet;
  340. Var
  341.     DataSet: TMySet;
  342.     Line: String;
  343. Begin
  344.     IsCorrect := True;
  345.     DataSet := TMySet.Create();
  346.     Try
  347.         If Eof(MyFile) Then
  348.         Begin
  349.             IsCorrect := False;
  350.             Write('File lack data for the specified set.');
  351.         End
  352.         Else
  353.         Begin
  354.             Readln(MyFile, Line);
  355.             IsCorrect := TryFillSet(DataSet, Line);
  356.         End;
  357.     Except
  358.         IsCorrect := False;
  359.         Writeln('File read error.');
  360.     End;
  361.     ReadSet := DataSet;
  362. End;
  363.  
  364. Function TryFillSourceSetsFromFile(Var Sets: TArrOfTMySet): Boolean;
  365. Var
  366.     Path: String;
  367.     MyFile: TextFile;
  368.     IsCorrect: Boolean;
  369.     Index: Integer;
  370. Begin
  371.     Path := InputFilePath(True);
  372.     IsCorrect := True;
  373.     Try
  374.         AssignFile(MyFile, Path);
  375.         Reset(MyFile);
  376.  
  377.         If IsCorrect Then
  378.             For Index := 0 To High(Sets) Do
  379.  
  380.             Begin
  381.                 Sets[Index] := ReadSet(MyFile, IsCorrect);
  382.             End;
  383.         If Not Eof(MyFile) Then
  384.         Begin
  385.             IsCorrect := False;
  386.             Writeln('Incorrect file format (too much data).');
  387.         End
  388.     Except
  389.         Writeln('Cannot read file.');
  390.     End;
  391.     TryFillSourceSetsFromFile := IsCorrect;
  392. End;
  393.  
  394. Function GetSourceSetsFromFile(SetsCount: Integer): TArrOfTMySet;
  395. Var
  396.     Sets: TArrOfTMySet;
  397.     IsCorrect: Boolean;
  398.     Path: String;
  399. Begin
  400.     SetLength(Sets, SetsCount);
  401.     Repeat
  402.         IsCorrect := TryFillSourceSetsFromFile(Sets);
  403.     Until IsCorrect;
  404.     GetSourceSetsFromFile := Sets;
  405. End;
  406.  
  407. Function InputSet(SetName: String): TMySet;
  408. Var
  409.     DataSet: TMySet;
  410.     Line: String;
  411. Begin
  412.     DataSet := TMySet.Create();
  413.     Writeln('Input ', SetName,
  414.       ' of symbols or numbers up to 255 (add empty line to finish): ');
  415.     Repeat
  416.         Readln(Line);
  417.         If Line.Length > 0 Then
  418.             TryAddElement(DataSet, Line)
  419.         Else If DataSet.IsEmpty Then
  420.             Writeln('Input at least one symbol.');
  421.     Until (Line.Length = 0) And (Not DataSet.IsEmpty());
  422.     InputSet := DataSet;
  423. End;
  424.  
  425. Function ReadCondition(): Char;
  426. Var
  427.     Condition: String;
  428.     IsCorrect: Boolean;
  429. Begin
  430.     Writeln('Would you like to enter sets manually [m] or get from file [f]?');
  431.     Repeat
  432.         IsCorrect := True;
  433.         Readln(Condition);
  434.         If (Condition <> 'm') And (Condition <> 'f') Then
  435.         Begin
  436.             IsCorrect := False;
  437.             Write('Please type [m] or [f]: ');
  438.         End;
  439.     Until IsCorrect;
  440.     ReadCondition := Condition[1];
  441. End;
  442.  
  443. Function InputSourceSetsManually(SetsCount: Integer): TArrOfTMySet;
  444. Var
  445.     Sets: TArrOfTMySet;
  446.     Index: Integer;
  447. Begin
  448.     SetLength(Sets, SetsCount);
  449.  
  450.     For Index := 0 To SetsCount - 1 Do
  451.         Sets[Index] := InputSet('SetX' + Inttostr(Index + 1));
  452.     InputSourceSetsManually := Sets;
  453. End;
  454.  
  455. Function GetSourceSets(SetsCount: Integer): TArrOfTMySet;
  456. Var
  457.     Condition: Char;
  458. Begin
  459.     Condition := ReadCondition();
  460.     Case Condition Of
  461.         'm':
  462.             GetSourceSets := InputSourceSetsManually(SetsCount);
  463.         'f':
  464.             GetSourceSets := GetSourceSetsFromFile(SetsCount);
  465.     End;
  466. End;
  467.  
  468. Function ExtractMultiple4(DataSet: TMySet): TMySet;
  469. Var
  470.     ResultSet: TMySet;
  471.     Index: Integer;
  472. Begin
  473.     ResultSet := TMySet.Create();
  474.     For Index := 1 To 255 Do
  475.     Begin
  476.         If (Index In DataSet.Numbers) And (Index Mod 4 = 0) Then
  477.         Begin
  478.             ResultSet.AddNumber(Index);
  479.         End;
  480.     End;
  481.     ExtractMultiple4 := ResultSet;
  482. End;
  483.  
  484. Function TrySaveSets(Path: String; Sets: TArrOfTMySet;
  485.   SetY, SetY1: TMySet): Boolean;
  486. Var
  487.     MyFile: TextFile;
  488.     Index: Integer;
  489. Begin
  490.     AssignFile(MyFile, Path);
  491.     Try
  492.         Rewrite(MyFile);
  493.         For Index := 0 To 2 Do
  494.         Begin
  495.             Write(MyFile, 'SetX' + Inttostr(Index + 1) + ': ');
  496.             Write(MyFile, Sets[Index].ToString());
  497.         End;
  498.  
  499.         Write(MyFile, 'SetY: ');
  500.         Write(MyFile, SetY.ToString());
  501.  
  502.         Write(MyFile, 'SetY1: ');
  503.         Write(MyFile, SetY1.ToString());
  504.  
  505.         TrySaveSets := True;
  506.         Writeln('File saved successfully!');
  507.         CloseFile(MyFile);
  508.     Except
  509.         Writeln('Unable to write file.');
  510.         TrySaveSets := False;
  511.     End;
  512. End;
  513.  
  514. Procedure SaveSets(Sets: TArrOfTMySet; SetY, SetY1: TMySet);
  515. Var
  516.     Path: String;
  517.     IsCorrect: Boolean;
  518. Begin
  519.     Repeat
  520.         IsCorrect := True;
  521.         Path := InputFilePath(False);
  522.         IsCorrect := TrySaveSets(Path, Sets, SetY, SetY1);
  523.     Until IsCorrect;
  524. End;
  525.  
  526. Procedure PrintCompleted();
  527. Begin
  528.     Write('Completed!');
  529. End;
  530.  
  531. Procedure Run();
  532. Var
  533.     Text: String;
  534.     SetY: TMySet;
  535.     SetY1: TMySet;
  536.     IsCorrect: Boolean;
  537.     Sets: TArrOfTMySet;
  538.     Index, SetsCount: Integer;
  539. Begin
  540.     PrintCondition();
  541.     SetsCount := 3;
  542.     Sets := GetSourceSets(SetsCount);
  543.     For Index := 0 To High(Sets) Do
  544.         Sets[Index].PrintSet('SetX' + Inttostr(Index + 1) + ': ');
  545.  
  546.     SetY := TMySet.Create();
  547.     For Index := 0 To High(Sets) Do
  548.         SetY := SetY.Union(Sets[Index]);
  549.  
  550.     SetY.PrintSet('SetY: ');
  551.  
  552.     SetY1 := ExtractMultiple4(SetY);
  553.     SetY1.PrintSet('SetY1: ');
  554.  
  555.     SaveSets(Sets, SetY, SetY1);
  556.     PrintCompleted();
  557. End;
  558.  
  559. Begin
  560.     Run();
  561.     Readln;
  562.  
  563. End.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement