Advertisement
believe_me

Untitled

Oct 29th, 2021
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 9.30 KB | None | 0 0
  1. program lab23;
  2.  
  3. {$APPTYPE CONSOLE}
  4. {$R *.res}
  5.  
  6. uses System.SysUtils;
  7.  
  8. Function CheckExtension(Path: String): Boolean; forward;
  9.  
  10. Function CheckPermission(Path: String): Boolean; forward;
  11.  
  12. type
  13.     TMatrix = array of array of Integer;
  14.  
  15. Function InputNumber(MinNumber, MaxNumber: Integer): Integer;
  16.  
  17. var
  18.     IsCorrect: Boolean;
  19.     Number: Integer;
  20.  
  21. begin
  22.     repeat
  23.         IsCorrect := true;
  24.         try
  25.             Readln(Number);
  26.         except
  27.             Writeln('Нужно ввести целое число, которое не меньше ', MinNumber,
  28.               ' и не больше ', MaxNumber);
  29.             IsCorrect := false;
  30.         end;
  31.         if IsCorrect and ((Number < MinNumber) or (Number > MaxNumber)) then
  32.         begin
  33.             Writeln('Нужно ввести целое число, которое не меньше ', MinNumber,
  34.               ' и не больше ', MaxNumber);
  35.             IsCorrect := false;
  36.         end;
  37.     until IsCorrect;
  38.     InputNumber := Number;
  39. end;
  40.  
  41. Function ChooseWayOfInput(): Integer;
  42.  
  43. var
  44.     UserWay: Integer;
  45.  
  46. begin
  47.     Repeat
  48.         Writeln('Выберите способ ввода:'#13#10'Нажмите "1", если хотите ввести матрицу через консоль.'#13#10'Нажмите "2", если хотите считать матрицу из файла.');
  49.         UserWay := InputNumber(1, 2);
  50.     Until (UserWay = 1) or (UserWay = 2);
  51.     ChooseWayOfInput := UserWay;
  52. end;
  53.  
  54. function InputPathToFile(): String;
  55.  
  56. var
  57.     Path: String;
  58.     IsCorrect, Flag: Boolean;
  59.     NewFile: TextFile;
  60. begin
  61.     Writeln('Введите путь к файлу:');
  62.     repeat
  63.         repeat
  64.             IsCorrect := true;
  65.             Readln(Path);
  66.             Try
  67.                 AssignFile(NewFile, Path);
  68.                 Reset(NewFile);
  69.             Except
  70.                 Writeln('Ошибка! Путь указан неверно. Введите его заново.');
  71.                 IsCorrect := false;
  72.             End
  73.         Until (IsCorrect);
  74.         Flag := CheckExtension(Path);
  75.     until Flag;
  76.  
  77.     InputPathToFile := Path;
  78. end;
  79.  
  80. Function ReceiveMatrixFromConsole(): TMatrix;
  81.  
  82. var
  83.     NumberOfLines, NumberOfColumns, i, j: Integer;
  84.     Matrix: TMatrix;
  85.  
  86. begin
  87.     Writeln('Введите количество строк матрицы:');
  88.     NumberOfLines := InputNumber(2, 5);
  89.     Writeln('Введите количество столбцов матрицы:');
  90.     NumberOfColumns := InputNumber(2, 5);
  91.     Setlength(Matrix, NumberOfLines, NumberOfColumns);
  92.     Dec(NumberOfLines);
  93.     Dec(NumberOfColumns);
  94.     for i := 0 to NumberOfLines do
  95.     begin
  96.         for j := 0 to NumberOfColumns do
  97.         begin;
  98.             Writeln('Введите элемент матрицы[', i + 1, '][', j + 1, ']: ');
  99.             Matrix[i][j] := InputNumber(-100, 100);
  100.         end;
  101.     end;
  102.     ReceiveMatrixFromConsole := Matrix;
  103. end;
  104.  
  105. Function ReceiveMatrixFromFile(Path: String): TMatrix;
  106.  
  107. var
  108.     InputFile: TextFile;
  109.     Matrix: TMatrix;
  110.     NumberOfLines, NumberOfColumns, i, j: Integer;
  111.     IsCorrect: Boolean;
  112.  
  113. begin;
  114.     repeat
  115.         IsCorrect := true;
  116.         Assign(InputFile, Path);
  117.         Reset(InputFile);
  118.         Read(InputFile, NumberOfLines);
  119.         Readln(InputFile, NumberOfColumns);
  120.         Setlength(Matrix, NumberOfLines, NumberOfColumns);
  121.         Dec(NumberOfLines);
  122.         Dec(NumberOfColumns, 2);
  123.         try
  124.             for i := 0 to NumberOfLines do
  125.             begin
  126.                 for j := 0 to NumberOfColumns do
  127.                 begin
  128.                     Read(InputFile, Matrix[i][j]);
  129.                 end;
  130.                 Readln(InputFile, Matrix[i][NumberOfColumns + 1]);
  131.             end;
  132.         except
  133.             IsCorrect := false;
  134.             Writeln('Некорректные данные в файле!');
  135.             Close(InputFile);
  136.             Path := InputPathToFile;
  137.         end;
  138.     until IsCorrect;
  139.     ReceiveMatrixFromFile := Matrix;
  140. end;
  141.  
  142. procedure CheckMatrix(var Path: String);
  143. var
  144.     InputFile: TextFile;
  145.     Matrix: TMatrix;
  146.     Line: String;
  147.     NumberOfLines, NumberOfColumns, i: Integer;
  148.     IsCorrect: Boolean;
  149. begin
  150.     repeat
  151.         IsCorrect := true;
  152.         Assign(InputFile, Path);
  153.         try
  154.             Reset(InputFile);
  155.             Read(InputFile, NumberOfLines);
  156.             Readln(InputFile, NumberOfColumns);
  157.         except
  158.             IsCorrect := false;
  159.             Writeln('Некорректные данные в файле!');
  160.             Close(InputFile);
  161.             Path := InputPathToFile;
  162.         end;
  163.  
  164.         if (IsCorrect) and ((NumberOfLines < 2) or (NumberOfColumns < 2) or
  165.           (NumberOfLines > 5) or (NumberOfColumns > 5)) then
  166.         begin
  167.             IsCorrect := false;
  168.             Writeln('Некорректные данные в файле!');
  169.             Close(InputFile);
  170.             Path := InputPathToFile;
  171.         end;
  172.  
  173.         if IsCorrect then
  174.         begin
  175.             Setlength(Matrix, NumberOfLines, NumberOfColumns);
  176.             i := 0;
  177.             While (IsCorrect) and (i < NumberOfLines) do
  178.             begin
  179.                 Readln(InputFile, Line);
  180.                 if Length(Line.Split([' '])) <> NumberOfColumns then
  181.                 begin
  182.                     IsCorrect := false;
  183.                     Writeln('Некорректные данные в файле!');
  184.                     Close(InputFile);
  185.                     Path := InputPathToFile();
  186.                 end;
  187.                 Inc(i);
  188.             end;
  189.         end;
  190.     until IsCorrect;
  191.     Close(InputFile);
  192. end;
  193.  
  194. Function ReceiveMatrix(UserWay: Integer): TMatrix;
  195.  
  196. var
  197.     Path: String;
  198.     Matrix: TMatrix;
  199.  
  200. begin;
  201.     case UserWay of
  202.         1:
  203.             begin
  204.                 Matrix := ReceiveMatrixFromConsole();
  205.             end;
  206.         2:
  207.             begin
  208.                 Path := InputPathToFile();
  209.                 CheckMatrix(Path);
  210.                 Matrix := ReceiveMatrixFromFile(Path);
  211.             end;
  212.     end;
  213.     ReceiveMatrix := Matrix;
  214.  
  215. end;
  216.  
  217. Function CounterOfSortedLines(Matrix: TMatrix): Integer;
  218.  
  219. var
  220.     Counter, NumberOfSortedLines, NumberOfLines, NumberOfColumns, i, j: Integer;
  221.  
  222. begin
  223.     NumberOfSortedLines := 0;
  224.     NumberOfLines := Length(Matrix);
  225.     NumberOfColumns := Length(Matrix[0]);
  226.     Dec(NumberOfLines);
  227.     Dec(NumberOfColumns);
  228.     for i := 0 to NumberOfLines do
  229.     begin
  230.         Counter := 0;
  231.         for j := 1 to NumberOfColumns do
  232.         begin
  233.             if (Matrix[i][j - 1] < Matrix[i][j]) then
  234.             begin
  235.                 Inc(Counter);
  236.             end;
  237.         end;
  238.         if (Counter = NumberOfColumns) then
  239.             Inc(NumberOfSortedLines);
  240.     end;
  241.     CounterOfSortedLines := NumberOfSortedLines;
  242. end;
  243.  
  244. Function CheckExtension(Path: String): Boolean;
  245.  
  246. var
  247.     RigthExtension: Boolean;
  248.  
  249. begin
  250.     if (ExtractFileExt(Path) = '.txt') then
  251.         RigthExtension := true
  252.     else
  253.     begin
  254.         Writeln('Неверное расширение файла.');
  255.         RigthExtension := false;
  256.     end;
  257.     CheckExtension := RigthExtension;
  258. end;
  259.  
  260. Function CheckPermission(Path: String): Boolean;
  261.  
  262. var
  263.     OutputFile: TextFile;
  264.     RightPermission: Boolean;
  265.  
  266. begin
  267.     Assign(OutputFile, Path);
  268.     RightPermission := true;
  269.     try
  270.         Rewrite(OutputFile);
  271.         Close(OutputFile);
  272.     except
  273.         Writeln('Файл закрыт для записи.');
  274.         RightPermission := false;
  275.     end;
  276.     CheckPermission := RightPermission;
  277. end;
  278.  
  279. Procedure PrintResultToFile(Path: String; NumberOfSortedLines: Integer);
  280.  
  281. var
  282.     OutputFile: TextFile;
  283.  
  284. begin
  285.     Assign(OutputFile, Path);
  286.     Rewrite(OutputFile);
  287.     Writeln(OutputFile, 'Количество строк, отсортированных по возрастанию: ',
  288.       NumberOfSortedLines);
  289.     Close(OutputFile);
  290. end;
  291.  
  292. Procedure UserWayOfOutput(NumberOfSortedLines: Integer);
  293.  
  294. var
  295.     UserWay: Char;
  296.     Path: String;
  297.  
  298. begin;
  299.     Writeln('Если хотите записать результат в файл, введите "1". Если не хотите - введите другой символ:');
  300.     Readln(UserWay);
  301.     if (UserWay = '1') then
  302.     begin;
  303.         repeat
  304.             Path := InputPathToFile();
  305.         until (CheckPermission(Path));
  306.         PrintResultToFile(Path, NumberOfSortedLines);
  307.         Writeln('Результат записан в файл.');
  308.     end;
  309. end;
  310.  
  311. Procedure PrintResult(NumberOfSortedLines: Integer);
  312.  
  313. begin;
  314.     Writeln('Количество строк, отсортированных по возрастанию: ',
  315.       NumberOfSortedLines);
  316. end;
  317.  
  318. Procedure Main();
  319.  
  320. var
  321.     UserWay, NumberOfSortedLines: Integer;
  322.     Matrix: TMatrix;
  323.  
  324. begin
  325.     Writeln('Программа считает количество строк данной матрицы, которые упорядочены по возрастанию.');
  326.     UserWay := ChooseWayOfInput();
  327.     Matrix := ReceiveMatrix(UserWay);
  328.     NumberOfSortedLines := CounterOfSortedLines(Matrix);
  329.     PrintResult(NumberOfSortedLines);
  330.     UserWayOfOutput(NumberOfSortedLines);
  331.     Writeln('Программа завершена.');
  332.     Readln;
  333. end;
  334.  
  335. begin
  336.     Main();
  337.  
  338. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement