Advertisement
believe_me

Untitled

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