Advertisement
believe_me

Untitled

Apr 5th, 2022
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 5.58 KB | None | 0 0
  1. unit MagicBoxUnit;
  2.  
  3. interface
  4.  
  5. uses
  6.     System.SysUtils, Classes;
  7.  
  8. type
  9.     TSquare = array of array of integer;
  10.  
  11.     TSquareOperations = class
  12.     class procedure buildSquare(var StartSquare: TSquare; Size: integer); static;
  13.     class procedure fillLowerPartOfFirstSquare(var FirstLowerLatinSquare: TSquare); static;
  14.     class procedure fillUpperPartOfFirstSquare(var FirstLatinSquare, FirstLowerLatinSquare: TSquare); static;
  15.     class procedure receiveSecondLatinSquare(var FirstLatinSquare: TSquare; var SecondLatinSquare: TSquare); static;
  16.     class function receiveResultSquare(var FirstLatinSquare: TSquare; var SecondLatinSquare: TSquare; var MagicSquare: TSquare) : TSquare;static;
  17.     class procedure receiveMagicSquare(var MagicSquare, FirstLatinSquare,SecondLatinSquare,
  18.                                         FirstLowerLatinSquare: TSquare; var Sum: integer; Size: integer); static;
  19.     class function findSum(var MagicSquare: TSquare): integer; static;
  20.     class procedure saveSquare(var StartSquare: TSquare; PathToFile: string; SquareName: string); overload; static;
  21.     class procedure saveSquare(var StartSquare: TSquare; PathToFile: string; SquareName: string; Sum: integer); overload; static;
  22.     end;
  23.  
  24. implementation
  25.  
  26.  
  27.  
  28.  
  29. class procedure TSquareOperations.buildSquare(var StartSquare: TSquare; Size: integer);
  30.  
  31. var
  32.     i, j: integer;
  33.  
  34. begin
  35.     setLength(StartSquare, Size);
  36.  
  37.     for i := 0 to high(StartSquare) do
  38.         setLength(StartSquare[i], Size);
  39.     for i := 0 to high(StartSquare) do
  40.         for j := 0 to high(StartSquare) do
  41.             StartSquare[i][j] := 0;
  42. end;
  43.  
  44.  
  45. class procedure TSquareOperations.fillLowerPartOfFirstSquare(var FirstLowerLatinSquare: TSquare);
  46.  
  47. var
  48.     Size, i, j, EndNumber: integer;
  49.  
  50. begin
  51.     Size := length(FirstLowerLatinSquare);
  52.     EndNumber := Size - 1;
  53.  
  54.     for i := (Size div 2) to EndNumber do
  55.         for j := 0 to EndNumber do
  56.             if ((j mod 2) = 0) then
  57.                 FirstLowerLatinSquare[i][j] := Size - i - 1
  58.             else
  59.                 FirstLowerLatinSquare[i][j] := i;
  60. end;
  61.  
  62.  
  63. class procedure TSquareOperations.fillUpperPartOfFirstSquare(var FirstLatinSquare, FirstLowerLatinSquare: TSquare);
  64.  
  65. var
  66.     Size, i, j, EndNumber, Middle: integer;
  67.  
  68. begin
  69.     Size := length(FirstLatinSquare);
  70.     EndNumber := Size - 1;
  71.     Middle := Size div 2 - 1;
  72.  
  73.     for i := (Size div 2) to EndNumber do
  74.         for j := 0 to EndNumber do
  75.             FirstLatinSquare[i][j] := FirstLowerLatinSquare[i][j];
  76.  
  77.     for i := 0 to Middle do
  78.         for j := 0 to EndNumber do
  79.             FirstLatinSquare[i][j] := FirstLatinSquare[Size div 2 + i][Size - j - 1];
  80. end;
  81.  
  82.  
  83. class procedure TSquareOperations.receiveSecondLatinSquare(var FirstLatinSquare: TSquare; var SecondLatinSquare: TSquare);
  84.  
  85. var
  86.     Size, i, j, EndNumber: integer;
  87.  
  88. begin
  89.     Size := length(FirstLatinSquare);
  90.     EndNumber := Size - 1;
  91.  
  92.     for i := EndNumber downto 0 do
  93.         for j := 0 to EndNumber do
  94.             SecondLatinSquare[j][Size - i - 1] := FirstLatinSquare[i][j];
  95. end;
  96.  
  97.  
  98. class function TSquareOperations.receiveResultSquare(var FirstLatinSquare: TSquare; var SecondLatinSquare: TSquare; var MagicSquare: TSquare) : TSquare;
  99.  
  100. var
  101.     Size, i, j, EndNumber: integer;
  102.  
  103. begin
  104.     Size := length(FirstLatinSquare);
  105.     EndNumber := Size - 1;
  106.  
  107.     for i := 0 to EndNumber do
  108.         for j := 0 to EndNumber do
  109.             MagicSquare[i][j] := Size * FirstLatinSquare[i][j] + SecondLatinSquare[i][j] + 1;
  110.  
  111.     Result := MagicSquare;
  112. end;
  113.  
  114.  
  115. class function TSquareOperations.findSum(var MagicSquare: TSquare): integer;
  116.  
  117. var
  118.     Sum, Size, j, EndNumber: integer;
  119.  
  120. begin
  121.     Sum := 0;
  122.     Size := length(MagicSquare);
  123.     EndNumber := Size - 1;
  124.  
  125.     for j := 0 to EndNumber do
  126.         Sum := Sum + MagicSquare[0][j];
  127.  
  128.     Result := Sum;
  129. end;
  130.  
  131.  
  132. class procedure TSquareOperations.saveSquare(var StartSquare: TSquare; PathToFile: string; SquareName: string);
  133.  
  134. var
  135.     OutputFile: TextFile;
  136.     i, j, Size, EndNumber: integer;
  137.  
  138. begin
  139.     AssignFile(OutputFile, PathToFile);
  140.     Rewrite(OutputFile);
  141.     Size := length(StartSquare);
  142.     EndNumber := Size - 1;
  143.  
  144.     writeln(OutputFile, SquareName);
  145.     for i := 0 to EndNumber do
  146.     begin
  147.         for j := 0 to EndNumber do
  148.             write(OutputFile, StartSquare[i][j]:4);
  149.         writeln(OutputFile);
  150.     end;
  151.  
  152.     CloseFile(OutputFile);
  153. end;
  154.  
  155.  
  156. class procedure TSquareOperations.saveSquare(var StartSquare: TSquare; PathToFile: string; SquareName: string; Sum: integer);
  157. var
  158.     OutputFile: TextFile;
  159.     i, j, Size, EndNumber: integer;
  160.  
  161. begin
  162.     AssignFile(OutputFile, PathToFile);
  163.     Rewrite(OutputFile);
  164.     Size := length(StartSquare);
  165.     EndNumber := Size - 1;
  166.  
  167.     writeln(OutputFile, SquareName + ':');
  168.     for i := 0 to EndNumber do
  169.     begin
  170.         for j := 0 to EndNumber do
  171.             write(OutputFile, StartSquare[i][j]:4);
  172.         writeln(OutputFile);
  173.     end;
  174.     write(OutputFile, 'Сумма элементов на каждой линии: ', Sum);
  175.  
  176.     CloseFile(OutputFile);
  177. end;
  178.  
  179. class procedure TSquareOperations.receiveMagicSquare(var MagicSquare,
  180.          FirstLatinSquare, SecondLatinSquare, FirstLowerLatinSquare: TSquare; var Sum: integer; Size: integer);
  181.  
  182. begin
  183.     buildSquare(FirstLowerLatinSquare, Size);
  184.     buildSquare(FirstLatinSquare, Size);
  185.     buildSquare(SecondLatinSquare, Size);
  186.         buildSquare(MagicSquare, Size);
  187.  
  188.     fillLowerPartOfFirstSquare(FirstLowerLatinSquare);
  189.     fillUpperPartOfFirstSquare(FirstLatinSquare, FirstLowerLatinSquare);
  190.     receiveSecondLatinSquare(FirstLatinSquare, SecondLatinSquare);
  191.     receiveResultSquare(FirstLatinSquare, SecondLatinSquare, MagicSquare);
  192.  
  193.     Sum := findSum(MagicSquare);
  194. end;
  195.  
  196. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement