Advertisement
Vanya_Shestakov

laba3.3 (Delphi)

Oct 19th, 2020 (edited)
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 6.42 KB | None | 0 0
  1. program laba3_3;
  2. uses
  3.     System.SysUtils;
  4.  
  5. type
  6.     DynamicArray = Array of Double;
  7.  
  8. function InputLengthFromConsole(): Integer; Forward;
  9. function InputLengthFromFile(Path: String): Integer; Forward;
  10. function InputPath(): String; Forward;
  11. procedure InputArrayFromConsole(var Arr: Array of Double); Forward;
  12. procedure InputArrayFromFile(var Arr: Array of Double; Path: String); Forward;
  13. procedure ViewIterations(var Arr: Array of Double; I: Integer; Current: Double); Forward;
  14.  
  15. function ChooseSource(): Integer;
  16. var
  17.     Choice: Integer;
  18.     IsCorrect: Boolean;
  19. begin
  20.     Writeln('Choose where to enter data. Enter 1 or 2:');
  21.     Writeln('1.File');
  22.     Writeln('2.Console');
  23.     repeat
  24.         IsCorrect := True;
  25.         try
  26.             Readln(Choice);
  27.         except
  28.             Writeln('Enter an integer!');
  29.             IsCorrect := False;
  30.         end;
  31.  
  32.         if (IsCorrect) and (Choice <> 1) and (Choice <> 2) then
  33.         begin
  34.             Writeln('Enter 1 or 2!');
  35.             IsCorrect := False;
  36.         end;
  37.     until IsCorrect;
  38.     ChooseSource := Choice;
  39. end;
  40.  
  41. function InputData(Source: Integer): DynamicArray;
  42. var
  43.     Length: Integer;
  44.     PathInput: String;
  45.     Arr: DynamicArray;
  46. begin
  47.     Case Source of
  48.         1:
  49.         begin
  50.             Writeln('Enter the absolute link to the input file');
  51.             PathInput := InputPath();
  52.             Length := InputLengthFromFile(PathInput);
  53.             SetLength(Arr, Length);
  54.             InputArrayFromFile(Arr, PathInput);
  55.         end;
  56.         2:
  57.         begin
  58.             Length := InputLengthFromConsole();
  59.             SetLength(Arr, Length);
  60.             InputArrayFromConsole(Arr);
  61.         end;
  62.     end;
  63.     InputData := Arr;
  64. end;
  65.  
  66. function InputPath(): String;
  67. var
  68.     Path: String;
  69.     IsCorrect: Boolean;
  70. begin
  71.     repeat
  72.         IsCorrect := True;
  73.         Readln(Path);
  74.  
  75.         if not FileExists(Path) then
  76.         begin
  77.             IsCorrect := False;
  78.             Writeln('File not found! Enter the absolute link to the file');
  79.         end;
  80.     until IsCorrect;
  81.     InputPath := Path;
  82. end;
  83.  
  84. function InputLengthFromFile(Path: String): Integer;
  85. var
  86.     Length: Integer;
  87.     Flag: Boolean;
  88.     InputFile: TextFile;
  89. begin
  90.     AssignFile(InputFile, Path);
  91.     Reset(InputFile);
  92.     Flag := True;
  93.  
  94.     Writeln('The length of the array is read from file...');
  95.     try
  96.         Readln(InputFile, Length);
  97.     except
  98.         Writeln('The file has incorrect length! Enter the length from console');
  99.         Flag := False;
  100.         Length := InputLengthFromConsole();
  101.     end;
  102.  
  103.     if (Flag) and ((Length < 1) or (Length > 20)) then
  104.     begin
  105.         Length := InputLengthFromConsole();
  106.     end;
  107.  
  108.     CloseFile(InputFile);
  109.     InputLengthFromFile := Length;
  110. end;
  111.  
  112. procedure InputArrayFromFile(var Arr: Array of Double; Path: String);
  113. var
  114.     I: Integer;
  115.     IsCorrect: Boolean;
  116.     InputFile: TextFile;
  117. begin
  118.     AssignFile(InputFile, Path);
  119.     Reset(InputFile);
  120.     Readln(InputFile);
  121.  
  122.     Writeln('The array is read from file...');
  123.     try
  124.         for I := 0 to High(Arr) do
  125.         begin
  126.             Read(InputFile, Arr[I]);
  127.         end;
  128.     except
  129.         Writeln('The file has incorrect data! Enter the array from console');
  130.         InputArrayFromConsole(Arr);
  131.     end;
  132.     CloseFile(InputFile);
  133. end;
  134.  
  135. function InputLengthFromConsole(): Integer;
  136. var
  137.     Length: Integer;
  138.     IsCorrect: Boolean;
  139. begin
  140.     Writeln('Enter the length of the array (from 1 to 20)');
  141.     repeat
  142.         IsCorrect := True;
  143.         try
  144.             Readln(Length);
  145.         except
  146.             Writeln('Enter an integer!');
  147.             IsCorrect := False;
  148.         end;
  149.  
  150.         if (IsCorrect) and ((Length < 1) or (Length > 20)) then
  151.         begin
  152.             Writeln('Incorrect input! Enter the length of the range from 1 to 20');
  153.             IsCorrect := False;
  154.         end;
  155.     until IsCorrect;
  156.     InputLengthFromConsole := Length;
  157. end;
  158.  
  159. procedure InputArrayFromConsole(var Arr: Array of Double);
  160. var
  161.     IsCorrect: Boolean;
  162.     I: Integer;
  163. begin
  164.     for I := 0 to High(Arr) do
  165.     begin
  166.         repeat
  167.             IsCorrect := True;
  168.             Writeln('Enter the array element at number ', i + 1);
  169.             try
  170.                 Readln(Arr[I]);
  171.             except
  172.                 Writeln('Incorrect input! Enter the number');
  173.                 IsCorrect := False;
  174.             end;
  175.         until IsCorrect;
  176.     end;
  177. end;
  178.  
  179. procedure PrintArray(Arr: Array of Double);
  180. var
  181.     I: Integer;
  182. begin
  183.     for I := 0 to High(Arr) do
  184.         Write(FormatFloat('0.####',Arr[I]) , '; ');
  185.     Writeln;
  186.     Writeln;
  187. end;
  188.  
  189. procedure DoInsertionSort(var Arr: Array of Double);
  190. var
  191.     I, J: Integer;
  192.     Current: Double;
  193. begin
  194.     for I := 0 to High(Arr) do
  195.     begin
  196.         Current := Arr[I];
  197.         J := I;
  198.         while (J > 0) and (Current < Arr[J - 1]) do
  199.         begin
  200.             Arr[J] := Arr[J - 1];
  201.             Dec(J);
  202.         end;
  203.         Arr[J] := Current;
  204.         ViewIterations(Arr, I, Current);
  205.     end;
  206. end;
  207.  
  208. procedure ViewIterations(var Arr: Array of Double; I: Integer; Current: Double);
  209. begin
  210.     Writeln('Iteration ', I + 1,':');
  211.     Writeln('Inserted element = ', FormatFloat('0.####',Current));
  212.     Writeln('Array:');
  213.     PrintArray(Arr);
  214. end;
  215.  
  216. procedure OutputToFile(var Arr: Array of Double; Path: String);
  217. var
  218.     OutputFile: TextFile;
  219.     I: Integer;
  220. begin
  221.     AssignFile(OutputFile, Path);
  222.     Reset(OutputFile);
  223.     Rewrite(OutputFile);
  224.  
  225.     for I := 0 to High(Arr) do
  226.     begin
  227.         Write(OutputFile, FormatFloat('0.####',Arr[I]), '; ');
  228.     end;
  229.     CloseFile(OutputFile);
  230.     Writeln('The data is successfully recorded in the file');
  231. end;
  232.  
  233. procedure OutputData(Source: Integer; Arr: Array of Double);
  234. var
  235.     PathOutput: String;
  236. begin
  237.     if Source = 1 then
  238.     begin
  239.         Writeln;
  240.         Writeln('Enter the absolute link to the output file');
  241.         PathOutput := InputPath();
  242.         OutputToFile(Arr, PathOutput);
  243.     end;
  244. end;
  245.  
  246. procedure Main();
  247. var
  248.     Length, Source: Integer;
  249.     Arr: DynamicArray;
  250. begin
  251.     Writeln('The program sorts the array using the simple insertion method.');
  252.     Source := ChooseSource();
  253.     Arr := InputData(Source);
  254.     Writeln('Source array:');
  255.     PrintArray(Arr);
  256.     DoInsertionSort(Arr);
  257.     Writeln('Final array:');
  258.     PrintArray(Arr);
  259.     OutputData(Source, Arr);
  260.     Readln;
  261. end;
  262. begin
  263.     Main;
  264. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement