Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program Laba2_4;
- uses
- System.SysUtils;
- Const
- MAX_SIZE = 6;
- MIN_SIZE = 2;
- type
- TMatrix = Array of Array of Integer;
- function ChooseIO(Text: String): Char;
- var
- Choose: Char;
- IsCorrect: Boolean;
- begin
- WriteLn('Выберите способ ' + text + ':' + #13#10 + 'f - file' + #13#10 + 'c - console');
- repeat
- IsCorrect := True;
- ReadLn(Choose);
- if (Choose <> 'f') and (Choose <> 'c') then
- begin
- WriteLn('Введите корректные данные!');
- IsCorrect := False;
- end
- until (IsCorrect);
- ChooseIO := Choose;
- end;
- function takePath(): String;
- var
- Path: String;
- IsCorrect: Boolean;
- begin
- repeat
- Writeln('Введите путь к файлу');
- IsCorrect := True;
- Readln(Path);
- if (Path[Length(Path) - 3] <> '.') or (Path[Length(Path) - 2]
- <> 't') or (Path[Length(Path) - 1] <> 'x') or
- (Path[Length(Path)] <> 't') then
- begin
- Writeln('Введите путь с расширением .txt');
- IsCorrect := False;
- end;
- until(IsCorrect);
- TakePath := Path;
- end;
- function FileCheck(Path: String): Boolean;
- var
- IsCorrect: Boolean;
- InputFile: TextFile;
- Number: Integer;
- begin
- IsCorrect := False;
- AssignFile(InputFile, Path);
- Reset(InputFile);
- if Not FileExists(path) then
- IsCorrect := True;
- if(Not IsCorrect) then
- begin
- While Not EOF(InputFile) do
- begin
- if Not IsCorrect then
- begin
- try
- Read(InputFile, Number);
- except
- IsCorrect := True;
- end;
- end;
- end;
- end;
- CloseFile(InputFile);
- if IsCorrect then
- FileCheck := False
- else
- FileCheck := True;
- end;
- function InputFile(): String;
- var
- Path: String;
- begin
- repeat
- Path := TakePath();
- until(FileCheck(Path));
- InputFile := Path;
- end;
- function InputMatrixSizeConsole(): Integer;
- var
- IsCorrect: Boolean;
- Size: Integer;
- begin
- WriteLn('Введите порядок матрицы');
- repeat
- IsCorrect := True;
- try
- ReadLn(Size);
- except
- WriteLn('Введите натуральное число!');
- IsCorrect := False;
- end;
- if (IsCorrect) and ((Size < MIN_SIZE) or (Size > MAX_SIZE)) then
- begin
- WriteLn('Введите число от 2 до 6!');
- IsCorrect := False;
- end;
- until(IsCorrect);
- InputMatrixSizeConsole := Size;
- end;
- function InputMatrixElemConsole(): Integer;
- var
- Element: Integer;
- IsCorrect: Boolean;
- begin
- repeat
- IsCorrect := True;
- try
- ReadLn(Element);
- except
- WriteLn('Введите число!');
- IsCorrect := False;
- end;
- until(IsCorrect);
- InputMatrixElemConsole := Element;
- end;
- function InputMatrixConsole(PrimarySize: Integer): TMatrix;
- var
- PrimaryMatrix: TMatrix;
- I, J: Integer;
- begin
- WriteLn('Введите элементы матрицы');
- SetLength(PrimaryMatrix, PrimarySize, PrimarySize);
- for I := 0 to PrimarySize - 1 do
- begin
- for J := 0 to PrimarySize - 1 do
- begin
- PrimaryMatrix[I, J] := InputMatrixElemConsole();
- end;
- end;
- InputMatrixConsole := PrimaryMatrix;
- end;
- function InputMatrixSizeFile(Path: String): Integer;
- var
- InputFile: TextFile;
- MatrixSize: Integer;
- begin
- AssignFile(InputFile, Path);
- Reset(InputFile);
- Read(InputFile, MatrixSize);
- InputMatrixSizeFile := MatrixSize;
- end;
- function InputMatrixFile(Path: String; MatrixSize: Integer): TMatrix;
- var
- InputFile: TextFile;
- Matrix: TMatrix;
- I, J: Integer;
- begin
- AssignFile(InputFile, Path);
- Reset(InputFile);
- Readln(InputFile);
- SetLength(Matrix, MatrixSize, MatrixSize);
- for I := 0 to High(Matrix) do
- begin
- for J := 0 to High(Matrix) do
- begin
- Read(InputFile, Matrix[I][J]);
- end;
- end;
- CloseFile(InputFile);
- InputMatrixFile := Matrix;
- end;
- function TakeFinalMatrix(MatrixSize: Integer; Matrix: TMatrix): TMatrix;
- var
- Size, Start, I, J, A, B, PrimarySize: Integer;
- IsCorrect: Boolean;
- begin
- PrimarySize := MatrixSize;
- SetLength(Matrix, MatrixSize, PrimarySize);
- PrimarySize := PrimarySize - 1;
- repeat
- IsCorrect := True;
- for I:= matrixsize - 1 downto 0 do
- begin
- for J := Primarysize downto 0 do
- begin
- Size := MatrixSize - 1;
- if Matrix[I, J] = 0 then
- begin
- Start := I;
- IsCorrect := False;
- for A := Start to Size-1 do
- begin
- for B := 0 to PrimarySize do
- begin
- Matrix[A, B] := Matrix[A + 1, B]
- end;
- end;
- Dec(MatrixSize);
- end;
- end;
- end;
- until(IsCorrect);
- TakeFinalMatrix := Matrix;
- end;
- function TakeFinalMatrixSize(MatrixSize: Integer; Matrix: TMatrix): Integer;
- var
- Size, Start, I, J, A, B, PrimarySize: Integer;
- IsCorrect: Boolean;
- begin
- PrimarySize := MatrixSize;
- SetLength(Matrix, MatrixSize, PrimarySize);
- PrimarySize := PrimarySize - 1;
- repeat
- IsCorrect := True;
- for I:= matrixsize - 1 downto 0 do
- begin
- for J := primarysize downto 0 do
- begin
- Size := MatrixSize - 1;
- if Matrix[I, J] = 0 then
- begin
- Start := I;
- IsCorrect := False;
- for A := Start to Size-1 do
- begin
- for B := 0 to PrimarySize do
- begin
- Matrix[A, B] := Matrix[A + 1, B]
- end;
- end;
- Dec(MatrixSize);
- end;
- end;
- end;
- until(IsCorrect);
- TakeFinalMatrixSize := MatrixSize;
- end;
- procedure OutputMatrixConsole(FinalMatrix: TMatrix; FinalSize, PrimarySize: Integer);
- var
- I, J: Integer;
- begin
- SetLength(FinalMatrix, FinalSize, PrimarySize);
- if (FinalSize <= 0) then
- WriteLn('Строк не осталось')
- else
- begin
- WriteLn('Полученная матрица:');
- FinalSize := FinalSize - 1;
- PrimarySize := PrimarySize - 1;
- for I := 0 to FinalSize do
- begin
- for J := 0 to PrimarySize do
- begin
- Write (FinalMatrix[i,j], ' ');
- end;
- Writeln;
- end;
- end;
- end;
- procedure OutputMatrixFile(Matrix: TMatrix; FinalSize, PrimarySize: Integer; Path: String);
- var
- OutputFile: TextFile;
- I, J: Integer;
- begin
- AssignFile(OutputFile, Path);
- Rewrite(OutputFile);
- Dec(FinalSize);
- Dec(PrimarySize);
- for I := 0 to FinalSize do
- begin
- for J := 0 to PrimarySize do
- Write(OutputFile, Matrix[I][J], ' ');
- Writeln(OutputFile);
- end;
- WriteLn('Матрица записана');
- CloseFile(OutputFile);
- end;
- var
- PrimaryMatrix, FinalMatrix: TMatrix;
- PrimarySize, FinalSize: Integer;
- Choice: Char;
- Path: String;
- begin
- WriteLn('Программа удаляет строки в матрице, содержащие нулевые элементы');
- Choice := ChooseIO('ввода матрицы');
- if Choice = 'c' then
- begin
- PrimarySize := InputMatrixSizeConsole();
- PrimaryMatrix := InputMatrixConsole(PrimarySize);
- end
- else
- begin
- Path := InputFile();
- PrimarySize := InputMatrixSizeFile(Path);
- PrimaryMatrix := InputMatrixFile(Path, PrimarySize);
- end;
- FinalSize := TakeFinalMatrixSize(PrimarySize, PrimaryMatrix);
- FinalMatrix := TakeFinalMatrix(PrimarySize, PrimaryMatrix);
- Choice := ChooseIO('вывода полученной матрицы');
- if Choice = 'c' then
- begin
- OutputMatrixConsole(FinalMatrix, FinalSize, PrimarySize);
- end
- else
- begin
- Path := TakePath;
- OutputMatrixFile(FinalMatrix, FinalSize, PrimarySize, Path);
- end;
- ReadLn;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement