d1bs

Lab3_1

Dec 4th, 2024 (edited)
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 9.86 KB | None | 0 0
  1. Program Lab3_1;
  2.  
  3. Uses
  4.     System.SysUtils;
  5.  
  6. Type
  7.     TErrorCode = (CORRECT,
  8.         INCORRECT_CHOICE, NON_NUMERIC,
  9.         OUT_OF_RANGE, FILE_NOT_TXT,
  10.         FILE_NOT_EXIST, FILE_NOT_READABLE,
  11.         FILE_NOT_WRITABLE,
  12.         FILE_IS_EMPTY, READING_GO_WRONG,
  13.         FILE_NOT_CLOSE, STRING_OUT_OF_RANGE);
  14.  
  15. Const
  16.     MIN_LEN = 1;
  17.     MAX_LEN = 50;
  18.     MAX_OPTION = 2;
  19.     MIN_OPTION = 1;
  20.     Err: Array [TErrorCode] Of String = ('',
  21.         'Error. Incorrect choice. Please try again.',
  22.         'Error. Non-numeric value. Please try again.',
  23.         'Error. Out of Range. Please try again.',
  24.         'Error. File not .txt. Please try again.',
  25.         'Error. File does not exist. Please try again.',
  26.         'Error. File not readable. Please try again.',
  27.         'Error. File not writable. Please try again.',
  28.         'Error. File is empty. Please try again.',
  29.         'Error. Reading went wrong. Please try again.',
  30.         'Error. File not closable. Please try again.',
  31.         'Error. String out of Range. Please try again.');
  32.  
  33. Procedure ProgramTask();
  34. Begin
  35.     Writeln('This program searches for the shortest word in a sentence and indicates the position at which it begins.');
  36. End;
  37.  
  38. Function ExtractSubstring(Var Str: String; PosStart, PosEnd: Integer): String;
  39. Var
  40.     I: Integer;
  41.     PartStr: String;
  42. Begin
  43.     PartStr := '';
  44.     For I := PosStart To PosEnd Do
  45.         PartStr := PartStr + Str[I];
  46.     ExtractSubstring := PartStr;
  47. End;
  48.  
  49. Function IsFileTxt(PathToFile: String): TErrorCode;
  50. Var
  51.     ErrorCode: TErrorCode;
  52. Begin
  53.     ErrorCode := CORRECT;
  54.     If (Length(PathToFile) < 5) Or (ExtractSubstring(PathToFile, Length(PathToFile) - 3, Length(PathToFile)) <> '.txt') Then
  55.         ErrorCode := FILE_NOT_TXT;
  56.     IsFileTxt := ErrorCode;
  57. End;
  58.  
  59. Function IsFileExist(PathToFile: String): TErrorCode;
  60. Var
  61.     ErrorCode: TErrorCode;
  62. Begin
  63.     ErrorCode := CORRECT;
  64.     If Not FileExists(PathToFile) Then
  65.         ErrorCode := FILE_NOT_EXIST;
  66.     IsFileExist := ErrorCode;
  67. End;
  68.  
  69. Function IsFileReadable(Var FileName: TextFile): TErrorCode;
  70. Var
  71.     ErrorCode: TErrorCode;
  72. Begin
  73.     ErrorCode := CORRECT;
  74.     Try
  75.         Reset(FileName);
  76.     Except
  77.         ErrorCode := FILE_NOT_READABLE
  78.     End;
  79.     IsFileReadable := ErrorCode;
  80. End;
  81.  
  82. Function IsFileWritable(Var FileName: TextFile): TErrorCode;
  83. Var
  84.     ErrorCode: TErrorCode;
  85. Begin
  86.     ErrorCode := CORRECT;
  87.     Try
  88.         Append(FileName);
  89.     Except
  90.         ErrorCode := FILE_NOT_WRITABLE;
  91.     End;
  92.     IsFileWritable := ErrorCode;
  93. End;
  94.  
  95. Function IsFileCloseable(Var FileName: TextFile): TErrorCode;
  96. Var
  97.     ErrorCode: TErrorCode;
  98. Begin
  99.     ErrorCode := CORRECT;
  100.     Try
  101.         CloseFile(FileName);
  102.     Except
  103.         ErrorCode := FILE_NOT_CLOSE
  104.     End;
  105.     IsFileCloseable := ErrorCode;
  106. End;
  107.  
  108. Procedure GetInputFilePath(Var FileName: TextFile);
  109. Var
  110.     Error: TErrorCode;
  111.     PathToFile: String;
  112. Begin
  113.     Repeat
  114.         Readln(PathToFile);
  115.         Error := IsFileTxt(PathToFile);
  116.         If Error = CORRECT Then
  117.         Begin
  118.             Error := IsFileExist(PathToFile);
  119.             AssignFile(FileName, PathToFile);
  120.         End;
  121.         If Error = CORRECT Then
  122.             Error := IsFileReadable(FileName);
  123.         If (Error = CORRECT) And (EOF(FileName)) Then
  124.             Error := FILE_IS_EMPTY;
  125.         If Error <> CORRECT Then
  126.             Writeln(ERR[Error]);
  127.     Until Error = CORRECT;
  128. End;
  129.  
  130. Procedure GetOutputFilePath(Var FileName: TextFile);
  131. Var
  132.     Error: TErrorCode;
  133.     PathToFile: String;
  134. Begin
  135.     Repeat
  136.         Readln(PathToFile);
  137.         Error := IsFileTxt(PathToFile);
  138.         If Error = CORRECT Then
  139.         Begin
  140.             Error := IsFileExist(PathToFile);
  141.             AssignFile(FileName, PathToFile);
  142.         End;
  143.         If Error = CORRECT Then
  144.             Error := IsFileWritable(FileName);
  145.         If Error <> CORRECT Then
  146.             Writeln(ERR[Error]);
  147.     Until Error = CORRECT;
  148. End;
  149.  
  150. Function ReadStringFromConsole(): String;
  151. Var
  152.     Str: String;
  153.     ErrorCode: TErrorCode;
  154. Begin
  155.     Repeat
  156.         ErrorCode := CORRECT;
  157.         Writeln('Enter a sentence: ');
  158.         Readln(Str);
  159.         If (Length(Str) < MIN_LEN) Or (Length(Str) > MAX_LEN) Then
  160.             ErrorCode := STRING_OUT_OF_RANGE;
  161.         If ErrorCode <> CORRECT Then
  162.             Write(ERR[ErrorCode]);
  163.     Until ErrorCode = CORRECT;
  164.     ReadStringFromConsole := Str;
  165. End;
  166.  
  167. Function ReadStringFromFile(Var FileName: TextFile): String;
  168. Var
  169.     Str: String;
  170.     ErrorCode: TErrorCode;
  171. Begin
  172.     Writeln('Enter the path to the file with the extension ".txt" to record the result: ');
  173.     GetInputFilePath(FileName);
  174.     ErrorCode := CORRECT;
  175.     Try
  176.         ReadLn(FileName, Str);
  177.     Except
  178.         ErrorCode := READING_GO_WRONG;
  179.     End;
  180.     If ErrorCode = CORRECT Then
  181.     Begin
  182.         If (Length(Str) < MIN_LEN) Or (Length(Str) > MAX_LEN) Then
  183.             ErrorCode := STRING_OUT_OF_RANGE;
  184.     End;
  185.     If ErrorCode <> CORRECT Then
  186.     Begin
  187.         Writeln(ERR[ErrorCode]);
  188.         Str := ReadStringFromConsole();
  189.     End;
  190.     ErrorCode := IsFileCloseable(FileName);
  191.     ReadStringFromFile := Str;
  192. End;
  193.  
  194. Function IsDelimiter(Ch: Char): Boolean;
  195. Begin
  196.     IsDelimiter := CharInSet(Ch, [' ', ',', '.', '!', '?', ';', ':', '-', '"']);
  197. End;
  198.  
  199. Procedure FindShortestWords(Sentence: String; Var ShortestWords: String; Var Positions: Array Of Integer; Var PositionCount: Integer);
  200. Var
  201.     CurrentPos, WordLength, WordStart: Integer;
  202.     Word: String;
  203. Begin
  204.     CurrentPos := 1;
  205.     Word := '';
  206.     ShortestWords := '';
  207.     WordLength := MAX_LEN;
  208.     WordStart := 1;
  209.     PositionCount := 0;
  210.  
  211.     While CurrentPos <= Length(Sentence) Do
  212.     Begin
  213.         If Not IsDelimiter(Sentence[CurrentPos]) Then
  214.         Begin
  215.             If Word = '' Then
  216.                 WordStart := CurrentPos;
  217.             Word := Word + Sentence[CurrentPos];
  218.         End
  219.         Else
  220.         Begin
  221.             If (WordLength > Length(Word)) And (Word <> '') Then
  222.             Begin
  223.                 ShortestWords := Word;
  224.                 WordLength := Length(ShortestWords);
  225.                 Positions[0] := WordStart;
  226.                 PositionCount := 1;
  227.             End
  228.             Else
  229.             Begin
  230.                 If (WordLength = Length(Word)) Then
  231.                 Begin
  232.                     ShortestWords := ShortestWords + ' ' + Word;
  233.                     Positions[PositionCount] := WordStart;
  234.                     Inc(PositionCount);
  235.                 End;
  236.             End;
  237.             Word := '';
  238.         End;
  239.         Inc(CurrentPos);
  240.     End;
  241.     If (Word <> '') Then
  242.     Begin
  243.         If (WordLength > Length(Word)) Then
  244.         Begin
  245.             ShortestWords := Word;
  246.             WordLength := Length(ShortestWords);
  247.             Positions[0] := WordStart;
  248.             PositionCount := 1;
  249.         End
  250.         Else
  251.             If (WordLength = Length(Word)) Then
  252.             Begin
  253.                 ShortestWords := ShortestWords + ' ' + Word;
  254.                 Positions[PositionCount] := WordStart;
  255.                 Inc(PositionCount);
  256.             End;
  257.     End;
  258. End;
  259.  
  260. Function GetUserOption(): Integer;
  261. Var
  262.     ErrorCode: TErrorCode;
  263.     Option: Integer;
  264. Begin
  265.     Option := 0;
  266.     Repeat
  267.         ErrorCode := CORRECT;
  268.         Try
  269.             Readln(Option);
  270.         Except
  271.             ErrorCode := NON_NUMERIC;
  272.         End;
  273.         If (ErrorCode = CORRECT) And ((Option < MIN_OPTION) Or (Option > MAX_OPTION)) Then
  274.             ErrorCode := INCORRECT_CHOICE;
  275.         If ErrorCode <> CORRECT Then
  276.             Write(ERR[ErrorCode]);
  277.     Until ErrorCode = CORRECT;
  278.     GetUserOption := Option;
  279. End;
  280.  
  281. Procedure ChooseInputMethod(Var Sentence: String; Var FileName: TextFile);
  282. Var
  283.     Option: Integer;
  284. Begin
  285.     Writeln('If you want to enter a sentence from the console, enter: 1');
  286.     Writeln('If you want to read from a file, enter: 2');
  287.     Option := GetUserOption();
  288.     If Option = 1 Then
  289.         Sentence := ReadStringFromConsole()
  290.     Else
  291.         Sentence := ReadStringFromFile(FileName);
  292. End;
  293.  
  294. Procedure PrintConsole(Sentence: String);
  295. Var
  296.     ShortestWords: String;
  297.     Positions: Array Of Integer;
  298.     PositionCount, I: Integer;
  299. Begin
  300.     ShortestWords := '';
  301.     SetLength(Positions, MAX_LEN);
  302.     FindShortestWords(Sentence, ShortestWords, Positions, PositionCount);
  303.     Writeln('Shortest word(s): ', ShortestWords);
  304.     Writeln('Positions: ');
  305.     For I := 0 To PositionCount - 1 Do
  306.         Writeln(Positions[I]);
  307. End;
  308.  
  309. Procedure PrintFile(Sentence: String);
  310. Var
  311.     FileName: TextFile;
  312.     ShortestWords: String;
  313.     Positions: Array Of Integer;
  314.     PositionCount, I: Integer;
  315.     ErrorCode: TErrorCode;
  316. Begin
  317.     WriteLn('Enter the path to the file with the extension ".txt" to record the result:');
  318.     GetOutputFilePath(FileName);
  319.     ErrorCode := IsFileWritable(FileName);
  320.  
  321.     If ErrorCode = CORRECT Then
  322.     Begin
  323.         Rewrite(FileName);
  324.         SetLength(Positions, MAX_LEN);
  325.         FindShortestWords(Sentence, ShortestWords, Positions, PositionCount);
  326.         Writeln(FileName, 'Shortest word(s): ', ShortestWords);
  327.         Writeln(FileName, 'Positions: ');
  328.         For I := 0 To PositionCount - 1 Do
  329.             Writeln(FileName, Positions[I]);
  330.         ErrorCode := IsFileCloseable(FileName);
  331.     End
  332.     Else
  333.         WriteLn(ERR[ErrorCode]);
  334. End;
  335.  
  336. Procedure PrintAnswer(Sentence: String);
  337. Var
  338.     Option: Integer;
  339. Begin
  340.     Writeln('If you want to print the answer to the console, enter: 1');
  341.     Writeln('If you want to output the answer to a file, enter: 2');
  342.     Option := GetUserOption();
  343.     If Option = 1 Then
  344.         PrintConsole(Sentence)
  345.     Else
  346.         PrintFile(Sentence);
  347. End;
  348.  
  349. Var
  350.     Option: Integer;
  351.     Sentence: String;
  352.     FileName: TextFile;
  353.  
  354. Begin
  355.     ProgramTask();
  356.     ChooseInputMethod(Sentence, FileName);
  357.     PrintAnswer(Sentence);
  358.     Readln;
  359. End.
Advertisement
Add Comment
Please, Sign In to add comment