Advertisement
LisunovaMaryna

lab2.3 delphi

Nov 3rd, 2023 (edited)
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 9.60 KB | None | 0 0
  1. program Project1;
  2.  
  3. {$APPTYPE CONSOLE}
  4.  
  5. {$R *.res}
  6.  
  7. Uses
  8.     System.SysUtils;
  9.  
  10. Type
  11.     TArrOI = Array Of Integer;
  12.     TArrOAOI = Array Of TArrOI;
  13.  
  14.  
  15. Procedure PrintCondition();
  16. Begin
  17.     Writeln('Goal: Input Matrix And add Rows into position 0 And n+1 with elements equal To 1 (use shift rows).');
  18. End;
  19.  
  20.  
  21. Procedure PrintMatrix(Matrix: TArrOAOI; Header : String);
  22. Var
  23.     Row, Column: Integer;
  24. Begin
  25.     Writeln(Header);
  26.     For Row := 0 To High(Matrix) Do
  27.     Begin
  28.         For Column := 0 To High(Matrix[Row]) Do
  29.             Write(Matrix[Row, Column], ' ');
  30.         Writeln;
  31.     End;
  32. End;
  33.  
  34.  
  35. Function InputNumUnlimited(): Integer;
  36. Var
  37.     Num: Integer;
  38.     IsCorrect: Boolean;
  39. Begin
  40.     Repeat
  41.         IsCorrect := True;
  42.         Try
  43.             Readln(Num);
  44.         Except
  45.             Write('Symbols have been entered or exceeding permissible limits. Enter a valid value: ');
  46.             IsCorrect := False;
  47.         End;
  48.     Until IsCorrect;
  49.     InputNumUnlimited := Num;
  50. End;
  51.  
  52.  
  53. Function InputNum(Min, Max: Integer): Integer;
  54. Var
  55.     Num: Integer;
  56.     IsCorrect: Boolean;
  57. Begin
  58.     Repeat
  59.         IsCorrect := True;
  60.         Num := InputNumUnlimited();
  61.         If (Num < Min) Or (Num > Max) Then
  62.         Begin
  63.             IsCorrect := False;
  64.             Write('Input error. Input number in range', Min, '-', Max, ': ');
  65.         End;
  66.     Until IsCorrect;
  67.     InputNum := Num;
  68. End;
  69.  
  70.  
  71. Function CreateMatrix(Rows, Columns, DefaultValue: Integer): TArrOAOI;
  72. Var
  73.     Matrix: TArrOAOI;
  74.     Row, Column: Integer;
  75. Begin
  76.     SetLength(Matrix, Rows);
  77.     For Row := 0 To High(Matrix) Do
  78.     Begin
  79.         SetLength(Matrix[Row], Columns);
  80.         For Column := 0 To High(Matrix[Row]) Do
  81.             Matrix[Row][Column] := DefaultValue;
  82.     End;
  83.     CreateMatrix := Matrix;
  84. End;
  85.  
  86.  
  87. Procedure ResizeMatrix(Var Matrix: TArrOAOI; ExtraRows: Integer; DefaultValue: Integer);
  88. Var
  89.     Row, Column, InitialRowsCount, FinalRowsCount, ColumnsCount: Integer;
  90. Begin
  91.     InitialRowsCount :=  Length(Matrix);
  92.     FinalRowsCount := InitialRowsCount + ExtraRows;
  93.     ColumnsCount := Length(Matrix[0]);
  94.  
  95.     SetLength(Matrix, FinalRowsCount);
  96.  
  97.     For Row := InitialRowsCount To FinalRowsCount - 1 Do
  98.     Begin
  99.         SetLength(Matrix[Row], ColumnsCount);
  100.  
  101.         For Column := 0 To ColumnsCount - 1 Do
  102.             Matrix[Row, Column] := DefaultValue;
  103.     End;
  104. End;
  105.  
  106.  
  107. Function ReadCondition(): Char;
  108. Var
  109.     Condition: String;
  110.     IsCorrect: Boolean;
  111. Begin
  112.     Writeln('Would you like to enter Matrix manually [m] or get from file [f]?');
  113.     Repeat
  114.         IsCorrect := True;
  115.         Readln(Condition);
  116.         If (Condition <> 'm') And (Condition <> 'f') Then
  117.         Begin
  118.             IsCorrect := False;
  119.             Write('Please type [m] or [f]: ');
  120.         End;
  121.     Until IsCorrect;
  122.     ReadCondition := Condition[1];
  123. End;
  124.  
  125.  
  126. Function InputMatrixRange(): Integer;
  127. Begin
  128.     Write('Input Matrix range (1...5): ');
  129.     InputMatrixRange := InputNum(1, 5);
  130. End;
  131.  
  132.  
  133. Function InputSourceMatrixFromManually(): TArrOAOI;
  134. Var
  135.     Matrix: TArrOAOI;
  136.     MatrixRange: Integer;
  137.     Row, Column: Integer;
  138. Begin
  139.     MatrixRange := InputMatrixRange();
  140.     Matrix := CreateMatrix(MatrixRange, MatrixRange, 0);
  141.     Writeln('Input Matrix values:');
  142.     For Row := 0 To High(Matrix) Do
  143.         For Column := 0 To High(Matrix[Row]) Do
  144.         Begin
  145.             Write('Input value for Row: ', Row, ' Column: ', Column, ': ');
  146.             Matrix[Row][Column] := InputNumUnlimited();
  147.         End;
  148.     InputSourceMatrixFromManually := Matrix;
  149. End;
  150.  
  151.  
  152. Function CheckFileAccess(Path: String): Boolean;
  153. Var
  154.     MyFile: TextFile;
  155. Begin
  156.     Try
  157.         AssignFile(MyFile, Path);
  158.         Reset(MyFile);
  159.         CheckFileAccess := True;
  160.     Except
  161.         CheckFileAccess := False;
  162.         Writeln('Cannot open file.');
  163.     End;
  164.     CloseFile(MyFile);
  165. End;
  166.  
  167.  
  168. Function InputFilePath(CheckExistence: Boolean): String;
  169. Var
  170.     IsCorrect: Boolean;
  171.     Path: String;
  172. Begin
  173.     Writeln('Input file path (*.txt): ');
  174.     Repeat
  175.         IsCorrect := True;
  176.         Try
  177.             Readln(Path);
  178.             If Not (ExtractFileExt(Path) = '.txt') Then
  179.             Begin
  180.                 IsCorrect := False;
  181.                 Writeln('Incorrect file extension. Repeat: ');
  182.             End
  183.             Else If CheckExistence And Not FileExists(Path) Then
  184.             Begin
  185.                 IsCorrect := False;
  186.                 Writeln('File is not exist. Repeat: ');
  187.             End;
  188.  
  189.             If CheckExistence And IsCorrect Then
  190.                 IsCorrect := CheckFileAccess(Path);
  191.         Except
  192.             IsCorrect := False;
  193.             Writeln('Something wrong happened. Repeat:');
  194.         End;
  195.  
  196.     Until IsCorrect;
  197.     InputFilePath := Path;
  198. End;
  199.  
  200.  
  201. Function GetMatrixRange(Var MyFile: TextFile): Integer;
  202. Var
  203.     MatrixRange: Integer;
  204. Begin
  205.     Try
  206.         Read(MyFile, MatrixRange);
  207.     Except
  208.         MatrixRange := 0;
  209.         Writeln('Cannot read matrix range.');
  210.     End;
  211.     GetMatrixRange := MatrixRange;
  212. End;
  213.  
  214.  
  215. Function ReadMatrix(Var MyFile: TextFile; MatrixRange: Integer; Var IsCorrect: Boolean): TArrOAOI;
  216. Var
  217.     Matrix: TArrOAOI;
  218.     Row, Column: Integer;
  219. Begin
  220.     Try
  221.         IsCorrect := True;
  222.         Matrix := CreateMatrix(MatrixRange, MatrixRange, 0);
  223.         Row := 0;
  224.          while (Row < Length(Matrix)) and (IsCorrect) Do
  225.          begin
  226.             Column := 0;
  227.             while (Column < Length(Matrix[Row])) and (IsCorrect) Do
  228.             Begin
  229.                 If Eof(MyFile) Then
  230.                   Begin
  231.                      IsCorrect := False;
  232.                      Write('Matrix lack data for the specified matrix range.');
  233.                   End
  234.                 Else
  235.                    Read(MyFile, Matrix[Row][Column]);
  236.  
  237.                    Column := Column+1;
  238.  
  239.             End;
  240.               Row := Row +1;
  241.          end;
  242.  
  243.         if IsCorrect = True then
  244.          Begin
  245.               If Eof(MyFile) Then
  246.               Begin
  247.                   ReadMatrix := Matrix;
  248.                   IsCorrect := True;
  249.               End
  250.               Else
  251.                 Begin
  252.                   Write('Matrix data exceed the specified matrix range.');
  253.                   IsCorrect := False;
  254.               End
  255.          End;
  256.     Except
  257.         IsCorrect := False;
  258.         Writeln('Matrix read error.');
  259.     End;
  260. End;
  261.  
  262.  
  263. Function GetSourceMatrixFromFile(Var IsCorrect: Boolean): TArrOAOI;
  264. Var
  265.     MatrixRange: Integer;
  266.     Path: String;
  267.     MyFile: TextFile;
  268. Begin
  269.     Path := InputFilePath(True);
  270.     IsCorrect := True;
  271.     Try
  272.         AssignFile(MyFile, Path);
  273.         Reset(MyFile);
  274.         MatrixRange := GetMatrixRange(MyFile);
  275.         If MatrixRange = 0 Then
  276.         Begin
  277.             Write('Matrix size error.');
  278.             IsCorrect := False;
  279.         End;
  280.         If IsCorrect Then
  281.         Begin
  282.             Writeln('Matrix dimension: ', MatrixRange, 'x', MatrixRange);
  283.             GetSourceMatrixFromFile := ReadMatrix(MyFile, MatrixRange, IsCorrect);
  284.         End;
  285.     Except
  286.         Writeln('Cannot read Matrix.');
  287.     End;
  288. End;
  289.  
  290.  
  291. Function GetSourceMatrix(Var IsCorrect: Boolean): TArrOAOI;
  292. Var
  293.     Condition: Char;
  294. Begin
  295.     Condition := ReadCondition();
  296.     IsCorrect := True;
  297.     Case Condition Of
  298.         'm': GetSourceMatrix := InputSourceMatrixFromManually();
  299.         'f': GetSourceMatrix := GetSourceMatrixFromFile(IsCorrect);
  300.     End;
  301. End;
  302.  
  303.  
  304. Procedure CopyRow(sourceRow, DestRow: TArrOI; Columns: Integer);
  305. Var
  306.     Column: Integer;
  307. Begin
  308.     For Column := 0 To Columns Do
  309.         DestRow[Column] := SourceRow[Column];
  310. End;
  311.  
  312.  
  313. Procedure CopyMatrix(sourceMatrix, destMatrix: TArrOAOI; Rows, Columns: Integer);
  314. Var
  315.     Row: Integer;
  316. Begin
  317.     For Row := 0 To Rows Do
  318.         CopyRow(sourceMatrix[Row], destMatrix[Row], Columns);
  319. End;
  320.  
  321.  
  322. Procedure ShiftMatrix(Matrix: TArrOAOI);
  323. Var
  324.     Row, Rows, Columns: Integer;
  325.     TempRow: TArrOI;
  326. Begin
  327.     Rows := High(Matrix);
  328.     Columns := High(Matrix[0]);
  329.     SetLength(TempRow, Length(Matrix[0]));
  330.     CopyRow(Matrix[Rows], TempRow, Columns);
  331.  
  332.     For Row := Rows DownTo 1 Do
  333.         CopyRow(Matrix[Row - 1], Matrix[Row], Columns);
  334.  
  335.     CopyRow(TempRow, Matrix[0], Columns);
  336. End;
  337.  
  338.  
  339. Function SaveMatrix(Path: String; Matrix: TArrOAOI): Boolean;
  340. Var
  341.     MyFile: TextFile;
  342.     Row, Column: Integer;
  343. Begin
  344.     AssignFile(MyFile, Path);
  345.     Try
  346.         Rewrite(MyFile);
  347.  
  348.         Writeln(MyFile, 'Modified Matrix: ');
  349.         For Row := 0 To High(Matrix) Do
  350.         Begin
  351.             For Column := 0 To High(Matrix[Row]) Do
  352.                 Write(MyFile, Matrix[Row][Column], ' ');
  353.             Writeln(MyFile);
  354.         End;
  355.  
  356.         SaveMatrix := True;
  357.         Write('File saved successfully!');
  358.         CloseFile(MyFile);
  359.     Except
  360.         Writeln('Unable To Write file.');
  361.         SaveMatrix := False;
  362.     End;
  363. End;
  364.  
  365. Procedure InputFilePathAndSaveMatrix(Matrix: TArrOAOI);
  366. Var
  367.     Path: String;
  368.     IsCorrect: Boolean;
  369. Begin
  370.     Repeat
  371.         IsCorrect := True;
  372.         Path := InputFilePath(false);
  373.         IsCorrect := SaveMatrix(Path, Matrix);
  374.     Until IsCorrect;
  375. End;
  376.  
  377.  
  378. Var
  379.     Row, Rows, Column, Columns: Integer;
  380.     Matrix: TArrOAOI;
  381.     Selected: Char;
  382.     Path: string;
  383.     IsCorrect: Boolean;
  384. Begin
  385.     PrintCondition();
  386.     Matrix := GetSourceMatrix(IsCorrect);
  387.     If IsCorrect Then
  388.     Begin
  389.         PrintMatrix(Matrix,'Source Matrix:');
  390.  
  391.         ResizeMatrix(Matrix, 2, 1);
  392.         ShiftMatrix(Matrix);
  393.  
  394.         PrintMatrix(Matrix,'Shifted Matrix:');
  395.         InputFilePathAndSaveMatrix(Matrix);
  396.     End;
  397.     readln;
  398. End.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement