Advertisement
lithie_oce

Lab 2.3 delphi

Nov 1st, 2023
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 6.72 KB | Source Code | 0 0
  1. Program lab2task3;
  2.  
  3. {$APPTYPE CONSOLE}
  4.  
  5. Uses
  6.     SysUtils;
  7.  
  8. Type
  9.     MyMatrix = Array of Array of Integer;
  10.  
  11. Function ChoiceCheck() : Integer;
  12. Var
  13.     Choice: Integer;
  14.     IsCorrect: Boolean;
  15. Begin
  16.     Repeat
  17.         IsCorrect:=True;
  18.         Try
  19.             Readln(Choice);
  20.         Except
  21.             Writeln('Error! Input a number');
  22.             IsCorrect := False;
  23.         End;
  24.         If (IsCorrect And ((Choice < 1) Or (Choice > 2))) Then
  25.         Begin
  26.             Writeln('Error! Input 1 or 2');
  27.             IsCorrect := False;
  28.         End;
  29.     Until  (IsCorrect);
  30.     ChoiceCheck := Choice;
  31. End;
  32.  
  33. Function InputCheck(): Integer;
  34. Var
  35.     IsCorrect: Boolean;
  36.     N: Integer;
  37. Begin
  38.     Writeln ('Input N');
  39.     Repeat
  40.         IsCorrect := True;
  41.         Try
  42.             Readln(N);
  43.         Except
  44.             Writeln('Error! Input a number');
  45.             IsCorrect := False;
  46.         End;
  47.         If (N < 1) Then
  48.         Begin
  49.             IsCorrect := False;
  50.             Writeln('Error! Input a number greater than 0');
  51.         End;
  52.     Until  (IsCorrect);
  53.     InputCheck := N;
  54. End;
  55.  
  56. Function InputCheckMatrix(N: Integer; Matr: MyMatrix): MyMatrix;
  57. Var
  58.     I, J, N1: Integer;
  59.     IsCorrect: Boolean;
  60. Begin
  61.     N1 := N-1;
  62.     For I := 1 To N Do
  63.     For J := 0 To N1 Do
  64.     Repeat
  65.         IsCorrect := True;
  66.         Try
  67.             Readln(Matr[I][J]);
  68.         Except
  69.             Writeln('Error! Input a number');
  70.             IsCorrect := False;
  71.         End;
  72.     Until  (IsCorrect);
  73.     InputCheckMatrix := Matr;
  74. End;
  75.  
  76. Function CheckInputFilePath(): String;
  77. Var
  78.     Path: String;
  79.     IsCorrect: Boolean;
  80.     InputFile: File;
  81. Begin
  82.     Repeat
  83.         Writeln('Input path to the file');
  84.         IsCorrect := True;
  85.         Readln(Path);
  86.         If (Not(FileExists(Path))) Then
  87.         Begin
  88.             IsCorrect := False;
  89.             Writeln('Could not find the file');
  90.         End;
  91.         If (IsCorrect) Then
  92.         Begin
  93.             Try
  94.                 AssignFile(InputFile, Path);
  95.                 ReSet(InputFile);
  96.             Except
  97.                 IsCorrect := False;
  98.                 Writeln('Could not open the file');
  99.             End;
  100.         End;
  101.     Until (IsCorrect);
  102.     CloseFile(InputFile);
  103.     CheckInputFilePath := Path;
  104. End;
  105.  
  106. Function FileCheckMatrix(Var N: Integer) : MyMatrix;
  107. Var
  108.     Matr: MyMatrix;
  109.     InputFile: TextFile;
  110.     I, J: Integer;
  111.     Path: String;
  112.     IsCorrect: Boolean;
  113. Begin
  114.     Repeat
  115.         Path := CheckInputFilePath();
  116.         IsCorrect := True;
  117.         AssignFile(InputFile, Path);
  118.         Try
  119.             Reset(InputFile);
  120.         Except
  121.             Writeln('Could not open the file');
  122.             IsCorrect := False;
  123.         End;
  124.         Try
  125.             Readln(InputFile, N);
  126.         Except
  127.             IsCorrect := False;
  128.             Writeln ('The data is incorrect');
  129.         End;
  130.         If (N < 1) Then
  131.         Begin
  132.             IsCorrect := False;
  133.             Writeln ('The data is incorrect');
  134.         End;
  135.         If (IsCorrect) Then
  136.         Begin
  137.             SetLength(Matr, N+2, N);
  138.             For I := 1 To N Do
  139.                 For J := 0 To High(Matr[0]) Do
  140.                 Try
  141.                     Read(InputFile, Matr[I][J]);
  142.                 Except
  143.                     IsCorrect := False;
  144.                     Writeln('The data is incorrect');
  145.                 End;
  146.             If (Not(EoF(InputFile))) Then
  147.             Begin
  148.                 IsCorrect := False;
  149.                 Writeln ('The data is incorrect');
  150.             End;
  151.         End;
  152.     Until (IsCorrect);
  153.     Close(InputFile);
  154.     FileCheckMatrix := Matr;
  155. End;
  156.  
  157. Function InputChoice (Var N: Integer; Matr: MyMatrix): MyMatrix;
  158. Var
  159.     Choice: Integer;
  160. Begin
  161.     Writeln('Choose input option: ', #10, '1.Input from console', #10,
  162.             '2.Input from file');
  163.     Choice := ChoiceCheck();
  164.     If (Choice = 1)  Then
  165.     Begin
  166.         N := InputCheck();
  167.         SetLength(Matr, N+2, N);
  168.         Matr := InputCheckMatrix(N, Matr);
  169.     End
  170.     Else
  171.     Begin
  172.         Matr := FileCheckMatrix(N);
  173.     End;
  174.     InputChoice := Matr;
  175. End;
  176.  
  177. Procedure Swap(Var N: Integer; Matr: MyMatrix);
  178. Var
  179.     J, I, N1: Integer;
  180.     HelpArray1, HelpArray2: Array of Integer;
  181. Begin
  182.     SetLength(HelpArray1, N);
  183.     SetLength(HelpArray2, N);
  184.     For J := 0 to N Do
  185.     Begin
  186.         HelpArray1[J] := Matr[1][J];
  187.         Matr[1][J] := 0;
  188.     End;
  189.     N1 := N+1;
  190.     For I := 2 To N1 Do
  191.     For J := 0 To N Do
  192.     Begin
  193.         HelpArray2[J] := Matr[I][J];
  194.         Matr[I][J] := HelpArray1[J];
  195.         HelpArray1[J] := HelpArray2[J];
  196.     End;
  197. End;
  198.  
  199. Function CheckOutputFilePath(): String;
  200. Var
  201.     Path: String;
  202.     IsCorrect: Boolean;
  203.     OutputFile: File;
  204. Begin
  205.     Writeln('Input file path and the name of the file for', #10, 'example, С:\Projects\Number\FileName.txt. If the ', #10, 'file does not exist, then it will be created', #10,'automatically in the root folder of the program');
  206.     IsCorrect := True;
  207.     Readln(Path);
  208.     If (Not(FileExists(Path))) Then
  209.     Begin
  210.         IsCorrect := False;
  211.         Writeln('Could not find the file');
  212.     End;
  213.     If (IsCorrect)Then
  214.     Try
  215.         AssignFile(OutputFile, Path);
  216.         ReSet(OutputFile);
  217.     Except
  218.         IsCorrect := False;
  219.         Writeln('Could not open the file');
  220.     End;
  221.     If (Not(IsCorrect)) Then
  222.     Begin
  223.     Writeln ('File will be created in the root folder of the program');
  224.     Path := 'Result.txt';
  225.     End
  226.     Else
  227.     CloseFile(OutputFile);
  228.     CheckOutputFilePath := Path;
  229. End;
  230.  
  231. Procedure OutputMatrix(Var N: Integer; Matr: MyMatrix);
  232. Var
  233.     I, J, N1: Integer;
  234. Begin
  235.     N1 := N+1;
  236.     Dec(N);
  237.     For I := 0 to N1 Do
  238.     Begin
  239.         For J := 0 to N Do
  240.         Write(Matr[I][J], ' ');
  241.         Writeln;
  242.     End;
  243. End;
  244.  
  245. Procedure OutputFile (Var N: Integer; Matr: MyMatrix);
  246. Var
  247.     Path: String;
  248.     N1, I, J: Integer;
  249.     OutputFile: TextFile;
  250. Begin
  251.     Path := CheckOutputFilePath();
  252.     AssignFile (OutputFile, Path);
  253.     ReWrite(OutputFile);
  254.     N1 := N+1;
  255.     Dec(N);
  256.     For I := 0 to N1 Do
  257.     Begin
  258.         For J := 0 to N Do
  259.         Write(OutputFile, Matr[I][J], ' ');
  260.     Write(OutputFile, #10);
  261.     End;
  262.     Close(OutputFile);
  263. End;
  264.  
  265. Procedure OutputChoice (Var N: Integer; Matr: MyMatrix);
  266. Var
  267.     Choice: Integer;
  268. Begin
  269.     Writeln('Choose output option: ', #10, '1.Output through console', #10,
  270.               '2.Output through file');
  271.     Choice := ChoiceCheck();
  272.     If (Choice = 1) Then
  273.     OutputMatrix(N, Matr)
  274.     Else
  275.     OutputFile(N, Matr);
  276. End;
  277.  
  278. Var
  279.     Matr: MyMatrix;
  280.     N: Integer;
  281. Begin
  282.     N := 0;
  283.     Matr := InputChoice(N, Matr);
  284.     Swap(N, Matr);
  285.     OutputChoice(N, Matr);
  286.     Readln
  287. End.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement