Advertisement
green1ant

2_4 delphi final

Oct 21st, 2018
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.01 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 Dots : TDots; 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. //Dots2 : TDots;
  100. InputFileName, OutputFileName : string;
  101. InputFile, OutputFile : TextFile;
  102. begin
  103. Writeln('This program can calculate how many dots are in the 1-st quarter');
  104. try
  105. InputFileName := EnterInputFileName();
  106. AssignFile(InputFile, InputFileName);
  107. Reset(InputFile);
  108. if IsFileValid(Dots, InputFile) then
  109. begin
  110. //InputDots(InputFile, Dots);
  111. OutputFileName := EnterOutputFileName();
  112. OutputAnswer(GetAmount(Dots), OutputFileName);
  113. end
  114. else
  115. Writeln(FileCompositionError);
  116. except
  117. Writeln(ReadFileError);
  118. end;
  119. CloseFile(InputFile);
  120. Readln;
  121. end;
  122.  
  123. begin
  124. Main();
  125. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement