Advertisement
RevolutIIon

Untitled

Nov 2nd, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 5.88 KB | None | 0 0
  1. program LabWork31;
  2.  
  3. {$APPTYPE CONSOLE}
  4.  
  5. uses
  6.   SysUtils;
  7.  
  8. type
  9.    TStringArray = array of String;
  10.  
  11. function UserChoise(): Char;
  12. var
  13.    IsCorrect: Boolean;
  14.    Choise: Char;
  15. begin
  16.    IsCorrect := False;
  17.    repeat
  18.       Write('Y/N: ');
  19.       Readln(Choise);
  20.       Choise := UpCase(Choise);
  21.       if (Choise = 'Y') or (Choise = 'N') then
  22.          begin
  23.             IsCorrect := True;
  24.             UserChoise := Choise;
  25.          end
  26.       else
  27.          Writeln('You made an incorrect choice, please, try again clearly following the instructions');
  28.    until IsCorrect;
  29. end;
  30.  
  31.  
  32. function InputName(): String;
  33. var
  34.    FileName: String;
  35.    IsCorrect: Boolean;
  36. begin
  37.    Writeln('Please, enter name of the file or way to the file.');
  38.    Writeln('Example: F:\Programming\Lab\3_1\Delphi\Input.txt');
  39.    IsCorrect := False;
  40.    repeat
  41.       Readln(FileName);
  42.       if FileExists(FileName) then
  43.       begin
  44.          IsCorrect := True;
  45.          InputName := FileName;
  46.       end
  47.       else
  48.          Writeln('File with the same name does not exist, please, enter name of the file again');
  49.    until IsCorrect;
  50. end;
  51.  
  52.  
  53. function EnterStringFromFile(FileName: String): String;
  54. var
  55.    Input: TextFile;
  56.    UserString: String;
  57. begin
  58.    AssignFile(Input, FileName);
  59.    Reset(Input);
  60.    Readln(Input, UserString);
  61.    EnterStringFromFile := UserString;
  62. end;
  63.  
  64.  
  65. function CountTheDimension(UserString: String): Integer;
  66. var
  67.    StringIndex, Iteration : Integer;
  68.    Dimension: Integer;
  69.    NumberLikeString: set of Char;
  70. begin
  71.     NumberLikeString := ['0'..'9'];
  72.     Iteration := length(UserString) - 1;
  73.     StringIndex := 1;
  74.     Dimension := 0;
  75.     while StringIndex < Iteration do
  76.     begin
  77.       if (UserString[StringIndex] = '+') or (UserString[StringIndex] = '-') then
  78.          if UserString[StringIndex + 1] in NumberLikeString then
  79.             Dimension := Dimension + 1;
  80.       inc(StringIndex);
  81.     end;
  82.     CountTheDimension := Dimension;
  83. end;
  84.  
  85.  
  86. function FindIntegers(UserString: String): TStringArray;
  87. var
  88.    IntegerLikeString: String;
  89.    NumbersFromString: TStringArray;
  90.    StringIndex, StringLen, ArrayIndex, Dimension: Integer;
  91.    NumberLikeString : set of Char;
  92. begin
  93.    NumberLikeString := ['0'..'9'];
  94.    StringLen := Length(UserString);
  95.    Dimension := CountTheDimension(UserString);
  96.    SetLength(NumbersFromString, Dimension);
  97.    if Dimension <> 0 then
  98.    begin
  99.       StringIndex := 1;
  100.       ArrayIndex := 0;
  101.       while StringIndex < StringLen do
  102.       begin
  103.          IntegerLikeString := '';
  104.          if (UserString[StringIndex] = '+') or (UserString[StringIndex] = '-') then
  105.          begin
  106.             if UserString[StringIndex] = '-' then
  107.                IntegerLikeString := '-';
  108.             inc(StringIndex);
  109.             while UserString[StringIndex] in NumberLikeString do
  110.             begin
  111.                IntegerLikeString := IntegerLikeString + UserString[StringIndex];
  112.                if  StringIndex <= StringLen then
  113.                   inc(StringIndex);
  114.             end;
  115.             if (IntegerLikeString <>  '-') and (IntegerLikeString <> '') then
  116.             begin
  117.                NumbersFromString[ArrayIndex] := IntegerLikeString;
  118.                inc(ArrayIndex);
  119.             end;
  120.          end
  121.          else
  122.             inc(StringIndex);
  123.       end;
  124.    end;
  125.    FindIntegers := NumbersFromString;
  126. end;
  127.  
  128.  
  129. procedure OutputInf(NumbersFromString: TStringArray);
  130. var
  131.    Output: TextFile;
  132.    FileName: String;
  133.    Iteration, Index: Integer;
  134.    OutputToFile, AppendOrRewrite: Char;
  135. begin
  136.    if Length(NumbersFromString) <> 0 then
  137.    begin
  138.       Writeln('There are ', length(NumbersFromString), ' integers in your string.');
  139.       Iteration := Length(NumbersFromString) - 1;
  140.       for Index := 0 to Iteration do
  141.          Writeln(NumbersFromString[Index]);
  142.       Writeln('Do you want to output data to a file?');
  143.       OutputToFile := UserChoise();
  144.       if OutputToFile = 'Y' then
  145.       begin
  146.          Writeln('Please, enter name of the file');
  147.          Readln(FileName);
  148.          AssignFile(Output, FileName);
  149.          if FileExists(FileName) then
  150.          begin
  151.             Write('A file with this name already exists, ');
  152.             Writeln('do you want to overwrite it');
  153.             AppendOrRewrite := UserChoise();
  154.             if AppendOrRewrite = 'Y' then
  155.                Append(Output)
  156.             else
  157.                Rewrite(Output);
  158.          end
  159.          else
  160.             Rewrite(Output);
  161.          Writeln(Output, 'There are ', length(NumbersFromString), ' integers in your string.');
  162.          for Index := 0 to Iteration do
  163.          Writeln(Output, NumbersFromString[Index]);
  164.          CloseFile(Output);
  165.          Writeln('Write was successfully');
  166.       end;
  167.    end
  168.    else
  169.       Writeln('Your string have not integers.')
  170. end;
  171.  
  172. procedure Main();
  173. var
  174.    ChoiseInput, RepeatProgram: Char;
  175.    FileName, UserString: String;
  176.    NumbersFromString: TStringArray;
  177.  
  178. begin
  179.    Writeln('Hello, this program extracts integers from a string');
  180.    repeat
  181.       Writeln('Do you want to enter data through a file? (otherwise via console)');
  182.       ChoiseInput := UserChoise;
  183.       case Ord(ChoiseInput) of
  184.          Ord('Y'):
  185.             begin
  186.                FileName := InputName();
  187.                UserString := EnterStringFromFile(FileName);
  188.             end;
  189.          Ord('N'):
  190.             begin
  191.                Writeln('Please, enter your string');
  192.                Readln(UserString);
  193.             end;
  194.       end;
  195.       UserString := Trim(UserString);
  196.       if UserString = '' then
  197.          Writeln('Your string is empty')
  198.       else
  199.       begin
  200.          NumbersFromString := FindIntegers(userString);
  201.          OutputInf(NumbersFromString);
  202.       end;
  203.       Writeln('Do you want enter another string?');
  204.       RepeatProgram := UserChoise();
  205.    until (RepeatProgram = 'N');
  206.    Writeln('Press enter to exit');
  207.    Readln;
  208. end;
  209.  
  210.  
  211. begin
  212.    Main;
  213. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement