Advertisement
believe_me

Untitled

Nov 24th, 2021
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 7.29 KB | None | 0 0
  1. program Project1;
  2.  
  3. {$APPTYPE CONSOLE}
  4. {$R *.res}
  5.  
  6. uses
  7.     System.SysUtils;
  8.  
  9. type
  10.     TSetOfChars = set Of Char;
  11.  
  12. Function CheckExtension(Path: String): Boolean; forward;
  13.  
  14. Function InputNumber(MinNumber, MaxNumber: Integer): Integer;
  15.  
  16. var
  17.     IsCorrect: Boolean;
  18.     Number: Integer;
  19.  
  20. begin
  21.     repeat
  22.         IsCorrect := true;
  23.         try
  24.             Readln(Number);
  25.         except
  26.             Writeln('You need to write an integer number, which is not less than ',
  27.               MinNumber, ' and more than ', MaxNumber);
  28.             IsCorrect := false;
  29.         end;
  30.         if IsCorrect and ((Number < MinNumber) or (Number > MaxNumber)) then
  31.         begin
  32.             Writeln('You need to write a number, which is not less than ',
  33.               MinNumber, ' and more than ', MaxNumber);
  34.             IsCorrect := false;
  35.         end;
  36.     until IsCorrect;
  37.     InputNumber := Number;
  38. end;
  39.  
  40. Function ChooseWayOfInputOrOutput(): Integer;
  41.  
  42. const
  43.     CONSOLE_WAY = 1;
  44.     FILE_WAY = 2;
  45.  
  46. var
  47.     UserWay: Integer;
  48.  
  49. begin
  50.     Repeat
  51.         UserWay := InputNumber(1, 2);
  52.     Until (UserWay = CONSOLE_WAY) or (UserWay = FILE_WAY);
  53.     ChooseWayOfInputOrOutput := UserWay;
  54. end;
  55.  
  56. Function InputPathToFile(): String;
  57.  
  58. var
  59.     Path: String;
  60.     IsCorrect: Boolean;
  61.     NewFile: TextFile;
  62. begin
  63.     Writeln('Input path to file:');
  64.     repeat
  65.         Readln(Path);
  66.         IsCorrect := CheckExtension(Path);
  67.     until IsCorrect;
  68.     InputPathToFile := Path;
  69. end;
  70.  
  71. Function CheckPermissionForReading(Path: String): Boolean;
  72.  
  73. var
  74.     OutputFile: TextFile;
  75.     RightPermission: Boolean;
  76.  
  77. begin
  78.     Assign(OutputFile, Path);
  79.     RightPermission := true;
  80.     try
  81.         Reset(OutputFile);
  82.         Close(OutputFile);
  83.     except
  84.         Writeln('File is not available for reading.');
  85.         RightPermission := false;
  86.     end;
  87.     CheckPermissionForReading := RightPermission;
  88. end;
  89.  
  90. Function ReceiveTextFromConsole(): String;
  91.  
  92. var
  93.     XStart, XEnd: Integer;
  94.     Text: String;
  95.  
  96. begin
  97.     Writeln('Input string: ');
  98.     Readln(Text);
  99.     ReceiveTextFromConsole := Text;
  100. end;
  101.  
  102. Function CheckText(Text: String; RequiredChars: TSetOfChars): Boolean;
  103.  
  104. var
  105.     IsCorrect: Boolean;
  106.     EndNumber, i: Integer;
  107.  
  108. begin
  109.     isCorrect:= false;
  110.     EndNumber:= Length(Text) + 1;
  111.     i:= 1;
  112.     while (i < EndNumber) and (not IsCorrect) do
  113.     begin;
  114.         if ansichar(Text[i]) in RequiredChars then
  115.             IsCorrect:= true;
  116.         Inc(i);
  117.     end;
  118.     CheckText := IsCorrect;
  119. end;
  120.  
  121. Function ReceiveTextFromFile(Path: String): String;
  122.  
  123. var
  124.     InputFile: TextFile;
  125.     Text: String;
  126.  
  127. begin;
  128.     Assign(InputFile, Path);
  129.     Reset(InputFile);
  130.     Read(InputFile, Text);
  131.     Close(InputFile);
  132.     ReceiveTextFromFile := Text;
  133. end;
  134.  
  135. Function ReceiveString(RequiredChars: TSetOfChars): String;
  136.  
  137. const
  138.     CONSOLE_WAY = 1;
  139.     FILE_WAY = 2;
  140.  
  141. var
  142.     Path: String;
  143.     Text: String;
  144.     UserWay: Integer;
  145.     IsTextCorrect: Boolean;
  146.     IsCorrect: Boolean;
  147.  
  148. begin;
  149.     repeat
  150.     begin
  151.         Writeln('Choose way of input: '#13#10'Type ''1'' if you want to receive string from console.'#13#10'Type ''2'' if you want to receieve string from file:');
  152.         UserWay := ChooseWayOfInputOrOutput();
  153.         case UserWay of
  154.             CONSOLE_WAY:
  155.                 begin
  156.                     Text := ReceiveTextFromConsole();
  157.                 end;
  158.             FILE_WAY:
  159.                 begin
  160.                     repeat
  161.                         Path := InputPathToFile();
  162.                         IsCorrect := CheckPermissionForReading(Path);
  163.                     until IsCorrect;
  164.                     Text := ReceiveTextFromFile(Path);
  165.                 end;
  166.         end;
  167.         IsTextCorrect := CheckText(Text, RequiredChars);
  168.         if not IsTextCorrect then
  169.             Writeln('There are no reqired elements.');
  170.     end;
  171.     until IsTextCorrect;
  172.     ReceiveString := Text;
  173. end;
  174.  
  175. Function ReceiveResultSet(Text: String; RequiredChars: TSetOfChars): TSetOfChars;
  176.  
  177. var
  178.     i, EndNumber: Integer;
  179.     ResultNumber: String;
  180.     IsCorrect: Boolean;
  181.     ResultSet: TSetOfChars;
  182.     ElementChar: String;
  183.     Elem: Char;
  184.     IntegerElem: Integer;
  185.  
  186. begin
  187.     EndNumber := length(Text);
  188.     ResultSet := [];
  189.     for i := 1 to EndNumber do
  190.     begin
  191.         if (Text[i] in RequiredChars) then
  192.             Include(ResultSet, AnsiChar(Text[i]))
  193.     end;
  194.     ReceiveResultSet := ResultSet;
  195. end;
  196.  
  197. Function CheckExtension(Path: String): Boolean;
  198.  
  199. var
  200.     RigthExtension: Boolean;
  201.  
  202. begin
  203.     if (ExtractFileExt(Path) = '.txt') then
  204.         RigthExtension := true
  205.     else
  206.     begin
  207.         Writeln('Wrong file extension.');
  208.         RigthExtension := false;
  209.     end;
  210.     CheckExtension := RigthExtension;
  211. end;
  212.  
  213. Function CheckPermissionForWriting(Path: String): Boolean;
  214.  
  215. var
  216.     OutputFile: TextFile;
  217.     RightPermission: Boolean;
  218.  
  219. begin
  220.     Assign(OutputFile, Path);
  221.     RightPermission := true;
  222.     try
  223.         Rewrite(OutputFile);
  224.         Close(OutputFile);
  225.     except
  226.         Writeln('File is not available for writing.');
  227.         RightPermission := false;
  228.     end;
  229.     CheckPermissionForWriting := RightPermission;
  230. end;
  231.  
  232. Procedure PrintResultToFile(Path: String; ResultSet: TSetOfChars);
  233.  
  234. const
  235.     START_INDEX = 40;
  236.     END_INDEX = 125;
  237.  
  238. var
  239.     OutputFile: TextFile;
  240.     i: Integer;
  241.  
  242. begin
  243.     Assign(OutputFile, Path);
  244.     Rewrite(OutputFile);
  245.     Write(OutputFile, 'RecievedSet: ');
  246.     for i := START_INDEX to END_INDEX do
  247.         if AnsiChar(i) in ResultSet then
  248.             Write(OutputFile, AnsiChar(i), ' ');
  249.     Close(OutputFile);
  250. end;
  251.  
  252. Procedure PrintResultInConsole(ResultSet: TSetOfChars);
  253.  
  254. const
  255.     START_INDEX = 40;
  256.     END_INDEX = 125;
  257.  
  258. var
  259.     i: Integer;
  260.  
  261. begin;
  262.     Write('RecievedSet: ');
  263.     for i := START_INDEX to END_INDEX do
  264.         if AnsiChar(i) in ResultSet then
  265.             Write(AnsiChar(i), ' ');
  266. end;
  267.  
  268. Procedure PrintResult(ResultString: String; ResultSet: TSetOfChars);
  269.  
  270. const
  271.     CONSOLE_WAY = 1;
  272.     FILE_WAY = 2;
  273.  
  274. var
  275.     UserWay: Integer;
  276.     Path: String;
  277.     IsCorrect: Boolean;
  278.  
  279. begin;
  280.     Writeln('Choose way of output: '#13#10'Type ''1'' if you want to print result in console.'#13#10'Type ''2'' if you want to print result in file.');
  281.     UserWay := ChooseWayOfInputOrOutput();
  282.     case UserWay of
  283.         CONSOLE_WAY:
  284.             begin
  285.                 PrintResultInConsole(ResultSet);
  286.             end;
  287.         FILE_WAY:
  288.             begin
  289.                 repeat
  290.                     Path := InputPathToFile;
  291.                     IsCorrect := CheckPermissionForWriting(Path);
  292.                 until IsCorrect;
  293.                 PrintResultToFile(Path, ResultSet);
  294.             end;
  295.     end;
  296. end;
  297.  
  298. Procedure Main();
  299.  
  300. var
  301.     RequiredChars, ResultSet: TSetOfChars;
  302.     Text, ResultNumber: String;
  303.  
  304. begin
  305.     Writeln('Program receives and prints even numbers, brackets and characters of arithmetic operations from the string you inserted');
  306.     RequiredChars := ['+', '-', '*', '/', '[', ']', '{', '}', '(', ')', '0',
  307.       '2', '4', '6', '8'];
  308.     Text := ReceiveString(RequiredChars);
  309.     ResultSet := ReceiveResultSet(Text, RequiredChars);
  310.     PrintResult(Text, ResultSet);
  311.     Writeln(''#13#10'Program is completed.');
  312.     Readln;
  313. end;
  314.  
  315. begin
  316.     Main();
  317.  
  318. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement