Advertisement
green1ant

2_4 finale

Oct 21st, 2018
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 2.77 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.    WriteError = 'Error! Check ''w'' permissions of this file. ';
  10. type
  11.    TDots = array of array [0..1] of Integer;
  12.  
  13. function EnterInputFileName() : string;
  14. var
  15.    Name : string;
  16. begin
  17.    Writeln('Enter input file name');
  18.    Readln(Name);
  19.    while not FileExists(Name) do
  20.    begin
  21.       Writeln(NoSuchFileError, 'Input file name again');
  22.       Readln(Name);
  23.    end;
  24.    EnterInputFileName := Name;
  25. end;
  26.  
  27. function EnterOutputFileName() : string;
  28. var
  29.    Name : string;
  30. begin
  31.    Writeln('Enter output file name');
  32.    Readln(Name);
  33.    EnterOutputFileName := Name;
  34. end;
  35.  
  36. function ReadFile(var Dots : TDots; var InputFile : TextFile) : Boolean;
  37. var
  38.    IsValid : Boolean;
  39.    //Dots : TDots;
  40.    i : Integer;
  41. begin
  42.    i := 0;
  43.    IsValid := True;
  44.    while not EoF(InputFile) and IsValid do
  45.       begin
  46.          SetLength(Dots, i + 1);
  47.          Read(InputFile, Dots[i][0]);
  48.          if EoLN(InputFile) then
  49.             IsValid := False;
  50.          Read(InputFile, Dots[i][1]);
  51.          if not EoLN(InputFile) then
  52.             IsValid := False;
  53.          Inc(i);
  54.       end;
  55.    ReadFile := IsValid;
  56. end;
  57.  
  58. function GetAmount(var Dots : TDots) : Integer;
  59. var
  60.    Counter, i, j, LastIndex : Integer;
  61. begin
  62.    Counter := 0;
  63.    LastIndex := High(Dots);
  64.    for i := 0 to LastIndex do
  65.       if (Dots[i][0] >= 0) and (Dots[i][1] >= 0) then
  66.          Counter := Counter + 1;
  67.    GetAmount := Counter;
  68. end;
  69.  
  70. procedure OutputAnswer(Answer : Integer; OutputFileName : string);
  71. var
  72.    OutputFile : TextFile;
  73. begin
  74.    try
  75.       AssignFile(OutputFile, OutputFileName);
  76.       Rewrite(OutputFile);
  77.       Writeln(Format('There are %d dots in the 1-st quarter', [Answer]));
  78.       Writeln(OutputFile, Format('There are %d dots in the 1-st quarter', [Answer]));
  79.    except
  80.       Writeln(WriteError);
  81.    end;
  82.    CloseFile(OutputFile);
  83. end;
  84.  
  85. procedure Main();
  86. var
  87.    Dots : TDots;
  88.    //Dots2 : TDots;
  89.    InputFileName, OutputFileName : string;
  90.    InputFile, OutputFile : TextFile;
  91. begin
  92.    Writeln('This program can calculate how many dots are in the 1-st quarter');
  93.    try
  94.       InputFileName := EnterInputFileName();
  95.       AssignFile(InputFile, InputFileName);
  96.       Reset(InputFile);
  97.       if ReadFile(Dots, InputFile) then
  98.       begin
  99.          OutputFileName := EnterOutputFileName();
  100.          OutputAnswer(GetAmount(Dots), OutputFileName);
  101.       end
  102.       else
  103.          Writeln(FileCompositionError);
  104.    except
  105.       Writeln(ReadFileError);
  106.    end;
  107.    CloseFile(InputFile);
  108.    Readln;
  109. end;
  110.  
  111. begin
  112.    Main();
  113. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement