Advertisement
green1ant

3_2 1

Nov 6th, 2018
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 4.31 KB | None | 0 0
  1. program Laba_3_2;
  2. {$APPTYPE CONSOLE}
  3. uses
  4.    SysUtils;
  5. type
  6.    TInputChoice = (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 GetOutputFilePath(): string;
  33. var
  34.    Path: string;
  35. begin
  36.    Writeln('Enter output file path');
  37.    Readln(Path);
  38.    while FileExists(Path) do
  39.    begin
  40.       Writeln(OutputFileExistsErrorMessage);
  41.       Readln(Path);
  42.    end;
  43.    GetOutputFilePath := Path;
  44. end;
  45.  
  46. function ChooseInputFromFileOrConsole(): TInputChoice;
  47. var
  48.    Choice: string;
  49. begin
  50.    Writeln(WayOfInputMessage);
  51.    Readln(Choice);
  52.    while (LowerCase(Choice) <> 'c') and (LowerCase(Choice) <> 'f') do
  53.    begin
  54.       Writeln(IncorrectWayOfInputMessage);
  55.       Readln(Choice);
  56.    end;
  57.  
  58.    if LowerCase(Choice) = 'f' then
  59.       ChooseInputFromFileOrConsole := WithFile
  60.    else
  61.       ChooseInputFromFileOrConsole := Console;
  62. end;
  63.  
  64. function GetStringFromFile(InputFilePath: string): string;
  65. var
  66.    InputFile: TextFile;
  67.    InputString: string;
  68. begin
  69.    AssignFile(InputFile, InputFilePath);
  70.    Reset(InputFile);
  71.    Readln(InputFile, InputString);
  72.    CloseFile(InputFile);
  73.    GetStringFromFile := InputString;
  74. end;
  75.  
  76. function GetStringFromConsole(): string;
  77. var
  78.    InputString: string;
  79. begin
  80.    Readln(InputString);
  81.    GetStringFromConsole := InputString;
  82. end;
  83.  
  84.  
  85. function GetSetFromString(InputString: string): TSet;
  86. var
  87.    StringLength, i: Integer;
  88.    Sequence: TSet;
  89. begin
  90.    Sequence := [];
  91.    StringLength := Length(InputString);
  92.    for i := 1 to StringLength do
  93.       if (Pos(InputString[i], Mask) <> 0) and not(InputString[i] in Sequence) then
  94.          Include(Sequence, InputString[i]);
  95.    GetSetFromString := Sequence;
  96. end;
  97.  
  98. procedure PrintSetToConsole(Sequence: TSet);
  99. var
  100.    i: Integer;
  101. begin
  102.    if Sequence = [] then
  103.       Writeln('Your sequence doesn''t contain any of assumed symbols')
  104.    else
  105.    begin
  106.       Writeln('Composed set based on your sequence');
  107.       for i := 0 to 255 do
  108.          if Chr(i) in Sequence then
  109.             Write(Chr(i), ' ');
  110.    end;
  111. end;
  112.  
  113. procedure PrintSetToFile(Sequence: TSet; OutputFilePath: string);
  114. var
  115.    i: Integer;
  116.    OutputFile: TextFile;
  117. begin
  118.    AssignFile(OutputFile, OutputFilePath);
  119.    Rewrite(OutputFile);
  120.    if Sequence = [] then
  121.       Writeln(OutputFile, 'Your sequence doesn''t contain any of assumed symbols')
  122.    else
  123.    begin
  124.       Writeln(OutputFile, 'Composed set based on your sequence');
  125.       for i := 0 to 255 do
  126.          if Chr(i) in Sequence then
  127.             Write(OutputFile, Chr(i), ' ');
  128.    end;
  129.    CloseFile(OutputFile);
  130. end;
  131.  
  132. procedure PrintSet(Sequence: TSet; OutputFilePath: string);//ïîäóìàòü íóæíà ëè òàêàÿ ôöèÿ
  133. begin
  134.    PrintSetToConsole(Sequence);
  135.    PrintSetToFile(Sequence, OutputFilePath);
  136. end;
  137.  
  138. procedure Main();
  139. var
  140.    InputFilePath, OutputFilePath, InputString: string;
  141.    SequenceString: TSet;
  142. begin
  143.    Writeln(InstructionMessage);
  144.    if ChooseInputFromFileOrConsole = WithFile then
  145.    begin
  146.       InputFilePath := GetInputFilePath();//ìîæíî çàñóíóòü â íèæíþþ ôöèþ
  147.       InputString := GetStringFromFile(InputFilePath);
  148.       OutputFilePath := GetOutputFilePath();
  149.       SequenceString := GetSetFromString(InputString);
  150.       PrintSet(SequenceString, OutputFilePath);
  151.    end
  152.    else
  153.    begin
  154.       Writeln('Input sequence of symbols');
  155.       InputString := GetStringFromConsole();
  156.       SequenceString := GetSetFromString(InputString);
  157.       PrintSetToConsole(SequenceString);
  158.    end;
  159.    Readln;
  160. end;
  161.  
  162. begin
  163.    Main();
  164. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement