Advertisement
ryabov

delphi 2_4

Oct 24th, 2021
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 8.57 KB | None | 0 0
  1. program Laba2_4;
  2. uses
  3.   System.SysUtils;
  4.  
  5. Const
  6.     MAX_SIZE = 6;
  7.     MIN_SIZE = 2;
  8. type
  9.     TMatrix = Array of Array of Integer;
  10. function ChooseIO(Text: String): Char;
  11. var
  12.     Choose: Char;
  13.     IsCorrect: Boolean;
  14. begin
  15.     WriteLn('Выберите способ ' + text + ':' + #13#10 + 'f - file' + #13#10 + 'c - console');
  16.     repeat
  17.         IsCorrect := True;
  18.         ReadLn(Choose);
  19.         if (Choose <> 'f') and (Choose <> 'c') then
  20.         begin
  21.             WriteLn('Введите корректные данные!');
  22.             IsCorrect := False;
  23.         end
  24.     until (IsCorrect);
  25.     ChooseIO := Choose;
  26. end;
  27. function takePath(): String;
  28. var
  29.     Path: String;
  30.     IsCorrect: Boolean;
  31. begin
  32.     repeat
  33.         Writeln('Введите путь к файлу');
  34.         IsCorrect := True;
  35.         Readln(Path);
  36.         if (Path[Length(Path) - 3] <> '.') or (Path[Length(Path) - 2]
  37.         <> 't') or (Path[Length(Path) - 1] <> 'x') or
  38.         (Path[Length(Path)] <> 't') then
  39.         begin
  40.             Writeln('Введите путь с расширением .txt');
  41.             IsCorrect := False;
  42.         end;
  43.     until(IsCorrect);
  44.     TakePath := Path;
  45. end;
  46. function FileCheck(Path: String): Boolean;
  47. var
  48.     IsCorrect: Boolean;
  49.     InputFile: TextFile;
  50.     Number: Integer;
  51. begin
  52.     IsCorrect := False;
  53.     AssignFile(InputFile, Path);
  54.     Reset(InputFile);
  55.     if Not FileExists(path) then
  56.         IsCorrect := True;
  57.     if(Not IsCorrect) then
  58.     begin
  59.         While Not EOF(InputFile) do
  60.         begin
  61.             if Not IsCorrect then
  62.             begin
  63.                 try
  64.                     Read(InputFile, Number);
  65.                 except
  66.                     IsCorrect := True;
  67.                 end;
  68.             end;
  69.         end;
  70.     end;
  71.     CloseFile(InputFile);
  72.     if IsCorrect then
  73.         FileCheck := False
  74.     else
  75.         FileCheck := True;
  76. end;
  77. function InputFile(): String;
  78. var
  79.     Path: String;
  80. begin
  81.     repeat
  82.         Path := TakePath();
  83.     until(FileCheck(Path));
  84.     InputFile := Path;
  85. end;
  86. function InputMatrixSizeConsole(): Integer;
  87. var
  88.     IsCorrect: Boolean;
  89.     Size: Integer;
  90. begin
  91.     WriteLn('Введите порядок матрицы');
  92.     repeat
  93.         IsCorrect := True;
  94.         try
  95.             ReadLn(Size);
  96.         except
  97.             WriteLn('Введите натуральное число!');
  98.             IsCorrect := False;
  99.         end;
  100.         if (IsCorrect) and ((Size < MIN_SIZE) or (Size > MAX_SIZE)) then
  101.         begin
  102.             WriteLn('Введите число от 2 до 6!');
  103.             IsCorrect := False;
  104.         end;
  105.     until(IsCorrect);
  106.     InputMatrixSizeConsole := Size;
  107. end;
  108. function InputMatrixElemConsole(): Integer;
  109. var
  110.     Element: Integer;
  111.     IsCorrect: Boolean;
  112. begin
  113.     repeat
  114.         IsCorrect := True;
  115.         try
  116.             ReadLn(Element);
  117.         except
  118.             WriteLn('Введите число!');
  119.             IsCorrect := False;
  120.         end;
  121.     until(IsCorrect);
  122.     InputMatrixElemConsole := Element;
  123. end;
  124. function InputMatrixConsole(PrimarySize: Integer): TMatrix;
  125. var
  126.     PrimaryMatrix: TMatrix;
  127.     I, J: Integer;
  128. begin
  129.     WriteLn('Введите элементы матрицы');
  130.     SetLength(PrimaryMatrix, PrimarySize, PrimarySize);
  131.  
  132.     for I := 0 to PrimarySize - 1 do
  133.     begin
  134.         for J := 0 to PrimarySize - 1 do
  135.         begin
  136.         PrimaryMatrix[I, J] := InputMatrixElemConsole();
  137.         end;
  138.     end;
  139.     InputMatrixConsole := PrimaryMatrix;
  140. end;
  141. function InputMatrixSizeFile(Path: String): Integer;
  142. var
  143.     InputFile: TextFile;
  144.     MatrixSize: Integer;
  145. begin
  146.     AssignFile(InputFile, Path);
  147.     Reset(InputFile);
  148.     Read(InputFile, MatrixSize);
  149.     InputMatrixSizeFile := MatrixSize;
  150. end;
  151. function InputMatrixFile(Path: String; MatrixSize: Integer): TMatrix;
  152. var
  153.     InputFile: TextFile;
  154.     Matrix: TMatrix;
  155.     I, J: Integer;
  156. begin
  157.     AssignFile(InputFile, Path);
  158.     Reset(InputFile);
  159.     Readln(InputFile);
  160.     SetLength(Matrix, MatrixSize, MatrixSize);
  161.     for I := 0 to High(Matrix) do
  162.     begin
  163.         for J := 0 to High(Matrix) do
  164.         begin
  165.         Read(InputFile, Matrix[I][J]);
  166.         end;
  167.     end;
  168.     CloseFile(InputFile);
  169.     InputMatrixFile := Matrix;
  170. end;
  171. function TakeFinalMatrix(MatrixSize: Integer; Matrix: TMatrix): TMatrix;
  172. var
  173.     Size, Start, I, J, A, B, PrimarySize: Integer;
  174.     IsCorrect: Boolean;
  175. begin
  176.     PrimarySize := MatrixSize;
  177.     SetLength(Matrix, MatrixSize, PrimarySize);
  178.     PrimarySize := PrimarySize - 1;
  179.     repeat
  180.         IsCorrect := True;
  181.         for I:= matrixsize - 1 downto 0 do
  182.         begin
  183.             for J := Primarysize downto 0 do
  184.             begin
  185.                 Size := MatrixSize - 1;
  186.                 if Matrix[I, J] = 0 then
  187.                 begin
  188.                     Start := I;
  189.                     IsCorrect := False;
  190.                     for A := Start to Size-1 do
  191.                     begin
  192.                         for B := 0 to PrimarySize do
  193.                         begin
  194.                             Matrix[A, B] := Matrix[A + 1, B]
  195.                         end;
  196.                     end;
  197.                     Dec(MatrixSize);
  198.                 end;
  199.             end;
  200.         end;
  201.     until(IsCorrect);
  202.     TakeFinalMatrix := Matrix;
  203. end;
  204. function TakeFinalMatrixSize(MatrixSize: Integer; Matrix: TMatrix): Integer;
  205. var
  206.     Size, Start, I, J, A, B, PrimarySize: Integer;
  207.     IsCorrect: Boolean;
  208. begin
  209.     PrimarySize := MatrixSize;
  210.     SetLength(Matrix, MatrixSize, PrimarySize);
  211.     PrimarySize := PrimarySize - 1;
  212.     repeat
  213.         IsCorrect := True;
  214.         for I:= matrixsize - 1 downto 0 do
  215.         begin
  216.             for J := primarysize downto 0 do
  217.             begin
  218.                 Size := MatrixSize - 1;
  219.                 if Matrix[I, J] = 0 then
  220.                 begin
  221.                     Start := I;
  222.                     IsCorrect := False;
  223.                     for A := Start to Size-1 do
  224.                     begin
  225.                         for B := 0 to PrimarySize do
  226.                         begin
  227.                             Matrix[A, B] := Matrix[A + 1, B]
  228.                         end;
  229.                     end;
  230.                     Dec(MatrixSize);
  231.                 end;
  232.             end;
  233.         end;
  234.     until(IsCorrect);
  235.     TakeFinalMatrixSize := MatrixSize;
  236. end;
  237. procedure OutputMatrixConsole(FinalMatrix: TMatrix; FinalSize, PrimarySize: Integer);
  238. var
  239.     I, J: Integer;
  240. begin
  241.     SetLength(FinalMatrix, FinalSize, PrimarySize);
  242.     if (FinalSize <= 0) then
  243.         WriteLn('Строк не осталось')
  244.     else
  245.     begin
  246.         WriteLn('Полученная матрица:');
  247.         FinalSize := FinalSize - 1;
  248.         PrimarySize := PrimarySize - 1;
  249.         for I := 0 to FinalSize do
  250.         begin
  251.             for J := 0 to PrimarySize do
  252.             begin
  253.                 Write (FinalMatrix[i,j], '  ');
  254.             end;
  255.             Writeln;
  256.         end;
  257.     end;
  258. end;
  259. procedure OutputMatrixFile(Matrix: TMatrix; FinalSize, PrimarySize: Integer; Path: String);
  260. var
  261.     OutputFile: TextFile;
  262.     I, J: Integer;
  263. begin
  264.     AssignFile(OutputFile, Path);
  265.     Rewrite(OutputFile);
  266.     Dec(FinalSize);
  267.     Dec(PrimarySize);
  268.     for I := 0 to FinalSize do
  269.     begin
  270.         for J := 0 to PrimarySize do
  271.         Write(OutputFile, Matrix[I][J], ' ');
  272.     Writeln(OutputFile);
  273.     end;
  274.     WriteLn('Матрица записана');
  275.     CloseFile(OutputFile);
  276. end;
  277. var
  278.     PrimaryMatrix, FinalMatrix: TMatrix;
  279.     PrimarySize, FinalSize: Integer;
  280.     Choice: Char;
  281.     Path: String;
  282. begin
  283.     WriteLn('Программа удаляет строки в матрице, содержащие нулевые элементы');
  284.     Choice := ChooseIO('ввода матрицы');
  285.     if Choice = 'c' then
  286.     begin
  287.         PrimarySize := InputMatrixSizeConsole();
  288.         PrimaryMatrix := InputMatrixConsole(PrimarySize);
  289.     end
  290.     else
  291.     begin
  292.         Path := InputFile();
  293.         PrimarySize := InputMatrixSizeFile(Path);
  294.         PrimaryMatrix := InputMatrixFile(Path, PrimarySize);
  295.     end;
  296.     FinalSize := TakeFinalMatrixSize(PrimarySize, PrimaryMatrix);
  297.     FinalMatrix := TakeFinalMatrix(PrimarySize, PrimaryMatrix);
  298.     Choice := ChooseIO('вывода полученной матрицы');
  299.     if Choice = 'c' then
  300.     begin
  301.         OutputMatrixConsole(FinalMatrix, FinalSize, PrimarySize);
  302.     end
  303.     else
  304.     begin
  305.         Path := TakePath;
  306.         OutputMatrixFile(FinalMatrix, FinalSize, PrimarySize, Path);
  307.     end;
  308.     ReadLn;
  309. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement