Advertisement
Guest User

Untitled

a guest
Nov 19th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 2.01 KB | None | 0 0
  1. program Project8;
  2.  
  3. {$APPTYPE CONSOLE}
  4. {$R *.res}
  5.  
  6. uses
  7.   System.SysUtils;
  8.  
  9. function NOD( M : integer; F : integer) : integer;
  10.    begin
  11.       while ( M <> F ) do
  12.          if ( M > F) then
  13.             M := M - F
  14.          else
  15.             F := F - M;
  16.       NOD := M;
  17.    end;
  18.  
  19. Function FindingFile(): integer;
  20. var
  21.   CorrectFile: Boolean;
  22.   NameOfFile: String;
  23.   Input : TextFile;
  24.   n, i , k , M , F: integer;
  25.   A : array of integer;
  26. begin
  27.   repeat
  28.     Write('Введите путь к файлу, с которого хотите считать информацию :');
  29.     Readln(NameOfFile);
  30.     CorrectFile := True;
  31.     try
  32.       AssignFile(Input, NameOfFile);
  33.       Reset(Input);
  34.       if not eof(Input) then
  35.          begin
  36.             ReadLn(Input, n);
  37.             SetLength(A , n);
  38.             for i := 0 to (n - 1) do
  39.                Read(Input, A[i]);
  40.             CloseFile(Input);
  41.          end
  42.       else
  43.          begin
  44.             WriteLn(' Файл оказался пустым ');
  45.             CloseFile(Input) ;
  46.          end;
  47.     except
  48.       CorrectFile := False;
  49.       Writeln('Не удалось найти файл по такому пути', NameOfFile);
  50.     end;
  51.   until CorrectFile;
  52.   k := NOD(A[1] , A[2]);
  53.   for i := 3 to n do
  54.      k := NOD(k, a[i]);
  55.   FindingFile := k;
  56. end;
  57.  
  58.  
  59. Procedure SaveFile(NameOutput : String ; nod: integer);
  60. var
  61.   Output: TextFile;
  62.   begin
  63.      Writeln('Введите путь к файлу для записи : ');
  64.      Readln(NameOutput);
  65.      AssignFile(Output,NameOutput);
  66.      Rewrite(Output);
  67.      WriteLn(Output , 'Наибольший общий делитель : ', nod);
  68.      CloseFile(Output);
  69.      ReadLn;
  70.   end;
  71.  
  72. var
  73.   k : integer;
  74.   NameOutput : String;
  75. begin
  76.   WriteLn('Дан массив А, состоящий из n-натуральных чисел.Найти наибольший общий делитель элементов массива.');
  77.   k := FindingFile();
  78.   SaveFile(NameOutput, k);
  79.   ReadLn;
  80. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement