Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program Project36;
- {$APPTYPE CONSOLE}
- uses
- SysUtils;
- const
- MinNumber = 1;
- MaxNumber = 10;
- type
- TMatrix = array Of array of integer;
- function CheckInputFile(var UserFile: TextFile; var FileName: string): string;
- var
- IsCorrect: Boolean;
- begin
- repeat
- IsCorrect := False;
- try
- Write('Please, Enter the Name of File with Input Data in Format name.txt: ');
- ReadLn(FileName);
- AssignFile(UserFile, FileName);
- Reset(UserFile);
- if Eof(UserFile) then
- WriteLn('This File Is Empty. So That:');
- CheckInputFile := FileName;
- IsCorrect := True;
- except
- on E:EInOutError do
- begin
- IsCorrect := False;
- WriteLn('This File with Such Name Does Not Exist. Please, Try Again.');
- end;
- end;
- until IsCorrect;
- CloseFile(UserFile);
- end;
- function IsValidMatrix(var UserFile: TextFile; FileName: string; var LenMatI, LenMatJ: Integer): Boolean;
- var
- i, j: Integer;
- Temp: string;
- //Temp: Integer;
- begin
- i := 0;
- AssignFile(UserFile, FileName);
- Reset(UserFile);
- j := 0;
- While (not Eoln(UserFile)) do
- begin
- Read(UserFile, Temp);
- Inc(j);
- end;
- Reset(UserFile);
- while (not Eof(UserFile)) do
- begin
- Readln(UserFile, Temp);
- Inc(i);
- end;
- IsValidMatrix := True;
- LenMatI := i;
- LenMatJ := j;
- end;
- function ReadMatrix(var UserFile: TextFile; var Matr: TMatrix; LenMatI, LenMatJ: Integer): Boolean;
- var
- i, j: Integer;
- Error: String;
- IsValidMatrix: Boolean;
- begin
- Reset(UserFile);
- IsValidMatrix := True;
- Error := ' ';
- SetLength (Matr, LenMatI, LenMatJ);
- for i := 0 to LenMatI - 1 do
- begin
- for j := 0 to LenMatJ - 1 do
- try
- Read(UserFile, Matr[i, j]);
- if (Matr[i, j] > MaxNumber) or (Matr[i, j] < MinNumber) then
- begin
- Error := Error + '[' + IntToStr(i + 1) + ', ' + IntToStr(j + 1) + '] ';
- IsValidMatrix := False;
- end;
- except
- Error := Error + '[' + IntToStr(i + 1) + ', ' + IntToStr(j + 1) + '] ';
- IsValidMatrix := False;
- end;
- ReadLn(UserFile);
- end;
- if not IsValidMatrix then
- begin
- Write('Element Must Be In the Range from ', MinNumber, ' to ', MaxNumber, '. Wrong Elements Are:');
- Writeln(Error);
- end;
- ReadMatrix := IsValidMatrix;
- CloseFile(UserFile);
- end;
- procedure GoodGreatMarks(Matr: TMatrix; var GoodRes, GreatRes: Integer);
- var i, j: Integer;
- Good, Great: Boolean;
- LenMatI, LenMatJ: Integer;
- begin
- GoodRes := 0;
- GreatRes := 0;
- SetLength(Matr, LenMatI, LenMatJ);
- for i := 0 to LenMatI - 1 do
- begin
- Good := True;
- Great := True;
- for j := 0 to LenMatJ - 1 do
- begin
- if (Matr[i, j] < 6) then
- begin
- Great := False;
- Good := False;
- end
- else
- if (Matr[i, j] < 8) then
- Great := False;
- end;
- if (Great = True) then
- inc(GreatRes)
- else
- if (Good = True) then
- inc(GoodRes);
- end;
- WriteLn('Amount of Students with Good Results: ', GoodRes);
- WriteLn('Amount of Students with Great Results: ', GreatRes);
- end;
- procedure OutputToFile(var OutFile: TextFile; GoodRes, GreatRes: Integer);
- var
- OutFileName: string;
- IsCorrect: Boolean;
- begin
- IsCorrect := False;
- repeat
- Write('Please, Enter the Name of Output File in Format name.txt: ');
- ReadLn(OutFileName);
- if FileExists(OutFileName) then
- begin
- IsCorrect := True;
- end
- else
- begin
- IsCorrect := False;
- WriteLn('This File with Such Name Does Not Exist. Please, Try Again.');
- end;
- until IsCorrect;
- AssignFile(OutFile, OutFileName);
- Append(OutFile);
- WriteLn(OutFile, 'Amount of Students with Good Results: ', GoodRes);
- WriteLn(OutFile, 'Amount of Students with Great Results: ', GreatRes);
- CloseFile(OutFile);
- end;
- procedure Main;
- var
- Matr: TMatrix;
- UserFile: TextFile;
- OutFile: TextFile;
- FileName: string;
- GoodRes, GreatRes, LenMatI, LenMatJ: Integer;
- IsValidInput: Boolean;
- begin
- WriteLn('This Program Determines How Many Students in the Group Have Good and Great Results.');
- IsValidInput := False;
- repeat
- FileName := CheckInputFile(UserFile, FileName);
- IsValidInput:= IsValidMatrix(UserFile, FileName, LenMatI, LenMatJ);
- IsValidInput := ReadMatrix(UserFile, Matr, LenMatI, LenMatJ);
- until IsValidInput;
- GoodGreatMarks(Matr, GoodRes, GreatRes);
- OutputToFile(OutFile, GoodRes, GreatRes);
- end;
- begin
- Main;
- ReadLn;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement