Advertisement
RevolutIIon

Untitled

Nov 10th, 2018
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 6.52 KB | None | 0 0
  1. program LabWork31;
  2.  
  3. {$APPTYPE CONSOLE}
  4.  
  5. uses
  6.   SysUtils;
  7.  
  8. type
  9.    TCharSet = set of Char;
  10.  
  11. const
  12.    LenModOrDiv = 2;          // because 5-3 = 2 +1 = 3 (+1 because we include third index)
  13.  
  14. function UserChooce(): Boolean;
  15. var
  16.    IsCorrect: Boolean;
  17.    Choice: Char;
  18. begin
  19.    IsCorrect := False;
  20.    repeat
  21.       Write('Y/N: ');
  22.       Readln(Choice);
  23.       Choice := UpCase(Choice);
  24.       case Ord(Choice) of
  25.          Ord('Y'):
  26.             begin
  27.                IsCorrect := True;
  28.                UserChooce := True;
  29.             end;
  30.          Ord('N'):
  31.             begin
  32.                IsCorrect := True;
  33.                UserChooce := False;
  34.             end
  35.       else
  36.          Writeln('You made an incorrect choice, please, try again clearly following the instructions.');
  37.       end;
  38.    until IsCorrect;
  39. end;
  40.  
  41.  
  42. function CorrectFormatOfTheFile(FileName: String): String;
  43. const
  44.    FormatLen = 4;
  45. begin
  46.    if AnsiCompareStr(copy(FileName, length(FileName) - FormatLen, FormatLen), '.txt') <> 0 then
  47.       FileName := FileName + '.txt';
  48.    CorrectFormatOfTheFile := FileName;
  49. end;
  50.  
  51. function InputFileName(): String;
  52. var
  53.    FileName: String;
  54.    IsCorrect: Boolean;
  55. begin
  56.    Writeln('Please, enter name of the file or way to the file.');
  57.    Writeln('Example: F:\Programming\Lab\3_1\Delphi\Input.txt');
  58.    IsCorrect := False;
  59.    repeat
  60.       Readln(FileName);
  61.       FileName := CorrectFormatOfTheFile(FileName);
  62.       if FileExists(FileName) then
  63.       begin
  64.          IsCorrect := True;
  65.          InputFileName := FileName;
  66.       end
  67.       else
  68.          Writeln('File with the same name does not exist, please, enter name of the file again.');
  69.    until IsCorrect;
  70. end;
  71.  
  72.  
  73. function EnterStringFromFile(): String;
  74. var
  75.    Input: TextFile;
  76.    UserString: String;
  77. begin
  78.    AssignFile(Input, InputFileName);
  79.    Reset(Input);
  80.    while seekEOF(Input) do
  81.    begin
  82.       Writeln('Your file haven''t strings.');
  83.       Writeln;
  84.       AssignFile(Input, InputFileName);
  85.       Reset(Input);
  86.    end;
  87.    Readln(Input, UserString);
  88.    Close(Input);
  89.    EnterStringFromFile := UserString;
  90. end;
  91.  
  92.  
  93. function EnterStringFromConsole(): String;
  94. var
  95.    UserString: String;
  96. begin
  97.    Write('Please, enter your string: ');
  98.    Readln(UserString);
  99.    EnterStringFromConsole := UserString;
  100. end;
  101.  
  102.  
  103. function ModOrDiv(const UserString: String; const Index: Integer): Boolean;
  104. var
  105.    StringLen: Integer;
  106.    Assumption: String;
  107.    IsCorrectAssumption: Boolean;
  108. begin
  109.    IsCorrectAssumption := False;
  110.    StringLen := length(UserString);
  111.    if StringLen - Index >= LenModOrDiv then
  112.       Assumption := copy(UserString, Index, LenModOrDiv + 1);
  113.       if (AnsiCompareStr(Assumption, 'mod') = 0)or(AnsiCompareStr(Assumption, 'div') = 0) then
  114.          IsCorrectAssumption := True;
  115.    ModOrDiv := IsCorrectAssumption;
  116. end;
  117.  
  118.  
  119. function CreateSet(const UserString: String): TCharSet;
  120. var
  121.    Symbol: Char;
  122.    Symbols: TCharSet;
  123.    StringIndex, StringLen: Integer;
  124. const
  125.    SetNumbers = ['0', '2', '4', '6', '8'];
  126.    OperationsAndParentheses =
  127.       ['(', ')', '{', '}', '[', ']', '+', '-', '*', '/'];
  128. begin
  129.    Symbols := [];
  130.    StringLen := Length(UserString);
  131.    StringIndex := 1;
  132.    while StringIndex <= StringLen do
  133.    begin
  134.       Symbol := UserString[StringIndex];
  135.       if not (Symbol in Symbols) then
  136.          if Symbol in OperationsAndParentheses then
  137.             Include(Symbols, Symbol)
  138.          else
  139.             if Symbol in SetNumbers then
  140.                Include(Symbols, Symbol)
  141.             else
  142.                if ModOrDiv(UserString, StringIndex) then
  143.                   begin
  144.                      Include(Symbols, UpCase(Symbol));
  145.                      StringIndex := StringIndex + LenModOrDiv;  // +2 because we inc(stringIndex) in next line
  146.                   end;
  147.       inc(StringIndex);
  148.    end;
  149.    CreateSet := Symbols;
  150. end;
  151.  
  152.  
  153. function SetToStr(const Symbols: TCharSet): String;
  154. var
  155.    CurrentChar: Char;
  156.    StringFromSet: String;
  157.    i: Integer;
  158. begin
  159.    StringFromSet := '';
  160.    for i := 0 to 255 do
  161.    begin
  162.       CurrentChar := Chr(i);
  163.       if CurrentChar in Symbols then
  164.          case Ord(CurrentChar) of
  165.             Ord('M'):
  166.                StringFromSet := StringFromSet + 'mod ';
  167.             Ord('D'):
  168.                StringFromSet := StringFromSet + 'div ';
  169.          else
  170.             StringFromSet := StringFromSet + CurrentChar + ' ';
  171.          end;
  172.    end;
  173.    SetToStr := StringFromSet;
  174. end;
  175.  
  176.  
  177. procedure OutputInf(const Symbols: TCharSet);
  178. var
  179.    Output: TextFile;
  180.    FileName, StringFromSet: String;
  181. begin
  182.    Writeln;
  183.    StringFromSet := SetToStr(Symbols);
  184.    if AnsiCompareStr(StringFromSet, '') = 0 then
  185.       Writeln('Your string haven''t symbols which can be append to set')
  186.    else
  187.    begin
  188.       Writeln('Your set: ', StringFromSet);
  189.       Writeln('Do you want to output data to a file?');
  190.       if UserChooce() then
  191.       begin
  192.          Writeln('Please, enter name of the file.');
  193.          Writeln('Example: Text.txt');
  194.          Readln(FileName);
  195.          FileName := CorrectFormatOfTheFile(FileName);
  196.          AssignFile(Output, FileName);
  197.          try
  198.             if FileExists(FileName) then
  199.             begin
  200.                Write('A file with this name already exists, ');
  201.                Writeln('do you want to append information(otherwise rewrite)?');
  202.                if UserChooce() then
  203.                   Append(Output)
  204.                else
  205.                   Rewrite(Output);
  206.                end
  207.                else
  208.                   Rewrite(Output);
  209.          except
  210.             Writeln('Error writing to file.');
  211.          end;
  212.          Write(Output, 'Set from your string: ', StringFromSet);
  213.          CloseFile(Output);
  214.          Writeln('Write was successfully.');
  215.       end;
  216.    end;
  217. end;
  218.  
  219.  
  220. procedure Main();
  221. var
  222.    UserString: String;
  223. begin
  224.    Writeln('Hello, this program extracts integers from a string.');
  225.    repeat
  226.       Writeln('Do you want to enter data through a file? (otherwise via console).');
  227.       if UserChooce then
  228.       begin
  229.          UserString := EnterStringFromFile();
  230.          Writeln('Your string: ', UserString);
  231.       end
  232.       else
  233.          UserString := EnterStringFromConsole();
  234.       UserString := StringReplace(UserString, ' ', '', [rfReplaceAll]);
  235.       if UserString = '' then
  236.          Writeln('Your string is empty.')
  237.       else
  238.          OutputInf(CreateSet(UserString));
  239.       Writeln('Do you want enter another string?');
  240.    until not UserChooce();
  241.    Writeln('Press enter to exit...');
  242.    Readln;
  243. end;
  244.  
  245.  
  246. begin
  247.    Main;
  248. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement