Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program Project1;
- {$APPTYPE CONSOLE}
- {$R *.res}
- Uses
- System.SysUtils;
- Type
- TArrOI = Array Of Integer;
- TArrOAOI = Array Of TArrOI;
- Procedure PrintCondition();
- Begin
- Writeln('Goal: Input Matrix And add Rows into position 0 And n+1 with elements equal To 1 (use shift rows).');
- End;
- Procedure PrintMatrix(Matrix: TArrOAOI; Header : String);
- Var
- Row, Column: Integer;
- Begin
- Writeln(Header);
- For Row := 0 To High(Matrix) Do
- Begin
- For Column := 0 To High(Matrix[Row]) Do
- Write(Matrix[Row, Column], ' ');
- Writeln;
- End;
- End;
- Function InputNumUnlimited(): Integer;
- Var
- Num: Integer;
- IsCorrect: Boolean;
- Begin
- Repeat
- IsCorrect := True;
- Try
- Readln(Num);
- Except
- Write('Symbols have been entered or exceeding permissible limits. Enter a valid value: ');
- IsCorrect := False;
- End;
- Until IsCorrect;
- InputNumUnlimited := Num;
- End;
- Function InputNum(Min, Max: Integer): Integer;
- Var
- Num: Integer;
- IsCorrect: Boolean;
- Begin
- Repeat
- IsCorrect := True;
- Num := InputNumUnlimited();
- If (Num < Min) Or (Num > Max) Then
- Begin
- IsCorrect := False;
- Write('Input error. Input number in range', Min, '-', Max, ': ');
- End;
- Until IsCorrect;
- InputNum := Num;
- End;
- Function CreateMatrix(Rows, Columns, DefaultValue: Integer): TArrOAOI;
- Var
- Matrix: TArrOAOI;
- Row, Column: Integer;
- Begin
- SetLength(Matrix, Rows);
- For Row := 0 To High(Matrix) Do
- Begin
- SetLength(Matrix[Row], Columns);
- For Column := 0 To High(Matrix[Row]) Do
- Matrix[Row][Column] := DefaultValue;
- End;
- CreateMatrix := Matrix;
- End;
- Procedure ResizeMatrix(Var Matrix: TArrOAOI; ExtraRows: Integer; DefaultValue: Integer);
- Var
- Row, Column, InitialRowsCount, FinalRowsCount, ColumnsCount: Integer;
- Begin
- InitialRowsCount := Length(Matrix);
- FinalRowsCount := InitialRowsCount + ExtraRows;
- ColumnsCount := Length(Matrix[0]);
- SetLength(Matrix, FinalRowsCount);
- For Row := InitialRowsCount To FinalRowsCount - 1 Do
- Begin
- SetLength(Matrix[Row], ColumnsCount);
- For Column := 0 To ColumnsCount - 1 Do
- Matrix[Row, Column] := DefaultValue;
- End;
- End;
- Function ReadCondition(): Char;
- Var
- Condition: String;
- IsCorrect: Boolean;
- Begin
- Writeln('Would you like to enter Matrix manually [m] or get from file [f]?');
- Repeat
- IsCorrect := True;
- Readln(Condition);
- If (Condition <> 'm') And (Condition <> 'f') Then
- Begin
- IsCorrect := False;
- Write('Please type [m] or [f]: ');
- End;
- Until IsCorrect;
- ReadCondition := Condition[1];
- End;
- Function InputMatrixRange(): Integer;
- Begin
- Write('Input Matrix range (1...5): ');
- InputMatrixRange := InputNum(1, 5);
- End;
- Function InputSourceMatrixFromManually(): TArrOAOI;
- Var
- Matrix: TArrOAOI;
- MatrixRange: Integer;
- Row, Column: Integer;
- Begin
- MatrixRange := InputMatrixRange();
- Matrix := CreateMatrix(MatrixRange, MatrixRange, 0);
- Writeln('Input Matrix values:');
- For Row := 0 To High(Matrix) Do
- For Column := 0 To High(Matrix[Row]) Do
- Begin
- Write('Input value for Row: ', Row, ' Column: ', Column, ': ');
- Matrix[Row][Column] := InputNumUnlimited();
- End;
- InputSourceMatrixFromManually := Matrix;
- End;
- Function CheckFileAccess(Path: String): Boolean;
- Var
- MyFile: TextFile;
- Begin
- Try
- AssignFile(MyFile, Path);
- Reset(MyFile);
- CheckFileAccess := True;
- Except
- CheckFileAccess := False;
- Writeln('Cannot open file.');
- End;
- CloseFile(MyFile);
- End;
- Function InputFilePath(CheckExistence: Boolean): String;
- Var
- IsCorrect: Boolean;
- Path: String;
- Begin
- Writeln('Input file path (*.txt): ');
- Repeat
- IsCorrect := True;
- Try
- Readln(Path);
- If Not (ExtractFileExt(Path) = '.txt') Then
- Begin
- IsCorrect := False;
- Writeln('Incorrect file extension. Repeat: ');
- End
- Else If CheckExistence And Not FileExists(Path) Then
- Begin
- IsCorrect := False;
- Writeln('File is not exist. Repeat: ');
- End;
- If CheckExistence And IsCorrect Then
- IsCorrect := CheckFileAccess(Path);
- Except
- IsCorrect := False;
- Writeln('Something wrong happened. Repeat:');
- End;
- Until IsCorrect;
- InputFilePath := Path;
- End;
- Function GetMatrixRange(Var MyFile: TextFile): Integer;
- Var
- MatrixRange: Integer;
- Begin
- Try
- Read(MyFile, MatrixRange);
- Except
- MatrixRange := 0;
- Writeln('Cannot read matrix range.');
- End;
- GetMatrixRange := MatrixRange;
- End;
- Function ReadMatrix(Var MyFile: TextFile; MatrixRange: Integer; Var IsCorrect: Boolean): TArrOAOI;
- Var
- Matrix: TArrOAOI;
- Row, Column: Integer;
- Begin
- Try
- IsCorrect := True;
- Matrix := CreateMatrix(MatrixRange, MatrixRange, 0);
- Row := 0;
- while (Row < Length(Matrix)) and (IsCorrect) Do
- begin
- Column := 0;
- while (Column < Length(Matrix[Row])) and (IsCorrect) Do
- Begin
- If Eof(MyFile) Then
- Begin
- IsCorrect := False;
- Write('Matrix lack data for the specified matrix range.');
- End
- Else
- Read(MyFile, Matrix[Row][Column]);
- Column := Column+1;
- End;
- Row := Row +1;
- end;
- if IsCorrect = True then
- Begin
- If Eof(MyFile) Then
- Begin
- ReadMatrix := Matrix;
- IsCorrect := True;
- End
- Else
- Begin
- Write('Matrix data exceed the specified matrix range.');
- IsCorrect := False;
- End
- End;
- Except
- IsCorrect := False;
- Writeln('Matrix read error.');
- End;
- End;
- Function GetSourceMatrixFromFile(Var IsCorrect: Boolean): TArrOAOI;
- Var
- MatrixRange: Integer;
- Path: String;
- MyFile: TextFile;
- Begin
- Path := InputFilePath(True);
- IsCorrect := True;
- Try
- AssignFile(MyFile, Path);
- Reset(MyFile);
- MatrixRange := GetMatrixRange(MyFile);
- If MatrixRange = 0 Then
- Begin
- Write('Matrix size error.');
- IsCorrect := False;
- End;
- If IsCorrect Then
- Begin
- Writeln('Matrix dimension: ', MatrixRange, 'x', MatrixRange);
- GetSourceMatrixFromFile := ReadMatrix(MyFile, MatrixRange, IsCorrect);
- End;
- Except
- Writeln('Cannot read Matrix.');
- End;
- End;
- Function GetSourceMatrix(Var IsCorrect: Boolean): TArrOAOI;
- Var
- Condition: Char;
- Begin
- Condition := ReadCondition();
- IsCorrect := True;
- Case Condition Of
- 'm': GetSourceMatrix := InputSourceMatrixFromManually();
- 'f': GetSourceMatrix := GetSourceMatrixFromFile(IsCorrect);
- End;
- End;
- Procedure CopyRow(sourceRow, DestRow: TArrOI; Columns: Integer);
- Var
- Column: Integer;
- Begin
- For Column := 0 To Columns Do
- DestRow[Column] := SourceRow[Column];
- End;
- Procedure CopyMatrix(sourceMatrix, destMatrix: TArrOAOI; Rows, Columns: Integer);
- Var
- Row: Integer;
- Begin
- For Row := 0 To Rows Do
- CopyRow(sourceMatrix[Row], destMatrix[Row], Columns);
- End;
- Procedure ShiftMatrix(Matrix: TArrOAOI);
- Var
- Row, Rows, Columns: Integer;
- TempRow: TArrOI;
- Begin
- Rows := High(Matrix);
- Columns := High(Matrix[0]);
- SetLength(TempRow, Length(Matrix[0]));
- CopyRow(Matrix[Rows], TempRow, Columns);
- For Row := Rows DownTo 1 Do
- CopyRow(Matrix[Row - 1], Matrix[Row], Columns);
- CopyRow(TempRow, Matrix[0], Columns);
- End;
- Function SaveMatrix(Path: String; Matrix: TArrOAOI): Boolean;
- Var
- MyFile: TextFile;
- Row, Column: Integer;
- Begin
- AssignFile(MyFile, Path);
- Try
- Rewrite(MyFile);
- Writeln(MyFile, 'Modified Matrix: ');
- For Row := 0 To High(Matrix) Do
- Begin
- For Column := 0 To High(Matrix[Row]) Do
- Write(MyFile, Matrix[Row][Column], ' ');
- Writeln(MyFile);
- End;
- SaveMatrix := True;
- Write('File saved successfully!');
- CloseFile(MyFile);
- Except
- Writeln('Unable To Write file.');
- SaveMatrix := False;
- End;
- End;
- Procedure InputFilePathAndSaveMatrix(Matrix: TArrOAOI);
- Var
- Path: String;
- IsCorrect: Boolean;
- Begin
- Repeat
- IsCorrect := True;
- Path := InputFilePath(false);
- IsCorrect := SaveMatrix(Path, Matrix);
- Until IsCorrect;
- End;
- Var
- Row, Rows, Column, Columns: Integer;
- Matrix: TArrOAOI;
- Selected: Char;
- Path: string;
- IsCorrect: Boolean;
- Begin
- PrintCondition();
- Matrix := GetSourceMatrix(IsCorrect);
- If IsCorrect Then
- Begin
- PrintMatrix(Matrix,'Source Matrix:');
- ResizeMatrix(Matrix, 2, 1);
- ShiftMatrix(Matrix);
- PrintMatrix(Matrix,'Shifted Matrix:');
- InputFilePathAndSaveMatrix(Matrix);
- End;
- readln;
- End.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement