Advertisement
MaxVashkevich

Untitled

Oct 21st, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 7.05 KB | None | 0 0
  1. program z2_3;
  2.  
  3. {$APPTYPE CONSOLE}
  4.  
  5. uses
  6.     System.SysUtils;
  7.  
  8. const
  9.     NumOfRows = 30;
  10.     NumOfCols = 10;
  11.  
  12. type
  13.     TMarks = array [1 .. 30] of array [1 .. 10] of Integer;
  14.     TExcellents = array [1 .. 30] of Boolean;
  15.  
  16. procedure OpenInputFile(var InputFile: Text);
  17. var
  18.     IsCorrect: Boolean;
  19.     InputPath: string;
  20. begin
  21.     IsCorrect := False;
  22.     repeat
  23.         Writeln('Enter path to the file:');
  24.         Readln(InputPath);
  25.         if FileExists(InputPath) then
  26.         begin
  27.             AssignFile(InputFile, InputPath);
  28.             try
  29.                 Reset(InputFile);
  30.                 if not EOF(InputFile) then
  31.                     IsCorrect := True
  32.                 else
  33.                     Writeln('Empty file provided.');
  34.             except
  35.                 on E: EInOutError do
  36.                     Writeln('This file can''t be opened.');
  37.             end;
  38.         end
  39.         else
  40.             Writeln('File with such name doesn''t exist.');
  41.     until IsCorrect;
  42. end;
  43.  
  44. procedure GetMenu(var Character: Char);
  45. var
  46.     IsCorrect: Boolean;
  47.     Buffer: string;
  48. const
  49.     AllowedChars = ['y', 'Y', 'n', 'N'];
  50. begin
  51.     IsCorrect := False;
  52.     repeat
  53.         try
  54.             Readln(Buffer);
  55.             if (Length(Buffer) = 1) and (Buffer[1] in AllowedChars) then
  56.             begin
  57.                 Character := Buffer[1];
  58.                 IsCorrect := True;
  59.             end
  60.             else
  61.                 Writeln('Invalid input!');
  62.         except
  63.             on EInOutError do
  64.                 Writeln('Invalid input!');
  65.         end;
  66.     until IsCorrect;
  67. end;
  68.  
  69. function RewriteFile(var OutputFile: Text;
  70.     const OutputPath, Prompt: string): Boolean;
  71. var
  72.     Menu: Char;
  73. const
  74.     ErrorMessage = 'File access denied. Please try another file.';
  75.     Yes = ['y', 'Y'];
  76. begin
  77.     RewriteFile := False;
  78.     Writeln(Prompt);
  79.     GetMenu(Menu);
  80.     if Menu in Yes then
  81.     begin
  82.         AssignFile(OutputFile, OutputPath);
  83.         try
  84.             Rewrite(OutputFile);
  85.             RewriteFile := True;
  86.         except
  87.             on E: EInOutError do
  88.                 Writeln(ErrorMessage);
  89.         end;
  90.     end;
  91. end;
  92.  
  93. procedure OpenOutputFile(var OutputFile: Text);
  94. var
  95.     IsCorrect: Boolean;
  96.     OutputPath: string;
  97. const
  98.     OverwritePrompt = 'File with such name exists. Overwrite? (Y-yes, N-no)';
  99.     NewFilePrompt =
  100.         'File with such name doesn''t exist. Create new file? (Y-yes, N-no)';
  101. begin
  102.     IsCorrect := False;
  103.     repeat
  104.         Writeln('Enter path to the file:');
  105.         Readln(OutputPath);
  106.         if FileExists(OutputPath) then
  107.             IsCorrect := RewriteFile(OutputFile, OutputPath, OverwritePrompt)
  108.         else
  109.             IsCorrect := RewriteFile(OutputFile, OutputPath, NewFilePrompt);
  110.     until IsCorrect;
  111. end;
  112.  
  113. function GetMarks(var InputFile: Text; var Marks: TMarks): Boolean;
  114. var
  115.     i, j: Byte;
  116.     IsCorrect: Boolean;
  117. const
  118.     MinMark = 0;
  119.     MaxMark = 10;
  120. begin
  121.     IsCorrect := True;
  122.     i := 1;
  123.     while (not EOF(InputFile)) and IsCorrect do
  124.     begin
  125.         if i > NumOfRows then
  126.         begin
  127.             Writeln('Too many rows.');
  128.             IsCorrect := False;
  129.         end
  130.         else
  131.         begin
  132.             j := 1;
  133.             while (not EOLN(InputFile)) and IsCorrect do
  134.             begin
  135.                 try
  136.                     if j > NumOfCols then
  137.                     begin
  138.                         Writeln('Too many marks in row ', i, '.');
  139.                         IsCorrect := False;
  140.                     end
  141.                     else
  142.                     begin
  143.                     ///////////////////
  144.                         Read(InputFile, Marks[i, j]);
  145.                         if ((Marks[i, j] < MinMark) or (Marks[i, j] > MaxMark))
  146.                         then
  147.                         begin
  148.                             Writeln('Invalid number in row ', i, ' col ', j,
  149.                                 '! Mark must be an integer from ', MinMark,
  150.                                 ' to ', MaxMark, ' inclusive.');
  151.                             IsCorrect := False;
  152.                         end;
  153.  
  154.                         Inc(j);
  155.                     end;
  156.                 except
  157.                     on EInOutError do
  158.                     begin
  159.                         Writeln('Invalid input! Mark must be an integer from ',
  160.                             MinMark, ' to ', MaxMark, ' inclusive in row ', i,
  161.                             ' col ', j, '.');
  162.                         IsCorrect := False;
  163.                     end;
  164.                 end;
  165.             end;
  166.             if (j < NumOfCols) and IsCorrect then
  167.             begin
  168.                 Writeln('Not enough marks in row ', i, '.');
  169.                 IsCorrect := False;
  170.             end;
  171.             Readln(InputFile);
  172.             Inc(i);
  173.         end;
  174.     end;
  175.     if (i < NumOfRows) and IsCorrect then
  176.     begin
  177.         Writeln('Not enough rows in file.');
  178.         IsCorrect := False;
  179.     end;
  180.     GetMarks := IsCorrect;
  181. end;
  182.  
  183. procedure FindExcellents(var Excellents: TExcellents; var Marks: TMarks);
  184. var
  185.     i, j: Integer;
  186. const
  187.     MinPositiveMark = 8;
  188. begin
  189.     for i := 1 to NumOfRows do
  190.     begin
  191.         Excellents[i] := True;
  192.         for j := 1 to NumOfCols do
  193.             if (Marks[i, j] < MinPositiveMark) and (Excellents[i]) then
  194.                 Excellents[i] := False;
  195.     end;
  196. end;
  197.  
  198. procedure PrintMarks(const Marks: TMarks; var OutputFile: Text);
  199. var
  200.     i, j: Integer;
  201. const
  202.     HorizontalDelimiter =
  203.         '----------------------------------------------------------------';
  204. begin
  205.     for i := 1 to NumOfRows do
  206.     begin
  207.         Writeln(OutputFile, HorizontalDelimiter);
  208.         Write(OutputFile, '| Student ', i:2, ' |');
  209.         for j := 1 to NumOfCols do
  210.             Write(OutputFile, ' ', Marks[i, j]:2, ' |');
  211.         Writeln(OutputFile);
  212.     end;
  213.     Writeln(OutputFile, HorizontalDelimiter);
  214. end;
  215.  
  216. procedure PrintExcellents(const Excellents: TExcellents; var OutputFile: Text);
  217. var
  218.     i: Integer;
  219. begin
  220.     Write(OutputFile, #10#13'Numbers of excellent students:');
  221.     for i := 1 to NumOfRows do
  222.         if Excellents[i] then
  223.             Write(OutputFile, ' ', i:2, ' ');
  224.     Writeln(OutputFile);
  225. end;
  226.  
  227. procedure PrintData(var OutputFile: Text; const Marks: TMarks;
  228.     const Excellents: TExcellents);
  229. begin
  230.     PrintMarks(Marks, OutputFile);
  231.     PrintExcellents(Excellents, OutputFile);
  232. end;
  233.  
  234. procedure Main;
  235. var
  236.     InputFile, OutputFile: Text;
  237.     Marks: TMarks;
  238.     Excellents: TExcellents;
  239.     IsCorrect: Boolean;
  240. begin
  241.     Writeln('This program prints marks and finds underperforming students.');
  242.     OpenInputFile(InputFile);
  243.     IsCorrect := GetMarks(InputFile, Marks);
  244.     CloseFile(InputFile);
  245.     if IsCorrect then
  246.     begin
  247.         FindExcellents(Excellents, Marks);
  248.         PrintData(Output, Marks, Excellents);
  249.         OpenOutputFile(OutputFile);
  250.         PrintData(OutputFile, Marks, Excellents);
  251.         Writeln('Data was written to file.');
  252.         CloseFile(OutputFile);
  253.     end;
  254.     Readln;
  255. end;
  256.  
  257. begin
  258.     Main;
  259.  
  260. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement