Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program z2_3;
- {$APPTYPE CONSOLE}
- uses
- System.SysUtils;
- const
- NumOfRows = 30;
- NumOfCols = 10;
- type
- TMarks = array [1 .. 30] of array [1 .. 10] of Integer;
- TExcellents = array [1 .. 30] of Boolean;
- procedure OpenInputFile(var InputFile: Text);
- var
- IsCorrect: Boolean;
- InputPath: string;
- begin
- IsCorrect := False;
- repeat
- Writeln('Enter path to the file:');
- Readln(InputPath);
- if FileExists(InputPath) then
- begin
- AssignFile(InputFile, InputPath);
- try
- Reset(InputFile);
- if not EOF(InputFile) then
- IsCorrect := True
- else
- Writeln('Empty file provided.');
- except
- on E: EInOutError do
- Writeln('This file can''t be opened.');
- end;
- end
- else
- Writeln('File with such name doesn''t exist.');
- until IsCorrect;
- end;
- procedure GetMenu(var Character: Char);
- var
- IsCorrect: Boolean;
- Buffer: string;
- const
- AllowedChars = ['y', 'Y', 'n', 'N'];
- begin
- IsCorrect := False;
- repeat
- try
- Readln(Buffer);
- if (Length(Buffer) = 1) and (Buffer[1] in AllowedChars) then
- begin
- Character := Buffer[1];
- IsCorrect := True;
- end
- else
- Writeln('Invalid input!');
- except
- on EInOutError do
- Writeln('Invalid input!');
- end;
- until IsCorrect;
- end;
- function RewriteFile(var OutputFile: Text;
- const OutputPath, Prompt: string): Boolean;
- var
- Menu: Char;
- const
- ErrorMessage = 'File access denied. Please try another file.';
- Yes = ['y', 'Y'];
- begin
- RewriteFile := False;
- Writeln(Prompt);
- GetMenu(Menu);
- if Menu in Yes then
- begin
- AssignFile(OutputFile, OutputPath);
- try
- Rewrite(OutputFile);
- RewriteFile := True;
- except
- on E: EInOutError do
- Writeln(ErrorMessage);
- end;
- end;
- end;
- procedure OpenOutputFile(var OutputFile: Text);
- var
- IsCorrect: Boolean;
- OutputPath: string;
- const
- OverwritePrompt = 'File with such name exists. Overwrite? (Y-yes, N-no)';
- NewFilePrompt =
- 'File with such name doesn''t exist. Create new file? (Y-yes, N-no)';
- begin
- IsCorrect := False;
- repeat
- Writeln('Enter path to the file:');
- Readln(OutputPath);
- if FileExists(OutputPath) then
- IsCorrect := RewriteFile(OutputFile, OutputPath, OverwritePrompt)
- else
- IsCorrect := RewriteFile(OutputFile, OutputPath, NewFilePrompt);
- until IsCorrect;
- end;
- function GetMarks(var InputFile: Text; var Marks: TMarks): Boolean;
- var
- i, j: Byte;
- IsCorrect: Boolean;
- const
- MinMark = 0;
- MaxMark = 10;
- begin
- IsCorrect := True;
- i := 1;
- while (not EOF(InputFile)) and IsCorrect do
- begin
- if i > NumOfRows then
- begin
- Writeln('Too many rows.');
- IsCorrect := False;
- end
- else
- begin
- j := 1;
- while (not EOLN(InputFile)) and IsCorrect do
- begin
- try
- if j > NumOfCols then
- begin
- Writeln('Too many marks in row ', i, '.');
- IsCorrect := False;
- end
- else
- begin
- ///////////////////
- Read(InputFile, Marks[i, j]);
- if ((Marks[i, j] < MinMark) or (Marks[i, j] > MaxMark))
- then
- begin
- Writeln('Invalid number in row ', i, ' col ', j,
- '! Mark must be an integer from ', MinMark,
- ' to ', MaxMark, ' inclusive.');
- IsCorrect := False;
- end;
- Inc(j);
- end;
- except
- on EInOutError do
- begin
- Writeln('Invalid input! Mark must be an integer from ',
- MinMark, ' to ', MaxMark, ' inclusive in row ', i,
- ' col ', j, '.');
- IsCorrect := False;
- end;
- end;
- end;
- if (j < NumOfCols) and IsCorrect then
- begin
- Writeln('Not enough marks in row ', i, '.');
- IsCorrect := False;
- end;
- Readln(InputFile);
- Inc(i);
- end;
- end;
- if (i < NumOfRows) and IsCorrect then
- begin
- Writeln('Not enough rows in file.');
- IsCorrect := False;
- end;
- GetMarks := IsCorrect;
- end;
- procedure FindExcellents(var Excellents: TExcellents; var Marks: TMarks);
- var
- i, j: Integer;
- const
- MinPositiveMark = 8;
- begin
- for i := 1 to NumOfRows do
- begin
- Excellents[i] := True;
- for j := 1 to NumOfCols do
- if (Marks[i, j] < MinPositiveMark) and (Excellents[i]) then
- Excellents[i] := False;
- end;
- end;
- procedure PrintMarks(const Marks: TMarks; var OutputFile: Text);
- var
- i, j: Integer;
- const
- HorizontalDelimiter =
- '----------------------------------------------------------------';
- begin
- for i := 1 to NumOfRows do
- begin
- Writeln(OutputFile, HorizontalDelimiter);
- Write(OutputFile, '| Student ', i:2, ' |');
- for j := 1 to NumOfCols do
- Write(OutputFile, ' ', Marks[i, j]:2, ' |');
- Writeln(OutputFile);
- end;
- Writeln(OutputFile, HorizontalDelimiter);
- end;
- procedure PrintExcellents(const Excellents: TExcellents; var OutputFile: Text);
- var
- i: Integer;
- begin
- Write(OutputFile, #10#13'Numbers of excellent students:');
- for i := 1 to NumOfRows do
- if Excellents[i] then
- Write(OutputFile, ' ', i:2, ' ');
- Writeln(OutputFile);
- end;
- procedure PrintData(var OutputFile: Text; const Marks: TMarks;
- const Excellents: TExcellents);
- begin
- PrintMarks(Marks, OutputFile);
- PrintExcellents(Excellents, OutputFile);
- end;
- procedure Main;
- var
- InputFile, OutputFile: Text;
- Marks: TMarks;
- Excellents: TExcellents;
- IsCorrect: Boolean;
- begin
- Writeln('This program prints marks and finds underperforming students.');
- OpenInputFile(InputFile);
- IsCorrect := GetMarks(InputFile, Marks);
- CloseFile(InputFile);
- if IsCorrect then
- begin
- FindExcellents(Excellents, Marks);
- PrintData(Output, Marks, Excellents);
- OpenOutputFile(OutputFile);
- PrintData(OutputFile, Marks, Excellents);
- Writeln('Data was written to file.');
- CloseFile(OutputFile);
- end;
- Readln;
- end;
- begin
- Main;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement