Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Program Project1;
- Uses
- System.SysUtils;
- Const
- SIZE = 24;
- Type
- TArray = Array[0..SIZE] of Integer;
- TString = Array[0..SIZE] of String;
- Const
- 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');
- Function Choose() : Boolean;
- Var
- InputNumber : Integer;
- IsCorrect : Boolean;
- Const
- MIN_NUM = 0;
- MAX_NUM = 1;
- Begin
- InputNumber := 0;
- Repeat
- IsCorrect := True;
- Try
- Readln(InputNumber);
- Except
- Writeln('Please, enter a number:');
- IsCorrect := False;
- End;
- If (Not(IsCorrect) and ((InputNumber < MIN_NUM) or (InputNumber > MAX_NUM ))) Then
- Begin
- Writeln('You are out of input range!');
- IsCorrect := False;
- End;
- Until IsCorrect;
- If (InputNumber = 1) then
- Choose := True
- Else
- Choose := False;
- End;
- Function ConsoleInputString(): String;
- Var
- IsCorrect: Boolean;
- StrInput: String;
- Begin
- Repeat
- IsCorrect := True;
- Try
- Readln(StrInput);
- Except
- Writeln('Check input data.');
- IsCorrect := False;
- End;
- Until (IsCorrect);
- ConsoleInputString := StrInput;
- End;
- Function FindRepetition (StrInput : String) : TArray;
- Var
- Counter, I, J, WhereIsPair, K: Integer;
- Repeatt : TArray;
- StrForTest : String;
- Begin
- J := 0;
- Counter := 0;
- For K := 0 to High(Repeatt) do
- Repeatt[K] := 0;
- For I := 0 to High(Arr) - 1 do
- Begin
- StrForTest := StrInput;
- Repeat
- If (Pos (Arr[I], StrForTest) <> 0)then
- Begin
- Inc (Counter);
- WhereIsPair := StrForTest.IndexOf(Arr[I], 0);
- StrForTest := StrForTest.Remove(0, WhereIsPair + 1);
- End;
- Until (Pos (Arr[I], StrForTest) = 0);
- If (Counter <> 0) Then
- Repeatt[J] := Counter;
- Inc(J);
- Counter := 0;
- End;
- FindRepetition := Repeatt;
- End;
- Function FindPairs (StrInput : String) : TString;
- Var
- Counter, I, J, WhereIsPair, K : Integer;
- Combinations : TString;
- StrForTest : String;
- Begin
- J := 0;
- Counter := 0;
- For K := 0 to High(Combinations) do
- Combinations[K] := '';
- For I := 0 to High(Arr) - 1 do
- Begin
- StrForTest := StrInput;
- Repeat
- If (Pos (Arr[I], StrForTest) <> 0)then
- Begin
- Inc (Counter);
- WhereIsPair := StrForTest.IndexOf(Arr[I], 0);
- StrForTest := StrForTest.Remove(0, WhereIsPair + 1);
- End;
- Until (Pos (Arr[I], StrForTest) = 0);
- If (Counter <> 0) Then
- Combinations[J] := Arr[I];
- Inc(J);
- Counter := 0;
- End;
- FindPairs := Combinations;
- End;
- Procedure Check (Repeatt : TArray);
- Var
- Counter, I : Integer;
- Const
- SIZE = 25;
- Begin
- Counter := 0;
- For I := 0 to High(Repeatt) do
- If (Repeatt[I] = 0) then
- Inc (Counter);
- If (Counter = SIZE) then
- Writeln('There are no pairs:(');
- End;
- Procedure ConsoleOutput (Repeatt : TArray; Combinations : TString);
- Var
- I : Integer;
- Begin
- For I := 0 to High(Repeatt) do
- If ((Repeatt[I] <> 0) and (Combinations[I] <> '')) then
- Writeln(Combinations[I], ' - ', Repeatt[I], ' times');
- End;
- //files downside, console upside
- Function InputFilePath() : String;
- Var
- IsCorrect : Boolean;
- Path : String;
- Begin
- Writeln('Input path to file: ');
- Repeat
- IsCorrect := True;
- Readln(Path);
- If(Not FileExists(Path)) Then
- Begin
- IsCorrect := False;
- Writeln('Wrong way to file. Input correct path.');
- End
- Else If (ExtractFileExt(Path) <> '.txt') Then
- Begin
- Writeln('Must have .txt');
- IsCorrect := False;
- End;
- Until IsCorrect;
- InputFilePath := Path;
- End;
- Function InputStringFromFile(Path : String) : String;
- Var
- StrInput : String;
- IsCorrect : Boolean;
- InputFile : TextFile;
- Begin
- AssignFile(InputFile, Path);
- Reset(InputFile);
- Repeat
- IsCorrect := True;
- Try
- Readln(InputFile, StrInput);
- Except
- IsCorrect := False;
- Writeln('Mistake of reading string from file.');
- End;
- Until IsCorrect;
- CloseFile(InputFile);
- InputStringFromFile := StrInput;
- End;
- Procedure FileOutput(Path : String; Repeatt : TArray; Combinations : TString);
- Var
- IsCorrect : Boolean;
- OutputFile : TextFile;
- I: Integer;
- Begin
- AssignFile(OutputFile, Path);
- Repeat
- IsCorrect := True;
- Try
- Rewrite(OutputFile);
- Except
- IsCorrect := False;
- Writeln('Mistake with writing in file. Input another path.');
- Path := InputFilePath();
- End;
- Until IsCorrect;
- For I := 0 to High(Repeatt) - 1 do
- If ((Repeatt[I] <> 0) and (Combinations[I] <> '')) then
- Writeln(OutputFile, Combinations[I], ' - ', Repeatt[I], ' times');
- CloseFile(OutputFile);
- Write('Success!');
- End;
- Var
- Chose : Boolean;
- StrInput, Path : String;
- Repeatt : TArray;
- Combinations : TString;
- Begin
- Writeln ('For pairs of adjacent letters (Latin) occurring in a given text, indicate how many times each of these two-letter combinations occurs.');
- Writeln ('Enter type of input.');
- Writeln ('1 is console input, 0 is file input.');
- Chose := Choose();
- If (Chose) then
- Begin
- Writeln('Input lowercase string without spaces:');
- StrInput := ConsoleInputString();
- End
- Else
- Begin
- Path := InputFilePath();
- StrInput := InputStringFromFile(Path);
- End;
- Repeatt := FindRepetition(StrInput);
- Combinations := FindPairs(StrInput);
- Writeln ('Enter type of output.');
- Writeln ('1 is console output, 0 is file output.');
- Chose := Choose();
- If (Chose) then
- Begin
- ConsoleOutput(Repeatt, Combinations);
- Check(Repeatt);
- End
- Else
- Begin
- Path := InputFilePath();
- FileOutput(Path, Repeatt, Combinations);
- End;
- Readln;
- End.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement