Advertisement
green1ant

2_4

Oct 20th, 2018
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 2.37 KB | None | 0 0
  1. program Project2;
  2. {$APPTYPE CONSOLE}
  3. uses
  4.    System.SysUtils;
  5. const
  6.    ErrorMessage = 'Error! Incorrect input (N should be natural number from 1 to 2147483647)';
  7.    InputFileName = 'input.txt';
  8.    OutputFileName = 'output.txt';
  9.    FileCompositionError = 'Error! Your file is composed incorrectly';
  10.    ReadFileError = 'Error! Flie contains invalid data';
  11. type
  12.    TDots = array of array [0..2] of Integer;
  13.  
  14. procedure Calculate(var Dots : TDots);
  15. var
  16.    Counter, i, j, LastIndex : Integer;
  17.    OutputFile : TextFile;
  18.  
  19. begin
  20.    Counter := 0;
  21.    LastIndex := High(Dots);
  22.    Assign(OutputFile, OutputFileName);
  23.    Rewrite(OutputFile);
  24.    Writeln('Next dots are in 1-st quarter:');
  25.    Writeln(OutputFile, 'Next dots are in 1-st quarter:');
  26.    for i := 0 to LastIndex do
  27.       if (Dots[i][0] >= 0) and (Dots[i][1] >= 0) then
  28.       begin
  29.          Counter := Counter + 1;
  30.          Writeln('(', Dots[i][0], ' , ', Dots[i][1], ') ');
  31.          Writeln(OutputFile, '(', Dots[i][0], ' , ', Dots[i][1], ') ');
  32.       end;
  33.    Writeln(' = ', Counter, ' in genereal');
  34.    Writeln(OutputFile, ' = ', Counter, ' in genereal');
  35.    CloseFile(OutputFile);
  36. end;
  37.  
  38.  
  39. procedure ReadAndPrintDots(var Dots : TDots);
  40. var
  41.    Counter, i : Integer;
  42.    IsCorrect : Boolean;
  43.    InputFile : TextFile;
  44.    Name, Input : string;
  45. begin
  46.    i := 0;
  47.  
  48.    Writeln('Enter file name');
  49.    Readln(Name);
  50.    while not FileExists(Name) do
  51.    begin
  52.       Writeln('No such file. Input file name again');
  53.       Readln(Name);
  54.    end;
  55.  
  56.    IsCorrect := True;
  57.    try
  58.       AssignFile(InputFile, InputFileName);
  59.       Reset(InputFile);
  60.       while not EoF(InputFile) and IsCorrect do
  61.       begin
  62.          SetLength(Dots, i + 1);
  63.          Read(InputFile, Dots[i][0]);
  64.          if EoLN(InputFile) then
  65.             IsCorrect := False;
  66.          Read(InputFile, Dots[i][1]);
  67.          if not EoLN(InputFile) then
  68.             IsCorrect := False;
  69.          //Write('(', Dots[i][0], ' ', Dots[i][1], '), ');
  70.          Inc(i);
  71.       end;
  72.       if not IsCorrect then
  73.          Writeln(FileCompositionError)
  74.       else
  75.          Calculate(Dots);
  76.    except
  77.       Writeln(ReadFileError);
  78.    end;
  79.    CloseFile(InputFile);
  80. end;
  81.  
  82. procedure Main();
  83. var
  84.    Dots : TDots;
  85. begin
  86.    Writeln('This program can calculate how many dots are in the 1-st quarter');
  87.    ReadAndPrintDots(Dots);
  88.    Readln;
  89. end;
  90.  
  91. begin
  92.    Main();
  93. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement