Dimaland

lab3(3)№2number9

Nov 27th, 2022 (edited)
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 6.06 KB | None | 0 0
  1. program Project4;
  2.  
  3. {$APPTYPE CONSOLE}
  4.  
  5. Uses
  6.     System.SysUtils;
  7.  
  8. Type
  9.     TMatrix = Array Of Array Of Integer;
  10.  
  11. Function FileConsoleChoice(): Integer;
  12.  
  13. Var
  14.     Choice: Integer;
  15.     IsCorrect: Boolean;
  16.  
  17. Const
  18.     MIN_NUM = 0;
  19.     MAX_NUM = 1;
  20.  
  21. Begin
  22.     Repeat
  23.         IsCorrect := True;
  24.  
  25.         Try
  26.             Readln(Choice);
  27.         Except
  28.             Writeln('Check data correctness.');
  29.             IsCorrect := False;
  30.         End;
  31.  
  32.         If (IsCorrect And (Choice < MIN_NUM) And (Choice > MAX_NUM)) Then
  33.         Begin
  34.             Writeln('Invalid input.');
  35.             IsCorrect := False;
  36.         End;
  37.  
  38.     Until IsCorrect;
  39.  
  40.     FileConsoleChoice := Choice;
  41.  
  42. End;
  43.  
  44. Function InputFilePath(): String;
  45.  
  46. Var
  47.     IsCorrect: Boolean;
  48.     Path: String;
  49.  
  50. Begin
  51.     Writeln('Input path to file: ');
  52.  
  53.     Repeat
  54.         IsCorrect := True;
  55.         Readln(Path);
  56.  
  57.         If(Not FileExists(Path)) Then
  58.         Begin
  59.             IsCorrect := False;
  60.             Writeln('Incorrect way to file.');
  61.         End
  62.         Else If (ExtractFileExt(Path) <> '.txt') Then
  63.         Begin
  64.             Writeln('Must have .txt');
  65.             IsCorrect := False;
  66.         End;
  67.  
  68.     Until IsCorrect;
  69.     InputFilePath := Path;
  70. End;
  71.  
  72. Function InputLengthOfMarix(): Integer;
  73.  
  74. Const
  75.     MAX = 7;
  76.     MIN = 2;
  77.  
  78. Var
  79.     IsCorrect: Boolean;
  80.     Length: Integer;
  81.  
  82. Begin
  83.     Writeln('Enter Size Of Matrix');
  84.  
  85.     Repeat
  86.         IsCorrect := True;
  87.  
  88.         Try
  89.             Readln(Length);
  90.         Except
  91.             Writeln('Type Err');
  92.             Iscorrect := False;
  93.         End;
  94.  
  95.         If (IsCorrect) And ((Length > MAX) Or (Length < MIN)) Then
  96.         Begin
  97.             IsCorrect := False;
  98.             Writeln('RangeErr');
  99.         End;
  100.  
  101.     Until (Iscorrect);
  102.  
  103.     InputLengthOfMarix := Length;
  104. End;
  105.  
  106. Function InputLengthFromFile(Path : String): Integer;
  107.  
  108. Var
  109.     IsCorrect: Boolean;
  110.     Length: Integer;
  111.     InputFile: TextFile;
  112.  
  113. Begin
  114.     AssignFile(InputFile, Path);
  115.     Reset(InputFile);
  116.  
  117.     Try
  118.         Readln(InputFile, Length);
  119.     Except
  120.         Writeln('Mistake of reading word from file.');
  121.     End;
  122.  
  123.     InputLengthFromFile := Length;
  124. End;
  125.  
  126. Function FillInMarixFromFile(Way: String; Length: Integer): TMatrix;
  127. Var
  128.     I, J, N1: Integer;
  129.     InputFile: TextFile;
  130.     Arr: TMatrix;
  131.  
  132. Begin
  133.     SetLength(Arr, Length, Length);
  134.     N1 := Length - 1;
  135.  
  136.     AssignFile(InputFile, Way);
  137.     Reset(InputFile);
  138.     Readln(InputFile);
  139.     For I := 0 To N1 Do
  140.     Begin
  141.         For J := 0 To N1 Do
  142.             Read(InputFile, Arr[I, J]);
  143.     End;
  144.  
  145.     CloseFile(InputFile);
  146.  
  147.     FillInMarixFromFile := Arr;
  148. End;
  149.  
  150. Function FillInMarix(Length: Integer): TMatrix;
  151.  
  152. Const
  153.     MAX = 9;
  154.     MIN = 0;
  155.  
  156. Var
  157.     IsCorrect: Boolean;
  158.     Arr: TMatrix;
  159.     I, J, N1: Integer;
  160.  
  161. Begin
  162.     SetLength(Arr, Length, Length);
  163.     N1 := Length - 1;
  164.     Writeln('Enter Matrix');
  165.     for I := 0 to N1 do
  166.         for J := 0 to N1 do
  167.         begin
  168.             Repeat
  169.                 IsCorrect := True;
  170.  
  171.                 Try
  172.                     Readln(Arr[i,j]);
  173.                 Except
  174.                     Writeln('Type Err');
  175.                     Iscorrect := False;
  176.                 End;
  177.  
  178.                 If (IsCorrect) And ((Length > MAX) Or (Length < MIN)) Then
  179.                 Begin
  180.                     IsCorrect := False;
  181.                     Writeln('RangeErr');
  182.                 End;
  183.  
  184.             Until (Iscorrect);
  185.         end;
  186.  
  187.     FillInMarix := Arr;
  188. End;
  189.  
  190. Function SortMarix(Length: Integer; Arr: TMatrix): TMatrix;
  191.  
  192. const
  193.     FIRST = 0;
  194.  
  195. Var
  196.     IsCorrect: Boolean;
  197.     TempArr: array of Integer;
  198.     E, I, N1, J, Min, IMin: Integer;
  199.  
  200. Begin
  201.     SetLength(TempArr, Length);
  202.     N1 := Length - 1;
  203.  
  204.     for E := 0 to N1 do
  205.     begin
  206.         Min := Arr[E, FIRST];
  207.         imin := E;
  208.         for I := E + 1 to N1 do
  209.         begin
  210.             if Arr[I,FIRST] < Min then
  211.             begin
  212.                 min := arr[i, FIRST];
  213.                 imin := i;
  214.             end;
  215.         end;
  216.  
  217.         for J := 0 To N1 Do
  218.         begin
  219.             TempArr[J] := Arr[E, j];
  220.             Arr[E, j] := Arr[IMIn, j];
  221.             Arr[Imin,j] := TempArr[j];
  222.         end;
  223.     end;
  224.  
  225.  
  226.     SortMarix := Arr;
  227. End;
  228.  
  229. Procedure ConsoleOutput(Arr: TMatrix; Length: Integer);
  230.  
  231. Var
  232.     I, J, N1: Integer;
  233.  
  234. begin
  235.     N1 := Length - 1;
  236.  
  237.     for I := 0 to N1 do
  238.     begin
  239.         for J := 0 to N1 do
  240.             Write(Arr[i,j], ' ');
  241.         Writeln;
  242.     end;
  243.  
  244. end;
  245.  
  246. Procedure FileOutput(Arr: TMatrix; Length: Integer);
  247.  
  248. Var
  249.     IsCorrect: Boolean;
  250.     OutputFile: TextFile;
  251.     Path: String;
  252.     I, J, N1: Integer;
  253. Begin
  254.     Path := InputFilePath();
  255.     AssignFile(OutputFile, Path);
  256.  
  257.     Repeat
  258.         IsCorrect := True;
  259.         Try
  260.             Rewrite(OutputFile);
  261.         Except
  262.             IsCorrect := False;
  263.             Writeln('Mistake with writing in file. Input another path.');
  264.             Path := InputFilePath();
  265.         End;
  266.  
  267.     Until IsCorrect;
  268.  
  269.     N1 := Length - 1;
  270.  
  271.     for I := 0 to N1 do
  272.     begin
  273.         for J := 0 to N1 do
  274.             Write(OutputFile, Arr[i,j], ' ');
  275.         Writeln(OutputFile);
  276.     end;
  277.  
  278.     CloseFile(OutputFile);
  279.  
  280.     Write('Output in file was successful.');
  281. End;
  282.  
  283. Var
  284.     Path: String;
  285.     Length: Integer;
  286.     Arr: TMatrix;
  287.  
  288. Begin
  289.     Writeln('The program arranges the rows of this matrix in ascending order of the elements of the first column.');
  290.     Writeln('Type 0 - console input, type 1 - file input.');
  291.  
  292.     If (FileConsoleChoice()= 0) Then
  293.     begin
  294.         Length := InputLengthOfMarix();
  295.         Arr := FillInMarix(Length);
  296.     End
  297.     Else
  298.     Begin
  299.         Path := InputFilePath();
  300.         Length := InputLengthFromFile(Path);
  301.         Arr := FillInMarixFromFile(Path, Length);
  302.     End;
  303.  
  304.     Arr := SortMarix(Length, Arr);
  305.  
  306.     Writeln('Type 0 - console output, type 1 - file output.');
  307.  
  308.     If (FileConsoleChoice()= 0) Then
  309.         ConsoleOutput(Arr, Length)
  310.     Else
  311.         FileOutput(Arr, Length);
  312.  
  313.     Readln;
  314. End.
Advertisement
Add Comment
Please, Sign In to add comment