Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Program Lab2task4;
- {$APPTYPE CONSOLE}
- uses
- SysUtils;
- Type
- MyArray = Array of Integer;
- Function ChoiceCheck() : Integer;
- Var
- Choice: Integer;
- IsCorrect: Boolean;
- Begin
- Repeat
- IsCorrect:=True;
- Try
- Readln(Choice);
- Except
- Writeln('Error! Input a number');
- IsCorrect := False;
- End;
- If (IsCorrect And ((Choice < 1) Or (Choice > 2))) Then
- Begin
- Writeln('Error! Input 1 or 2');
- IsCorrect := False;
- End;
- Until (IsCorrect);
- ChoiceCheck := Choice;
- End;
- Function InputCheck(): Integer;
- Var
- IsCorrect: Boolean;
- N: Integer;
- Begin
- Writeln('Input the number of elements');
- Repeat
- IsCorrect:=True;
- Try
- Readln(N);
- Except
- Writeln('Error! Input a number');
- IsCorrect:=False;
- End;
- If (IsCorrect And(N<2)) Then
- Begin
- IsCorrect:=False;
- Writeln('Error! Input a number greater than 1');
- End;
- Until (IsCorrect);
- InputCheck:=N;
- End;
- Function InputCheckArray(Var N: Integer; Arr: MyArray): MyArray;
- Var
- I: Integer;
- IsCorrect: Boolean;
- Begin
- For I:=0 To N Do
- Repeat
- IsCorrect:=True;
- Writeln('Input an element');
- Try
- Readln(Arr[I]);
- Except
- Writeln('Error! Input a number');
- IsCorrect:=False;
- End;
- Until (IsCorrect);
- InputCheckArray := Arr;
- End;
- Function CheckInputFilePath(): String;
- Var
- Path: String;
- IsCorrect: Boolean;
- InputFile: File;
- Begin
- Repeat
- Writeln('Input path to the file');
- IsCorrect := True;
- Readln(Path);
- If (Not(FileExists(Path))) Then
- Begin
- IsCorrect := False;
- Writeln('Could not find the file');
- End;
- If (IsCorrect) Then
- Begin
- Try
- AssignFile(InputFile, Path);
- ReSet(InputFile);
- Except
- IsCorrect := False;
- Writeln('Could not open the file');
- End;
- End;
- Until (IsCorrect);
- CloseFile(InputFile);
- CheckInputFilePath := Path;
- End;
- Function FileCheckArray() : MyArray;
- Var
- Arr: MyArray;
- InputFile: TextFile;
- I, N: Integer;
- Path: String;
- IsCorrect: Boolean;
- Begin
- Repeat
- Path := CheckInputFilePath();
- IsCorrect := True;
- AssignFile(InputFile, Path);
- Try
- Reset(InputFile);
- Except
- Writeln('Could not open the file');
- IsCorrect := False;
- End;
- Try
- Readln(InputFile, N);
- Except
- IsCorrect := False;
- Writeln ('The data is incorrect');
- End;
- If (N < 2) Then
- Begin
- IsCorrect := False;
- Writeln ('The data is incorrect');
- End;
- If (IsCorrect) Then
- Begin
- SetLength(Arr, N);
- Dec(N);
- For I := 0 To N Do
- Try
- Read(InputFile, Arr[I]);
- Except
- IsCorrect := False;
- Writeln('The data is incorrect');
- End;
- If (Not(EoF(InputFile))) Then
- Begin
- IsCorrect := False;
- Writeln ('The data is incorrect');
- End;
- End;
- Writeln(High(Arr));
- Until (IsCorrect);
- Close(InputFile);
- FileCheckArray := Arr;
- End;
- Function InputChoice (Var N: Integer; Arr: MyArray): MyArray;
- Var
- Choice: Integer;
- Begin
- Writeln('Choose input option: ', #10, '1.Input from console', #10,
- '2.Input from file');
- Choice := ChoiceCheck();
- If (Choice = 1) Then
- Begin
- N := InputCheck();
- SetLength(Arr, N);
- Dec(N);
- Arr := InputCheckArray(N, Arr);
- End
- Else
- Arr := FileCheckArray();
- InputChoice := Arr;
- End;
- Procedure Sum(N: Integer; Arr, SumArr: MyArray);
- Var
- I: Integer;
- Begin
- Dec(N);
- For I:=0 to N Do
- SumArr[I]:=Arr[I]+Arr[I+1];
- End;
- Function FindMax(SumArr: MyArray): Integer;
- Var
- I, Max: Integer;
- Begin
- Max:=SumArr[0];
- For I:=0 To High(SumArr) Do
- If (SumArr[I]>Max) Then
- Max:=SumArr[I];
- FindMax:=Max;
- End;
- Function CheckOutputFilePath(): String;
- Var
- Path: String;
- IsCorrect: Boolean;
- OutputFile: File;
- Begin
- 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');
- IsCorrect := True;
- Readln(Path);
- If (Not(FileExists(Path))) Then
- Begin
- IsCorrect := False;
- Writeln('Could not find the file');
- End;
- If (IsCorrect)Then
- Try
- AssignFile(OutputFile, Path);
- ReSet(OutputFile);
- Except
- IsCorrect := False;
- Writeln('Could not open the file');
- End;
- If (Not(IsCorrect)) Then
- Begin
- Writeln ('File will be created in the root folder of the program');
- Path := 'Result.txt';
- End
- Else
- CloseFile(OutputFile);
- CheckOutputFilePath := Path;
- End;
- Procedure OutputMax(Max: Integer);
- Begin
- Writeln(Max);
- End;
- Procedure OutputFile (Var Max: Integer);
- Var
- Path: String;
- OutputFile: TextFile;
- Begin
- Path := CheckOutputFilePath();
- AssignFile (OutputFile, Path);
- ReWrite(OutputFile);
- Writeln(OutputFile, Max);
- Close(OutputFile);
- End;
- Procedure OutputChoice (Var Max: Integer);
- Var
- Choice: Integer;
- Begin
- Writeln('Choose output option: ', #10, '1.Output through console', #10,
- '2.Output through file');
- Choice := ChoiceCheck();
- If (Choice = 1) Then
- OutputMax (Max)
- Else
- OutputFile(Max);
- End;
- Var
- Arr, SumArr: MyArray;
- N, Max: Integer;
- Begin
- N := 0;
- Arr := InputChoice (N, Arr);
- N := High(Arr);
- SetLength(SumArr, N);
- Sum(N, Arr, SumArr);
- Max:=FindMax(SumArr);
- OutputChoice(Max);
- Readln
- End.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement