MadCortez

Untitled

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