Advertisement
MadCortez

Untitled

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