SHARE
TWEET

Untitled

a guest Nov 12th, 2019 77 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Program Laba3_1;
  2. Uses
  3.     System.SysUtils;
  4. Const
  5.     Min = 1;
  6.     Max = 50;
  7. Procedure Condition();
  8. Begin
  9.     Writeln('This program converts a string according to the rule: ');
  10.     Writeln('1)If the string length > N, then the program will discard the first characters.');
  11.     Writeln('2)If the string length < N, then the program will add "."');
  12.     Writeln;
  13. End;
  14. Function ChoiseInput(): Char;
  15. Var
  16.     Input: Char;
  17.     IsCorrect: Boolean;
  18. Begin
  19.     Repeat
  20.         Readln(Input);
  21.         Input := UpCase(Input);
  22.         if ((Input = 'Y') or (Input = 'N')) then
  23.             IsCorrect := True
  24.         else
  25.         Begin
  26.             IsCorrect := False;
  27.             Writeln('Incorrect input. Enter Y(Yes) or N(No): ');
  28.         End;
  29.     Until IsCorrect;
  30.     ChoiseInput := Input;
  31. End;
  32. Function InputFromFile(): String;
  33. Var
  34.     Sentence: String;
  35.     FileName: String;
  36.     IsCorrect: Boolean;
  37.     InputFile: Text;
  38. begin
  39.     Repeat
  40.         Writeln('Enter the name of the file you want to read from: ');
  41.         Readln(FileName);
  42.         FileName := FileName + '.txt';
  43.         IsCorrect := True;
  44.         Assign(InputFile, FileName);
  45.         {$I-}
  46.         Reset(InputFile);
  47.         {$I+}
  48.         if IOResult <> 0 then
  49.         begin
  50.             Writeln('File ',FileName,' does not exist!');
  51.             IsCorrect := False;
  52.         end
  53.         else
  54.         begin
  55.             Read(InputFile, Sentence);
  56.             if length(Sentence) = 0 then
  57.             begin
  58.                 Writeln('File ',FileName,' is empty!');
  59.                 IsCorrect := False;
  60.             end;
  61.             if ((Length(Sentence) < Min) or (Length(Sentence) > Max)) then
  62.             Begin
  63.                 Writeln('Check the file! String length must be in the interval [',Min,'...',Max,']: ');
  64.                 IsCorrect := False;
  65.             End;
  66.         end;
  67.     Until IsCorrect;
  68.     Writeln('Source string: ',Sentence);
  69.     Close(InputFile);
  70.     InputFromFile := Sentence;
  71. end;
  72. Function CheckInputString(): String;
  73. Var
  74.     IsCorrect: Boolean;
  75.     Sentence: String;
  76. Begin
  77.     Repeat
  78.         Readln(Sentence);
  79.         if ((Length(Sentence) >= Min) and (Length(Sentence) <= Max)) then
  80.             IsCorrect := True
  81.         else
  82.         Begin
  83.             Writeln('Enter string length from interval [',Min,'...',Max,']: ');
  84.             IsCorrect := False;
  85.         End;
  86.     Until IsCorrect;
  87.     CheckInputString := Sentence;
  88. End;
  89. Function InputFromConsole(): String;
  90. Var
  91.     Sentence: String;
  92. Begin
  93.     Writeln('Please, enter a string: ');
  94.     Sentence := CheckInputString();
  95.     InputFromConsole := Sentence;
  96. End;
  97. Function CheckInput(): Integer;
  98. Var
  99.     N: Integer;
  100.     IsCorrect: Boolean;
  101. Begin
  102.     IsCorrect := False;
  103.     Repeat
  104.         Try
  105.             Readln(N);
  106.             if ((N >= Min) and (N <= Max)) then
  107.                 IsCorrect := True
  108.             else
  109.                 Writeln('Enter number from interval [',Min,'...',Max,']: ');
  110.         Except
  111.             Writeln('Check entered data. Enter number from interval [',Min,'...',Max,']: ');
  112.         End;
  113.     Until IsCorrect;
  114.     CheckInput := N;
  115. End;
  116. Procedure StringConversion(Var Sentence: String; N: integer);
  117. Var
  118.     i, Len: integer;
  119. Begin
  120.     Len := Length(Sentence);
  121.     if ((Len > N) and (Len mod 2 = 0)) then
  122.     Begin
  123.         Sentence := copy (Sentence,len - N + 1,Len - n );
  124.         Writeln ('Final string: ',Sentence);
  125.     End
  126.     else
  127.         if ((Len > N) and (Len mod 2 <> 0)) then
  128.         Begin
  129.             Sentence := copy(Sentence, Len - N + 1, Len - N + 1);
  130.             Writeln ('Final string: ',Sentence);
  131.         End;
  132.     if Len < N then
  133.     Begin
  134.         for i := Len + 1 to N do
  135.             Insert('.',Sentence,1);
  136.         Writeln ('Final string: ',Sentence);
  137.     End;
  138.     if Len = N then
  139.         Writeln ('Final string: ',Sentence);
  140. End;
  141. Procedure OutputInFile(Sentence: String);
  142. Var
  143.     NameOfFile: String;
  144.     OutputFile: Text;
  145. Begin
  146.     Writeln('Enter the file name for the entry: ');
  147.     Readln(NameOfFile);
  148.     NameOfFile := NameOfFile + '.txt';
  149.     Assign(OutputFile,NameOfFile);
  150.     Rewrite(OutputFile);
  151.     Writeln(OutputFile,'Final string: ');
  152.     Writeln(OutputFile,Sentence);
  153.     Close(OutputFile);
  154.     Writeln('String successfully written to file ',NameOfFile);
  155. End;
  156. Procedure Main();
  157. Var
  158.     Sentence: String;
  159.     N: Integer;
  160. begin
  161.     Condition();
  162.     Writeln('Would you like to use File input instead of Console input?',
  163.                 'Enter Y(Yes) or N(No): ');
  164.     if ChoiseInput = 'Y' then
  165.         Sentence := InputFromFile()
  166.     else
  167.         Sentence := InputFromConsole();
  168.     Writeln('Enter N: ');
  169.     N := CheckInput();
  170.     StringConversion(Sentence,N);
  171.     Write('Would you like to write down the answer to File? Enter Y(Yes) or N(No): ');
  172.     if ChoiseInput = 'Y' then
  173.         OutputInFile(Sentence);
  174.     Writeln('Press "Enter" to exit.');
  175.     Readln;
  176. end;
  177. Begin
  178.     Main();
  179. End.
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top