Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program lab3_1;
- {$APPTYPE CONSOLE}
- {$R *.res}
- uses
- System.SysUtils;
- type
- TArray = array of String;
- Function TakeSource: Byte;
- const
- CHOOSING_CONSOLE = 1;
- CHOOSING_FILE = 2;
- var
- Source: Byte;
- IsCorrect: Boolean;
- begin
- Source := 0;
- repeat
- IsCorrect := True;
- try
- Readln(Source);
- except
- Write('Incorrect input!!! Select the source:' + #13#10 + '1:Console' + #13#10 + '2:File' + #13#10 + 'Enter 1 or 2: ');
- IsCorrect := False;
- end;
- if (IsCorrect and (Source <> CHOOSING_CONSOLE) and (Source <> CHOOSING_FILE)) then
- begin
- Write('Incorrect input!!! Select the source:' + #13#10 + '1:Console' + #13#10 + '2:File' + #13#10 + 'Enter 1 or 2: ');
- IsCorrect := False;
- end;
- until IsCorrect;
- TakeSource := Source;
- end;
- Function TakeInPath: String;
- var
- Path: String;
- IsCorrect: Boolean;
- InFile: TextFile;
- begin
- Write('Enter file path: ');
- repeat
- IsCorrect := True;
- Readln(Path);
- if not FileExists(Path) then
- begin
- Write('File is not found' + #13#10 + 'Enter file path: ');
- IsCorrect := False;
- end
- else
- if ((Path[Length(Path)] <> 't') or (Path[Length(Path) - 1] <> 'x') or
- (Path[Length(Path)- 2] <> 't') or (Path[Length(Path)- 3] <> '.')) then
- begin
- Write('File is found, but it is not ".txt" type file' + #13#10 + 'Enter file path: ');
- IsCorrect := False;
- end
- else
- begin
- Assign(InFile, Path);
- try
- Reset(InFile);
- except
- Write('Error!!! Program can''t open file!' + #13#10 + 'Enter file path: ');
- IsCorrect := False;
- end;
- end;
- until IsCorrect;
- CloseFile(InFile);
- TakeInPath := Path;
- end;
- Function TakeOutPath: String;
- var
- Path: String;
- IsCorrect: Boolean;
- OutFile: TextFile;
- begin
- Write('Enter file path: ');
- repeat
- IsCorrect := True;
- Readln(Path);
- if ((Path[Length(Path)] <> 't') or (Path[Length(Path) - 1] <> 'x') or
- (Path[Length(Path)- 2] <> 't') or (Path[Length(Path) - 3] <> '.')) then
- begin
- Write('It should be a ".txt" type file' + #13#10 + 'Enter file path: ');
- IsCorrect := False;
- end
- else
- begin
- AssignFile(OutFile, Path);
- try
- Rewrite(OutFile);
- except
- Write('Error!!! There are problems with file!' + #13#10 + 'Enter file path: ');
- IsCorrect := False;
- end;
- end;
- until IsCorrect;
- CloseFile(OutFile);
- TakeOutPath := Path;
- end;
- Function TakeSentenceFromConsole: String;
- var
- Sentence: String;
- begin
- Write('Enter the sentence: ');
- Readln(Sentence);
- TakeSentenceFromConsole := Sentence;
- end;
- Function TakeSentenceFromFile(const Path: String): String;
- var
- InFile: TextFile;
- IsCorrect: Boolean;
- Sentence: String;
- begin
- IsCorrect := True;
- AssignFile(InFile, Path);
- Reset(InFile);
- try
- Read(InFile,Sentence);
- except
- Writeln('Incorrect file content!!!');
- IsCorrect := False;
- end;
- if ((IsCorrect) and (not Eof(InFile))) then
- begin
- Writeln('Incorrect file content!!!');
- IsCorrect := False;
- end;
- if not IsCorrect then
- Sentence := '';
- TakeSentenceFromFile := Sentence;
- CloseFile(InFile);
- end;
- Function TakeSentence(Source: Byte): String;
- var
- InPath: String;
- Sentence: String;
- begin
- if (Source = 1) then
- Sentence := TakeSentenceFromConsole
- else
- begin
- InPath := TakeInPath;
- Sentence := TakeSentenceFromFile(InPath);
- while (Sentence = '') do
- begin
- InPath := TakeInPath;
- Sentence := TakeSentenceFromFile(InPath);
- end;
- end;
- TakeSentence := Sentence;
- end;
- Function MakeSentenceNormal(Sentence: String): String;
- begin
- Sentence := StringReplace(Sentence, '.', ' ', [rfReplaceAll, rfIgnoreCase]);
- Sentence := StringReplace(Sentence, ',', ' ', [rfReplaceAll, rfIgnoreCase]);
- Sentence := StringReplace(Sentence, ',', ' ', [rfReplaceAll, rfIgnoreCase]);
- Sentence := StringReplace(Sentence, '-', ' ', [rfReplaceAll, rfIgnoreCase]);
- Sentence := StringReplace(Sentence, ';', ' ', [rfReplaceAll, rfIgnoreCase]);
- Sentence := StringReplace(Sentence, ':', ' ', [rfReplaceAll, rfIgnoreCase]);
- Sentence := StringReplace(Sentence, '!', ' ', [rfReplaceAll, rfIgnoreCase]);
- Sentence := StringReplace(Sentence, '?', ' ', [rfReplaceAll, rfIgnoreCase]);
- Sentence := StringReplace(Sentence, '—', ' ', [rfReplaceAll, rfIgnoreCase]);
- Sentence := Trim(Sentence);
- MakeSentenceNormal := Sentence;
- end;
- Function FindArrayOfWords(Sentence: String): TArray;
- var
- ArrayOfWords, TotalArrayOfWords: TArray;
- WordsCounter, Index, i: Integer;
- Word: String;
- begin
- Sentence := MakeSentenceNormal(Sentence);
- SetLength(ArrayOfWords, 100);
- WordsCounter := 0;
- Index := Pos(' ', Sentence);
- while (Index <> 0) do
- begin
- Word := Copy(Sentence, 0, Index - 1);
- If (Word <> '') then
- begin
- ArrayOfWords[wordsCounter] := Word;
- WordsCounter := WordsCounter + 1;
- end;
- Sentence := Copy(Sentence, Index + 1);
- Index := Pos(' ', Sentence);
- end;
- if (Sentence <> '') then
- begin
- ArrayOfWords[wordsCounter] := Sentence;
- WordsCounter := WordsCounter + 1;
- end;
- SetLength(TotalArrayOfWords, WordsCounter);
- for i := 0 to (WordsCounter - 1) do
- TotalArrayOfWords[i] := ArrayOfWords[i];
- FindArrayOfWords := TotalArrayOfWords;
- end;
- Function FindAnswer(ArrayOfWords: TArray): Integer;
- var
- Counter, i: Integer;
- begin
- Counter := 0;
- for i := 0 to (Length(ArrayOfWords) - 1) do
- if (AnsiCompareText(ArrayOfWords[i],ArrayOfWords[Length(ArrayOfWords) - 1]) = 0) then
- Counter := Counter + 1;
- FindAnswer := Counter;
- end;
- Procedure OutputToConsole(const Sentence: String; const Answer: Integer);
- begin
- Writeln('Sentence is:', #13#10, Sentence, #13#10, 'Last word founded ', Answer, ' times.');
- end;
- Procedure OutputToFile(const Path: String; const Sentence: String; const Answer: Integer);
- var
- OutFile: TextFile;
- begin
- AssignFile(OutFile, Path);
- Rewrite(OutFile);
- Writeln(OutFile, 'Sentence is:', #13#10, Sentence, #13#10, 'Last word founded ', Answer, ' times.');
- CloseFile(OutFile);
- Writeln('Done');
- end;
- Procedure Output(const Source: Byte; const Sentence: String; const Answer: Integer);
- var
- OutPath: String;
- begin
- if (Source = 1) then
- OutputToConsole(Sentence,Answer)
- else
- begin
- OutPath := TakeOutPath;
- OutputToFile(OutPath,Sentence,Answer);
- end;
- end;
- var
- InputSource, OutputSource: Byte;
- Sentence: String;
- Answer: Integer;
- ArrayOfWords: TArray;
- begin
- Write('Welcome to the program that count how many times the last word occurs in a sentence.' + #13#10 + 'Select the source for entering the sentence:' + #13#10 + '1:Console' + #13#10 + '2:File' + #13#10 + 'Enter 1 or 2: ');
- InputSource := TakeSource();
- Sentence := TakeSentence(InputSource);
- ArrayOfWords := FindArrayOfWords(Sentence);
- Answer := FindAnswer(ArrayOfWords);
- Write('Select the source for output: '+ #13#10 + '1:Console' + #13#10 + '2:File' + #13#10 + 'Enter 1 or 2: ');
- OutputSource := TakeSource();
- Output(OutputSource, Sentence, Answer);
- Readln;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement