Advertisement
green1ant

3_2 *2

Nov 7th, 2018
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 4.49 KB | None | 0 0
  1. program Laba_3_2;
  2. {$APPTYPE CONSOLE}
  3. uses
  4.    SysUtils;
  5. type
  6.    TInputMode = (WithFile, Console);
  7.    TSet = set of Char;
  8.  
  9. const
  10.    InstructionMessage
  11.       = 'This program composes a set consisting of arithmetic operands and even digits';
  12.    WayOfInputMessage = 'Where do you want to input data from? [F]ile or [C]onsole';
  13.    IncorrectInputFilePath = 'Incorrect input file path, check if file exists and try again';
  14.    IncorrectWayOfInputMessage = 'Incorrect way of input, choose [F]ile or [C]onsole';
  15.    OutputFileExistsErrorMessage = 'Incorrect output file, such file already exists, try again';
  16.    Mask = '-+*/02468';
  17.  
  18. function GetInputFilePath(): string;//ïðîâåðêà íà ïóñòîòó
  19. var
  20.    Path: string;
  21. begin
  22.    Writeln('Enter input file path');
  23.    Readln(Path);
  24.    while not FileExists(Path) do
  25.    begin
  26.       Writeln(IncorrectInputFilePath);
  27.       Readln(Path);
  28.    end;
  29.    GetInputFilePath := Path;
  30. end;
  31.  
  32. function CreateOutputFile(): Boolean;
  33. var
  34.    Answer: string;
  35. begin
  36.    Writeln('Do you want to create output file? [Y]es or [N]o');
  37.    Readln(Answer);
  38.    Answer := LowerCase(Answer);
  39.    while (Answer <> 'y') and (Answer <> 'n') do
  40.    begin
  41.       Writeln('Incorrect way of input, choose [Y]es or [N]o');
  42.       Readln(Answer);
  43.    end;
  44.  
  45.    if Answer = 'y' then
  46.       CreateOutputFile := True
  47.    else
  48.       CreateOutputFile := False;
  49. end;
  50.  
  51. function GetOutputFilePath(): string;
  52. var
  53.    Path: string;
  54. begin
  55.    if CreateOutputFile then
  56.    begin
  57.       Writeln('Enter output file path');
  58.       Readln(Path);
  59.       while FileExists(Path) do
  60.       begin
  61.          Writeln(OutputFileExistsErrorMessage);
  62.          Readln(Path);
  63.       end;
  64.    end
  65.    else
  66.       Path := '';
  67.    GetOutputFilePath := Path;
  68. end;
  69.  
  70. function ChooseInputMode(): TInputMode;
  71. var
  72.    Mode: string;
  73. begin
  74.    Writeln(WayOfInputMessage);
  75.    Readln(Mode);
  76.    Mode := LowerCase(Mode);
  77.    while (Mode <> 'c') and (Mode <> 'f') do
  78.    begin
  79.       Writeln(IncorrectWayOfInputMessage);
  80.       Readln(Mode);
  81.    end;
  82.    if Mode = 'f' then
  83.       ChooseInputMode := WithFile
  84.    else
  85.       ChooseInputMode := Console;
  86. end;
  87.  
  88. function ReadFromFile(InputFilePath: string): string;
  89. var
  90.    InputFile: TextFile;
  91.    InputString: string;
  92. begin
  93.    AssignFile(InputFile, InputFilePath);
  94.    Reset(InputFile);
  95.    Readln(InputFile, InputString);
  96.    CloseFile(InputFile);
  97.    ReadFromFile := InputString;
  98. end;
  99.  
  100. function ReadFromConsole(): string;
  101. var
  102.    InputString: string;
  103. begin
  104.    Writeln('Input sequence of symbols');
  105.    Readln(InputString);
  106.    ReadFromConsole := InputString;
  107. end;
  108.  
  109. function GetSetFromString(InputString: string): TSet;
  110. var
  111.    StringLength, i: Integer;
  112.    Sequence: TSet;
  113. begin
  114.    Sequence := [];
  115.    StringLength := Length(InputString);
  116.    for i := 1 to StringLength do
  117.       if (Pos(InputString[i], Mask) <> 0) and not(InputString[i] in Sequence) then
  118.          Include(Sequence, InputString[i]);
  119.    GetSetFromString := Sequence;
  120. end;
  121.  
  122. procedure PrintSet(Sequence: TSet; OutputFilePath: string);
  123. var
  124.    i: Integer;
  125.    OutputFile: TextFile;
  126.    ShouldWriteToFile: Boolean;
  127. begin
  128.    if OutputFilePath = '' then
  129.       ShouldWriteToFile := False
  130.    else
  131.       ShouldWriteToFile := True;
  132.    AssignFile(OutputFile, OutputFilePath);
  133.    Rewrite(OutputFile);
  134.    if Sequence = [] then
  135.    begin
  136.       if ShouldWriteToFile then
  137.          Writeln(OutputFile, 'Your sequence doesn''t contain any of assumed symbols');
  138.       Writeln('Your sequence doesn''t contain any of assumed symbols')
  139.    end
  140.    else
  141.    begin
  142.       if ShouldWriteToFile then
  143.          Writeln(OutputFile, 'Composed set based on your sequence');
  144.       Writeln('Composed set based on your sequence')
  145.       for i := 0 to 255 do
  146.          if Chr(i) in Sequence then
  147.          begin
  148.             if ShouldWriteToFile then
  149.                Write(OutputFile, Chr(i), ' ');
  150.             Write(Chr(i), ' ');
  151.          end;
  152.    end;
  153.    CloseFile(OutputFile);
  154. end;
  155.  
  156. procedure Main();
  157. var
  158.    InputFilePath, OutputFilePath, InputString: string;
  159.    SequenceString: TSet;
  160. begin
  161.    Writeln(InstructionMessage);
  162.    case ChooseInputMode() of
  163.       WithFile:
  164.       begin
  165.          InputFilePath := GetInputFilePath();
  166.          InputString := ReadFromFile(InputFilePath);
  167.          OutputFilePath := GetOutputFilePath();
  168.       end;
  169.       Console:
  170.          InputString := ReadFromConsole();
  171.    end;
  172.    SequenceString := GetSetFromString(InputString);
  173.    PrintSet(SequenceString, OutputFilePath);
  174.    Readln;
  175. end;
  176.  
  177. begin
  178.    Main();
  179. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement