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;
- var
- MainMatrix: Matrix;
- function GetOutputDirectory(): String;
- var
- Ret: String;
- IsCorrect: Boolean;
- begin
- IsCorrect := false;
- repeat
- Writeln('Введите директорию, в которую хотите сохранить матрицу');
- Readln(Ret);
- if DirectoryExists(Ret) then
- IsCorrect := true
- else
- Writeln('Такой директории не существует.Попробуйте ещё раз');
- until IsCorrect;
- GetOutputDirectory := 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;
- procedure PrintMatrixToFile(MatrixToPrint: Matrix);
- var
- I, J: Integer;
- OutputFile: TextFile;
- Directory: String;
- begin
- Directory := GetOutputDirectory();
- AssignFile(OutputFile, Directory + '\output.txt');
- Rewrite(OutputFile);
- for I := 0 to High(MatrixToPrint) do
- begin
- for J := 0 to High(MatrixToPrint) do
- Write(OutputFile, MatrixToPrint[I, J]:6);
- Writeln(OutputFile);
- end;
- Writeln('Матрица сохранена по указанному пути');
- CloseFile(OutputFile);
- end;
- function GetInputType(): String;
- var
- Ret: string;
- IsCorrect: Boolean;
- begin
- IsCorrect := false;
- 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 IsFileCorrect(Path: String; Size: Integer): Boolean;
- var
- ISize, JSize, Num: Integer;
- IsCorrect: Boolean;
- MatrixFile: TextFile;
- begin
- ISize := 0;
- JSize := 0;
- IsCorrect := true;
- AssignFile(MatrixFile, Path);
- Reset(MatrixFile);
- while not(SeekEof(MatrixFile)) and IsCorrect do
- begin
- inc(ISize);
- while not(SeekEoln(MatrixFile)) and IsCorrect do
- begin
- try
- Read(MatrixFile, Num);
- except
- IsCorrect := false;
- end;
- inc(JSize);
- end;
- Readln(MatrixFile);
- if (JSize <> Size) then
- IsCorrect := false;
- JSize := 0;
- end;
- if ISize <> Size then
- IsCorrect := false;
- CloseFile(MatrixFile);
- IsFileCorrect := IsCorrect;
- end;
- 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;
- function GetMatrixFromConsole(Size: Integer): Matrix;
- var
- Ret: Matrix;
- I, J: Integer;
- begin
- SetLength(Ret, Size, Size);
- for I := 0 to High(Ret) do
- for J := 0 to High(Ret) do
- Ret[I, J] := GetMatrixItem(I, J);
- GetMatrixFromConsole := Ret;
- end;
- function GetMatrixFilePath(MatrixSize: Integer): String;
- var
- Path: String;
- IsCorrect: Boolean;
- begin
- repeat
- Writeln('Введите абсолютный путь к файлу ');
- Readln(Path);
- IsCorrect := false;
- if not(FileExists(Path)) then
- Writeln('Файл не найден')
- else
- begin
- if IsFileCorrect(Path, MatrixSize) then
- IsCorrect := true
- else
- Writeln('Данные в файле некорректны');
- end;
- until IsCorrect;
- GetMatrixFilePath := Path;
- end;
- function GetMatrixFromFile(Size: Integer): Matrix;
- var
- Ret: Matrix;
- I, J: Integer;
- FilePath: string;
- MatrixFile: TextFile;
- begin
- SetLength(Ret, Size, Size);
- FilePath := GetMatrixFilePath(Size);
- AssignFile(MatrixFile, FilePath);
- Reset(MatrixFile);
- for I := 0 to High(Ret) do
- begin
- for J := 0 to High(Ret) do
- Read(MatrixFile, Ret[I, J]);
- Readln(MatrixFile);
- end;
- CloseFile(MatrixFile);
- GetMatrixFromFile := Ret;
- end;
- function GetMatrix(): Matrix;
- var
- RetMatrix: Matrix;
- Size: Integer;
- InputType: string;
- begin
- Size := GetMatrixSize();
- InputType := GetInputType();
- if (InputType = 'Console') then
- RetMatrix := GetMatrixFromConsole(Size)
- else if (InputType = 'File') then
- RetMatrix := GetMatrixFromFile(Size);
- 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);
- end;
- begin
- MainMatrix := GetMatrix();
- PrintMatrix(MainMatrix);
- Swap(MainMatrix);
- Writeln;
- PrintMatrix(MainMatrix);
- PrintMatrixToFile(MainMatrix);
- Readln;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement