Advertisement
Egor_Vakar

lab2_4(delphi)

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