Advertisement
MadCortez

Untitled

Nov 13th, 2020
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 6.98 KB | None | 0 0
  1. program laba3_2;
  2.  
  3. Uses
  4.    System.SysUtils;
  5.  
  6. Type
  7.    TByteSet = set of Byte;
  8.  
  9. procedure PrintTask; forward;
  10. function InputValue(Min, Max: Integer): Integer; forward;
  11. function UserInputFromConsole(): Byte; forward;
  12. function UserInputFromFile(Path: String): Byte; forward;
  13. function CheckPath(Path: String): Boolean; forward;
  14. function UserOutputPath(): String; forward;
  15. procedure PrintInConsole(a: TByteSet; n: Byte); forward;
  16. procedure PrintInFile(Path: String; a: TByteSet; n: Byte); forward;
  17. function CheckFile(Path: String): Boolean; forward;
  18. function UserInputPath(): String; forward;
  19. function InputMethod: Word; forward;
  20. function OutputMethod(): Word; forward;
  21. function FillSet(n: Byte): TByteSet; forward;
  22. function Eratosfen(a: TByteSet; n: Byte): TByteSet; forward;
  23. function UserInput(): Byte; forward;
  24. procedure ResultPrint(a: TByteSet; n: Byte); forward;
  25.  
  26. function InputValue(Min, Max: Integer): Integer;
  27. var
  28.    CurrentValue: Integer;
  29.    IsValid: Boolean;
  30. begin
  31.    repeat
  32.    IsValid := True;
  33.    try
  34.       Read(CurrentValue);
  35.    except
  36.       begin
  37.          IsValid := False;
  38.          Writeln('Введено нецелое число');
  39.       end;
  40.    end;
  41.    if IsValid then
  42.       if (CurrentValue < Min) or (CurrentValue > Max) then
  43.       begin
  44.          IsValid := False;
  45.          Writeln('Введите число в заданном диапазоне');
  46.       end;
  47.    until IsValid;
  48.    InputValue := CurrentValue;
  49. end;
  50.  
  51. function UserInputFromConsole(): Byte;
  52. var
  53.    n: Integer;
  54.    const MIN_SIZE = 2;
  55.    const MAX_SIZE = 255;
  56. begin
  57.    Write('Введите число, до которого нужно найти все простые числа в диапазоне ', MIN_SIZE, '..', MAX_SIZE, ': ');
  58.    n := InputValue(MIN_SIZE, MAX_SIZE);
  59.    Readln;
  60.    UserInputFromConsole := n;
  61. end;
  62.  
  63. function UserInputFromFile(Path: String): Byte;
  64. var
  65.    n: Integer;
  66.    MyFile: TextFile;
  67. begin
  68.    AssignFile(MyFile, Path);
  69.    reset(MyFile);
  70.    Readln(MyFile, n);
  71.    closefile(MyFile);
  72.    UserInputFromFile := n;
  73. end;
  74.  
  75. function CheckPath(Path: String): Boolean;
  76. begin
  77.    if FileExists(Path) then
  78.    begin
  79.       Writeln(Path, ' существует');
  80.       CheckPath := True;
  81.    end
  82.    else
  83.    begin
  84.       Writeln(Path, ' не существует');
  85.       Writeln('Введите корректный путь к файлу');
  86.    end;
  87. end;
  88.  
  89. function UserOutputPath(): String;
  90. var
  91.    Path: String;
  92. begin
  93.    Writeln('Введите абсолютный путь к файлу для вывода результата');
  94.    Readln(Path);
  95.    UserOutputPath := Path;
  96. end;
  97.  
  98. procedure PrintInConsole(a: TByteSet; n: Byte);
  99. var
  100.    i: Integer;
  101. begin
  102.    for i := 2 to n do
  103.       if i in a then
  104.          Write(i, ' ');
  105.    Writeln;
  106. end;
  107.  
  108. procedure PrintInFile(Path: String; a: TByteSet; n: Byte);
  109. var
  110.    i: Integer;
  111.    MyFile: TextFile;
  112. begin
  113.    AssignFile(MyFile, Path);
  114.    rewrite(MyFile);
  115.    for i := 2 to n do
  116.       if i in a then
  117.          Write(MyFile, i, ' ');
  118.    close(MyFile);
  119.    Writeln('Результат работы помещён в файл');
  120. end;
  121.  
  122. function CheckFile(Path: String): Boolean;
  123. var
  124.    IsValid: Boolean;
  125.    n: Integer;
  126.    MyFile: TextFile;
  127.    const MIN_SIZE = 2;
  128.    const MAX_SIZE = 256;
  129. begin
  130.    AssignFile(MyFile, Path);
  131.    reset(MyFile);
  132.    IsValid := True;
  133.    try
  134.       Read(MyFile, n);
  135.    except
  136.       IsValid := False;
  137.    end;
  138.    if IsValid then
  139.       if (n < MIN_SIZE) or (n > MAX_SIZE) then
  140.          IsValid := False;
  141.    close(MyFile);
  142.    CheckFile := IsValid;
  143. end;
  144.  
  145. function UserInputPath(): String;
  146. var
  147.    Path: String;
  148. begin
  149.    repeat
  150.       repeat
  151.          Writeln('Введите абсолютный путь к файлу с входными данными');
  152.          Readln(Path);
  153.       until CheckPath(Path);
  154.       if not(CheckFile(Path)) then
  155.          Writeln('Неккоректные данные в файле, исправьте файл');
  156.    until (CheckFile(Path));
  157.    UserInputPath := Path;
  158. end;
  159.  
  160. function InputMethod: Word;
  161. var
  162.    Method: Word;
  163.    IsValid: Boolean;
  164. begin
  165.    Writeln('Каким способом хотите ввести данные?');
  166.    Writeln('1 - с помощью консоли');
  167.    Writeln('2 - с помощью файла');
  168.    repeat
  169.       IsValid := True;
  170.       try
  171.          Readln(Method);
  172.       except
  173.          begin
  174.             IsValid := False;
  175.             Writeln('Введено нецелое число');
  176.          end;
  177.       end;
  178.       if IsValid then
  179.          if (Method <> 1) and (Method <> 2) then
  180.          begin
  181.             IsValid := False;
  182.             Writeln('Введите 1 или 2');
  183.          end;
  184.    until IsValid;
  185.    InputMethod := Method;
  186. end;
  187.  
  188. function OutputMethod(): Word;
  189. var
  190.    Method: Word;
  191.    IsValid: Boolean;
  192. begin
  193.    Writeln('Куда хотите вывести результат?');
  194.    Writeln('1 - в консоль');
  195.    Writeln('2 - в файл');
  196.    repeat
  197.       IsValid := True;
  198.       try
  199.          Readln(Method);
  200.       except
  201.          begin
  202.             IsValid := False;
  203.             Writeln('Введено нецелое число');
  204.          end;
  205.       end;
  206.       if IsValid then
  207.          if (Method <> 1) and (Method <> 2) then
  208.          begin
  209.             IsValid := False;
  210.             Writeln('Введите 1 или 2');
  211.          end;
  212.    until IsValid;
  213.    OutputMethod := Method;
  214. end;
  215.  
  216. procedure PrintTask;
  217. begin
  218.    Write('Данная программа находит все простые числа, не превосходящие n, ');
  219.    Writeln('с помощью алгоритма «решето Эратосфена»');
  220. end;
  221.  
  222. function FillSet(n: Byte): TByteSet;
  223. var
  224.    i: Integer;
  225.    a: TByteSet;
  226. begin
  227.    for i := 2 to n do
  228.       Include(a, i);
  229.    FillSet := a;
  230. end;
  231.  
  232. function Eratosfen(a: TByteSet; n: Byte): TByteSet;
  233. var
  234.    i, j: Integer;
  235. begin
  236.    for i := 2 to n do
  237.       if i in a then
  238.          for j := i + 1 to n do
  239.             if j in a then
  240.                if (j mod i = 0) then
  241.                   Exclude(a, j);
  242.    Eratosfen := a;
  243. end;
  244.  
  245. function UserInput(): Byte;
  246. var
  247.    Method: Word;
  248.    Path: String;
  249. begin
  250.    Method := InputMethod;
  251.    if (Method = 1) then
  252.       UserInput := UserInputFromConsole
  253.    else
  254.    begin
  255.       Path := UserInputPath;
  256.       UserInput := UserInputFromFile(Path);
  257.    end;
  258. end;
  259.  
  260. procedure ResultPrint(a: TByteSet; n: Byte);
  261. var
  262.    Method: Word;
  263.    Path: String;
  264. begin
  265.    Method := OutputMethod;
  266.    if (Method = 1) then
  267.       PrintInConsole(a, n)
  268.    else
  269.    begin
  270.       Path := UserOutputPath;
  271.       PrintInFile(Path, a, n);
  272.    end;
  273. end;
  274.  
  275. procedure Main();
  276. var
  277.    Method: Word;
  278.    n: Byte;
  279.    a: TByteSet;
  280.    i: Integer;
  281.    MyFile: TextFile;
  282.    Path: String;
  283. begin
  284.    PrintTask;
  285.    n := UserInput;
  286.    a := FillSet(n);
  287.    a := Eratosfen(a, n);
  288.    ResultPrint(a, n);
  289.    Writeln('Нажмите Enter для выхода из программы');
  290.    Readln;
  291. end;
  292.  
  293. begin
  294.    Main();
  295. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement