Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Program lab2task3;
- {$APPTYPE CONSOLE}
- Uses
- SysUtils;
- Type
- MyMatrix = Array of 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 N');
- Repeat
- IsCorrect := True;
- Try
- Readln(N);
- Except
- Writeln('Error! Input a number');
- IsCorrect := False;
- End;
- If (N < 1) Then
- Begin
- IsCorrect := False;
- Writeln('Error! Input a number greater than 0');
- End;
- Until (IsCorrect);
- InputCheck := N;
- End;
- Function InputCheckMatrix(N: Integer; Matr: MyMatrix): MyMatrix;
- Var
- I, J, N1: Integer;
- IsCorrect: Boolean;
- Begin
- N1 := N-1;
- For I := 1 To N Do
- For J := 0 To N1 Do
- Repeat
- IsCorrect := True;
- Try
- Readln(Matr[I][J]);
- Except
- Writeln('Error! Input a number');
- IsCorrect := False;
- End;
- Until (IsCorrect);
- InputCheckMatrix := Matr;
- 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 FileCheckMatrix(Var N: Integer) : MyMatrix;
- Var
- Matr: MyMatrix;
- InputFile: TextFile;
- I, J: 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 < 1) Then
- Begin
- IsCorrect := False;
- Writeln ('The data is incorrect');
- End;
- If (IsCorrect) Then
- Begin
- SetLength(Matr, N+2, N);
- For I := 1 To N Do
- For J := 0 To High(Matr[0]) Do
- Try
- Read(InputFile, Matr[I][J]);
- Except
- IsCorrect := False;
- Writeln('The data is incorrect');
- End;
- If (Not(EoF(InputFile))) Then
- Begin
- IsCorrect := False;
- Writeln ('The data is incorrect');
- End;
- End;
- Until (IsCorrect);
- Close(InputFile);
- FileCheckMatrix := Matr;
- End;
- Function InputChoice (Var N: Integer; Matr: MyMatrix): MyMatrix;
- 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(Matr, N+2, N);
- Matr := InputCheckMatrix(N, Matr);
- End
- Else
- Begin
- Matr := FileCheckMatrix(N);
- End;
- InputChoice := Matr;
- End;
- Procedure Swap(Var N: Integer; Matr: MyMatrix);
- Var
- J, I, N1: Integer;
- HelpArray1, HelpArray2: Array of Integer;
- Begin
- SetLength(HelpArray1, N);
- SetLength(HelpArray2, N);
- For J := 0 to N Do
- Begin
- HelpArray1[J] := Matr[1][J];
- Matr[1][J] := 0;
- End;
- N1 := N+1;
- For I := 2 To N1 Do
- For J := 0 To N Do
- Begin
- HelpArray2[J] := Matr[I][J];
- Matr[I][J] := HelpArray1[J];
- HelpArray1[J] := HelpArray2[J];
- End;
- 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 OutputMatrix(Var N: Integer; Matr: MyMatrix);
- Var
- I, J, N1: Integer;
- Begin
- N1 := N+1;
- Dec(N);
- For I := 0 to N1 Do
- Begin
- For J := 0 to N Do
- Write(Matr[I][J], ' ');
- Writeln;
- End;
- End;
- Procedure OutputFile (Var N: Integer; Matr: MyMatrix);
- Var
- Path: String;
- N1, I, J: Integer;
- OutputFile: TextFile;
- Begin
- Path := CheckOutputFilePath();
- AssignFile (OutputFile, Path);
- ReWrite(OutputFile);
- N1 := N+1;
- Dec(N);
- For I := 0 to N1 Do
- Begin
- For J := 0 to N Do
- Write(OutputFile, Matr[I][J], ' ');
- Write(OutputFile, #10);
- End;
- Close(OutputFile);
- End;
- Procedure OutputChoice (Var N: Integer; Matr: MyMatrix);
- Var
- Choice: Integer;
- Begin
- Writeln('Choose output option: ', #10, '1.Output through console', #10,
- '2.Output through file');
- Choice := ChoiceCheck();
- If (Choice = 1) Then
- OutputMatrix(N, Matr)
- Else
- OutputFile(N, Matr);
- End;
- Var
- Matr: MyMatrix;
- N: Integer;
- Begin
- N := 0;
- Matr := InputChoice(N, Matr);
- Swap(N, Matr);
- OutputChoice(N, Matr);
- Readln
- End.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement