Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program Lab2_4;
- uses
- System.SysUtils;
- type
- Matrix = array of array of Integer;
- function GetMatrixItem(I, J: Integer): Integer;
- var
- Ret: Integer;
- IsCorrect: Boolean;
- begin
- repeat
- Writeln('Введите значение элемента матрицы [', I, ',', J, ']');
- IsCorrect := true;
- try
- Readln(Ret)
- except
- IsCorrect := false;
- Writeln('Значение матрицы должно быть числом')
- end;
- until IsCorrect;
- GetMatrixItem := Ret;
- end;
- function GetMatrixSize(): Integer;
- var
- Ret: Integer;
- IsCorrect: Boolean;
- begin
- Ret := 0;
- repeat
- Writeln('Введите размер матрицы ');
- IsCorrect := true;
- try
- Readln(Ret)
- except
- IsCorrect := false;
- Writeln('Размер матрицы должен быть числом')
- end;
- if ((((Ret < 2) or (Ret > 10000)) or (Ret mod 2 = 1)) and IsCorrect)
- then
- begin
- Writeln('Размер матрицы должен быть кратен двум и принадлежать промежутку от 2 до 10000');
- IsCorrect := false
- end;
- until IsCorrect;
- GetMatrixSize := Ret;
- end;
- procedure PrintMatrix(MatrixToPrint: Matrix);
- var
- I, J: Integer;
- begin
- for I := 0 to High(MatrixToPrint) do
- begin
- for J := 0 to High(MatrixToPrint) do
- Write(MatrixToPrint[I, J]:6);
- Writeln;
- end;
- end;
- function GetInputType(): String;
- var
- Ret: string;
- IsCorrect: Boolean;
- begin
- repeat
- Writeln('Выберите способ задания матрицы файл/консоль (ф/к)');
- Read(Ret);
- if (Ret = 'ф') or (Ret = 'Ф') then
- begin
- Ret := 'File';
- IsCorrect := true;
- end
- else if (Ret = 'к') or (Ret = 'К') then
- begin
- Ret := 'Console';
- IsCorrect := true;
- end;
- Readln;
- until IsCorrect;
- GetInputType := Ret;
- end;
- function GetMatrix(): Matrix;
- var
- RetMatrix: Matrix;
- I, J, Size: Integer;
- InputType: string;
- IsCorrect: Boolean;
- FilePath: string;
- MatrixFile: TextFile;
- begin
- Size := GetMatrixSize();
- SetLength(RetMatrix, Size, Size);
- InputType := GetInputType();
- dec(Size);
- if (InputType = 'Console') then
- for I := 0 to Size do
- for J := 0 to Size do
- RetMatrix[I, J] := GetMatrixItem(I, J)
- else if (InputType = 'File') then
- begin
- repeat
- IsCorrect := true;
- Writeln('Введите абсолютный путь к файлу ');
- Readln(FilePath);
- if not(FileExists(FilePath)) then
- begin
- Writeln('Файл не найден, попробуйте ещё раз');
- IsCorrect := false;
- end;
- until IsCorrect;
- AssignFile(MatrixFile, FilePath);
- Reset(MatrixFile);
- for I := 0 to Size do
- begin
- for J := 0 to Size do
- try
- Read(MatrixFile, RetMatrix[I, J]);
- except
- Writeln('Значения матрицы должны быть числами ')
- end;
- Readln(MatrixFile);
- end;
- end;
- Writeln('Исходная матрица ');
- PrintMatrix(RetMatrix);
- GetMatrix := RetMatrix;
- end;
- function GetMatrixPart(Matr: Matrix; I, J, Size: Integer): Matrix;
- var
- Ret: Matrix;
- indexI, indexJ: Integer;
- begin
- SetLength(Ret, Size, Size);
- for indexI := 0 to High(Ret) do
- for indexJ := 0 to High(Ret) do
- Ret[indexI, indexJ] := Matr[indexI + I, indexJ + J];
- GetMatrixPart := Ret;
- end;
- procedure InsertMatrix(MainMatriix, ToInsert: Matrix; I, J: Integer);
- var
- indexI, indexJ: Integer;
- begin
- for indexI := 0 to High(ToInsert) do
- for indexJ := 0 to High(ToInsert) do
- MainMatriix[indexI + I, indexJ + J] := ToInsert[indexI, indexJ];
- end;
- procedure Swap(ToSwap: Matrix);
- var
- Ret, Matrix1, Matrix2, Matrix3, Matrix4: Matrix;
- HalfSize: Integer;
- begin
- HalfSize := Length(ToSwap) div 2;
- Matrix1 := GetMatrixPart(ToSwap, 0, 0, HalfSize);
- Matrix2 := GetMatrixPart(ToSwap, 0, HalfSize, HalfSize);
- Matrix3 := GetMatrixPart(ToSwap, HalfSize, 0, HalfSize);
- Matrix4 := GetMatrixPart(ToSwap, HalfSize, HalfSize, HalfSize);
- InsertMatrix(ToSwap, Matrix1, HalfSize, HalfSize);
- InsertMatrix(ToSwap, Matrix2, HalfSize, 0);
- InsertMatrix(ToSwap, Matrix3, 0, 0);
- InsertMatrix(ToSwap, Matrix4, 0, HalfSize);
- Writeln('Матрица после смещения');
- PrintMatrix(ToSwap);
- end;
- begin
- Swap(GetMatrix());
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement