Advertisement
Guest User

Untitled

a guest
Dec 5th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.94 KB | None | 0 0
  1. program Project;
  2.  
  3. {$APPTYPE CONSOLE}
  4.  
  5. {$R *.res}
  6.  
  7. uses
  8. System.SysUtils;
  9.  
  10. type
  11. TNums = set of 1..255;
  12.  
  13. function PrimeSearch(A : Integer): boolean;
  14. var
  15. I: Integer;
  16. begin
  17. if A = 1 then
  18. PrimeSearch := False
  19. else
  20. begin
  21. PrimeSearch := True;
  22. for I := 2 to Round(Sqrt(A)) do
  23. if A mod I = 0 then
  24. PrimeSearch := False;
  25. end;
  26. end;
  27.  
  28. function InputNameOfFile(): String;
  29. var
  30. Path: String;
  31. begin
  32. WriteLn('Input name of the file');
  33. ReadLn(Path);
  34. Path := Path + '.txt';
  35. InputNameOfFile := Path;
  36. end;
  37.  
  38. procedure ReadingOfSetFromFile(Path: String);
  39. const
  40. MinSize = 1;
  41. MaxSize = 100;
  42. var
  43. Nums: TNums;
  44. I, Num, Size, First, Last: Integer;
  45. InputFile: textfile;
  46. CorrectFile, SizeCorrect: boolean;
  47. begin
  48. CorrectFile := True;
  49. if FileExists(Path) then
  50. begin
  51. try
  52. Assign(InputFile, Path);
  53. Reset(InputFile);
  54. except
  55. WriteLn('Could not open file. ', path);
  56. CorrectFile := False;
  57. end;
  58. if CorrectFile then
  59. begin
  60. if EoF(InputFile) then
  61. begin
  62. WriteLn('File is empty.');
  63. CorrectFile := False;
  64. end
  65. else
  66. while not EoF(InputFile) do
  67. begin
  68. try
  69. ReadLn(InputFile, Size);
  70. WriteLn('Amount of numbers:', size);
  71. except
  72. // WriteLn('File data error. Repeat entry. Number was expected.');
  73. CorrectFile := False;
  74. end;
  75. Nums := [];
  76. Size := Size - 1;
  77. for I := 0 to Size do
  78. begin
  79. try
  80. Read(InputFile, Num);
  81. Nums := Nums + [Num];
  82. except
  83. // WriteLn('File data error in item with index ', I,'. Repeat entry. Number was expected.');
  84. CorrectFile := False;
  85. end;
  86.  
  87. end;
  88. end;
  89. CloseFile(InputFile);
  90. end;
  91. end
  92. else
  93. begin
  94. WriteLn('File was not found.');
  95. CorrectFile := False;
  96. end;
  97. end;
  98.  
  99. function FindTheFirst(Nums: TNums): Integer;
  100. var
  101. I, First: Integer;
  102. begin
  103. First := 255;
  104. for I := 0 to 255 do
  105. if I in Nums then
  106. begin
  107. First := I;
  108. FindTheFirst := First;
  109.  
  110. end;
  111. WriteLn(First);
  112. end;
  113.  
  114. function FindTheLast(Nums: TNums): Integer;
  115. var
  116. I, Last: Integer;
  117. begin
  118. Last := 0;
  119. for I := 0 to 255 do
  120. if I in Nums then
  121. Last := I;
  122. FindTheLast := Last;
  123. end;
  124.  
  125. function CreationOfPrimeSet(First, Last : Integer; Nums: TNums): TNums;
  126. var
  127. I, K: Byte;
  128. PrimeNums: TNums;
  129. begin
  130. PrimeNums := [];
  131. for I := 0 to 255 do
  132. if I in Nums then
  133. if PrimeSearch(i) then
  134. PrimeNums := PrimeNums + [I];
  135. WriteLn('Prime numbers: ');
  136. K := 0;
  137. for I := First to Last do
  138. if I in PrimeNums then
  139. begin
  140. Write(I, ' ');
  141. K := K + 1;
  142. end;
  143. if K = 0 then
  144. WriteLn('There are not any prime numbers. ')
  145. else
  146. WriteLn('There are ', K ,' prime numbers in the set');
  147. CreationOfPrimeSet := PrimeNums;
  148. end;
  149.  
  150. function CreationOfNotPrimeSet(First, Last : Integer; Nums, PrimeNums: TNums): TNums;
  151. var
  152. I, K: Byte;
  153. CompoundNums: TNums;
  154. begin
  155. CompoundNums := Nums - PrimeNums;
  156. WriteLn('Compound numbers: ');
  157. K := 0;
  158. for I := First to Last do
  159. if I in CompoundNums then
  160. begin
  161. Write(I, ' ');
  162. K := K + 1;
  163. end;
  164. if K = 0 then
  165. WriteLn('There are prime numbers only. ')
  166. else
  167. WriteLn('There are ', K ,' compound numbers in the set');
  168. CreationOfNotPrimeSet := CompoundNums;
  169. end;
  170.  
  171.  
  172.  
  173. procedure SaveInFile(Path: String; PrimeNums, CompoundNums: TNums; First, Last: Integer);
  174. var
  175. I, K: Integer;
  176. OutFile: Text;
  177. begin
  178. Assign(OutFile, Path);
  179. Rewrite(OutFile);
  180. WriteLn(OutFile, 'Prime numbers: ');
  181. K := 0;
  182. for I := First to Last do
  183. if I in PrimeNums then
  184. begin
  185. Write(OutFile, I, ' ');
  186. K := K + 1;
  187. end;
  188. if K = 0 then
  189. WriteLn(OutFile, 'There are not any prime numbers. ')
  190. else
  191. WriteLn(OutFile, 'There are ', K ,' prime numbers in the set');
  192. WriteLn(OutFile, 'Not prime numbers: ');
  193. K := 0;
  194. for I := First to Last do
  195. if I in CompoundNums then
  196. begin
  197. Write(OutFile, I, ' ');
  198. K := K + 1;
  199. end;
  200. if K = 0 then
  201. WriteLn(OutFile, 'There are prime numbers only. ')
  202. else
  203. WriteLn(OutFile, 'There are ', K ,' compound numbers in the set');
  204. Close(OutFile);
  205. end;
  206.  
  207. procedure Main();
  208. var
  209. First, Last: Integer;
  210. Path: String;
  211. Nums, PrimeNums, CompoundNums: TNums;
  212. begin
  213. 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.');
  214. Path := InputNameOfFile();
  215. ReadingOfSetFromFile(Path);
  216. First := FindTheFirst(Nums);
  217. Last := FindTheLast(Nums);
  218. PrimeNums := CreationOfPrimeSet(First, Last, Nums);
  219. CompoundNums := CreationOfNotPrimeSet(First, Last, Nums, PrimeNums);
  220. SaveInFile(Path, PrimeNums, CompoundNums, First, Last);
  221. ReadLn;
  222. end;
  223.  
  224. begin
  225. Main();
  226. ReadLn;
  227. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement