Advertisement
Vladislav8653

laba 3_1 delphi

Dec 1st, 2022 (edited)
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 6.59 KB | None | 0 0
  1. Program Project1;
  2. Uses
  3.     System.SysUtils;
  4. Const
  5.     SIZE = 24;
  6. Type
  7.     TArray = Array[0..SIZE] of Integer;
  8.     TString = Array[0..SIZE] of String;
  9. Const
  10.     Arr : TString = ('ab', 'bc', 'cd', 'de', 'ef', 'fg', 'gh', 'hi', 'ig', 'gk', 'kl', 'lm', 'mn', 'no', 'op', 'pq', 'qr', 'rs', 'st', 'tu', 'uv', 'vw', 'wz', 'xy', 'yz');
  11.  
  12. Function Choose() : Boolean;
  13. Var
  14.     InputNumber : Integer;
  15.     IsCorrect : Boolean;
  16. Const
  17.     MIN_NUM = 0;
  18.     MAX_NUM = 1;
  19. Begin
  20.     InputNumber := 0;
  21.     Repeat
  22.         IsCorrect := True;
  23.         Try
  24.             Readln(InputNumber);
  25.         Except
  26.             Writeln('Please, enter a number:');
  27.             IsCorrect := False;
  28.         End;
  29.         If (Not(IsCorrect) and ((InputNumber < MIN_NUM) or (InputNumber > MAX_NUM ))) Then
  30.         Begin
  31.             Writeln('You are out of input range!');
  32.             IsCorrect := False;
  33.         End;
  34.     Until IsCorrect;
  35.     If (InputNumber = 1) then
  36.         Choose := True
  37.     Else
  38.         Choose := False;
  39. End;
  40.  
  41. Function ConsoleInputString(): String;
  42. Var
  43.     IsCorrect: Boolean;
  44.     StrInput: String;
  45. Begin
  46.     Repeat
  47.         IsCorrect := True;
  48.         Try
  49.            Readln(StrInput);
  50.         Except
  51.            Writeln('Check input data.');
  52.            IsCorrect := False;
  53.         End;
  54.     Until (IsCorrect);
  55.     ConsoleInputString := StrInput;
  56. End;
  57.  
  58. Function FindRepetition (StrInput : String) : TArray;
  59. Var
  60.     Counter, I, J, WhereIsPair, K: Integer;
  61.     Repeatt : TArray;
  62.     StrForTest : String;
  63. Begin
  64.     J := 0;
  65.     Counter := 0;
  66.     For K := 0 to High(Repeatt) do
  67.         Repeatt[K] := 0;
  68.     For I := 0 to High(Arr) - 1 do
  69.         Begin
  70.             StrForTest := StrInput;
  71.             Repeat
  72.                     If (Pos (Arr[I], StrForTest) <> 0)then
  73.                     Begin
  74.                         Inc (Counter);
  75.                         WhereIsPair := StrForTest.IndexOf(Arr[I], 0);
  76.                         StrForTest := StrForTest.Remove(0, WhereIsPair + 1);
  77.                     End;
  78.             Until (Pos (Arr[I], StrForTest) = 0);
  79.               If (Counter <> 0) Then
  80.                   Repeatt[J] := Counter;
  81.               Inc(J);
  82.               Counter := 0;
  83.         End;
  84.     FindRepetition := Repeatt;
  85. End;
  86.  
  87. Function FindPairs (StrInput : String) : TString;
  88. Var
  89.     Counter, I, J, WhereIsPair, K : Integer;
  90.     Combinations : TString;
  91.     StrForTest : String;
  92. Begin
  93.     J := 0;
  94.     Counter := 0;
  95.     For K := 0 to High(Combinations) do
  96.         Combinations[K] := '';
  97.     For I := 0 to High(Arr) - 1 do
  98.         Begin
  99.             StrForTest := StrInput;
  100.             Repeat
  101.             If (Pos (Arr[I], StrForTest) <> 0)then
  102.                 Begin
  103.                     Inc (Counter);
  104.                     WhereIsPair := StrForTest.IndexOf(Arr[I], 0);
  105.                     StrForTest := StrForTest.Remove(0, WhereIsPair + 1);
  106.                 End;
  107.             Until (Pos (Arr[I], StrForTest) = 0);
  108.             If (Counter <> 0) Then
  109.                 Combinations[J] := Arr[I];
  110.             Inc(J);
  111.             Counter := 0;
  112.         End;
  113.     FindPairs := Combinations;
  114. End;
  115.  
  116. Procedure Check (Repeatt : TArray);
  117. Var
  118.     Counter, I : Integer;
  119. Const
  120.     SIZE = 25;
  121. Begin
  122.     Counter := 0;
  123.     For I := 0 to High(Repeatt) do
  124.         If (Repeatt[I] = 0) then
  125.             Inc (Counter);
  126.     If (Counter = SIZE) then
  127.         Writeln('There are no pairs:(');
  128. End;
  129.  
  130. Procedure ConsoleOutput (Repeatt : TArray; Combinations : TString);
  131. Var
  132.     I : Integer;
  133. Begin
  134.     For I := 0 to High(Repeatt) do
  135.         If ((Repeatt[I] <> 0) and (Combinations[I] <> '')) then
  136.             Writeln(Combinations[I], ' - ', Repeatt[I], ' times');
  137. End;
  138. //files downside, console upside
  139. Function InputFilePath() : String;
  140. Var
  141.     IsCorrect : Boolean;
  142.     Path : String;
  143. Begin
  144.     Writeln('Input path to file: ');
  145.     Repeat
  146.         IsCorrect := True;
  147.         Readln(Path);
  148.         If(Not FileExists(Path)) Then
  149.         Begin
  150.             IsCorrect := False;
  151.             Writeln('Wrong way to file. Input correct path.');
  152.         End
  153.         Else If (ExtractFileExt(Path) <> '.txt') Then
  154.         Begin
  155.             Writeln('Must have .txt');
  156.             IsCorrect := False;
  157.         End;
  158.     Until IsCorrect;
  159.     InputFilePath := Path;
  160. End;
  161.  
  162. Function InputStringFromFile(Path : String) : String;
  163. Var
  164.     StrInput : String;
  165.     IsCorrect : Boolean;
  166.     InputFile : TextFile;
  167. Begin
  168.     AssignFile(InputFile, Path);
  169.     Reset(InputFile);
  170.     Repeat
  171.         IsCorrect := True;
  172.         Try
  173.             Readln(InputFile, StrInput);
  174.         Except
  175.             IsCorrect := False;
  176.             Writeln('Mistake of reading string from file.');
  177.         End;
  178.     Until IsCorrect;
  179.     CloseFile(InputFile);
  180.     InputStringFromFile := StrInput;
  181. End;
  182.  
  183. Procedure FileOutput(Path : String; Repeatt : TArray; Combinations : TString);
  184. Var
  185.     IsCorrect : Boolean;
  186.     OutputFile : TextFile;
  187.     I: Integer;
  188. Begin
  189.     AssignFile(OutputFile, Path);
  190.     Repeat
  191.         IsCorrect := True;
  192.         Try
  193.             Rewrite(OutputFile);
  194.         Except
  195.             IsCorrect := False;
  196.             Writeln('Mistake with writing in file. Input another path.');
  197.             Path := InputFilePath();
  198.         End;
  199.     Until IsCorrect;
  200.      For I := 0 to High(Repeatt) - 1 do
  201.         If ((Repeatt[I] <> 0) and (Combinations[I] <> '')) then
  202.            Writeln(OutputFile, Combinations[I], ' - ', Repeatt[I], ' times');
  203.     CloseFile(OutputFile);
  204.     Write('Success!');
  205. End;
  206.  
  207. Var
  208.     Chose : Boolean;
  209.     StrInput, Path : String;
  210.     Repeatt : TArray;
  211.     Combinations : TString;
  212. Begin
  213.     Writeln ('For pairs of adjacent letters (Latin) occurring in a given text, indicate how many times each of these two-letter combinations occurs.');
  214.     Writeln ('Enter type of input.');
  215.     Writeln ('1 is console input, 0 is file input.');
  216.     Chose := Choose();
  217.     If (Chose) then
  218.         Begin
  219.             Writeln('Input lowercase string without spaces:');
  220.             StrInput := ConsoleInputString();
  221.         End
  222.     Else
  223.         Begin
  224.             Path := InputFilePath();
  225.             StrInput := InputStringFromFile(Path);
  226.         End;
  227.     Repeatt := FindRepetition(StrInput);
  228.     Combinations := FindPairs(StrInput);
  229.     Writeln ('Enter type of output.');
  230.     Writeln ('1 is console output, 0 is file output.');
  231.     Chose := Choose();
  232.     If (Chose) then
  233.         Begin
  234.             ConsoleOutput(Repeatt, Combinations);
  235.             Check(Repeatt);
  236.         End
  237.     Else
  238.         Begin
  239.             Path := InputFilePath();
  240.             FileOutput(Path, Repeatt, Combinations);
  241.         End;
  242.     Readln;
  243. End.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement