Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- unit MagicBoxUnit;
- interface
- uses
- System.SysUtils, Classes;
- type
- TSquare = array of array of integer;
- TSquareOperations = class
- class procedure buildSquare(var StartSquare: TSquare; Size: integer); static;
- class procedure fillLowerPartOfFirstSquare(var FirstLowerLatinSquare: TSquare); static;
- class procedure fillUpperPartOfFirstSquare(var FirstLatinSquare, FirstLowerLatinSquare: TSquare); static;
- class procedure receiveSecondLatinSquare(var FirstLatinSquare: TSquare; var SecondLatinSquare: TSquare); static;
- class function receiveResultSquare(var FirstLatinSquare: TSquare; var SecondLatinSquare: TSquare; var MagicSquare: TSquare) : TSquare;static;
- class procedure receiveMagicSquare(var MagicSquare, FirstLatinSquare,SecondLatinSquare,
- FirstLowerLatinSquare: TSquare; var Sum: integer; Size: integer); static;
- class function findSum(var MagicSquare: TSquare): integer; static;
- class procedure saveSquare(var StartSquare: TSquare; PathToFile: string; SquareName: string); overload; static;
- class procedure saveSquare(var StartSquare: TSquare; PathToFile: string; SquareName: string; Sum: integer); overload; static;
- end;
- implementation
- class procedure TSquareOperations.buildSquare(var StartSquare: TSquare; Size: integer);
- var
- i, j: integer;
- begin
- setLength(StartSquare, Size);
- for i := 0 to high(StartSquare) do
- setLength(StartSquare[i], Size);
- for i := 0 to high(StartSquare) do
- for j := 0 to high(StartSquare) do
- StartSquare[i][j] := 0;
- end;
- class procedure TSquareOperations.fillLowerPartOfFirstSquare(var FirstLowerLatinSquare: TSquare);
- var
- Size, i, j, EndNumber: integer;
- begin
- Size := length(FirstLowerLatinSquare);
- EndNumber := Size - 1;
- for i := (Size div 2) to EndNumber do
- for j := 0 to EndNumber do
- if ((j mod 2) = 0) then
- FirstLowerLatinSquare[i][j] := Size - i - 1
- else
- FirstLowerLatinSquare[i][j] := i;
- end;
- class procedure TSquareOperations.fillUpperPartOfFirstSquare(var FirstLatinSquare, FirstLowerLatinSquare: TSquare);
- var
- Size, i, j, EndNumber, Middle: integer;
- begin
- Size := length(FirstLatinSquare);
- EndNumber := Size - 1;
- Middle := Size div 2 - 1;
- for i := (Size div 2) to EndNumber do
- for j := 0 to EndNumber do
- FirstLatinSquare[i][j] := FirstLowerLatinSquare[i][j];
- for i := 0 to Middle do
- for j := 0 to EndNumber do
- FirstLatinSquare[i][j] := FirstLatinSquare[Size div 2 + i][Size - j - 1];
- end;
- class procedure TSquareOperations.receiveSecondLatinSquare(var FirstLatinSquare: TSquare; var SecondLatinSquare: TSquare);
- var
- Size, i, j, EndNumber: integer;
- begin
- Size := length(FirstLatinSquare);
- EndNumber := Size - 1;
- for i := EndNumber downto 0 do
- for j := 0 to EndNumber do
- SecondLatinSquare[j][Size - i - 1] := FirstLatinSquare[i][j];
- end;
- class function TSquareOperations.receiveResultSquare(var FirstLatinSquare: TSquare; var SecondLatinSquare: TSquare; var MagicSquare: TSquare) : TSquare;
- var
- Size, i, j, EndNumber: integer;
- begin
- Size := length(FirstLatinSquare);
- EndNumber := Size - 1;
- for i := 0 to EndNumber do
- for j := 0 to EndNumber do
- MagicSquare[i][j] := Size * FirstLatinSquare[i][j] + SecondLatinSquare[i][j] + 1;
- Result := MagicSquare;
- end;
- class function TSquareOperations.findSum(var MagicSquare: TSquare): integer;
- var
- Sum, Size, j, EndNumber: integer;
- begin
- Sum := 0;
- Size := length(MagicSquare);
- EndNumber := Size - 1;
- for j := 0 to EndNumber do
- Sum := Sum + MagicSquare[0][j];
- Result := Sum;
- end;
- class procedure TSquareOperations.saveSquare(var StartSquare: TSquare; PathToFile: string; SquareName: string);
- var
- OutputFile: TextFile;
- i, j, Size, EndNumber: integer;
- begin
- AssignFile(OutputFile, PathToFile);
- Rewrite(OutputFile);
- Size := length(StartSquare);
- EndNumber := Size - 1;
- writeln(OutputFile, SquareName);
- for i := 0 to EndNumber do
- begin
- for j := 0 to EndNumber do
- write(OutputFile, StartSquare[i][j]:4);
- writeln(OutputFile);
- end;
- CloseFile(OutputFile);
- end;
- class procedure TSquareOperations.saveSquare(var StartSquare: TSquare; PathToFile: string; SquareName: string; Sum: integer);
- var
- OutputFile: TextFile;
- i, j, Size, EndNumber: integer;
- begin
- AssignFile(OutputFile, PathToFile);
- Rewrite(OutputFile);
- Size := length(StartSquare);
- EndNumber := Size - 1;
- writeln(OutputFile, SquareName + ':');
- for i := 0 to EndNumber do
- begin
- for j := 0 to EndNumber do
- write(OutputFile, StartSquare[i][j]:4);
- writeln(OutputFile);
- end;
- write(OutputFile, 'Сумма элементов на каждой линии: ', Sum);
- CloseFile(OutputFile);
- end;
- class procedure TSquareOperations.receiveMagicSquare(var MagicSquare,
- FirstLatinSquare, SecondLatinSquare, FirstLowerLatinSquare: TSquare; var Sum: integer; Size: integer);
- begin
- buildSquare(FirstLowerLatinSquare, Size);
- buildSquare(FirstLatinSquare, Size);
- buildSquare(SecondLatinSquare, Size);
- buildSquare(MagicSquare, Size);
- fillLowerPartOfFirstSquare(FirstLowerLatinSquare);
- fillUpperPartOfFirstSquare(FirstLatinSquare, FirstLowerLatinSquare);
- receiveSecondLatinSquare(FirstLatinSquare, SecondLatinSquare);
- receiveResultSquare(FirstLatinSquare, SecondLatinSquare, MagicSquare);
- Sum := findSum(MagicSquare);
- end;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement