Advertisement
Vladislav8653

laba_2_4_delphi

Nov 17th, 2022 (edited)
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 7.56 KB | None | 0 0
  1. Program laba24;
  2. Uses
  3.   System.SysUtils;
  4. Type
  5.     TMatrix = Array of Array of Integer;
  6.     TVector = Array of Integer;
  7.  
  8. Function Choose() : ShortInt;
  9. Var
  10.     Choice : Integer;
  11.     IsCorrect : Boolean;
  12. Const
  13.     MIN = 0;
  14.     MAX = 1;
  15. Begin
  16.     Repeat
  17.         IsCorrect := True;
  18.         Try
  19.             Readln(Choice);
  20.         Except
  21.             Writeln('Please, enter a positive integer number:');
  22.             IsCorrect := False;
  23.         End;
  24.        If (Not(IsCorrect) Or Not((Choice = MIN) Or (Choice = MAX))) Then
  25.         Begin
  26.             Writeln('Please, choose.');
  27.             Writeln('1 is console input/output, 0 is file input/output.');
  28.             IsCorrect := False;
  29.         End;
  30.     Until IsCorrect;
  31.     Choose := Choice;
  32. End;
  33. Function InputData() : Integer;
  34. Var
  35.     IsCorrect : Boolean;
  36.     N : Integer;
  37.  
  38. Begin
  39.     Repeat
  40.         IsCorrect := True;
  41.         Try
  42.             Readln(N)
  43.         Except
  44.             Writeln('Please, enter a integer number:');
  45.             IsCorrect := False;
  46.         End;
  47.     Until(IsCorrect);
  48.     InputData := N;
  49. End;
  50.  
  51. Function InputPositiveNumber() : Integer;
  52. Var
  53.     Npos : Integer;
  54.     IsCorrect : Boolean;
  55. Const
  56.     MIN_NUM = 1;
  57. Begin
  58.     Repeat
  59.         IsCorrect := True;
  60.         Npos := InputData();
  61.         If (Npos < MIN_NUM) Then
  62.         Begin
  63.             Writeln('Please, enter a positive integer number:');
  64.             IsCorrect := False
  65.         End;
  66.     Until IsCorrect;
  67.     InputPositiveNumber := Npos;
  68. End;
  69.  
  70. Function InputArraySize() : ShortInt;
  71. Var
  72.     Num : Integer;
  73.     IsCorrect : Boolean;
  74. Begin
  75.     Repeat
  76.         IsCorrect := True;
  77.         Num := InputPositiveNumber();
  78.         If (Num Mod 2 <> 0) Then
  79.         Begin
  80.             Writeln('Please, enter an even number:');
  81.             IsCorrect := False
  82.         End;
  83.     Until IsCorrect;
  84.     InputArraySize := Num;
  85. End;
  86.  
  87. Function InputArray(Num : Integer) : TMatrix;
  88. Var
  89.     I, J : ShortInt;
  90.     Arr : TMatrix;
  91. Begin
  92.     SetLength(Arr, Num, Num);
  93.     Dec(Num);
  94.     For I := 0 To Num Do
  95.         For J := 0 To Num Do
  96.             Arr[I,J] := InputData();
  97.     InputArray := Arr;
  98. End;
  99.  
  100. Function TransferMatrixVector (N, StartI, StartJ: Integer; Arr : TMatrix): TVector;
  101. Var
  102.     I, K, J, HalfN: ShortInt;
  103.     Quarter : TVector;
  104. Const
  105.     Min = 4;
  106. Begin
  107.     K := 0;
  108.     SetLength(Quarter,(N * N div Min));
  109.     HalfN := N div 2;
  110.     For I := StartI To StartI + HalfN - 1 Do
  111.         For J := StartJ To StartJ + HalfN - 1 Do
  112.             Begin
  113.                 Quarter[K] := Arr[I][J];
  114.                 Inc (K);
  115.             End;
  116.     TransferMatrixVector := Quarter;
  117. End;
  118.  
  119. Procedure OutputArray (Arr : TMatrix; Num : Integer);
  120. Var
  121.     I, J : ShortInt;
  122. Begin
  123.     Dec(Num);
  124.     For I := 0 To Num Do
  125.         Begin
  126.             For J := 0 To Num Do
  127.                 Write (Arr[I, J], ' ');
  128.             Writeln;
  129.         End;
  130. End;
  131.  
  132. //functions for files:
  133.  
  134. Function InputFilePath() : String;
  135. Var
  136.     IsCorrect : Boolean;
  137.     Path : String;
  138. Begin
  139.     Writeln('Input path to file: ');
  140.     Repeat
  141.         IsCorrect := True;
  142.         Readln(Path);
  143.         If(Not FileExists(Path)) Then
  144.         Begin
  145.             IsCorrect := False;
  146.             Writeln('"Wrong way to file. Input correct path.');
  147.         End
  148.         Else If (ExtractFileExt(Path) <> '.txt') Then
  149.         Begin
  150.             Writeln('Must have .txt');
  151.             IsCorrect := False;
  152.         End;
  153.     Until IsCorrect;
  154.     InputFilePath := Path;
  155. End;
  156.  
  157. Function InputSizeOfMatrixFromFile(Path : String) : Integer;
  158. Var
  159.     Num : Integer;
  160.     IsCorrect : Boolean;
  161.     InputFile : TextFile;
  162. Begin
  163.     AssignFile(InputFile, Path);
  164.     Reset(InputFile);
  165.     IsCorrect := True;
  166.     Try
  167.         Readln(InputFile, Num);
  168.     Except
  169.         IsCorrect := False;
  170.         Writeln('Mistake of reading size of matrix from file.');
  171.     End;
  172.     If (IsCorrect And (Num < 0)) Then
  173.         Writeln('Mistake of matrix elements.');
  174.     CloseFile(InputFile);
  175.     InputSizeOfMatrixFromFile := Num;
  176. End;
  177.  
  178. Function InputMatrixFile(Path : String; Num : Integer) : TMatrix;
  179. Var
  180.     I, J : ShortInt;
  181.     Arr : TMatrix;
  182.     IsCorrect : Boolean;
  183.     InputFile : TextFile;
  184. Begin
  185.     SetLength(Arr, Num, Num);
  186.     Dec(Num);
  187.     AssignFile(InputFile, Path);
  188.     Reset(InputFile);
  189.     Readln(InputFile);
  190.     Repeat
  191.         IsCorrect := True;
  192.         For I := 0 To Num Do
  193.         Begin
  194.             For J := 0 To Num Do
  195.             Begin
  196.                 Try
  197.                     Read(InputFile, Arr[I, J]);
  198.                 Except
  199.                     IsCorrect := False;
  200.                     Writeln('Mistake of reading from file.')
  201.                 End;
  202.             End;
  203.         End;
  204.     Until IsCorrect;
  205.     Close(InputFile);
  206.     InputMatrixFile := Arr;
  207. End;
  208.  
  209. Procedure FileOutput(Path : String; ArrOfMatrixElements : TMatrix; Num : Integer);
  210. Var
  211.     IsCorrect : Boolean;
  212.     OutputFile : TextFile;
  213.     I, J : ShortInt;
  214. Begin
  215.     AssignFile(OutputFile, Path);
  216.     Repeat
  217.         IsCorrect := True;
  218.         Try
  219.             Rewrite(OutputFile);
  220.         Except
  221.             IsCorrect := False;
  222.             Writeln('Mistake with writing in file. Input another path.');
  223.             Path := InputFilePath();
  224.         End;
  225.     Until IsCorrect;
  226.     Dec(Num);
  227.     For I := 0 To Num Do
  228.         Begin
  229.             For J := 0 To Num Do
  230.                 Write(OutputFile, ArrOfMatrixElements[I, J], ' ');
  231.             Writeln(OutputFile);
  232.         End;
  233.     CloseFile(OutputFile);
  234.     Write('Success!');
  235. End;
  236.  
  237. Var
  238.     Arr : TMatrix;
  239.     BufferFor1, BufferFor2, BufferFor3, BufferFor4 : TVector;
  240.     StartI, StartJ, Chose, K, I, J: ShortInt;
  241.     N : Integer;
  242.     Str : String;
  243. Begin
  244.     Writeln ('Enter 1 or 0.');
  245.     Writeln ('1 is console input, 0 is file input.');
  246.     Chose := Choose();
  247.     If Chose = 1 then
  248.         Begin
  249.             Writeln ('Input matrix size: ');
  250.             N := InputArraySize ();
  251.             Writeln ('Enter the numbers one by one: ');
  252.             Arr := InputArray (N);
  253.         End
  254.     Else
  255.         Begin
  256.             Str := InputFilePath ();
  257.             N := InputSizeOfMatrixfromFile(Str);
  258.             Arr := InputMatrixFile (Str, N);
  259.         End;
  260.     StartI := 0;
  261.     StartJ := StartI;
  262.     BufferFor1 := TransferMatrixVector (N, StartI, StartJ, Arr);
  263.     StartI := 0;
  264.     StartJ := N div 2;
  265.     BufferFor2 := TransferMatrixVector (N, StartI, StartJ, Arr);
  266.     StartI := N div 2;
  267.     StartJ := 0;
  268.     BufferFor3 := TransferMatrixVector (N, StartI, StartJ, Arr);
  269.     StartI := N div 2;
  270.     StartJ := StartI;
  271.     BufferFor4 := TransferMatrixVector (N, StartI, StartJ, Arr);
  272.     K := 0;
  273.     For I := 0 To N - 1  do
  274.         Begin
  275.             For J := 0 To N - 1 Do
  276.                 Begin
  277.                     If ((I < N div 2) and (J < N div 2)) then
  278.                         Arr[I,J] := BufferFor4[K];
  279.                     K := 0;
  280.                     If ((I < N div 2) and (J > N div 2 - 1)) then
  281.                         Arr[I,J] := BufferFor3[K];
  282.                     If ((I > N div 2 - 1) and (J < N div 2)) then
  283.                         Arr[I,J] := BufferFor1[K];
  284.                     If ((I > N div 2 - 1) and (J > N div 2 - 1)) then
  285.                         Arr[I,J] := BufferFor2[K];
  286.                     Inc (K);
  287.                 End;
  288.         End;
  289.  
  290.     Writeln ('Enter 1 or 0.');
  291.     Writeln ('1 is console output, 0 is file output.');
  292.     Chose := Choose ();
  293.     If (Chose = 1) then
  294.     OutputArray (Arr, N)
  295.     Else
  296.         Begin
  297.             Str := InputFilePath();
  298.             FileOutput(Str, Arr, N);
  299.         End;
  300.     Readln;
  301. End.
  302.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement