Advertisement
lithie_oce

lab 2.4 delphi

Oct 31st, 2023
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 6.24 KB | Source Code | 0 0
  1. Program Lab2task4;
  2.  
  3. {$APPTYPE CONSOLE}
  4.  
  5. uses
  6.   SysUtils;
  7.  
  8. Type
  9.     MyArray = Array of Integer;
  10.  
  11. Function ChoiceCheck() : Integer;
  12. Var
  13.     Choice: Integer;
  14.     IsCorrect: Boolean;
  15. Begin
  16.     Repeat
  17.         IsCorrect:=True;
  18.         Try
  19.             Readln(Choice);
  20.         Except
  21.             Writeln('Error! Input a number');
  22.             IsCorrect := False;
  23.         End;
  24.         If (IsCorrect And ((Choice < 1) Or (Choice > 2))) Then
  25.         Begin
  26.             Writeln('Error! Input 1 or 2');
  27.             IsCorrect := False;
  28.         End;
  29.     Until  (IsCorrect);
  30.     ChoiceCheck := Choice;
  31. End;
  32.  
  33. Function InputCheck(): Integer;
  34. Var
  35.     IsCorrect: Boolean;
  36.     N: Integer;
  37. Begin
  38.     Writeln('Input the number of elements');
  39.     Repeat
  40.         IsCorrect:=True;
  41.         Try
  42.             Readln(N);
  43.         Except
  44.             Writeln('Error! Input a number');
  45.             IsCorrect:=False;
  46.         End;
  47.         If (IsCorrect And(N<2)) Then
  48.         Begin
  49.             IsCorrect:=False;
  50.             Writeln('Error! Input a number greater than 1');
  51.         End;
  52.     Until  (IsCorrect);
  53.     InputCheck:=N;
  54. End;
  55.  
  56. Function InputCheckArray(Var N: Integer; Arr: MyArray): MyArray;
  57. Var
  58.     I: Integer;
  59.     IsCorrect: Boolean;
  60. Begin
  61.     For I:=0 To N Do
  62.     Repeat
  63.         IsCorrect:=True;
  64.         Writeln('Input an element');
  65.         Try
  66.             Readln(Arr[I]);
  67.         Except
  68.             Writeln('Error! Input a number');
  69.             IsCorrect:=False;
  70.         End;
  71.     Until  (IsCorrect);
  72.     InputCheckArray := Arr;
  73. End;
  74.  
  75. Function CheckInputFilePath(): String;
  76. Var
  77.     Path: String;
  78.     IsCorrect: Boolean;
  79.     InputFile: File;
  80. Begin
  81.     Repeat
  82.         Writeln('Input path to the file');
  83.         IsCorrect := True;
  84.         Readln(Path);
  85.         If (Not(FileExists(Path))) Then
  86.         Begin
  87.             IsCorrect := False;
  88.             Writeln('Could not find the file');
  89.         End;
  90.         If (IsCorrect) Then
  91.         Begin
  92.             Try
  93.                 AssignFile(InputFile, Path);
  94.                 ReSet(InputFile);
  95.             Except
  96.                 IsCorrect := False;
  97.                 Writeln('Could not open the file');
  98.             End;
  99.         End;
  100.     Until (IsCorrect);
  101.     CloseFile(InputFile);
  102.     CheckInputFilePath := Path;
  103. End;
  104.  
  105. Function FileCheckArray() : MyArray;
  106. Var
  107.     Arr: MyArray;
  108.     InputFile: TextFile;
  109.     I, N: Integer;
  110.  
  111.     Path: String;
  112.     IsCorrect: Boolean;
  113. Begin
  114.     Repeat
  115.         Path := CheckInputFilePath();
  116.         IsCorrect := True;
  117.         AssignFile(InputFile, Path);
  118.         Try
  119.             Reset(InputFile);
  120.         Except
  121.             Writeln('Could not open the file');
  122.             IsCorrect := False;
  123.         End;
  124.             Try
  125.             Readln(InputFile, N);
  126.         Except
  127.             IsCorrect := False;
  128.             Writeln ('The data is incorrect');
  129.         End;
  130.         If (N < 2) Then
  131.         Begin
  132.             IsCorrect := False;
  133.             Writeln ('The data is incorrect');
  134.         End;
  135.         If (IsCorrect) Then
  136.         Begin
  137.             SetLength(Arr, N);
  138.             Dec(N);
  139.             For I := 0 To N Do
  140.                 Try
  141.                     Read(InputFile, Arr[I]);
  142.                 Except
  143.                     IsCorrect := False;
  144.                     Writeln('The data is incorrect');
  145.                 End;
  146.             If (Not(EoF(InputFile))) Then
  147.             Begin
  148.                 IsCorrect := False;
  149.                 Writeln ('The data is incorrect');
  150.             End;
  151.         End;
  152.         Writeln(High(Arr));
  153.     Until (IsCorrect);
  154.     Close(InputFile);
  155.     FileCheckArray := Arr;
  156. End;
  157.  
  158. Function InputChoice (Var N: Integer; Arr: MyArray): MyArray;        
  159. Var
  160.     Choice: Integer;
  161. Begin
  162.     Writeln('Choose input option: ', #10, '1.Input from console', #10,
  163.             '2.Input from file');
  164.     Choice := ChoiceCheck();
  165.     If (Choice = 1)  Then
  166.     Begin
  167.         N := InputCheck();
  168.         SetLength(Arr, N);
  169.         Dec(N);
  170.         Arr := InputCheckArray(N, Arr);
  171.     End
  172.     Else
  173.         Arr := FileCheckArray();
  174.     InputChoice := Arr;
  175. End;
  176.  
  177. Procedure Sum(N: Integer; Arr, SumArr: MyArray);
  178. Var
  179.     I: Integer;
  180. Begin
  181.     Dec(N);
  182.     For I:=0 to N Do
  183.     SumArr[I]:=Arr[I]+Arr[I+1];
  184. End;
  185.  
  186. Function FindMax(SumArr: MyArray): Integer;
  187. Var
  188.     I, Max: Integer;
  189. Begin
  190.     Max:=SumArr[0];
  191.     For I:=0 To High(SumArr) Do
  192.     If (SumArr[I]>Max) Then
  193.     Max:=SumArr[I];
  194.     FindMax:=Max;
  195. End;
  196.  
  197. Function CheckOutputFilePath(): String;
  198. Var
  199.     Path: String;
  200.     IsCorrect: Boolean;
  201.     OutputFile: File;
  202. Begin
  203.     Writeln('Input file path and the name of the file for', #10, 'example, Ñ:\Projects\Number\FileName.txt. If the ', #10, 'file does not exist, then it will be created', #10,'automatically in the root folder of the program');
  204.     IsCorrect := True;
  205.     Readln(Path);
  206.     If (Not(FileExists(Path))) Then
  207.     Begin
  208.         IsCorrect := False;
  209.         Writeln('Could not find the file');
  210.     End;
  211.     If (IsCorrect)Then
  212.     Try
  213.         AssignFile(OutputFile, Path);
  214.         ReSet(OutputFile);
  215.     Except
  216.         IsCorrect := False;
  217.         Writeln('Could not open the file');
  218.     End;
  219.     If (Not(IsCorrect)) Then
  220.     Begin
  221.     Writeln ('File will be created in the root folder of the program');
  222.     Path := 'Result.txt';
  223.     End
  224.     Else
  225.     CloseFile(OutputFile);
  226.     CheckOutputFilePath := Path;
  227. End;
  228.  
  229. Procedure OutputMax(Max: Integer);
  230. Begin
  231.     Writeln(Max);
  232. End;
  233.  
  234. Procedure OutputFile (Var Max: Integer);
  235. Var
  236.     Path: String;
  237.     OutputFile: TextFile;
  238. Begin
  239.     Path := CheckOutputFilePath();
  240.     AssignFile (OutputFile, Path);
  241.     ReWrite(OutputFile);
  242.     Writeln(OutputFile, Max);
  243.     Close(OutputFile);
  244. End;
  245.  
  246. Procedure OutputChoice (Var Max: Integer);
  247. Var
  248.     Choice: Integer;
  249. Begin
  250.     Writeln('Choose output option: ', #10, '1.Output through console', #10,
  251.               '2.Output through file');
  252.     Choice := ChoiceCheck();
  253.     If (Choice = 1) Then
  254.     OutputMax (Max)
  255.     Else
  256.     OutputFile(Max);
  257. End;
  258.  
  259. Var
  260.     Arr, SumArr: MyArray;
  261.     N, Max: Integer;
  262. Begin
  263.     N := 0;
  264.     Arr := InputChoice (N, Arr);
  265.     N := High(Arr);
  266.     SetLength(SumArr, N);
  267.     Sum(N, Arr, SumArr);
  268.     Max:=FindMax(SumArr);
  269.     OutputChoice(Max);
  270.     Readln
  271. End.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement