Advertisement
green1ant

2_4 last

Oct 21st, 2018
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 2.96 KB | None | 0 0
  1. program Project2;
  2. {$APPTYPE CONSOLE}
  3. uses
  4.    System.SysUtils, Math;
  5. const
  6.    FileCompositionError = 'Error! Your file is composed incorrectly. ';
  7.    ReadFileError = 'Error! Flie contains invalid data. ';
  8.    NoSuchFileError = 'Error! No such file. ';
  9. type
  10.    TDots = array of array [0..1] of Integer;
  11.  
  12. function EnterInputFileName() : string;
  13. var
  14.    Name : string;
  15. begin
  16.    Writeln('Enter input file name');
  17.    Readln(Name);
  18.    while not FileExists(Name) do
  19.    begin
  20.       Writeln(NoSuchFileError, 'Input file name again');
  21.       Readln(Name);
  22.    end;
  23.    EnterInputFileName := Name;
  24. end;
  25.  
  26. function EnterOutputFileName() : string;
  27. var
  28.    Name : string;
  29. begin
  30.    Writeln('Enter output file name');
  31.    Readln(Name);
  32.    EnterOutputFileName := Name;
  33. end;
  34.  
  35. function IsFileValid(var InputFile : TextFile) : Boolean;
  36. var
  37.    IsValid : Boolean;
  38.    Dots : TDots;
  39.    i : Integer;
  40. begin
  41.    i := 0;
  42.    IsValid := True;
  43.    while not EoF(InputFile) and IsValid do
  44.       begin
  45.          SetLength(Dots, i + 1);
  46.          Read(InputFile, Dots[i][0]);
  47.          if EoLN(InputFile) then
  48.             IsValid := False;
  49.          Read(InputFile, Dots[i][1]);
  50.          if not EoLN(InputFile) then
  51.             IsValid := False;
  52.          Inc(i);
  53.       end;
  54.    IsFileValid := IsValid;
  55. end;
  56.  
  57. procedure InputDots(var InputFile : TextFile; var Dots : TDots);
  58. var
  59.    i : Integer;
  60.    Name, Input : string;
  61. begin
  62.    Reset(InputFile);
  63.    i := 0;
  64.    while not EoF(InputFile) do
  65.    begin
  66.       SetLength(Dots, i + 1);
  67.       Read(InputFile, Dots[i][0]);
  68.       Read(InputFile, Dots[i][1]);
  69.       Inc(i);
  70.    end;
  71. end;
  72.  
  73. function GetAmount(var Dots : TDots) : Integer;
  74. var
  75.    Counter, i, j, LastIndex : Integer;
  76. begin
  77.    Counter := 0;
  78.    LastIndex := High(Dots);
  79.    for i := 0 to LastIndex do
  80.       if (Dots[i][0] >= 0) and (Dots[i][1] >= 0) then
  81.          Counter := Counter + 1;
  82.    GetAmount := Counter;
  83. end;
  84.  
  85. procedure OutputAnswer(Answer : Integer; OutputFileName : string);
  86. var
  87.    OutputFile : TextFile;
  88. begin
  89.    AssignFile(OutputFile, OutputFileName);
  90.    Rewrite(OutputFile);
  91.    Writeln(Format('There are %d dots in the 1-st quarter', [Answer]));
  92.    Writeln(OutputFile, Format('There are %d dots in the 1-st quarter', [Answer]));
  93.    CloseFile(OutputFile);
  94. end;
  95.  
  96. procedure Main();
  97. var
  98.    Dots : TDots;
  99.    InputFileName, OutputFileName : string;
  100.    InputFile, OutputFile : TextFile;
  101. begin
  102.    Writeln('This program can calculate how many dots are in the 1-st quarter');
  103.    try
  104.       InputFileName := EnterInputFileName();
  105.       AssignFile(InputFile, InputFileName);
  106.       Reset(InputFile);
  107.       if IsFileValid(InputFile) then
  108.       begin
  109.          InputDots(InputFile, Dots);
  110.          OutputFileName := EnterOutputFileName();
  111.          OutputAnswer(GetAmount(Dots), OutputFileName);
  112.       end
  113.       else
  114.          Writeln(FileCompositionError);
  115.    except
  116.       Writeln(ReadFileError);
  117.    end;
  118.    CloseFile(InputFile);
  119.    Readln;
  120. end;
  121.  
  122. begin
  123.    Main();
  124. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement