d1bs

Lab2_4

Nov 7th, 2024
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 10.25 KB | None | 0 0
  1. Program Lab2_4;
  2.  
  3. Uses
  4.     System.SysUtils;
  5.  
  6. Type
  7.     TMass = Array Of Array Of Double;
  8.     TErrorCode = (CORRECT, INCORRECT_CHOISE, NON_NUMERIC, OUT_OF_RANGE, FILE_NOT_TXT, FILE_NOT_EXIST, FILE_NOT_READABLE, FILE_NOT_WRITABLE,
  9.         FILE_IS_EMPTY, READING_GO_WRONG, FILE_NOT_FULL);
  10.  
  11. Const
  12.     MIN_ARR = 2;
  13.     MAX_ARR = 20;
  14.     MAX_OPTION = 2;
  15.     MIN_COUNT = -999.999;
  16.     MAX_COUNT = 999.999;
  17.     Err: Array [TErrorCode] Of String = ('', 'Error. Incorrect choise. Please try again. ', 'Error. Non-numeric value. Please try again. ',
  18.         'Error. Out of Range. Please try again. ', 'Error. File not .txt. Please try again', 'Error. File not Exist. Please try again.',
  19.         'Error. File not readable. Please try again.', 'Error. File not writable. Please try again.',
  20.         'Error. File is empty. Please try again.', 'Error. Reading go wrong. Please try again.',
  21.         'Error. The file lacks sufficient information . Please try again. ');
  22.  
  23. Procedure ProgramTask();
  24. Begin
  25.     Writeln('This program removes rows containing 0 elements');
  26. End;
  27.  
  28. Function GetLastPartStr(Var Str: String; PosStart, PosEnd: Integer): String;
  29. Var
  30.     I: Integer;
  31.     PartStr: String;
  32. Begin
  33.     PartStr := '';
  34.     For I := PosStart To PosEnd Do
  35.         PartStr := PartStr + Str[I];
  36.     GetLastPartStr := PartStr;
  37. End;
  38.  
  39. Function IsFileTxt(PathToFile: String): TErrorCode;
  40. Var
  41.     ErrorCode: TErrorCode;
  42. Begin
  43.     ErrorCode := CORRECT;
  44.     If (Length(PathToFile) < 5) Or (GetLastPartStr(PathToFile, Length(PathToFile) - 3, Length(PathToFile)) <> '.txt') Then
  45.         ErrorCode := FILE_NOT_TXT;
  46.     IsFileTxt := ErrorCode;
  47. End;
  48.  
  49. Function IsFileExist(PathToFile: String): TErrorCode;
  50. Var
  51.     ErrorCode: TErrorCode;
  52. Begin
  53.     ErrorCode := CORRECT;
  54.     If Not FileExists(PathToFile) Then
  55.         ErrorCode := FILE_NOT_EXIST;
  56.     IsFileExist := ErrorCode;
  57. End;
  58.  
  59. Function IsFileReadble(Var FileName: TextFile): TErrorCode;
  60. Var
  61.     ErrorCode: TErrorCode;
  62. Begin
  63.     ErrorCode := CORRECT;
  64.     Try
  65.         Reset(FileName);
  66.     Except
  67.         ErrorCode := FILE_NOT_READABLE
  68.     End;
  69.     IsFileReadble := ErrorCode;
  70. End;
  71.  
  72. Function IsFileWritable(Var FileName: TextFile): TErrorCode;
  73. Var
  74.     ErrorCode: TErrorCode;
  75. Begin
  76.     ErrorCode := CORRECT;
  77.     Try
  78.         Append(FileName);
  79.     Except
  80.         ErrorCode := FILE_NOT_WRITABLE;
  81.     End;
  82.     IsFileWritable := ErrorCode;
  83. End;
  84.  
  85. Procedure GetFileNormalReading(Var FileName: TextFile);
  86. Var
  87.     ErrorCode: TErrorCode;
  88.     PathToFile: String;
  89. Begin
  90.     Repeat
  91.         Readln(PathToFile);
  92.         ErrorCode := IsFileTxt(PathToFile);
  93.         If ErrorCode = CORRECT Then
  94.         Begin
  95.             ErrorCode := IsFileExist(PathToFile);
  96.             AssignFile(FileName, PathToFile);
  97.             ErrorCode := IsFileReadble(FileName);
  98.         End;
  99.         If (ErrorCode = CORRECT) And (EOF(FileName)) Then
  100.             ErrorCode := FILE_IS_EMPTY;
  101.         If ErrorCode <> CORRECT Then
  102.             Writeln(ERR[ErrorCode]);
  103.     Until ErrorCode = CORRECT;
  104. End;
  105.  
  106. Procedure GetFileNormalWriting(Var FileName: TextFile);
  107. Var
  108.     ErrorCode: TErrorCode;
  109.     PathToFile: String;
  110. Begin
  111.     Repeat
  112.         Readln(PathToFile);
  113.         ErrorCode := IsFileTxt(PathToFile);
  114.         If ErrorCode = CORRECT Then
  115.         Begin
  116.             ErrorCode := IsFileExist(PathToFile);
  117.             AssignFile(FileName, PathToFile);
  118.             ErrorCode := IsFileWritable(FileName);
  119.         End;
  120.         If ErrorCode <> CORRECT Then
  121.             Writeln(ERR[ErrorCode]);
  122.     Until ErrorCode = CORRECT;
  123. End;
  124.  
  125. Function MatrixSetingFile(Var FileName: TextFile; Var Matrix: TMass; Var Size: Integer): TErrorCode;
  126. Var
  127.     ErrorCode: TErrorCode;
  128.     I: Integer;
  129. Begin
  130.     ErrorCode := CORRECT;
  131.     Writeln('Чтение настроек матрицы...');
  132.     Try
  133.         Read(FileName, Size);
  134.     Except
  135.         ErrorCode := READING_GO_WRONG;
  136.     End;
  137.     If EOF(FileName) Then
  138.         ErrorCode := FILE_NOT_FULL;
  139.     If (ErrorCode = CORRECT) And (Size < MIN_ARR) Or (Size > MAX_ARR) Then
  140.         ErrorCode := OUT_OF_RANGE;
  141.     If ErrorCode = CORRECT Then
  142.     Begin
  143.         SetLength(Matrix, Size);
  144.         For I := 0 To Size - 1 Do
  145.             SetLength(Matrix[I], Size);
  146.     End;
  147.  
  148.     MatrixSetingFile := ErrorCode;
  149. End;
  150.  
  151. Procedure MatrixSetingConsole(Var Matrix: TMass; Var Size: Integer);
  152. Var
  153.     ErrorCode: TErrorCode;
  154. Begin
  155.     Repeat
  156.         ErrorCode := CORRECT;
  157.         Writeln('Please write size of matrix through the space in the range ', MIN_ARR, ' .. ', MAX_ARR);
  158.         Try
  159.             Readln(Size);
  160.         Except
  161.             ErrorCode := NON_NUMERIC;
  162.         End;
  163.         If (ErrorCode = CORRECT) And ((Size < MIN_ARR) Or (Size > MAX_ARR)) Then
  164.             ErrorCode := OUT_OF_RANGE;
  165.         If ErrorCode <> CORRECT Then
  166.             Write(ERR[ErrorCode]);
  167.     Until ErrorCode = CORRECT;
  168.     SetLength(Matrix, Size);
  169. End;
  170.  
  171. Procedure ReadMatrix(Var FileName: TextFile; Var Matrix: TMass);
  172. Var
  173.     I, J: Integer;
  174.     ErrorCode: TErrorCode;
  175. Begin
  176.     ErrorCode := CORRECT;
  177.     For I := 0 To High(Matrix) Do
  178.     Begin
  179.         For J := 0 To High(Matrix[I]) Do
  180.         Begin
  181.             If Not EOF(FileName) Then
  182.             Begin
  183.                 Try
  184.                     Read(FileName, Matrix[I][J]);
  185.                 Except
  186.                     ErrorCode := READING_GO_WRONG;
  187.                 End;
  188.             End
  189.             Else
  190.                 ErrorCode := FILE_NOT_FULL;
  191.         End;
  192.     End;
  193.     If ErrorCode <> CORRECT Then
  194.         WriteLn(ERR[ErrorCode]);
  195. End;
  196.  
  197. Procedure ReadMatrixFromFile(Var Matrix: TMass; Var Size: Integer);
  198. Var
  199.     FileName: TextFile;
  200.     ErrorCode: TErrorCode;
  201. Begin
  202.     WriteLn('Enter the path to the file with extension ".txt" with matrix dimensions and free terms from ', MIN_ARR, ' .. ', MAX_ARR);
  203.     GetFileNormalReading(FileName);
  204.     ErrorCode := MatrixSetingFile(FileName, Matrix, Size);
  205.  
  206.     If ErrorCode = CORRECT Then
  207.         ReadMatrix(FileName, Matrix)
  208.     Else
  209.         WriteLn(ERR[ErrorCode]);
  210.     CloseFile(FileName);
  211. End;
  212.  
  213. Procedure ReadMatrixFromConsole(Var Matrix: TMass; Var Size: Integer);
  214. Var
  215.     I, J: Integer;
  216.     ErrorCode: TErrorCode;
  217. Begin
  218.     MatrixSetingConsole(Matrix, Size);
  219.     Writeln('Matrix size [', Size, ',', Size, ']');
  220.     For I := 0 To High(Matrix) Do
  221.     Begin
  222.         SetLength(Matrix[I], Size);
  223.         WriteLn('Please enter the elements for row ', I, ' (diagonal and above) through the space in the range ', MIN_COUNT:7:2, ' .. ',
  224.             MAX_COUNT:7:2);
  225.         For J := 0 To High(Matrix[I]) Do
  226.         Begin
  227.             Repeat
  228.                 ErrorCode := CORRECT;
  229.                 Try
  230.                     Readln(Matrix[I][J]);
  231.                 Except
  232.                     ErrorCode := NON_NUMERIC;
  233.                 End;
  234.                 If (ErrorCode = CORRECT) And ((Matrix[I][J] < MIN_COUNT) Or (Matrix[I][J] > MAX_COUNT)) Then
  235.                     ErrorCode := OUT_OF_RANGE;
  236.                 If ErrorCode <> CORRECT Then
  237.                     WriteLn(ERR[ErrorCode]);
  238.             Until ErrorCode = CORRECT;
  239.         End;
  240.  
  241.     End;
  242. End;
  243.  
  244. Function OptionRead(): Integer;
  245. Var
  246.     ErrorCode: TErrorCode;
  247.     Option: Integer;
  248. Begin
  249.     Option := 0;
  250.     Repeat
  251.         ErrorCode := CORRECT;
  252.         Try
  253.             Readln(Option);
  254.         Except
  255.             ErrorCode := NON_NUMERIC;
  256.         End;
  257.         If (ErrorCode = CORRECT) And ((Option < 1) Or (Option > MAX_OPTION)) Then
  258.             ErrorCode := INCORRECT_CHOISE;
  259.         If ErrorCode <> CORRECT Then
  260.             Write(ERR[ErrorCode]);
  261.     Until ErrorCode = CORRECT;
  262.     OptionRead := Option;
  263. End;
  264.  
  265. Procedure OptionHowToRead(Var Matrix: TMass; Size: Integer);
  266. Var
  267.     Option: Integer;
  268. Begin
  269.     Writeln('If you want to read from console enter: 1');
  270.     Writeln('If you want to read from File enter: 2');
  271.     Option := OptionRead();
  272.     If Option = 2 Then
  273.         ReadMatrixFromFile(Matrix, Size)
  274.     Else
  275.         ReadMatrixFromConsole(Matrix, Size);
  276.  
  277. End;
  278.  
  279. Procedure PrintConsole(Matrix: Tmass; Var NewRowCount, Size: Integer);
  280. Var
  281.     I, J: Integer;
  282. Begin
  283.     WriteLn('Solutions:');
  284.     For I := 0 To NewRowCount - 1 Do
  285.     Begin
  286.         For J := 0 To High(Matrix) Do
  287.             Write(Matrix[I][J]:0:2, ' ');
  288.         WriteLn;
  289.     End;
  290. End;
  291.  
  292. Procedure PrintFile(Matrix: Tmass; Var NewRowCount, Size: Integer);
  293. Var
  294.     FileName: TextFile;
  295.     I, J: Integer;
  296.     ErrorCode: TErrorCode;
  297. Begin
  298.     WriteLn('Enter the path to the file with the extension ".txt" to get the answer: ');
  299.     GetFileNormalWriting(FileName);
  300.     ErrorCode := IsFileWritable(FileName);
  301.  
  302.     If ErrorCode = CORRECT Then
  303.     Begin
  304.         Rewrite(FileName);
  305.         WriteLn(FileName, 'Solutions:');
  306.         For I := 0 To NewRowCount - 1 Do
  307.         Begin
  308.             For J := 0 To High(Matrix) Do
  309.                 Write(FileName, Matrix[I][J]:0:4, ' ');
  310.             WriteLn(Filename);
  311.         End;
  312.         CloseFile(FileName);
  313.     End
  314.     Else
  315.         WriteLn(ERR[ErrorCode]);
  316. End;
  317.  
  318. Function RemoveZeroRows(Var Matrix: TMass; Var Size: Integer): Integer;
  319. Var
  320.     NewRowCount, I, J: Integer;
  321.     HasZero: Boolean;
  322. Begin
  323.     NewRowCount := 0;
  324.     For I := 0 To High(Matrix) Do
  325.     Begin
  326.         HasZero := False;
  327.         For J := 0 To High(Matrix[I]) Do
  328.         Begin
  329.             If Matrix[I][J] = 0 Then
  330.                 HasZero := True;
  331.         End;
  332.  
  333.         If Not HasZero Then
  334.         Begin
  335.             For J := 0 To High(Matrix[I]) Do
  336.                 Matrix[NewRowCount][J] := Matrix[I][J];
  337.             Inc(NewRowCount);
  338.         End;
  339.     End;
  340.     RemoveZeroRows := NewRowCount;
  341. End;
  342.  
  343. Procedure PrintAnswer(Matrix: Tmass; NewRowCount, Size: Integer);
  344. Var
  345.     Option, I: Integer;
  346. Begin
  347.     Writeln('If you want to print answer in console enter: 1');
  348.     Writeln('If you want to print answer in File enter: 2');
  349.     Option := OptionRead();
  350.     If Option = 2 Then
  351.         PrintFile(Matrix, NewRowCount, Size)
  352.     Else
  353.         PrintConsole(Matrix, NewRowCount, Size);
  354. End;
  355.  
  356. Var
  357.     Matrix: TMass;
  358.     Size, NewRowCount: Integer;
  359.  
  360. Begin
  361.     Size := 0;
  362.     ProgramTask();
  363.     OptionHowToRead(Matrix, Size);
  364.     NewRowCount := RemoveZeroRows(Matrix, Size);
  365.     PrintAnswer(Matrix, NewRowCount, Size);
  366.     Writeln('Press Enter to exit');
  367.     Readln;
  368.  
  369. End.
Advertisement
Add Comment
Please, Sign In to add comment