Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program Project;
- {$APPTYPE CONSOLE}
- {$R *.res}
- uses
- System.SysUtils;
- type
- TNums = set of 1..255;
- function PrimeSearch(A : Integer): boolean;
- var
- I: Integer;
- begin
- if A = 1 then
- PrimeSearch := False
- else
- begin
- PrimeSearch := True;
- for I := 2 to Round(Sqrt(A)) do
- if A mod I = 0 then
- PrimeSearch := False;
- end;
- end;
- function InputNameOfFile(): String;
- var
- Path: String;
- begin
- WriteLn('Input name of the file');
- ReadLn(Path);
- Path := Path + '.txt';
- InputNameOfFile := Path;
- end;
- procedure ReadingOfSetFromFile(Path: String);
- const
- MinSize = 1;
- MaxSize = 100;
- var
- Nums: TNums;
- I, Num, Size, First, Last: Integer;
- InputFile: textfile;
- CorrectFile, SizeCorrect: boolean;
- begin
- CorrectFile := True;
- if FileExists(Path) then
- begin
- try
- Assign(InputFile, Path);
- Reset(InputFile);
- except
- WriteLn('Could not open file. ', path);
- CorrectFile := False;
- end;
- if CorrectFile then
- begin
- if EoF(InputFile) then
- begin
- WriteLn('File is empty.');
- CorrectFile := False;
- end
- else
- while not EoF(InputFile) do
- begin
- try
- ReadLn(InputFile, Size);
- WriteLn('Amount of numbers:', size);
- except
- // WriteLn('File data error. Repeat entry. Number was expected.');
- CorrectFile := False;
- end;
- Nums := [];
- Size := Size - 1;
- for I := 0 to Size do
- begin
- try
- Read(InputFile, Num);
- Nums := Nums + [Num];
- except
- // WriteLn('File data error in item with index ', I,'. Repeat entry. Number was expected.');
- CorrectFile := False;
- end;
- end;
- end;
- CloseFile(InputFile);
- end;
- end
- else
- begin
- WriteLn('File was not found.');
- CorrectFile := False;
- end;
- end;
- function FindTheFirst(Nums: TNums): Integer;
- var
- I, First: Integer;
- begin
- First := 255;
- for I := 0 to 255 do
- if I in Nums then
- begin
- First := I;
- FindTheFirst := First;
- end;
- WriteLn(First);
- end;
- function FindTheLast(Nums: TNums): Integer;
- var
- I, Last: Integer;
- begin
- Last := 0;
- for I := 0 to 255 do
- if I in Nums then
- Last := I;
- FindTheLast := Last;
- end;
- function CreationOfPrimeSet(First, Last : Integer; Nums: TNums): TNums;
- var
- I, K: Byte;
- PrimeNums: TNums;
- begin
- PrimeNums := [];
- for I := 0 to 255 do
- if I in Nums then
- if PrimeSearch(i) then
- PrimeNums := PrimeNums + [I];
- WriteLn('Prime numbers: ');
- K := 0;
- for I := First to Last do
- if I in PrimeNums then
- begin
- Write(I, ' ');
- K := K + 1;
- end;
- if K = 0 then
- WriteLn('There are not any prime numbers. ')
- else
- WriteLn('There are ', K ,' prime numbers in the set');
- CreationOfPrimeSet := PrimeNums;
- end;
- function CreationOfNotPrimeSet(First, Last : Integer; Nums, PrimeNums: TNums): TNums;
- var
- I, K: Byte;
- CompoundNums: TNums;
- begin
- CompoundNums := Nums - PrimeNums;
- WriteLn('Compound numbers: ');
- K := 0;
- for I := First to Last do
- if I in CompoundNums then
- begin
- Write(I, ' ');
- K := K + 1;
- end;
- if K = 0 then
- WriteLn('There are prime numbers only. ')
- else
- WriteLn('There are ', K ,' compound numbers in the set');
- CreationOfNotPrimeSet := CompoundNums;
- end;
- procedure SaveInFile(Path: String; PrimeNums, CompoundNums: TNums; First, Last: Integer);
- var
- I, K: Integer;
- OutFile: Text;
- begin
- Assign(OutFile, Path);
- Rewrite(OutFile);
- WriteLn(OutFile, 'Prime numbers: ');
- K := 0;
- for I := First to Last do
- if I in PrimeNums then
- begin
- Write(OutFile, I, ' ');
- K := K + 1;
- end;
- if K = 0 then
- WriteLn(OutFile, 'There are not any prime numbers. ')
- else
- WriteLn(OutFile, 'There are ', K ,' prime numbers in the set');
- WriteLn(OutFile, 'Not prime numbers: ');
- K := 0;
- for I := First to Last do
- if I in CompoundNums then
- begin
- Write(OutFile, I, ' ');
- K := K + 1;
- end;
- if K = 0 then
- WriteLn(OutFile, 'There are prime numbers only. ')
- else
- WriteLn(OutFile, 'There are ', K ,' compound numbers in the set');
- Close(OutFile);
- end;
- procedure Main();
- var
- First, Last: Integer;
- Path: String;
- Nums, PrimeNums, CompoundNums: TNums;
- begin
- WriteLn('There is a set containing natural numbers from a certain range. Form two sets, the first of which contains all the primes from the given set, and the second all the constituents.');
- Path := InputNameOfFile();
- ReadingOfSetFromFile(Path);
- First := FindTheFirst(Nums);
- Last := FindTheLast(Nums);
- PrimeNums := CreationOfPrimeSet(First, Last, Nums);
- CompoundNums := CreationOfNotPrimeSet(First, Last, Nums, PrimeNums);
- SaveInFile(Path, PrimeNums, CompoundNums, First, Last);
- ReadLn;
- end;
- begin
- Main();
- ReadLn;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement