Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program laba3_2;
- uses
- System.SysUtils;
- function InputLineFromConsole(): String; Forward;
- function InputLineFromFile(Path: String): String; Forward;
- function InputPath(): String; Forward;
- procedure OutputToFile(Path: String; NumberOfVowels: Integer; NumberOfConsonants: Integer); Forward;
- procedure OutputToConsole(NumberOfVowels: Integer; NumberOfConsonants: Integer); Forward;
- function ChooseSource(): Integer;
- var
- Choice: Integer;
- IsCorrect: Boolean;
- begin
- Writeln('Choose where to enter data. Enter 1 or 2:');
- Writeln('1.File');
- Writeln('2.Console');
- repeat
- IsCorrect := True;
- try
- Readln(Choice);
- except
- Writeln('Enter an integer!');
- IsCorrect := False;
- end;
- if (IsCorrect) and (Choice <> 1) and (Choice <> 2) then
- begin
- Writeln('Enter 1 or 2!');
- IsCorrect := False;
- end;
- until IsCorrect;
- ChooseSource := Choice;
- end;
- function InputData(Source: Integer): String;
- var
- Line, PathInput: String;
- begin
- Case Source of
- 1:
- begin
- Writeln('Enter the absolute link to the input file');
- PathInput := InputPath();
- Line := InputLineFromFile(PathInput);
- end;
- 2:
- begin
- Line := InputLineFromConsole();
- end;
- end;
- InputData := Line;
- end;
- function InputPath(): String;
- var
- Path: String;
- IsCorrect: Boolean;
- begin
- repeat
- IsCorrect := True;
- Readln(Path);
- if not FileExists(Path) then
- begin
- IsCorrect := False;
- Writeln('File not found! Enter the absolute link to the file');
- end;
- until IsCorrect;
- InputPath := Path;
- end;
- function InputLineFromFile(Path: String): String;
- var
- Line: String;
- InputFile: TextFile;
- begin
- AssignFile(InputFile, Path);
- Reset(InputFile);
- if not eof(InputFile) then
- Readln(InputFile, Line)
- else
- begin
- Writeln('The line is missing from your file! Enter it from console');
- Line := InputLineFromConsole();
- end;
- CloseFile(InputFile);
- InputLineFromFile := Line;
- end;
- function InputLineFromConsole(): String;
- var
- Line: String;
- IsCorrect: Boolean;
- begin
- Writeln('Enter the line');
- repeat
- IsCorrect := True;
- Readln(Line);
- if (Length(Line) = 0) then
- begin
- Writeln('You entered an empty line! Repeat enter');
- IsCorrect := False;
- end;
- until IsCorrect;
- InputLineFromConsole := Line;
- end;
- function CalculateVowels(Line: String): Integer;
- const
- VOWELS_LETTERS = ['а','о','у','ы','э','я','ё','ю','и','е'];
- var
- Amount, I: Integer;
- begin
- Amount := 0;
- Line := AnsiLowerCase(Line);
- for I := 1 to Length(Line) do
- begin
- if CharInSet(AnsiString(Line)[I], VOWELS_LETTERS) then
- Amount := Amount + 1;
- end;
- CalculateVowels := Amount;
- end;
- function CalculateConsonants(Line: String): Integer;
- const
- CONSONANTS_LETTERS = ['б','в','г','д','ж','з','й','к','л','м','н','п','р','с','т','ф','х','ц','ч','ш','щ'];
- var
- Amount, I: Integer;
- begin
- Amount := 0;
- Line := AnsiLowerCase(Line);
- for I := 1 to High(Line) do
- begin
- if CharInSet(AnsiString(Line)[i], CONSONANTS_LETTERS) then
- Inc(Amount);
- end;
- CalculateConsonants := Amount;
- end;
- procedure OutputData(Source: Integer; NumberOfVowels: Integer; NumberOfConsonants: Integer);
- var
- PathOutput: String;
- begin
- if Source = 1 then
- begin
- Writeln;
- Writeln('Enter the absolute link to the output file');
- PathOutput := InputPath();
- OutputToFile(PathOutput, NumberOfVowels, NumberOfConsonants);
- end
- else
- begin
- OutputToConsole(NumberOfVowels, NumberOfConsonants);
- end;
- end;
- procedure OutputToFile(Path: String; NumberOfVowels: Integer; NumberOfConsonants: Integer);
- var
- OutputFile: TextFile;
- begin
- AssignFile(OutputFile, Path);
- Reset(OutputFile);
- Rewrite(OutputFile);
- Writeln(OutputFile,'Vowels: ', NumberOfVowels, ' Consonants: ',NumberOfConsonants );
- CloseFile(OutputFile);
- Writeln('The data is successfully recorded in the file');
- end;
- procedure OutputToConsole(NumberOfVowels: Integer; NumberOfConsonants: Integer);
- begin
- Writeln('Vowels: ', NumberOfVowels, ' Consonants: ',NumberOfConsonants );
- end;
- procedure Main();
- var
- Source, numberOfVowels, numberOfConsonants: Integer;
- Line: String;
- begin
- Source := ChooseSource();
- Line := InputData(Source);
- NumberOfVowels := CalculateVowels(Line);
- NumberOfConsonants := CalculateConsonants(Line);
- OutputData(Source, NumberOfVowels, NumberOfConsonants);
- Readln;
- end;
- begin
- Main;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement