negtab

Polina Delphi

Nov 3rd, 2024 (edited)
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 9.04 KB | None | 0 0
  1. Program Polina;
  2.  
  3. Uses
  4.     SysUtils;
  5.  
  6. Type
  7.     Matrix = Array Of Array Of Real;
  8.  
  9.  
  10. Procedure PrintTask();
  11. Begin
  12.     Writeln('This program counts the number of columns in a given matrix whose elements are arranged in ascending order.');
  13. End;
  14.  
  15.  
  16. Function UserChoice(): Byte;
  17. Var
  18.     Choice: Byte;
  19.     IsCorrect: Boolean;
  20. Begin  Choice := 0;
  21.     Writeln('Select the input method : 1 - console, 2 - file: ');
  22.     Repeat
  23.         Try
  24.             IsCorrect := True;
  25.             Readln(Choice);
  26.         Except
  27.             IsCorrect := False;
  28.         End;
  29.         If (Choice < 1) Or (Choice > 2) Or Not IsCorrect Then
  30.         Begin
  31.             Writeln('Select the input method : 1 -  console, 2 - file: ');
  32.             IsCorrect := False;
  33.         End;
  34.     Until (IsCorrect);
  35.     UserChoice := Choice;
  36. End;
  37.  
  38. Function EnterSizeFromConsole(): Integer;
  39. Const
  40.     MIN_N = 2;
  41. Var
  42.     N: Integer;
  43.     IsCorrect: Boolean;
  44. Begin
  45.     N := 2;
  46.     Writeln('Enter matrix order N: ');
  47.     Repeat
  48.         IsCorrect := True;
  49.         Try
  50.             Readln(N);
  51.         Except
  52.             IsCorrect := False;
  53.             Writeln('Enter correct value: ');
  54.         End;
  55.         If (IsCorrect) And (N < MIN_N) Then
  56.         Begin
  57.             IsCorrect := False;
  58.             Writeln('Enter correct value: ');
  59.         End;
  60.     Until IsCorrect;
  61.     EnterSizeFromConsole := N;
  62. End;
  63.  
  64. Function EnterSizeFromFile(Var PathToFile : String): Integer;
  65.  
  66. Const
  67.     MIN_N = 2;
  68. Var
  69.     N: Integer;
  70.     IsCorrect: Boolean;
  71.     T : TextFile;
  72. Begin
  73.     Assign(T, PathToFile);
  74.     Reset(T);
  75.     N := 2;
  76.     IsCorrect := True;
  77.     Try
  78.         Readln(T, N);
  79.     Except
  80.         IsCorrect := False;
  81.     End;
  82.     If (IsCorrect) And (N < MIN_N) Then
  83.     Begin
  84.         IsCorrect := False;
  85.         Writeln('Размер матрицы в файле не корректен');
  86.         N := EnterSizeFromConsole();
  87.     End;
  88.     CloseFile(T);
  89.     EnterSizeFromFile := N;
  90. End;
  91.  
  92. Function ReadPathToFile() : String;
  93. Var
  94.     PathToFile: String;
  95.     IsCorrect: Boolean;
  96. Begin
  97.     Repeat
  98.         IsCorrect := True;
  99.         Write('Введите путь к файлу с расширением.txt с количеством точек и координатами точек: ');
  100.         Readln(PathToFile);
  101.         If ExtractFileExt(PathToFile) <> '.txt' Then
  102.         Begin
  103.             Writeln('Расширение файла не .txt!');
  104.             IsCorrect := False;
  105.         End;
  106.     Until(IsCorrect);
  107.     ReadPathToFile := PathToFile;
  108. End;
  109.  
  110. Function IsExists(PathToFile : String) : Boolean;
  111. Var
  112.     IsRight: Boolean;
  113. Begin
  114.     IsRight := False;
  115.     If FileExists(PathToFile) Then
  116.         IsRight := True;
  117.     IsExists := IsRight;
  118. End;
  119.  
  120. Function IsNotAbleToReading(Var T: TextFile) : Boolean;
  121. Var
  122.     IsRight: Boolean;
  123. Begin
  124.     IsRight := False;
  125.     Try
  126.         Reset(T);
  127.         CloseFile(T);
  128.     Except
  129.         IsRight := True;
  130.     End;
  131.     IsNotAbleToReading := IsRight;
  132. End;
  133.  
  134. Function IsNotAbleToWriting(PathToFile: String) : Boolean;
  135. Var
  136.     IsRight: Boolean;
  137. Begin
  138.     IsRight := False;
  139.     If FileIsReadOnly(PathToFile) Then
  140.         IsRight := True;
  141.     IsNotAbleToWriting := IsRight;
  142. End;
  143.  
  144. Function IsEmpty(Var T: TextFile) : Boolean;
  145. Var
  146.     IsRight: Boolean;
  147. Begin
  148.     IsRight := False;
  149.     Reset(T);
  150.     If EOF(T) Then
  151.         IsRight := True;
  152.     CloseFile(T);
  153.     IsEmpty := IsRight;
  154. End;
  155.  
  156. Function GetFileNormalReading() : String;
  157. Var
  158.     IsCorrect: Boolean;
  159.     T : TextFile;
  160.     PathToFile: String;
  161. Begin
  162.     Repeat
  163.         IsCorrect := True;
  164.         PathToFile := ReadPathToFile();
  165.         If Not IsExists(PathToFile) Then
  166.         Begin
  167.             IsCorrect := False;
  168.             Writeln('Проверьте корректность ввода пути к файлу!');
  169.         End;
  170.         If IsCorrect Then
  171.             AssignFile(T, PathToFile);
  172.         If IsCorrect And IsNotAbleToReading(T) Then
  173.         Begin
  174.             IsCorrect := False;
  175.             Writeln('Файл закрыт для чтения!');
  176.         End;
  177.         If IsCorrect And IsEmpty(T) Then
  178.         Begin
  179.             IsCorrect := False;
  180.             WriteLn('Файл пуст!');
  181.         End;
  182.     Until IsCorrect;
  183.     GetFileNormalReading := PathToFile;
  184. End;
  185.  
  186. Function GetFileNormalWriting() : String;
  187. Var
  188.     T : TextFile;
  189.     IsCorrect: Boolean;
  190.     PathToFile: String;
  191. Begin
  192.     Repeat
  193.         IsCorrect := True;
  194.         PathToFile := ReadPathToFile();
  195.         If Not IsExists(PathToFile) Then
  196.         Begin
  197.             IsCorrect := False;
  198.             Writeln('Проверьте корректность ввода пути к файлу!');
  199.         End;
  200.         If IsCorrect Then
  201.             AssignFile(T, PathToFile);
  202.         If IsCorrect And IsNotAbleToWriting(PathToFile) Then
  203.         Begin
  204.             IsCorrect := False;
  205.             WriteLn('Файл закрыт для записи!');
  206.         End;
  207.     Until IsCorrect;
  208.     GetFileNormalWriting := PathToFile;
  209. End;
  210.  
  211. Function EnterMatrixFromConsole(N: Integer) : Matrix;
  212. Var
  213.    IsCorrect: Boolean;
  214.    OutputMatrix: Matrix;
  215. Begin
  216.     SetLength(OutputMatrix, N, N);
  217.     Dec(N);
  218.     For Var I := 0 To N Do
  219.         For Var J := 0 To N Do
  220.         Begin
  221.             Write('Введите элемент матрицы Matrix[', I + 1, ',', J + 1, ']: ');
  222.             Repeat
  223.                 IsCorrect := True;
  224.                 Try
  225.                     Read(OutputMatrix[I][J]);
  226.                 Except
  227.                     IsCorrect := False;
  228.                     Writeln('Ошибка! Введите корректное значение: ');
  229.                 End;
  230.             Until IsCorrect;
  231.         End;
  232.  
  233.     EnterMatrixfromConsole := OutputMatrix;
  234. End;
  235.  
  236. Function FindTheSolution(MatrixForSolution : Matrix; N : Integer) : Integer;
  237. Var
  238.     Increasing : Boolean;
  239.     IncreasingCount : Integer;
  240. Begin
  241.     IncreasingCount := 0;
  242.     Dec(N);
  243.     For Var J := 0 to N do
  244.     Begin
  245.         Increasing := true;
  246.         For Var I := 1 to N do
  247.             If MatrixForSolution[I-1, j] > MatrixForSolution[I, J] then
  248.               Increasing := False;
  249.         If Increasing then
  250.                 Inc(IncreasingCount);
  251.     End;
  252.     FindTheSolution := IncreasingCount;
  253. End;
  254.  
  255. Function ReadMatrix(WayToFile: String; N: Integer) : Matrix;
  256. Var
  257.     MyFile: TextFile;
  258.     NullElement : Char;
  259.     InputMatrix : Matrix;
  260.     IsEndOfFile, IsCorrect : Boolean;
  261. Begin
  262.     Assign(MyFile, WayToFile);
  263.     Reset(MyFile);
  264.     Read(MyFile, NullElement);
  265.     SetLength(InputMatrix, N, N);
  266.     Dec(N);
  267.     For Var I := 0 To N Do
  268.     Begin
  269.         If Not EOF(MyFile) Then
  270.         Begin
  271.             For Var J := 0 To N Do
  272.             Begin
  273.                 If Not EOF(MyFile) Then
  274.                 Begin
  275.                     Try
  276.                         Read(MyFile, InputMatrix[I][J]);
  277.                     Except
  278.                         IsCorrect := False;
  279.                         Writeln('Ошибка! Введите корректное значение: ');
  280.                     End;
  281.                     Write(Format('%8.1f', [InputMatrix[I][J]]));
  282.                 End
  283.                 Else
  284.                 Begin
  285.                     Repeat
  286.                         IsCorrect := True;
  287.                         Try
  288.                             Read(InputMatrix[I][J]);
  289.                         Except
  290.                             IsCorrect := False;
  291.                             Writeln('Ошибка! Введите корректное значение: ');
  292.                         End;
  293.                     Until IsCorrect;
  294.                 End;
  295.             End;
  296.             Writeln;
  297.         End;
  298.     End;
  299.     CloseFile(MyFile);
  300.     ReadMatrix := InputMatrix;
  301. End;
  302.  
  303. Procedure Input(Var Size : Integer; Var InputMatrix : Matrix);
  304. Var
  305.     Choose : Integer;
  306.     PathToFile : String;
  307. Begin
  308.     Choose := UserChoice();
  309.     If (Choose = 1) Then
  310.     Begin
  311.         Size := EnterSizeFromConsole();
  312.         InputMatrix := EnterMatrixFromConsole(Size);
  313.     End
  314.     Else
  315.     Begin
  316.         PathToFile := GetFileNormalReading();
  317.         Size := EnterSizeFromFile(PathToFile);
  318.         InputMatrix := ReadMatrix(PathToFile, Size);
  319.     End;
  320. End;
  321.  
  322. Procedure OutputInFile(PathToFile : String; Answer : Integer);
  323. Var
  324.     T : TextFile;
  325. Begin
  326.     AssignFile(T, PathToFile);
  327.     Rewrite(T);
  328.     Write(T, 'Какой-то текст: ');
  329.     Write(T, Answer);
  330.     CloseFile(T);
  331. End;
  332.  
  333. Procedure Output(Answer : Integer);
  334. Var
  335.     Choose : Integer;
  336.     PathToFile : String;
  337. Begin
  338.     Choose := UserChoice();
  339.     If (Choose = 1) Then
  340.         Writeln('Какой-то текст: ', Answer)
  341.     Else
  342.     Begin
  343.         PathToFile := GetFileNormalWriting();
  344.         Writeln('Какой-то текст: ', Answer);
  345.         OutputInFile(PathToFile, Answer);
  346.     End;
  347.  
  348.     Readln;
  349. End;
  350.  
  351. Var
  352.     Size, Answer : Integer;
  353.     MatrixForSolution : Matrix;
  354.  
  355. Begin
  356.     PrintTask();
  357.     Input(Size, MatrixForSolution);
  358.     Answer := FindTheSolution(MatrixForSolution, Size);
  359.     Output(Answer);
  360. End.
  361.  
Advertisement
Add Comment
Please, Sign In to add comment