Advertisement
believe_me

Untitled

Oct 28th, 2021
219
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.     InputPathToFile := Path;
  77. end;
  78.  
  79. Function ReceiveMatrixFromConsole(): TMatrix;
  80.  
  81. var
  82.     NumberOfLines, NumberOfColumns, i, j: Integer;
  83.     Matrix: TMatrix;
  84.  
  85. begin
  86.     Writeln('Введите количество строк матрицы:');
  87.     NumberOfLines := InputNumber(2, 5);
  88.     Writeln('Введите количество столбцов матрицы:');
  89.     NumberOfColumns := InputNumber(2, 5);
  90.     Setlength(Matrix, NumberOfLines, NumberOfColumns);
  91.     Dec(NumberOfLines);
  92.     Dec(NumberOfColumns);
  93.     for i := 0 to NumberOfLines do
  94.     begin
  95.         for j := 0 to NumberOfColumns do
  96.         begin;
  97.             Writeln('Введите элемент матрицы[', i + 1, '][', j + 1, ']: ');
  98.             Matrix[i][j] := InputNumber(-100, 100);
  99.         end;
  100.     end;
  101.     ReceiveMatrixFromConsole := Matrix;
  102. end;
  103.  
  104. Function ReceiveMatrixFromFile(Path: String): TMatrix;
  105.  
  106. var
  107.     InputFile: TextFile;
  108.     Matrix: TMatrix;
  109.     NumberOfLines, NumberOfColumns, i, j: Integer;
  110.     IsCorrect: Boolean;
  111.  
  112. begin;
  113.     repeat
  114.         IsCorrect := true;
  115.         Assign(InputFile, Path);
  116.         Reset(InputFile);
  117.         Read(InputFile, NumberOfLines);
  118.         Readln(InputFile, NumberOfColumns);
  119.         Setlength(Matrix, NumberOfLines, NumberOfColumns);
  120.         Dec(NumberOfLines);
  121.         Dec(NumberOfColumns, 2);
  122.         try
  123.             for i := 0 to NumberOfLines do
  124.             begin
  125.                 for j := 0 to NumberOfColumns do
  126.                 begin
  127.                     Read(InputFile, Matrix[i][j]);
  128.                 end;
  129.                 Readln(InputFile, Matrix[i][NumberOfColumns + 1]);
  130.             end;
  131.         except
  132.             IsCorrect := false;
  133.             Writeln('Некорректные данные в файле!');
  134.             Close(InputFile);
  135.             Path := InputPathToFile;
  136.         end;
  137.     until IsCorrect;
  138.     ReceiveMatrixFromFile := Matrix;
  139. end;
  140.  
  141. procedure CheckMatrix(var Path: String);
  142. var
  143.     InputFile: TextFile;
  144.     Matrix: TMatrix;
  145.     Line: String;
  146.     NumberOfLines, NumberOfColumns, i: Integer;
  147.     IsCorrect: Boolean;
  148. begin
  149.     repeat
  150.         IsCorrect := true;
  151.         Assign(InputFile, Path);
  152.         try
  153.             Reset(InputFile);
  154.             Read(InputFile, NumberOfLines);
  155.             Readln(InputFile, NumberOfColumns);
  156.         except
  157.             IsCorrect := false;
  158.             Writeln('Некорректные данные в файле!');
  159.             Close(InputFile);
  160.             Path := InputPathToFile;
  161.         end;
  162.  
  163.         if (IsCorrect) and ((NumberOfLines < 2) or (NumberOfColumns < 2) or
  164.           (NumberOfLines > 5) or (NumberOfColumns > 5)) then
  165.         begin
  166.             IsCorrect := false;
  167.             Writeln('Некорректные данные в файле!');
  168.             Close(InputFile);
  169.             Path := InputPathToFile;
  170.         end;
  171.  
  172.         if IsCorrect then
  173.         begin
  174.             Setlength(Matrix, NumberOfLines, NumberOfColumns);
  175.             i := 0;
  176.             While (IsCorrect) and (i < NumberOfLines) do
  177.             begin
  178.                 Readln(InputFile, Line);
  179.                 if Length(Line.Split([' '])) <> NumberOfColumns then
  180.                 begin
  181.                     IsCorrect := false;
  182.                     Writeln('Некорректные данные в файле!');
  183.                     Close(InputFile);
  184.                     Path := InputPathToFile();
  185.                 end;
  186.                 Inc(i);
  187.             end;
  188.         end;
  189.     until IsCorrect;
  190.     Close(InputFile);
  191. end;
  192.  
  193. Function ReceiveMatrix(UserWay: Integer): TMatrix;
  194.  
  195. var
  196.     Path: String;
  197.     Matrix: TMatrix;
  198.  
  199. begin;
  200.     case UserWay of
  201.         1:
  202.             begin
  203.                 Matrix := ReceiveMatrixFromConsole();
  204.             end;
  205.         2:
  206.             begin
  207.                 Path := InputPathToFile();
  208.                 CheckMatrix(Path);
  209.                 Matrix := ReceiveMatrixFromFile(Path);
  210.             end;
  211.     end;
  212.     ReceiveMatrix := Matrix;
  213.  
  214. end;
  215.  
  216. Function CounterOfSortedLines(Matrix: TMatrix): Integer;
  217.  
  218. var
  219.     Counter, NumberOfSortedLines, NumberOfLines, NumberOfColumns, i, j: Integer;
  220.  
  221. begin
  222.     NumberOfSortedLines := 0;
  223.     NumberOfLines := Length(Matrix);
  224.     NumberOfColumns := Length(Matrix[0]);
  225.     Dec(NumberOfLines);
  226.     Dec(NumberOfColumns);
  227.     for i := 0 to NumberOfLines do
  228.     begin
  229.         Counter := 0;
  230.         for j := 1 to NumberOfColumns do
  231.         begin
  232.             if (Matrix[i][j - 1] < Matrix[i][j]) then
  233.             begin
  234.                 Inc(Counter);
  235.             end;
  236.         end;
  237.         if (Counter = NumberOfColumns) then
  238.             Inc(NumberOfSortedLines);
  239.     end;
  240.     CounterOfSortedLines := NumberOfSortedLines;
  241. end;
  242.  
  243. Function CheckExtension(Path: String): Boolean;
  244.  
  245. var
  246.     RigthExtension: Boolean;
  247.  
  248. begin
  249.     if (ExtractFileExt(Path) = '.txt') then
  250.         RigthExtension := true
  251.     else
  252.     begin
  253.         Writeln('Неверное расширение файла.');
  254.         RigthExtension := false;
  255.     end;
  256.     CheckExtension := RigthExtension;
  257. end;
  258.  
  259. Function CheckPermission(Path: String): Boolean;
  260.  
  261. var
  262.     OutputFile: TextFile;
  263.     RightPermission: Boolean;
  264.  
  265. begin
  266.     Assign(OutputFile, Path);
  267.     RightPermission := true;
  268.     try
  269.         Rewrite(OutputFile);
  270.         Close(OutputFile);
  271.     except
  272.         Writeln('Файл закрыт для записи.');
  273.         RightPermission := false;
  274.     end;
  275.     CheckPermission := RightPermission;
  276. end;
  277.  
  278. Procedure PrintResultToFile(Path: String; NumberOfSortedLines: Integer);
  279.  
  280. var
  281.     OutputFile: TextFile;
  282.  
  283. begin
  284.     Assign(OutputFile, Path);
  285.     Rewrite(OutputFile);
  286.     Writeln(OutputFile, 'Количество строк, отсортированных по возрастанию: ',
  287.       NumberOfSortedLines);
  288.     Close(OutputFile);
  289. end;
  290.  
  291. Procedure UserWayOfOutput(NumberOfSortedLines: Integer);
  292.  
  293. var
  294.     UserWay: Char;
  295.     Path: String;
  296.  
  297. begin;
  298.     Writeln('Если хотите записать результат в файл, введите "1". Если не хотите - введите другой символ:');
  299.     Readln(UserWay);
  300.     if (UserWay = '1') then
  301.     begin;
  302.         repeat
  303.             Path := InputPathToFile();
  304.         until (CheckPermission(Path));
  305.         PrintResultToFile(Path, NumberOfSortedLines);
  306.         Writeln('Результат записан в файл.');
  307.     end;
  308. end;
  309.  
  310. Procedure PrintResult(NumberOfSortedLines: Integer);
  311.  
  312. begin;
  313.     Writeln('Количество строк, отсортированных по возрастанию: ',
  314.       NumberOfSortedLines);
  315. end;
  316.  
  317. Procedure Main();
  318.  
  319. var
  320.     UserWay, NumberOfSortedLines: Integer;
  321.     Matrix: TMatrix;
  322.  
  323. begin
  324.     Writeln('Программа считает количество строк данной матрицы, которые упорядочены по возрастанию.');
  325.     UserWay := ChooseWayOfInput();
  326.     Matrix := ReceiveMatrix(UserWay);
  327.     NumberOfSortedLines := CounterOfSortedLines(Matrix);
  328.     PrintResult(NumberOfSortedLines);
  329.     UserWayOfOutput(NumberOfSortedLines);
  330.     Writeln('Программа завершена.');
  331.     Readln;
  332. end;
  333.  
  334. begin
  335.     Main();
  336.  
  337. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement