Advertisement
Guest User

Untitled

a guest
Nov 19th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 7.14 KB | None | 0 0
  1. program L2D;
  2. {$APPTYPE CONSOLE}
  3.  
  4. uses
  5.    SysUtils, Generics.Collections;
  6.  
  7. const
  8.    BYTE_MAX_VAL = 255;
  9.  
  10. type
  11.    TByteSet = Set of Byte;
  12.    Option = (UserFile, Console);
  13.  
  14. function GetPrimesSet(N: Integer): TByteSet;
  15. var
  16.    LeastPrime: array of Integer;
  17.    Primes: TList<Integer>;
  18.    PrimeSet: TByteSet;
  19.    i, j: Integer;
  20. begin
  21.    SetLength(LeastPrime, N);
  22.    Primes := TList<Integer>.Create;
  23.    PrimeSet := [];
  24.    for i := 2 to N do
  25.    begin
  26.       if (LeastPrime[i] = 0) then
  27.       begin
  28.          LeastPrime[i] := i;
  29.          Primes.Add(i);
  30.          Include(PrimeSet, i);
  31.       end;
  32.       j := 0;
  33.       while (j < Primes.Count) And (Primes[j] <= LeastPrime[i]) And
  34.         (i * Primes[j] <= N) do
  35.       begin
  36.          LeastPrime[i * Primes[j]] := Primes[j];
  37.          Inc(j);
  38.       end;
  39.    end;
  40.    GetPrimesSet := PrimeSet;
  41. end;
  42.  
  43. procedure PrintByteSet(SetToPrint: TByteSet);
  44. var
  45.    i: Integer;
  46. begin
  47.    for i := 1 to BYTE_MAX_VAL do
  48.       if i in SetToPrint then
  49.          Write(Output, i, ' ');
  50.    Writeln(Output);
  51. end;
  52.  
  53. procedure OpenFileToWrite;
  54. var
  55.    Option: Char;
  56.    StopReadFileName, StopReadOption: Boolean;
  57.    FileName: String;
  58. begin
  59.    StopReadFileName := True;
  60.    repeat
  61.       Writeln('Enter file name: ');
  62.       Readln(FileName);
  63.       try
  64.          if FileExists(FileName) then
  65.          begin
  66.             Writeln('Do you want to clear file? [Y/N]: ');
  67.             AssignFile(Output, FileName);
  68.             StopReadOption := False;
  69.             repeat
  70.                Readln(Option);
  71.                if (UpperCase(Option) = 'Y') then
  72.                begin
  73.                   StopReadOption := True;
  74.                   Rewrite(Output);
  75.                end
  76.                else if UpperCase(Option) = 'N' then
  77.                begin
  78.                   StopReadOption := True;
  79.                   Append(Output);
  80.                end;
  81.             until StopReadOption;
  82.          end
  83.          else
  84.          begin
  85.             AssignFile(Output, FileName);
  86.             Rewrite(Output);
  87.          end;
  88.       except
  89.          StopReadFileName := False;
  90.          AssignFile(Output, '');
  91.          Rewrite(Output);
  92.          Writeln('Can not open the file...');
  93.       end;
  94.    until StopReadFileName;
  95. end;
  96.  
  97. procedure CloseFileAfterWrite;
  98. begin
  99.    CloseFile(Output);
  100.    AssignFile(Output, '');
  101.    Rewrite(Output);
  102.    Writeln('Done.');
  103. end;
  104.  
  105. function ConvertNumOption(NumOption: Char): Option;
  106. begin
  107.    case NumOption of
  108.       '1':
  109.          ConvertNumOption := UserFile;
  110.       '2':
  111.          ConvertNumOption := Console;
  112.    end;
  113. end;
  114.  
  115. procedure WriteResult(SetToPrint1, SetToPrint2: TByteSet);
  116. var
  117.    NumOption: Char;
  118.    StopReadOption: Boolean;
  119.    WriteTo: Option;
  120. begin
  121.    Writeln('[1] Write data to file');
  122.    Writeln('[2] Write data to console');
  123.    StopReadOption := False;
  124.    repeat
  125.       Readln(NumOption);
  126.       WriteTo := ConvertNumOption(NumOption);
  127.       case WriteTo of
  128.          UserFile:
  129.             begin
  130.                StopReadOption := True;
  131.                OpenFileToWrite;
  132.                Writeln(Output, 'Prime numbers in set are: ');
  133.                PrintByteSet(SetToPrint1);
  134.                Writeln(Output, 'Composite numbers in set are: ');
  135.                PrintByteSet(SetToPrint2);
  136.                CloseFileAfterWrite;
  137.             end;
  138.          Console:
  139.             begin
  140.                StopReadOption := True;
  141.                Writeln('Prime numbers in set are: ');
  142.                PrintByteSet(SetToPrint1);
  143.                Writeln('Composite numbers in set are: ');
  144.                PrintByteSet(SetToPrint2);
  145.             end;
  146.       end;
  147.    until StopReadOption;
  148. end;
  149.  
  150. procedure CloseFileAfterRead;
  151. begin
  152.    AssignFile(Input, '');
  153.    Reset(Input);
  154. end;
  155.  
  156. procedure OpenFileToRead;
  157. var
  158.    StopReadFileName: Boolean;
  159.    FileName: String;
  160. begin
  161.    StopReadFileName := False;
  162.    repeat
  163.       Writeln('Enter file name: ');
  164.       Readln(FileName);
  165.       if FileExists(FileName) then
  166.       begin
  167.          StopReadFileName := True;
  168.          AssignFile(Input, FileName);
  169.          try
  170.             Reset(Input);
  171.          except
  172.  
  173.             begin
  174.                StopReadFileName := False;
  175.                Writeln('Can not open the file...');
  176.                CloseFileAfterRead;
  177.             end;
  178.          end;
  179.       end
  180.       else
  181.          Writeln('No such file...');
  182.    until StopReadFileName;
  183. end;
  184.  
  185. function GetPositiveByte(var N: Byte): Boolean;
  186. var
  187.    Buffer: Integer;
  188. begin
  189.    GetPositiveByte := True;
  190.    try
  191.       Read(Input, Buffer);
  192.    except
  193.       Writeln('N is not a number');
  194.       GetPositiveByte := False;
  195.    end;
  196.    if Result then
  197.  
  198.       if (Buffer <= 0) or (Buffer >= 256) then
  199.       begin
  200.          Writeln('Number must be positive and less then 256...');
  201.          GetPositiveByte := False;
  202.       end
  203.       else
  204.          N := Buffer;
  205. end;
  206.  
  207. function ReadSet(var InputSet: TByteSet; SourceType: Option): Boolean;
  208. var
  209.    StopReadN, IsNumber, ExitFunction: Boolean;
  210.    i: Integer;
  211.    N, Buffer: Byte;
  212. begin
  213.    ExitFunction := False;
  214.    StopReadN := False;
  215.    repeat
  216.       Writeln('Enter number of elements in set: ');
  217.       if not GetPositiveByte(N) then
  218.       begin
  219.          if SourceType = UserFile then
  220.             ExitFunction := True;
  221.       end
  222.       else
  223.          StopReadN := True;
  224.    until StopReadN or ExitFunction;
  225.    if not ExitFunction then
  226.    begin
  227.       Writeln(Output, 'Done.');
  228.       Writeln('Enter numbers: ');
  229.       for i := 0 to N - 1 do
  230.          if not ExitFunction then
  231.          begin
  232.             StopReadN := False;
  233.             repeat
  234.                if not GetPositiveByte(N) then
  235.                begin
  236.                   if SourceType = UserFile then
  237.                      ExitFunction := True;
  238.                end
  239.                else
  240.                begin
  241.                   StopReadN := True;
  242.                   Include(InputSet, N);
  243.                end;
  244.             until StopReadN or ExitFunction;
  245.          end;
  246.       if not ExitFunction then
  247.          Writeln('Done.')
  248.       else
  249.          Writeln('File contains wrong data...');
  250.    end;
  251.    ReadSet := not ExitFunction;
  252. end;
  253.  
  254. function ReadUserData(var InputSet: TByteSet): Boolean;
  255. var
  256.    ReadInputThen: Boolean;
  257.    NumOption: Char;
  258.    ReadFrom: Option;
  259. begin
  260.    ReadInputThen := True;
  261.    Writeln('[1] Read data from file');
  262.    Writeln('[2] Read data from console');
  263.    Writeln('[~] Close');
  264.    Readln(NumOption);
  265.    ReadFrom := ConvertNumOption(NumOption);
  266.    case ReadFrom of
  267.       UserFile:
  268.          begin
  269.             OpenFileToRead;
  270.             ReadSet(InputSet, UserFile);
  271.             CloseFileAfterRead;
  272.          end;
  273.       Console:
  274.          ReadSet(InputSet, Console);
  275.    else
  276.       ReadInputThen := False;
  277.    end;
  278.    ReadUserData := ReadInputThen;
  279. end;
  280.  
  281. procedure Main();
  282. var
  283.    InputSet, Primes, PrimesInInputSet, NotPrimesFromInputSet: TByteSet;
  284. begin
  285.    Primes := GetPrimesSet(BYTE_MAX_VAL);
  286.    InputSet := [];
  287.    while ReadUserData(InputSet) do
  288.    begin
  289.       PrimesInInputSet := InputSet * Primes;
  290.       NotPrimesFromInputSet := InputSet - Primes - [1, 2];
  291.       WriteResult(PrimesInInputSet, NotPrimesFromInputSet);
  292.    end;
  293. end;
  294.  
  295. begin
  296.    Main;
  297.  
  298. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement