Advertisement
Vanya_Shestakov

Untitled

Nov 22nd, 2020
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 3.85 KB | None | 0 0
  1. unit MainForm;
  2.  
  3. interface
  4.  
  5. uses
  6.     Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  7.     Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ComCtrls, Vcl.ToolWin;
  8.  
  9. type
  10.   TForm1 = class(TForm)
  11.     ToolBar: TToolBar;
  12.     ToolButtonAbout: TToolButton;
  13.     ToolButtonOpen: TToolButton;
  14.     ToolButtonSave: TToolButton;
  15.     EditOfLine: TEdit;
  16.     LabelInf1: TLabel;
  17.     ButtonCalculate: TButton;
  18.     LabelInf2: TLabel;
  19.     LabelInf3: TLabel;
  20.     OpenDialog: TOpenDialog;
  21.     LabelOfVowels: TLabel;
  22.     LabelOfConsonants: TLabel;
  23.     SaveDialog: TSaveDialog;
  24.     procedure ToolButtonAboutClick(Sender: TObject);
  25.     procedure ToolButtonOpenClick(Sender: TObject);
  26.     procedure ButtonCalculateClick(Sender: TObject);
  27.     function CalculateVowels(Line: string): Integer;
  28.     function CalculateConsonants(Line: string): Integer;
  29.     procedure ToolButtonSaveClick(Sender: TObject);
  30.   private
  31.     { Private declarations }
  32.   public
  33.     { Public declarations }
  34.   end;
  35.  
  36. var
  37.     Form1: TForm1;
  38.  
  39. implementation
  40.  
  41. {$R *.dfm}
  42.  
  43. procedure TForm1.ButtonCalculateClick(Sender: TObject);
  44. var
  45.     Line: string;
  46. begin
  47.     Line := EditOfLine.Text;
  48.     if Length(Line) <> 0   then
  49.     begin
  50.         LabelOfVowels.Caption := IntToStr(CalculateVowels(Line));
  51.         LabelOfConsonants.Caption := IntToStr(CalculateConsonants(Line));
  52.     end
  53.     else
  54.         MessageDlg('Enter the line!', mtError, [mbOK], 0);
  55. end;
  56.  
  57. function TForm1.CalculateVowels(Line: string): Integer;
  58. const
  59.     VOWELS_LETTERS = ['а','о','у','ы','э','я','ё','ю','и','е'];
  60. var
  61.     Amount, I: Integer;
  62. begin
  63.     Amount := 0;
  64.     Line := AnsiLowerCase(Line);
  65.     for I := 1 to Length(Line) do
  66.     begin
  67.         if AnsiString(Line)[i] in VOWELS_LETTERS then
  68.             Amount := Amount + 1;
  69.     end;
  70.     CalculateVowels := Amount;
  71. end;
  72.  
  73. function TForm1.CalculateConsonants(Line: string): Integer;
  74. const
  75.      CONSONANTS_LETTERS = ['б','в','г','д','ж','з','й','к','л','м','н','п','р','с','т','ф','х','ц','ч','ш','щ'];
  76. var
  77.     Amount, I: Integer;
  78.     Consonants: set of char;
  79. begin
  80.     Amount := 0;
  81.     Line := AnsiLowerCase(Line);
  82.     for I := 1 to High(Line) do
  83.     begin
  84.         if AnsiString(Line)[i] in CONSONANTS_LETTERS then
  85.             Inc(Amount);
  86.     end;
  87.     CalculateConsonants := Amount;
  88. end;
  89.  
  90. procedure TForm1.ToolButtonAboutClick(Sender: TObject);
  91. begin
  92.     MessageDlg('The program describes the set of vowels and consonants of the Russian language,' +
  93.     'and determines the number of vowels and consonants' +
  94.     'in a sentence entered from the keyboard.', mtInformation, [mbOK], 0);
  95. end;
  96.  
  97. procedure TForm1.ToolButtonOpenClick(Sender: TObject);
  98. var
  99.     InputFile: TextFile;
  100.     IsCorrect: Boolean;
  101.     Line: string;
  102. begin
  103.     if OpenDialog.Execute then
  104.     begin
  105.         AssignFile(InputFile,OpenDialog.FileName);
  106.         Reset(InputFile);
  107.         if not EOF(InputFile) then
  108.         begin
  109.             Readln(InputFile, Line);
  110.             if not SeekEOF(InputFile) then
  111.                 MessageDlg('Part of the file was not read. The program only reads the first line!', mtWarning, [mbOK], 0);
  112.         end
  113.         else
  114.             MessageDlg('File is empty', mtError, [mbOK], 0);
  115.         CloseFile(InputFile);
  116.     end;
  117.     EditOfLine.Text := Line;
  118. end;
  119.  
  120. procedure TForm1.ToolButtonSaveClick(Sender: TObject);
  121. var
  122.     OutputFile: TextFile;
  123. begin
  124.     if SaveDialog.Execute then
  125.     begin
  126.         try
  127.             AssignFile(OutputFile, SaveDialog.FileName);
  128.             Rewrite(OutputFile);
  129.             Writeln(OutputFile, 'Vowels: ', LabelOfVowels.Caption);
  130.             Writeln(OutputFile, 'Consonants: ', LabelOfConsonants.Caption);
  131.             CloseFile(OutputFile);
  132.         except
  133.             MessageDlg('Acces error!', mtError, [mbOK], 0);
  134.         end;
  135.     end
  136. end;
  137. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement