Advertisement
Vanya_Shestakov

laba3.2 (Delphi)

Oct 22nd, 2020
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 4.90 KB | None | 0 0
  1. program laba3_2;
  2. uses
  3.     System.SysUtils;
  4.  
  5. function InputLineFromConsole(): String; Forward;
  6. function InputLineFromFile(Path: String): String; Forward;
  7. function InputPath(): String; Forward;
  8. procedure OutputToFile(Path: String; NumberOfVowels: Integer; NumberOfConsonants: Integer); Forward;
  9. procedure OutputToConsole(NumberOfVowels: Integer; NumberOfConsonants: Integer); Forward;
  10.  
  11. function ChooseSource(): Integer;
  12. var
  13.     Choice: Integer;
  14.     IsCorrect: Boolean;
  15. begin
  16.     Writeln('Choose where to enter data. Enter 1 or 2:');
  17.     Writeln('1.File');
  18.     Writeln('2.Console');
  19.     repeat
  20.         IsCorrect := True;
  21.         try
  22.             Readln(Choice);
  23.         except
  24.             Writeln('Enter an integer!');
  25.             IsCorrect := False;
  26.         end;
  27.  
  28.         if (IsCorrect) and (Choice <> 1) and (Choice <> 2) then
  29.         begin
  30.             Writeln('Enter 1 or 2!');
  31.             IsCorrect := False;
  32.         end;
  33.     until IsCorrect;
  34.     ChooseSource := Choice;
  35. end;
  36.  
  37. function InputData(Source: Integer): String;
  38. var
  39.     Line, PathInput: String;
  40. begin
  41.     Case Source of
  42.         1:
  43.         begin
  44.             Writeln('Enter the absolute link to the input file');
  45.             PathInput := InputPath();
  46.             Line := InputLineFromFile(PathInput);
  47.         end;
  48.         2:
  49.         begin
  50.             Line := InputLineFromConsole();
  51.         end;
  52.     end;
  53.     InputData := Line;
  54. end;
  55.  
  56. function InputPath(): String;
  57. var
  58.     Path: String;
  59.     IsCorrect: Boolean;
  60. begin
  61.     repeat
  62.         IsCorrect := True;
  63.         Readln(Path);
  64.  
  65.         if not FileExists(Path) then
  66.         begin
  67.             IsCorrect := False;
  68.             Writeln('File not found! Enter the absolute link to the file');
  69.         end;
  70.     until IsCorrect;
  71.     InputPath := Path;
  72. end;
  73.  
  74. function InputLineFromFile(Path: String): String;
  75. var
  76.     Line: String;
  77.     InputFile: TextFile;
  78. begin
  79.     AssignFile(InputFile, Path);
  80.     Reset(InputFile);
  81.     if not eof(InputFile) then
  82.         Readln(InputFile, Line)
  83.     else
  84.     begin
  85.         Writeln('The line is missing from your file! Enter it from console');
  86.         Line := InputLineFromConsole();
  87.     end;
  88.     CloseFile(InputFile);
  89.     InputLineFromFile := Line;
  90. end;
  91.  
  92. function InputLineFromConsole(): String;
  93. var
  94.     Line: String;
  95.     IsCorrect: Boolean;
  96. begin
  97.     Writeln('Enter the line');
  98.     repeat
  99.         IsCorrect := True;
  100.         Readln(Line);
  101.         if (Length(Line) = 0) then
  102.         begin
  103.             Writeln('You entered an empty line! Repeat enter');
  104.             IsCorrect := False;
  105.         end;
  106.     until IsCorrect;
  107.     InputLineFromConsole := Line;
  108. end;
  109.  
  110. function CalculateVowels(Line: String): Integer;
  111. const
  112.     VOWELS_LETTERS = ['а','о','у','ы','э','я','ё','ю','и','е'];
  113. var
  114.     Amount, I: Integer;
  115. begin
  116.     Amount := 0;
  117.     Line := AnsiLowerCase(Line);
  118.     for I := 1 to Length(Line) do
  119.     begin
  120.         if CharInSet(AnsiString(Line)[I], VOWELS_LETTERS) then
  121.             Amount := Amount + 1;
  122.     end;
  123.     CalculateVowels := Amount;
  124. end;
  125.  
  126. function CalculateConsonants(Line: String): Integer;
  127. const
  128.      CONSONANTS_LETTERS = ['б','в','г','д','ж','з','й','к','л','м','н','п','р','с','т','ф','х','ц','ч','ш','щ'];
  129. var
  130.     Amount, I: Integer;
  131. begin
  132.     Amount := 0;
  133.     Line := AnsiLowerCase(Line);
  134.     for I := 1 to High(Line) do
  135.     begin
  136.         if CharInSet(AnsiString(Line)[i], CONSONANTS_LETTERS) then
  137.             Inc(Amount);
  138.     end;
  139.     CalculateConsonants := Amount;
  140. end;
  141.  
  142. procedure OutputData(Source: Integer; NumberOfVowels: Integer; NumberOfConsonants: Integer);
  143. var
  144.     PathOutput: String;
  145. begin
  146.     if Source = 1 then
  147.     begin
  148.         Writeln;
  149.         Writeln('Enter the absolute link to the output file');
  150.         PathOutput := InputPath();
  151.         OutputToFile(PathOutput, NumberOfVowels, NumberOfConsonants);
  152.     end
  153.     else
  154.     begin
  155.         OutputToConsole(NumberOfVowels, NumberOfConsonants);
  156.     end;
  157. end;
  158.  
  159. procedure OutputToFile(Path: String; NumberOfVowels: Integer; NumberOfConsonants: Integer);
  160. var
  161.     OutputFile: TextFile;
  162. begin
  163.     AssignFile(OutputFile, Path);
  164.     Reset(OutputFile);
  165.     Rewrite(OutputFile);
  166.     Writeln(OutputFile,'Vowels: ', NumberOfVowels, ' Consonants: ',NumberOfConsonants );
  167.     CloseFile(OutputFile);
  168.     Writeln('The data is successfully recorded in the file');
  169. end;
  170.  
  171. procedure OutputToConsole(NumberOfVowels: Integer; NumberOfConsonants: Integer);
  172. begin
  173.     Writeln('Vowels: ', NumberOfVowels, ' Consonants: ',NumberOfConsonants );
  174. end;
  175.  
  176. procedure Main();
  177. var
  178.     Source, numberOfVowels, numberOfConsonants: Integer;
  179.     Line: String;
  180. begin
  181.     Source := ChooseSource();
  182.     Line := InputData(Source);
  183.     NumberOfVowels := CalculateVowels(Line);
  184.     NumberOfConsonants := CalculateConsonants(Line);
  185.     OutputData(Source, NumberOfVowels, NumberOfConsonants);
  186.     Readln;
  187. end;
  188.  
  189. begin
  190.     Main;
  191. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement