Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program L2D;
- {$APPTYPE CONSOLE}
- uses
- SysUtils, Generics.Collections;
- const
- BYTE_MAX_VAL = 255;
- type
- TByteSet = Set of Byte;
- Option = (UserFile, Console);
- function GetPrimesSet(N: Integer): TByteSet;
- var
- LeastPrime: array of Integer;
- Primes: TList<Integer>;
- PrimeSet: TByteSet;
- i, j: Integer;
- begin
- SetLength(LeastPrime, N);
- Primes := TList<Integer>.Create;
- PrimeSet := [];
- for i := 2 to N do
- begin
- if (LeastPrime[i] = 0) then
- begin
- LeastPrime[i] := i;
- Primes.Add(i);
- Include(PrimeSet, i);
- end;
- j := 0;
- while (j < Primes.Count) And (Primes[j] <= LeastPrime[i]) And
- (i * Primes[j] <= N) do
- begin
- LeastPrime[i * Primes[j]] := Primes[j];
- Inc(j);
- end;
- end;
- GetPrimesSet := PrimeSet;
- end;
- procedure PrintByteSet(SetToPrint: TByteSet);
- var
- i: Integer;
- begin
- for i := 1 to BYTE_MAX_VAL do
- if i in SetToPrint then
- Write(Output, i, ' ');
- Writeln(Output);
- end;
- procedure OpenFileToWrite;
- var
- Option: Char;
- StopReadFileName, StopReadOption: Boolean;
- FileName: String;
- begin
- StopReadFileName := True;
- repeat
- Writeln('Enter file name: ');
- Readln(FileName);
- try
- if FileExists(FileName) then
- begin
- Writeln('Do you want to clear file? [Y/N]: ');
- AssignFile(Output, FileName);
- StopReadOption := False;
- repeat
- Readln(Option);
- if (UpperCase(Option) = 'Y') then
- begin
- StopReadOption := True;
- Rewrite(Output);
- end
- else if UpperCase(Option) = 'N' then
- begin
- StopReadOption := True;
- Append(Output);
- end;
- until StopReadOption;
- end
- else
- begin
- AssignFile(Output, FileName);
- Rewrite(Output);
- end;
- except
- StopReadFileName := False;
- AssignFile(Output, '');
- Rewrite(Output);
- Writeln('Can not open the file...');
- end;
- until StopReadFileName;
- end;
- procedure CloseFileAfterWrite;
- begin
- CloseFile(Output);
- AssignFile(Output, '');
- Rewrite(Output);
- Writeln('Done.');
- end;
- function ConvertNumOption(NumOption: Char): Option;
- begin
- case NumOption of
- '1':
- ConvertNumOption := UserFile;
- '2':
- ConvertNumOption := Console;
- end;
- end;
- procedure WriteResult(SetToPrint1, SetToPrint2: TByteSet);
- var
- NumOption: Char;
- StopReadOption: Boolean;
- WriteTo: Option;
- begin
- Writeln('[1] Write data to file');
- Writeln('[2] Write data to console');
- StopReadOption := False;
- repeat
- Readln(NumOption);
- WriteTo := ConvertNumOption(NumOption);
- case WriteTo of
- UserFile:
- begin
- StopReadOption := True;
- OpenFileToWrite;
- Writeln(Output, 'Prime numbers in set are: ');
- PrintByteSet(SetToPrint1);
- Writeln(Output, 'Composite numbers in set are: ');
- PrintByteSet(SetToPrint2);
- CloseFileAfterWrite;
- end;
- Console:
- begin
- StopReadOption := True;
- Writeln('Prime numbers in set are: ');
- PrintByteSet(SetToPrint1);
- Writeln('Composite numbers in set are: ');
- PrintByteSet(SetToPrint2);
- end;
- end;
- until StopReadOption;
- end;
- procedure CloseFileAfterRead;
- begin
- AssignFile(Input, '');
- Reset(Input);
- end;
- procedure OpenFileToRead;
- var
- StopReadFileName: Boolean;
- FileName: String;
- begin
- StopReadFileName := False;
- repeat
- Writeln('Enter file name: ');
- Readln(FileName);
- if FileExists(FileName) then
- begin
- StopReadFileName := True;
- AssignFile(Input, FileName);
- try
- Reset(Input);
- except
- begin
- StopReadFileName := False;
- Writeln('Can not open the file...');
- CloseFileAfterRead;
- end;
- end;
- end
- else
- Writeln('No such file...');
- until StopReadFileName;
- end;
- function GetPositiveByte(var N: Byte): Boolean;
- var
- Buffer: Integer;
- begin
- GetPositiveByte := True;
- try
- Read(Input, Buffer);
- except
- Writeln('N is not a number');
- GetPositiveByte := False;
- end;
- if Result then
- if (Buffer <= 0) or (Buffer >= 256) then
- begin
- Writeln('Number must be positive and less then 256...');
- GetPositiveByte := False;
- end
- else
- N := Buffer;
- end;
- function ReadSet(var InputSet: TByteSet; SourceType: Option): Boolean;
- var
- StopReadN, IsNumber, ExitFunction: Boolean;
- i: Integer;
- N, Buffer: Byte;
- begin
- ExitFunction := False;
- StopReadN := False;
- repeat
- Writeln('Enter number of elements in set: ');
- if not GetPositiveByte(N) then
- begin
- if SourceType = UserFile then
- ExitFunction := True;
- end
- else
- StopReadN := True;
- until StopReadN or ExitFunction;
- if not ExitFunction then
- begin
- Writeln(Output, 'Done.');
- Writeln('Enter numbers: ');
- for i := 0 to N - 1 do
- if not ExitFunction then
- begin
- StopReadN := False;
- repeat
- if not GetPositiveByte(N) then
- begin
- if SourceType = UserFile then
- ExitFunction := True;
- end
- else
- begin
- StopReadN := True;
- Include(InputSet, N);
- end;
- until StopReadN or ExitFunction;
- end;
- if not ExitFunction then
- Writeln('Done.')
- else
- Writeln('File contains wrong data...');
- end;
- ReadSet := not ExitFunction;
- end;
- function ReadUserData(var InputSet: TByteSet): Boolean;
- var
- ReadInputThen: Boolean;
- NumOption: Char;
- ReadFrom: Option;
- begin
- ReadInputThen := True;
- Writeln('[1] Read data from file');
- Writeln('[2] Read data from console');
- Writeln('[~] Close');
- Readln(NumOption);
- ReadFrom := ConvertNumOption(NumOption);
- case ReadFrom of
- UserFile:
- begin
- OpenFileToRead;
- ReadSet(InputSet, UserFile);
- CloseFileAfterRead;
- end;
- Console:
- ReadSet(InputSet, Console);
- else
- ReadInputThen := False;
- end;
- ReadUserData := ReadInputThen;
- end;
- procedure Main();
- var
- InputSet, Primes, PrimesInInputSet, NotPrimesFromInputSet: TByteSet;
- begin
- Primes := GetPrimesSet(BYTE_MAX_VAL);
- InputSet := [];
- while ReadUserData(InputSet) do
- begin
- PrimesInInputSet := InputSet * Primes;
- NotPrimesFromInputSet := InputSet - Primes - [1, 2];
- WriteResult(PrimesInInputSet, NotPrimesFromInputSet);
- end;
- end;
- begin
- Main;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement