Advertisement
Vanya_Shestakov

laba2.2 (Delphi)

Oct 6th, 2020
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 2.01 KB | None | 0 0
  1. program laba2_2;
  2. uses
  3.   System.SysUtils;
  4.  
  5. function InputPath(): String;
  6. var
  7.     Path: String;
  8.     IsCorrect: Boolean;
  9.  
  10. begin
  11.     Writeln('Введите абсолютную ссылку на файл');
  12.     repeat
  13.         IsCorrect := True;
  14.         Readln(Path);
  15.  
  16.         if not FileExists(Path) then
  17.         begin
  18.             IsCorrect := False;
  19.             Writeln('Файл не найден! Введите абсолютную ссылку на файл');
  20.         end;
  21.     until IsCorrect;
  22.     InputPath := Path;
  23. end;
  24.  
  25. function FindSumOfDividers(Number: Integer): Integer;
  26. var
  27.     Sum, I: Integer;
  28.  
  29. begin
  30.     Sum := 0;
  31.     for I := 1 to Number - 1  do
  32.     begin
  33.         if Number mod i = 0 then
  34.             Sum := Sum + i;
  35.     end;
  36.     FindSumOfDividers := Sum;
  37. end;
  38.  
  39. procedure OutputToFile(Path: String);
  40. var
  41.     FirstNumber, SecondNumber, I: Integer;
  42.     NotRepeatNumbers: Boolean;
  43.     OutputFile: TextFile;
  44.  
  45. begin
  46.     AssignFile(OutputFile, Path);
  47.     Reset(OutputFile);
  48.     Rewrite(OutputFile);
  49.  
  50.     NotRepeatNumbers := True;
  51.     for I := 1 to 32000 do
  52.     begin
  53.         FirstNumber := FindSumOfDividers(i);
  54.         SecondNumber := FindSumOfDividers(firstNumber);
  55.  
  56.         if (I = SecondNumber) and (FirstNumber <> SecondNumber)  then
  57.         begin
  58.             if NotRepeatNumbers then
  59.             begin
  60.                 Writeln(FirstNumber ,' и ', SecondNumber);
  61.                 Writeln(OutputFile, FirstNumber ,' и ', SecondNumber);
  62.                 NotRepeatNumbers := False;
  63.             end
  64.             else
  65.                 NotRepeatNumbers := True;
  66.         end;
  67.     end;
  68.     CloseFile(OutputFile);
  69.     Writeln('Информация успешно записана в файл!');
  70. end;
  71.  
  72. //D:\FilesOAIP\output.txt
  73. procedure Main();
  74. var
  75.     Path: String;
  76.  
  77. begin
  78.     Writeln('Программа выводит все дружественные числа до 32000');
  79.  
  80.     Path := InputPath();
  81.     OutputToFile(Path);
  82.  
  83.     Readln;
  84. end;
  85.  
  86. begin
  87.     Main;
  88. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement