Advertisement
Egor_Vakar

3_2(delphi)

Nov 22nd, 2021
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 7.05 KB | None | 0 0
  1. program lab3_2;
  2. {$APPTYPE CONSOLE}
  3. {$R *.res}
  4. uses
  5.   System.SysUtils;
  6. const
  7.     SetOfDigitsAndOperations = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '-', '*', '/', ':', '^', '√'];
  8. type
  9.     TCharSet = set of Char;
  10.  
  11. Function TakeSize: Integer;
  12. const
  13.     MIN_SIZE = 2;
  14.     MAX_SIZE = 30;
  15. var
  16.     IsCorrect: Boolean;
  17.     Number: Integer;
  18. begin
  19.     Number := 0;
  20.     repeat
  21.         IsCorrect := True;
  22.         try
  23.             Readln(Number);
  24.         except
  25.             Writeln('Incorrect input!!!');
  26.             IsCorrect := False;
  27.         end;
  28.         if (IsCorrect and (Number < MIN_SIZE) or (Number > MAX_SIZE)) then
  29.         begin
  30.             Writeln('Incorrect input!!!');
  31.             IsCorrect := False;
  32.         end;
  33.     until IsCorrect;
  34.     TakeSize := Number;
  35. end;
  36.  
  37. Function TakeChar: AnsiChar;
  38. var
  39.     IsCorrect: Boolean;
  40.     InputChar: AnsiChar;
  41. begin
  42.     repeat
  43.         IsCorrect := True;
  44.         try
  45.             Readln(InputChar);
  46.         except
  47.             Writeln('Incorrect input!!!');
  48.             IsCorrect := False;
  49.         end;
  50.     until IsCorrect;
  51.     TakeChar := InputChar;
  52. end;
  53.  
  54. Function TakeSource: Byte;
  55. const
  56.     CHOOSING_CONSOLE = 1;
  57.     CHOOSING_FILE = 2;
  58. var
  59.     Source: Byte;
  60.     IsCorrect: Boolean;
  61. begin
  62.     Source := 0;
  63.     repeat
  64.         IsCorrect := True;
  65.         try
  66.             Readln(Source);
  67.         except
  68.             Write('Incorrect input!!! Select the source:' + #13#10 + '1:Console'  + #13#10 + '2:File'  + #13#10 + 'Enter 1 or 2: ');
  69.             IsCorrect := False;
  70.         end;
  71.         if (IsCorrect and (Source <> CHOOSING_CONSOLE) and (Source <> CHOOSING_FILE)) then
  72.         begin
  73.             Write('Incorrect input!!! Select the source:' + #13#10 + '1:Console'  + #13#10 + '2:File'  + #13#10 + 'Enter 1 or 2: ');
  74.             IsCorrect := False;
  75.         end;
  76.     until IsCorrect;
  77.     TakeSource := Source;
  78. end;
  79.  
  80. Function TakeInPath: String;
  81. var
  82.     Path: String;
  83.     IsCorrect: Boolean;
  84.     InFile: TextFile;
  85. begin
  86.     Write('Enter file path: ');
  87.     repeat
  88.         IsCorrect := True;
  89.         Readln(Path);
  90.         if not FileExists(Path) then
  91.         begin
  92.             Write('File is not found' + #13#10 + 'Enter file path: ');
  93.             IsCorrect := False;
  94.         end
  95.         else
  96.             if ((Path[Length(Path)] <> 't') or (Path[Length(Path) - 1] <> 'x') or
  97.                 (Path[Length(Path)- 2] <> 't') or (Path[Length(Path)- 3] <> '.')) then
  98.             begin
  99.                 Write('File is found, but it is not ".txt" type file' + #13#10 + 'Enter file path: ');
  100.                 IsCorrect := False;
  101.             end
  102.             else
  103.             begin
  104.                 Assign(InFile, Path);
  105.                 try
  106.                     Reset(InFile);
  107.                 except
  108.                     Write('Error!!! Program can''t open file!' + #13#10 + 'Enter file path: ');
  109.                     IsCorrect := False;
  110.  
  111.                 end;
  112.             end;
  113.     until IsCorrect;
  114.     CloseFile(InFile);
  115.     TakeInPath := Path;
  116. end;
  117.  
  118. Function TakeOutPath: String;
  119. var
  120.     Path: String;
  121.     IsCorrect: Boolean;
  122.     OutFile: TextFile;
  123. begin
  124.     Write('Enter file path: ');
  125.     repeat
  126.         IsCorrect := True;
  127.         Readln(Path);
  128.         if ((Path[Length(Path)] <> 't') or (Path[Length(Path) - 1] <> 'x') or
  129.             (Path[Length(Path)- 2] <> 't') or (Path[Length(Path) - 3] <> '.')) then
  130.         begin
  131.             Write('It should be a ".txt" type file' + #13#10 + 'Enter file path: ');
  132.             IsCorrect := False;
  133.         end
  134.         else
  135.         begin
  136.             AssignFile(OutFile, Path);
  137.             try
  138.                 Rewrite(OutFile);
  139.             except
  140.                 Write('Error!!! There are problems with file!' + #13#10 + 'Enter file path: ');
  141.                 IsCorrect := False;
  142.             end;
  143.         end;
  144.     until IsCorrect;
  145.     CloseFile(OutFile);
  146.     TakeOutPath := Path;
  147. end;
  148.  
  149. Function TakeSetFromConsole: TCharSet;
  150. var
  151.     Size, i: Integer;
  152.     CharSet: TCharSet;
  153.     InputChar: AnsiChar;
  154. begin
  155.     Write('Enter size: ');
  156.     Size := TakeSize;
  157.     for i := 0 to (Size - 1) do
  158.     begin
  159.         Write('Enter element ', (i + 1), ': ');
  160.         InputChar := TakeChar;
  161.         Include(CharSet, InputChar);
  162.     end;
  163.     TakeSetFromConsole := CharSet;
  164. end;
  165.  
  166. Function TakeSetFromFile(const Path: String): TCharSet;
  167. const
  168.     MIN_SIZE = 2;
  169. var
  170.     InFile: TextFile;
  171.     Sign: AnsiChar;
  172.     IsCorrect: Boolean;
  173.     CharSet: TCharSet;
  174. begin
  175.     IsCorrect := True;
  176.     AssignFile(InFile, Path);
  177.     Reset(InFile);
  178.     while(IsCorrect and (not Eof(InFile))) do
  179.     begin
  180.         try
  181.             Read(InFile, Sign);
  182.             Include(CharSet, Sign);
  183.         except
  184.             Writeln('Incorrect file content!!!');
  185.             IsCorrect := False;
  186.         end;
  187.     end;
  188.     if (IsCorrect and (not Eof(InFile))) then
  189.     begin
  190.         Writeln('Incorrect file content!!!');
  191.         IsCorrect := False;
  192.     end;
  193.     if  not IsCorrect then
  194.         CharSet := [];
  195.     CloseFile(InFile);
  196.     TakeSetFromFile := CharSet;
  197. end;
  198.  
  199. Function TakeSet(Source: Byte): TCharSet;
  200. var
  201.     InPath: String;
  202.     CharSet: TCharSet;
  203. begin
  204.     if (Source = 1) then
  205.         CharSet := TakeSetFromConsole
  206.     else
  207.     begin
  208.         InPath := TakeInPath;
  209.         CharSet := TakeSetFromFile(InPath);
  210.         while (CharSet = []) do
  211.         begin
  212.             InPath := TakeInPath;
  213.             CharSet := TakeSetFromFile(InPath);
  214.         end;
  215.     end;
  216.     TakeSet := CharSet;
  217. end;
  218.  
  219. Function FindAnswers(CharSet: TCharSet): TCharSet;
  220. var
  221.     Answers: TCharSet;
  222. begin
  223.     Answers := CharSet * SetOfDigitsAndOperations;
  224.     FindAnswers := Answers;
  225. end;
  226.  
  227. Procedure OutputToConsole(Answers: TCharSet);
  228. var
  229.     Element: AnsiChar;
  230. begin
  231.     for Element in Answers do
  232.         Write(Element, ' ');
  233. end;
  234.  
  235. Procedure OutputToFile(const Path: String; Answers: TCharSet);
  236. var
  237.     OutFile: TextFile;
  238.     Element: AnsiChar;
  239. begin
  240.     AssignFile(OutFile, Path);
  241.     Rewrite(OutFile);
  242.     for Element in Answers do
  243.         Write(OutFile, Element, ' ');
  244.     CloseFile(OutFile);
  245.     Writeln('Done');
  246. end;
  247.  
  248. Procedure Output(const Source: Byte; Answers: TCharSet);
  249. var
  250.     OutPath: String;
  251. begin
  252.     if (Source = 1) then
  253.         OutputToConsole(Answers)
  254.     else
  255.     begin
  256.         OutPath := TakeOutPath;
  257.         OutputToFile(OutPath,Answers);
  258.     end;
  259. end;
  260.  
  261. var
  262.     InputSource, OutputSource: Byte;
  263.     InputSet: TCharSet;
  264.     Answers: TCharSet;
  265. begin
  266.     Write('Welcome to the program that will print a set, whose elements are signs of arithmetic operations and digits from a given set' + #13#10+ 'Select the source for entering the array:'  + #13#10 + '1:Console'  + #13#10 + '2:File'  + #13#10 + 'Enter 1 or 2: ');
  267.     InputSource := TakeSource;
  268.     InputSet := TakeSet(InputSource);
  269.     Answers := FindAnswers(InputSet);
  270.     Write('Select the source for output:'  + #13#10 + '1:Console'  + #13#10 + '2:File'  + #13#10 + 'Enter 1 or 2: ');
  271.     OutputSource := TakeSource;
  272.     Output(OutputSource, Answers);
  273.     Readln;
  274. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement