Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Program laba24;
- Uses
- System.SysUtils;
- Type
- TMatrix = Array of Array of Integer;
- TVector = Array of Integer;
- Function Choose() : ShortInt;
- Var
- Choice : Integer;
- IsCorrect : Boolean;
- Const
- MIN = 0;
- MAX = 1;
- Begin
- Repeat
- IsCorrect := True;
- Try
- Readln(Choice);
- Except
- Writeln('Please, enter a positive integer number:');
- IsCorrect := False;
- End;
- If (Not(IsCorrect) Or Not((Choice = MIN) Or (Choice = MAX))) Then
- Begin
- Writeln('Please, choose.');
- Writeln('1 is console input/output, 0 is file input/output.');
- IsCorrect := False;
- End;
- Until IsCorrect;
- Choose := Choice;
- End;
- Function InputData() : Integer;
- Var
- IsCorrect : Boolean;
- N : Integer;
- Begin
- Repeat
- IsCorrect := True;
- Try
- Readln(N)
- Except
- Writeln('Please, enter a integer number:');
- IsCorrect := False;
- End;
- Until(IsCorrect);
- InputData := N;
- End;
- Function InputPositiveNumber() : Integer;
- Var
- Npos : Integer;
- IsCorrect : Boolean;
- Const
- MIN_NUM = 1;
- Begin
- Repeat
- IsCorrect := True;
- Npos := InputData();
- If (Npos < MIN_NUM) Then
- Begin
- Writeln('Please, enter a positive integer number:');
- IsCorrect := False
- End;
- Until IsCorrect;
- InputPositiveNumber := Npos;
- End;
- Function InputArraySize() : ShortInt;
- Var
- Num : Integer;
- IsCorrect : Boolean;
- Begin
- Repeat
- IsCorrect := True;
- Num := InputPositiveNumber();
- If (Num Mod 2 <> 0) Then
- Begin
- Writeln('Please, enter an even number:');
- IsCorrect := False
- End;
- Until IsCorrect;
- InputArraySize := Num;
- End;
- Function InputArray(Num : Integer) : TMatrix;
- Var
- I, J : ShortInt;
- Arr : TMatrix;
- Begin
- SetLength(Arr, Num, Num);
- Dec(Num);
- For I := 0 To Num Do
- For J := 0 To Num Do
- Arr[I,J] := InputData();
- InputArray := Arr;
- End;
- Function TransferMatrixVector (N, StartI, StartJ: Integer; Arr : TMatrix): TVector;
- Var
- I, K, J, HalfN: ShortInt;
- Quarter : TVector;
- Const
- Min = 4;
- Begin
- K := 0;
- SetLength(Quarter,(N * N div Min));
- HalfN := N div 2;
- For I := StartI To StartI + HalfN - 1 Do
- For J := StartJ To StartJ + HalfN - 1 Do
- Begin
- Quarter[K] := Arr[I][J];
- Inc (K);
- End;
- TransferMatrixVector := Quarter;
- End;
- Procedure OutputArray (Arr : TMatrix; Num : Integer);
- Var
- I, J : ShortInt;
- Begin
- Dec(Num);
- For I := 0 To Num Do
- Begin
- For J := 0 To Num Do
- Write (Arr[I, J], ' ');
- Writeln;
- End;
- End;
- //functions for files:
- Function InputFilePath() : String;
- Var
- IsCorrect : Boolean;
- Path : String;
- Begin
- Writeln('Input path to file: ');
- Repeat
- IsCorrect := True;
- Readln(Path);
- If(Not FileExists(Path)) Then
- Begin
- IsCorrect := False;
- Writeln('"Wrong way to file. Input correct path.');
- End
- Else If (ExtractFileExt(Path) <> '.txt') Then
- Begin
- Writeln('Must have .txt');
- IsCorrect := False;
- End;
- Until IsCorrect;
- InputFilePath := Path;
- End;
- Function InputSizeOfMatrixFromFile(Path : String) : Integer;
- Var
- Num : Integer;
- IsCorrect : Boolean;
- InputFile : TextFile;
- Begin
- AssignFile(InputFile, Path);
- Reset(InputFile);
- IsCorrect := True;
- Try
- Readln(InputFile, Num);
- Except
- IsCorrect := False;
- Writeln('Mistake of reading size of matrix from file.');
- End;
- If (IsCorrect And (Num < 0)) Then
- Writeln('Mistake of matrix elements.');
- CloseFile(InputFile);
- InputSizeOfMatrixFromFile := Num;
- End;
- Function InputMatrixFile(Path : String; Num : Integer) : TMatrix;
- Var
- I, J : ShortInt;
- Arr : TMatrix;
- IsCorrect : Boolean;
- InputFile : TextFile;
- Begin
- SetLength(Arr, Num, Num);
- Dec(Num);
- AssignFile(InputFile, Path);
- Reset(InputFile);
- Readln(InputFile);
- Repeat
- IsCorrect := True;
- For I := 0 To Num Do
- Begin
- For J := 0 To Num Do
- Begin
- Try
- Read(InputFile, Arr[I, J]);
- Except
- IsCorrect := False;
- Writeln('Mistake of reading from file.')
- End;
- End;
- End;
- Until IsCorrect;
- Close(InputFile);
- InputMatrixFile := Arr;
- End;
- Procedure FileOutput(Path : String; ArrOfMatrixElements : TMatrix; Num : Integer);
- Var
- IsCorrect : Boolean;
- OutputFile : TextFile;
- I, J : ShortInt;
- Begin
- AssignFile(OutputFile, Path);
- Repeat
- IsCorrect := True;
- Try
- Rewrite(OutputFile);
- Except
- IsCorrect := False;
- Writeln('Mistake with writing in file. Input another path.');
- Path := InputFilePath();
- End;
- Until IsCorrect;
- Dec(Num);
- For I := 0 To Num Do
- Begin
- For J := 0 To Num Do
- Write(OutputFile, ArrOfMatrixElements[I, J], ' ');
- Writeln(OutputFile);
- End;
- CloseFile(OutputFile);
- Write('Success!');
- End;
- Var
- Arr : TMatrix;
- BufferFor1, BufferFor2, BufferFor3, BufferFor4 : TVector;
- StartI, StartJ, Chose, K, I, J: ShortInt;
- N : Integer;
- Str : String;
- Begin
- Writeln ('Enter 1 or 0.');
- Writeln ('1 is console input, 0 is file input.');
- Chose := Choose();
- If Chose = 1 then
- Begin
- Writeln ('Input matrix size: ');
- N := InputArraySize ();
- Writeln ('Enter the numbers one by one: ');
- Arr := InputArray (N);
- End
- Else
- Begin
- Str := InputFilePath ();
- N := InputSizeOfMatrixfromFile(Str);
- Arr := InputMatrixFile (Str, N);
- End;
- StartI := 0;
- StartJ := StartI;
- BufferFor1 := TransferMatrixVector (N, StartI, StartJ, Arr);
- StartI := 0;
- StartJ := N div 2;
- BufferFor2 := TransferMatrixVector (N, StartI, StartJ, Arr);
- StartI := N div 2;
- StartJ := 0;
- BufferFor3 := TransferMatrixVector (N, StartI, StartJ, Arr);
- StartI := N div 2;
- StartJ := StartI;
- BufferFor4 := TransferMatrixVector (N, StartI, StartJ, Arr);
- K := 0;
- For I := 0 To N - 1 do
- Begin
- For J := 0 To N - 1 Do
- Begin
- If ((I < N div 2) and (J < N div 2)) then
- Arr[I,J] := BufferFor4[K];
- K := 0;
- If ((I < N div 2) and (J > N div 2 - 1)) then
- Arr[I,J] := BufferFor3[K];
- If ((I > N div 2 - 1) and (J < N div 2)) then
- Arr[I,J] := BufferFor1[K];
- If ((I > N div 2 - 1) and (J > N div 2 - 1)) then
- Arr[I,J] := BufferFor2[K];
- Inc (K);
- End;
- End;
- Writeln ('Enter 1 or 0.');
- Writeln ('1 is console output, 0 is file output.');
- Chose := Choose ();
- If (Chose = 1) then
- OutputArray (Arr, N)
- Else
- Begin
- Str := InputFilePath();
- FileOutput(Str, Arr, N);
- End;
- Readln;
- End.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement