Advertisement
venik2405

lab3_1_0

Dec 2nd, 2020
330
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 4.44 KB | None | 0 0
  1. Program lab3_1;
  2.  
  3. {$APPTYPE CONSOLE}
  4.  
  5. uses
  6.   System.SysUtils;
  7.  
  8. type
  9.     TString = array of string;
  10.  
  11. Function InputFileLocation(): string;
  12. Var
  13.     IsCorrect: Boolean;
  14.     Location: String;
  15. Begin
  16.     Repeat
  17.         IsCorrect := false;
  18.         WriteLn('Enter file location:');
  19.         ReadLn(Location);
  20.         If FileExists(Location) then
  21.             IsCorrect := true
  22.         Else
  23.         Begin
  24.             WriteLn('Please enter the correct location');
  25.             WriteLn('');
  26.         End;
  27.     Until IsCorrect;
  28.     InputFileLocation := Location;
  29. End;
  30.  
  31.  
  32. Function ChooseInput(): integer;
  33. Var
  34.     Line: String;
  35.     IsCorrect: Boolean;
  36. Begin
  37.     Repeat
  38.         IsCorrect := true;
  39.         WriteLn('Do you want to input from file? (y/n)');
  40.         ReadLn(line);
  41.         Line := Line.ToLower();
  42.         If(Line <> '') and (Line <> 'y') and (Line <> 'n') then
  43.         Begin
  44.             IsCorrect := false;
  45.             WriteLn('Enter valid answer');
  46.         End;
  47.     Until IsCorrect;
  48.     If (Line = '') or (Line = 'y') then
  49.         ChooseInput := 0
  50.     Else
  51.         ChooseInput := 1;
  52. End;
  53.  
  54. Procedure GetLineFromFile(Str: String);
  55. var
  56.     IsCorrect: Boolean;
  57.     TFile: TextFile;
  58. begin
  59.     repeat
  60.         IsCorrect := true;
  61.         AssignFile(TFile, InputFileLocation());
  62.         Reset(TFile);
  63.         ReadLn(TFile, Str);
  64.         CloseFile(TFile);
  65.         if (Length(Str) = 0) then
  66.         begin
  67.             WriteLn('One or both strings are empty. Enter another file location');
  68.             IsCorrect := false;
  69.         end;
  70.     until IsCorrect;
  71. end;
  72.  
  73. Procedure OutputToFile(ShortestWordPos: Integer);
  74. Var
  75.     TxtFile: TextFile;
  76. Begin
  77.     AssignFile(TxtFile, InputFileLocation());
  78.     Rewrite(TxtFile);
  79.     Writeln(TxtFile, ShortestWordPos);
  80. End;
  81.  
  82. Function GetLineFromConsole(): String;
  83. var
  84.     IsCorrect: Boolean;
  85.     InputLine: String;
  86. begin
  87.      repeat
  88.         IsCorrect := true;
  89.         ReadLn(InputLine);
  90.         if Length(InputLine) = 0 then
  91.         begin
  92.             WriteLn('The line is empty, re-enter please');
  93.             IsCorrect := false;
  94.         end;
  95.     until IsCorrect;
  96.     GetLineFromConsole := InputLine;
  97. end;
  98.  
  99. Function FindAmountOfWordsInSentence(Str: String): Integer;
  100. Var
  101.     Amount, I: Integer;
  102. Begin
  103.     Amount := 1;
  104.     for I := 1 to High(Str) do
  105.         if Str[I] = ' ' then
  106.             Inc(Amount);
  107.     FindAmountOfWordsInSentence := Amount;
  108. End;
  109.  
  110. Function FindWords(Str: String; Amount: Integer): TString;
  111. Var
  112.     Words: TString;
  113.     I, J: Integer;
  114. Begin
  115.     J := 1;
  116.     SetLength(Words, Amount);
  117.     for I := 0 to Amount - 1 do
  118.     Begin
  119.         while (Str[J] <> ' ') and (J <= length(Str) ) do
  120.         Begin
  121.             Words[I] := Words[I] + Str[J];
  122.             Inc(J);
  123.         End;
  124.         Inc(J);
  125.     End;
  126.     FindWords := Words;
  127. End;
  128.  
  129. Function FindShortestWord(Amount: Integer; Words: TString): String;
  130. Var
  131.     I, MinLength: Integer;
  132.     ShortestWord: String;
  133. Begin
  134.     MinLength := Length(Words[0]);
  135.     for I := 0 to Amount - 1 do
  136.         if MinLength > Length(Words[I]) then
  137.         Begin
  138.             MinLength := Length(Words[I]);
  139.             ShortestWord :=  Words[I];
  140.         End;
  141.     FindShortestWord := ShortestWord;
  142. End;
  143.  
  144. Function FindShortestWordPos(Amount: Integer; Words: TString; ShortestWord, Str: String): Integer;
  145. var
  146.     ShortestWordPos, I: Integer;
  147. Begin
  148.     ShortestWordPos := Pos(ShortestWord, Str);
  149.     FindShortestWordPos := ShortestWordPos;
  150. End;
  151.  
  152. Procedure Main();
  153. Var
  154.     ChosenInput, ShortestWordPos, Amount: Integer;
  155.     Str,  ShortestWord: String;
  156.     Words : TString;
  157. Begin
  158.     WriteLn('Программа находит самое короткое слово в предложении и указывает позицию , с которой но начинается.');
  159.     ChosenInput := ChooseInput();
  160.     If (ChosenInput = 0) then
  161.         GetLineFromFile(Str)
  162.     Else
  163.     Begin
  164.         Writeln('Введите предложение');
  165.         Str := GetLineFromConsole();
  166.     End;
  167.     Amount := FindAmountOfWordsInSentence(Str);
  168.     Words := FindWords(Str, Amount);
  169.     ShortestWord := FindShortestWord(Amount, Words);
  170.     ShortestWordPos := FindShortestWordPos(Amount, Words, ShortestWord, Str);
  171.     writeln('Позиция самого короткого слова предложения');
  172.     Writeln(ShortestWordPos);
  173.     WriteLn;
  174.     OutputToFile(ShortestWordPos);
  175.     ReadLn;
  176. End;
  177.  
  178. Begin
  179.     Main();
  180. End.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement