Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Unit MySet;
- Interface
- Uses
- Classes, Sysutils;
- Type
- TSetChar = Set Of Char;
- TSetByte = Set Of Byte;
- Type
- TMySet = Class
- Private
- SymbolsPrivate: TSetChar;
- NumbersPrivate: TSetByte;
- Function IsDigit(Symbol: Char): Boolean;
- Public
- Constructor Create;
- Procedure AddSymbol(Symbol: Char);
- Procedure AddNumber(Number: Byte);
- Property Symbols: TSetChar Read SymbolsPrivate Write SymbolsPrivate;
- Property Numbers: TSetByte Read NumbersPrivate Write NumbersPrivate;
- Procedure PrintSet(Caption: String);
- Function ToString(): String;
- Function Union(SetB: TMySet): TMySet;
- Function IsEmpty(): Boolean;
- Function GetLength(): Integer;
- End;
- Implementation
- Constructor TMySet.Create;
- Begin
- End;
- Function TMySet.GetLength(): Integer;
- Var
- Index: Integer;
- Count: Integer;
- Begin
- Count := 0;
- For Index := 0 To 255 Do
- Begin
- If Chr(Index) In SymbolsPrivate Then
- Begin
- Inc(Count);
- End;
- If Index In NumbersPrivate Then
- Begin
- Inc(Count);
- End;
- End;
- GetLength := Count;
- End;
- Function TMySet.IsEmpty(): Boolean;
- Begin
- IsEmpty := GetLength() = 0;
- End;
- Function TMySet.IsDigit(Symbol: Char): Boolean;
- Begin
- IsDigit := ((Symbol > '0') And (Symbol < '9')) Or (Symbol = '0') Or
- (Symbol = '9');
- End;
- Procedure TMySet.AddSymbol(Symbol: Char);
- Begin
- If IsDigit(Symbol) Then
- AddNumber(Byte(Symbol) - Byte('0'))
- Else
- Include(SymbolsPrivate, AnsiChar(Symbol));
- End;
- Procedure TMySet.AddNumber(Number: Byte);
- Begin
- Include(NumbersPrivate, Number);
- End;
- Procedure TMySet.PrintSet(Caption: String);
- Begin
- Write(Caption);
- Write(ToString());
- End;
- Function TMySet.ToString(): String;
- Var
- Index: Integer;
- First: Boolean;
- Line: String;
- Begin
- First := True;
- For Index := 0 To 255 Do
- Begin
- If Chr(Index) In SymbolsPrivate Then
- Begin
- If Not First Then
- Line := Line + ', ';
- Line := Line + Chr(Index);
- First := False;
- End;
- If Index In NumbersPrivate Then
- Begin
- If Not First Then
- Line := Line + ', ';
- Line := Line + InttoStr(Index);
- First := False;
- End;
- End;
- Line := Line + #10;
- ToString := Line;
- End;
- Function TMySet.Union(SetB: TMySet): TMySet;
- Var
- ResultSet: TMySet;
- Begin
- ResultSet := TMySet.Create();
- ResultSet.Symbols := Symbols + SetB.Symbols;
- ResultSet.Numbers := Numbers + SetB.Numbers;
- Union := ResultSet;
- End;
- End.
- Program ab3_2_delphi;
- {$APPTYPE CONSOLE}
- {$R *.res}
- Uses
- System.SysUtils,
- MySet In 'MySet.pas';
- Type
- TArrOfTMySet = Array Of TMySet;
- Procedure PrintCondition();
- Begin
- 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.');
- End;
- Function IsOneSymbol(Line: String): Boolean;
- Begin
- IsOneSymbol := Line.Length = 1;
- End;
- Function IsDigit(Symbol: Char): Boolean;
- Begin
- IsDigit := ((Symbol > '0') And (Symbol < '9')) Or (Symbol = '0') Or
- (Symbol = '9');
- End;
- Function IsNumber(Line: String): Boolean;
- Var
- Index: Integer;
- AllDigits: Boolean;
- Begin
- If Line.Length = 0 Then
- IsNumber := False
- Else
- Begin
- AllDigits := True;
- Index := 1;
- While (Index < Line.Length + 1) And (AllDigits) Do
- Begin
- If Not IsDigit(Line[Index]) Then
- AllDigits := False;
- Inc(Index);
- End;
- IsNumber := AllDigits;
- End;
- End;
- Function TryAddElement(DataSet: TMySet; Line: String): Boolean;
- Var
- Number: Integer;
- IsCorrect: Boolean;
- Begin
- IsCorrect := True;
- If IsOneSymbol(Line) Then
- DataSet.AddSymbol(Line[1])
- Else If IsNumber(Line) Then
- Begin
- Try
- Number := Strtoint(Line);
- If Number > 255 Then
- Begin
- Writeln('Max allowed number is 255.');
- IsCorrect := False;
- End
- Else
- DataSet.AddNumber(Number);
- Except
- Writeln('Failed to parse number.');
- IsCorrect := False;
- End;
- End
- Else
- Begin
- Writeln('Only one symbol is allowed.');
- IsCorrect := False;
- End;
- TryAddElement := IsCorrect;
- End;
- Function CheckFileAccess(Path: String): Boolean;
- Var
- MyFile: TextFile;
- Begin
- Try
- AssignFile(MyFile, Path);
- Reset(MyFile);
- CheckFileAccess := True;
- Except
- CheckFileAccess := False;
- Writeln('Cannot open file.');
- End;
- CloseFile(MyFile);
- End;
- Function InputFilePath(CheckExistence: Boolean): String;
- Var
- IsCorrect: Boolean;
- Path: String;
- Begin
- Writeln('Input file path (*.txt): ');
- Repeat
- IsCorrect := True;
- Try
- Readln(Path);
- If Not(ExtractFileExt(Path) = '.txt') Then
- Begin
- IsCorrect := False;
- Writeln('Incorrect file extension. Repeat: ');
- End
- Else If CheckExistence And Not FileExists(Path) Then
- Begin
- IsCorrect := False;
- Writeln('File is not exist. Repeat: ');
- End;
- If CheckExistence And IsCorrect Then
- IsCorrect := CheckFileAccess(Path);
- Except
- IsCorrect := False;
- Writeln('Something wrong happened. Repeat:');
- End;
- Until IsCorrect;
- InputFilePath := Path;
- End;
- Function IsDivider(Symbol: Char): Boolean;
- Begin
- If (Symbol = ' ') Or (Symbol = #9) Or (Symbol = #10) Or (Symbol = #13) Then
- IsDivider := True
- Else
- IsDivider := False;
- End;
- Function ReadWord(Text: String; CurrentIndex: Integer): String;
- Var
- IsCompleted: Boolean;
- Word: String;
- Symbol: Char;
- Begin
- IsCompleted := False;
- While (Not IsCompleted And (CurrentIndex < Length(Text) + 1)) Do
- Begin
- Symbol := Text[CurrentIndex];
- If Not IsDivider(Symbol) Then
- Begin
- Word := Word + Symbol;
- Inc(CurrentIndex);
- End
- Else
- IsCompleted := True;
- End;
- ReadWord := Word;
- End;
- Function ReadDividerBlock(Text: String; CurrentIndex: Integer): String;
- Var
- IsCompleted: Boolean;
- DividerBlock: String;
- Symbol: Char;
- Begin
- IsCompleted := False;
- While (Not IsCompleted And (CurrentIndex < Length(Text) + 1)) Do
- Begin
- Symbol := Text[CurrentIndex];
- If IsDivider(Symbol) Then
- Begin
- DividerBlock := DividerBlock + Symbol;
- Inc(CurrentIndex);
- End
- Else
- IsCompleted := True;
- End;
- ReadDividerBlock := DividerBlock;
- End;
- Function TryFillSet(DataSet: TMySet; Text: String): Boolean;
- Var
- Word, DividerBlock: String;
- CurrentIndex: Integer;
- IsCorrect: Boolean;
- Begin
- CurrentIndex := 1;
- IsCorrect := True;
- While (IsCorrect And (CurrentIndex < Length(Text) + 1)) Do
- Begin
- DividerBlock := ReadDividerBlock(Text, CurrentIndex);
- CurrentIndex := CurrentIndex + Length(DividerBlock);
- Word := ReadWord(Text, CurrentIndex);
- IsCorrect := TryAddElement(DataSet, Word);
- CurrentIndex := CurrentIndex + Length(Word);
- End;
- TryFillSet := IsCorrect;
- End;
- Function ReadSet(Var MyFile: TextFile; Var IsCorrect: Boolean): TMySet;
- Var
- DataSet: TMySet;
- Line: String;
- Begin
- IsCorrect := True;
- DataSet := TMySet.Create();
- Try
- If Eof(MyFile) Then
- Begin
- IsCorrect := False;
- Write('File lack data for the specified set.');
- End
- Else
- Begin
- Readln(MyFile, Line);
- IsCorrect := TryFillSet(DataSet, Line);
- End;
- Except
- IsCorrect := False;
- Writeln('File read error.');
- End;
- ReadSet := DataSet;
- End;
- Function TryFillSourceSetsFromFile(Var Sets: TArrOfTMySet): Boolean;
- Var
- Path: String;
- MyFile: TextFile;
- IsCorrect: Boolean;
- Index: Integer;
- Begin
- Path := InputFilePath(True);
- IsCorrect := True;
- Try
- AssignFile(MyFile, Path);
- Reset(MyFile);
- If IsCorrect Then
- For Index := 0 To High(Sets) Do
- Begin
- Sets[Index] := ReadSet(MyFile, IsCorrect);
- End;
- If Not Eof(MyFile) Then
- Begin
- IsCorrect := False;
- Writeln('Incorrect file format (too much data).');
- End
- Except
- Writeln('Cannot read file.');
- End;
- TryFillSourceSetsFromFile := IsCorrect;
- End;
- Function GetSourceSetsFromFile(SetsCount: Integer): TArrOfTMySet;
- Var
- Sets: TArrOfTMySet;
- IsCorrect: Boolean;
- Path: String;
- Begin
- SetLength(Sets, SetsCount);
- Repeat
- IsCorrect := TryFillSourceSetsFromFile(Sets);
- Until IsCorrect;
- GetSourceSetsFromFile := Sets;
- End;
- Function InputSet(SetName: String): TMySet;
- Var
- DataSet: TMySet;
- Line: String;
- Begin
- DataSet := TMySet.Create();
- Writeln('Input ', SetName,
- ' of symbols or numbers up to 255 (add empty line to finish): ');
- Repeat
- Readln(Line);
- If Line.Length > 0 Then
- TryAddElement(DataSet, Line)
- Else If DataSet.IsEmpty Then
- Writeln('Input at least one symbol.');
- Until (Line.Length = 0) And (Not DataSet.IsEmpty());
- InputSet := DataSet;
- End;
- Function ReadCondition(): Char;
- Var
- Condition: String;
- IsCorrect: Boolean;
- Begin
- Writeln('Would you like to enter sets manually [m] or get from file [f]?');
- Repeat
- IsCorrect := True;
- Readln(Condition);
- If (Condition <> 'm') And (Condition <> 'f') Then
- Begin
- IsCorrect := False;
- Write('Please type [m] or [f]: ');
- End;
- Until IsCorrect;
- ReadCondition := Condition[1];
- End;
- Function InputSourceSetsManually(SetsCount: Integer): TArrOfTMySet;
- Var
- Sets: TArrOfTMySet;
- Index: Integer;
- Begin
- SetLength(Sets, SetsCount);
- For Index := 0 To SetsCount - 1 Do
- Sets[Index] := InputSet('SetX' + Inttostr(Index + 1));
- InputSourceSetsManually := Sets;
- End;
- Function GetSourceSets(SetsCount: Integer): TArrOfTMySet;
- Var
- Condition: Char;
- Begin
- Condition := ReadCondition();
- Case Condition Of
- 'm':
- GetSourceSets := InputSourceSetsManually(SetsCount);
- 'f':
- GetSourceSets := GetSourceSetsFromFile(SetsCount);
- End;
- End;
- Function ExtractMultiple4(DataSet: TMySet): TMySet;
- Var
- ResultSet: TMySet;
- Index: Integer;
- Begin
- ResultSet := TMySet.Create();
- For Index := 1 To 255 Do
- Begin
- If (Index In DataSet.Numbers) And (Index Mod 4 = 0) Then
- Begin
- ResultSet.AddNumber(Index);
- End;
- End;
- ExtractMultiple4 := ResultSet;
- End;
- Function TrySaveSets(Path: String; Sets: TArrOfTMySet;
- SetY, SetY1: TMySet): Boolean;
- Var
- MyFile: TextFile;
- Index: Integer;
- Begin
- AssignFile(MyFile, Path);
- Try
- Rewrite(MyFile);
- For Index := 0 To 2 Do
- Begin
- Write(MyFile, 'SetX' + Inttostr(Index + 1) + ': ');
- Write(MyFile, Sets[Index].ToString());
- End;
- Write(MyFile, 'SetY: ');
- Write(MyFile, SetY.ToString());
- Write(MyFile, 'SetY1: ');
- Write(MyFile, SetY1.ToString());
- TrySaveSets := True;
- Writeln('File saved successfully!');
- CloseFile(MyFile);
- Except
- Writeln('Unable to write file.');
- TrySaveSets := False;
- End;
- End;
- Procedure SaveSets(Sets: TArrOfTMySet; SetY, SetY1: TMySet);
- Var
- Path: String;
- IsCorrect: Boolean;
- Begin
- Repeat
- IsCorrect := True;
- Path := InputFilePath(False);
- IsCorrect := TrySaveSets(Path, Sets, SetY, SetY1);
- Until IsCorrect;
- End;
- Procedure PrintCompleted();
- Begin
- Write('Completed!');
- End;
- Procedure Run();
- Var
- Text: String;
- SetY: TMySet;
- SetY1: TMySet;
- IsCorrect: Boolean;
- Sets: TArrOfTMySet;
- Index, SetsCount: Integer;
- Begin
- PrintCondition();
- SetsCount := 3;
- Sets := GetSourceSets(SetsCount);
- For Index := 0 To High(Sets) Do
- Sets[Index].PrintSet('SetX' + Inttostr(Index + 1) + ': ');
- SetY := TMySet.Create();
- For Index := 0 To High(Sets) Do
- SetY := SetY.Union(Sets[Index]);
- SetY.PrintSet('SetY: ');
- SetY1 := ExtractMultiple4(SetY);
- SetY1.PrintSet('SetY1: ');
- SaveSets(Sets, SetY, SetY1);
- PrintCompleted();
- End;
- Begin
- Run();
- Readln;
- End.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement