Advertisement
Egor_Vakar

lab3_3(Delphi)

Nov 2nd, 2021 (edited)
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 9.01 KB | None | 0 0
  1. program lab3_3dproj;
  2. {$APPTYPE CONSOLE}
  3. {$R *.res}
  4. uses
  5.   System.SysUtils;
  6.  
  7. type
  8.     TArray = array of Integer;
  9.     TAnswers = array of array of Integer;
  10.     TSortStages = array of array of Integer;
  11.  
  12. Function TakeInt(Min,Max: Integer): Integer;
  13. var
  14.     IsCorrect: Boolean;
  15.     Number: Integer;
  16. begin
  17.     Number := 0;
  18.     repeat
  19.         IsCorrect := True;
  20.         try
  21.             Readln(Number);
  22.         except
  23.             Writeln('Incorrect input!!!');
  24.             IsCorrect := False;
  25.         end;
  26.         if (IsCorrect and (Number < Min) or (Number > Max)) then
  27.         begin
  28.             Writeln('Incorrect input!!!');
  29.             IsCorrect := False;
  30.         end;
  31.     until IsCorrect;
  32.     TakeInt := Number;
  33. end;
  34.  
  35. Function TakeSource: Byte;
  36. const
  37.     CHOOSING_CONSOLE = 1;
  38.     CHOOSING_FILE = 2;
  39. var
  40.     Source: Byte;
  41.     IsCorrect: Boolean;
  42. begin
  43.     Source := 0;
  44.     repeat
  45.         IsCorrect := True;
  46.         try
  47.             Readln(Source);
  48.         except
  49.             Write('Incorrect input!!! Select the source:' + #13#10 + '1:Console'  + #13#10 + '2:File'  + #13#10 + 'Enter 1 or 2: ');
  50.             IsCorrect := False;
  51.         end;
  52.         if (IsCorrect and (Source <> CHOOSING_CONSOLE) and (Source <> CHOOSING_FILE)) then
  53.         begin
  54.             Write('Incorrect input!!! Select the source:' + #13#10 + '1:Console'  + #13#10 + '2:File'  + #13#10 + 'Enter 1 or 2: ');
  55.             IsCorrect := False;
  56.         end;
  57.     until IsCorrect;
  58.     TakeSource := Source;
  59. end;
  60.  
  61. Function TakeInPath: String;
  62. var
  63.     Path: String;
  64.     IsCorrect: Boolean;
  65.     InFile: TextFile;
  66. begin
  67.     Write('Enter file path: ');
  68.     repeat
  69.         IsCorrect := True;
  70.         Readln(Path);
  71.         if not FileExists(Path) then
  72.         begin
  73.             Write('File is not found' + #13#10 + 'Enter file path: ');
  74.             IsCorrect := False;
  75.         end
  76.         else
  77.             if ((Path[Length(Path)] <> 't') or (Path[Length(Path) - 1] <> 'x') or
  78.                 (Path[Length(Path)- 2] <> 't') or (Path[Length(Path)- 3] <> '.')) then
  79.             begin
  80.                 Write('File is found, but it is not ".txt" type file' + #13#10 + 'Enter file path: ');
  81.                 IsCorrect := False;
  82.             end
  83.             else
  84.             begin
  85.                 Assign(InFile, Path);
  86.                 try
  87.                     Reset(InFile);
  88.                 except
  89.                     Write('Error!!! Program can''t open file!' + #13#10 + 'Enter file path: ');
  90.                     IsCorrect := False;
  91.  
  92.                 end;
  93.             end;
  94.     until IsCorrect;
  95.     CloseFile(InFile);
  96.     TakeInPath := Path;
  97. end;
  98.  
  99. Function TakeOutPath: String;
  100. var
  101.     Path: String;
  102.     IsCorrect: Boolean;
  103.     OutFile: TextFile;
  104. begin
  105.     Write('Enter file path: ');
  106.     repeat
  107.         IsCorrect := True;
  108.         Readln(Path);
  109.         if ((Path[Length(Path)] <> 't') or (Path[Length(Path) - 1] <> 'x') or
  110.             (Path[Length(Path)- 2] <> 't') or (Path[Length(Path) - 3] <> '.')) then
  111.         begin
  112.             Write('It should be a ".txt" type file' + #13#10 + 'Enter file path: ');
  113.             IsCorrect := False;
  114.         end
  115.         else
  116.         begin
  117.             AssignFile(OutFile, Path);
  118.             try
  119.                 Rewrite(OutFile);
  120.             except
  121.                 Write('Error!!! There are problems with file!' + #13#10 + 'Enter file path: ');
  122.                 IsCorrect := False;
  123.             end;
  124.         end;
  125.     until IsCorrect;
  126.     CloseFile(OutFile);
  127.     TakeOutPath := Path;
  128. end;
  129.  
  130. Function TakeArrayFromConsole: TArray;
  131. const
  132.     MIN_SIZE = 2;
  133.     MAX_SIZE = 200;
  134.     MIN_ELEMENT = -1000;
  135.     MAX_ELEMENT = 1000;
  136. var
  137.     Size, i: Integer;
  138.     Arr: TArray;
  139. begin
  140.     Write('Enter size: ');
  141.     Size := TakeInt(MIN_SIZE, MAX_SIZE);
  142.     SetLength(Arr, Size);
  143.     for i := 0 to (Length(Arr) - 1) do
  144.     begin
  145.         Write('Enter element ', (i + 1), ': ');
  146.         Arr[i] := TakeInt(MIN_ELEMENT, MAX_ELEMENT);
  147.     end;
  148.     TakeArrayFromConsole := Arr;
  149. end;
  150.  
  151. Function TakeArrayFromFile(const Path: String): TArray;
  152. const
  153.     MIN_SIZE = 2;
  154. var
  155.     InFile: TextFile;
  156.     Size, i: Integer;
  157.     IsCorrect: Boolean;
  158.     Arr: TArray;
  159. begin
  160.     IsCorrect := True;
  161.     AssignFile(InFile, Path);
  162.     Reset(InFile);
  163.     Size := 0;
  164.     try
  165.         Read(InFile,Size);
  166.     except
  167.         Writeln('Incorrect file content!!!');
  168.         IsCorrect := False;
  169.     end;
  170.     if (IsCorrect and (Size < MIN_SIZE)) then
  171.     begin
  172.         Write('Incorrect file content!!! The size must be higher than 1');
  173.         IsCorrect := False;
  174.     end;
  175.     i := 0;
  176.     if IsCorrect then
  177.     begin
  178.         SetLength(Arr,Size);
  179.         While (IsCorrect and (i < Size) and (not Eof(InFile))) do
  180.         begin
  181.             try
  182.                 Read(InFile, Arr[i]);
  183.             except
  184.                 Writeln('Incorrect file content!!!');
  185.                 IsCorrect := False;
  186.             end;
  187.             i := i + 1;
  188.         end;
  189.     end;
  190.     if (IsCorrect and((i < Size) or ((not Eof(InFile))))) then
  191.     begin
  192.         Writeln('Incorrect file content!!!');
  193.         IsCorrect := False;
  194.     end;
  195.     if  not IsCorrect then
  196.         SetLength(Arr, 0);
  197.     TakeArrayFromFile := Arr;
  198.     CloseFile(InFile);
  199. end;
  200.  
  201. Function TakeArray(Source: Byte): TArray;
  202. var
  203.     InPath: String;
  204.     Arr: TArray;
  205. begin
  206.     if (Source = 1) then
  207.         Arr := TakeArrayFromConsole
  208.     else
  209.     begin
  210.         InPath := TakeInPath;
  211.         Arr := TakeArrayFromFile(InPath);
  212.         while (Length(Arr) = 0) do
  213.         begin
  214.             InPath := TakeInPath;
  215.             Arr := TakeArrayFromFile(InPath);
  216.         end;
  217.     end;
  218.     TakeArray := Arr;
  219. end;
  220.  
  221. Function FindAnswers(Arr: TArray): TAnswers;
  222. var
  223.     Stages: TSortStages;
  224.     Answers: TAnswers;
  225.     i, j, k, Counter, LeftBorder, RightBorder: Integer;
  226.     WasThereSwap: Boolean;
  227. begin
  228.     SetLength(Stages,Length(Arr),Length(Arr));
  229.     for k := 0 to (Length(Arr) - 1) do
  230.         Stages[0][k] := Arr[k];
  231.     Counter := 1;
  232.     LeftBorder := 0;
  233.     RightBorder := Length(Arr) - 1;
  234.     WasThereSwap := True;
  235.     while ((LeftBorder < RightBorder) and WasThereSwap) do
  236.     begin
  237.         WasThereSwap := False;
  238.         for i := LeftBorder to (RightBorder - 1) do
  239.             if (Arr[i] > Arr[i + 1]) then
  240.             begin
  241.                 Arr[i] := Arr[i] xor Arr[i + 1];
  242.                 Arr[i + 1] := Arr[i + 1] xor Arr[i];
  243.                 Arr[i] := Arr[i] xor Arr[i + 1];
  244.                 WasThereSwap := True;
  245.             end;
  246.         if(wasThereSwap) then
  247.         begin
  248.             for k := 0 to (Length(Arr) - 1) do
  249.                 Stages[Counter][k] := Arr[k];
  250.             Counter := Counter + 1;
  251.         end;
  252.         WasThereSwap := False;
  253.         RightBorder := RightBorder - 1;;
  254.         for i := RightBorder downto (LeftBorder + 1) do
  255.             if (Arr[i - 1] > Arr[i]) then
  256.             begin
  257.                 Arr[i - 1] := Arr[i - 1] xor Arr[i];
  258.                 Arr[i] := Arr[i] xor Arr[i - 1];
  259.                 Arr[i - 1] := Arr[i - 1] xor Arr[i];
  260.                 WasThereSwap := True;
  261.             end;
  262.         LeftBorder := LeftBorder + 1;
  263.         if(wasThereSwap) then
  264.         begin
  265.             for k := 0 to (Length(Arr) - 1) do
  266.                 Stages[Counter + 1][k] := Arr[k];
  267.             Counter := Counter + 1;
  268.         end;
  269.     end;
  270.     SetLength(Answers,Counter,Length(Arr));
  271.     for i := 0 to (Counter - 1) do
  272.         for j := 0 to (Length(Arr) - 1) do
  273.         Answers[i,j] := Stages[i,j];
  274.     FindAnswers := Answers;
  275. end;
  276.  
  277. Procedure OutputToConsole(Answers: TAnswers);
  278. var
  279.     i,j: Integer;
  280. begin
  281.     for i := 0 to (Length(Answers) - 1) do
  282.     begin
  283.         for j := 0 to (Length(Answers[0]) - 1) do
  284.             Write(Answers[i,j], ' ');
  285.         Writeln;
  286.     end;
  287. end;
  288.  
  289. Procedure OutputToFile(const Path: String; Answers: TAnswers);
  290. var
  291.     OutFile: TextFile;
  292.     i,j: Integer;
  293. begin
  294.     AssignFile(OutFile, Path);
  295.     Rewrite(OutFile);
  296.     for i := 0 to (Length(Answers) - 1) do
  297.     begin
  298.         for j := 0 to (Length(Answers[0]) - 1) do
  299.             Write(OutFile, Answers[i,j], ' ');
  300.         Writeln(OutFile);
  301.     end;
  302.     CloseFile(OutFile);
  303.     Writeln('Done');
  304. end;
  305.  
  306. Procedure Output(const Source: Byte; Answers: TAnswers);
  307. var
  308.     OutPath: String;
  309. begin
  310.     if (Source = 1) then
  311.         OutputToConsole(Answers)
  312.     else
  313.     begin
  314.         OutPath := TakeOutPath;
  315.         OutputToFile(OutPath,Answers);
  316.     end;
  317. end;
  318.  
  319. var
  320.     InputSource, OutputSource: Byte;
  321.     Arr: TArray;
  322.     Answers: TAnswers;
  323. begin
  324.     Write('Welcome to the program that will sort an array using cocktail sort' + #13#10+ 'Select the source for entering the array:'  + #13#10 + '1:Console'  + #13#10 + '2:File'  + #13#10 + 'Enter 1 or 2: ');
  325.     InputSource := TakeSource;
  326.     Arr := TakeArray(InputSource);
  327.     Answers := FindAnswers(Arr);
  328.     Write('Select the source for output:'  + #13#10 + '1:Console'  + #13#10 + '2:File'  + #13#10 + 'Enter 1 or 2: ');
  329.     OutputSource := TakeSource;
  330.     Output(OutputSource, Answers);
  331.     Readln;
  332. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement