Advertisement
Guest User

Untitled

a guest
Oct 21st, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 4.08 KB | None | 0 0
  1. program Project125;
  2.  
  3. {$APPTYPE CONSOLE}
  4. uses
  5.   SysUtils;
  6.  
  7. const
  8.    Rows = 31;
  9.    Columns = 9;
  10.    MinNumber = -32768;
  11.    MaxNumber = 32767;
  12. type
  13.    Matrix = array [1 .. Rows, 1 .. Columns] of integer;
  14.  
  15. function CheckInputFile(var FileName: string): string;
  16. var
  17.    IsCorrect: Boolean;
  18.    UserFile: TextFile;
  19. begin
  20.    IsCorrect := False;
  21.    repeat
  22.       try
  23.          Write('Please, Enter the Name of File with Input Data in Format name.txt: ');
  24.          ReadLn(FileName);
  25.          AssignFile(UserFile, FileName);
  26.          Reset(UserFile);
  27.          if FileExists(FileName) then
  28.          begin
  29.             if Eof(UserFile) then
  30.                WriteLn('This File Is Empty. So That:');
  31.             CheckInputFile := FileName;
  32.             IsCorrect := True;
  33.          end;
  34.       except
  35.          on E:EInOutError do
  36.          begin
  37.             IsCorrect := False;
  38.             WriteLn('This File with Such Name Does Not Exist. Please, Try Again.');
  39.          end;
  40.       end;
  41.    until IsCorrect;
  42.    CloseFile(UserFile);
  43. end;
  44.  
  45. function ReadMatrix(var UserFile: TextFile; var Matr: Matrix): Boolean;
  46. var
  47.    i, j: Integer;
  48.    Error: String;
  49.    IsValidMatrix: Boolean;
  50. begin
  51.    Reset(UserFile);
  52.    IsValidMatrix := True;
  53.    Error := ' ';
  54.    for i := 1 to Rows do
  55.    begin
  56.       for j := 1 to Columns do
  57.       //begin
  58.          try
  59.             Read(UserFile, Matr[i, j]);
  60.             if (Matr[i, j] > MaxNumber) or (Matr[i, j] < MinNumber) then
  61.             begin
  62.                Error := Error + '[' + IntToStr(i) + ', ' + IntToStr(j) + '] ';
  63.                IsValidMatrix := False;
  64.             end;
  65.          except
  66.             Error := Error + '[' + IntToStr(i) + ', ' + IntToStr(j) + '] ';
  67.             IsValidMatrix := False;
  68.          end;
  69.       //end;
  70.       ReadLn(UserFile);
  71.    end;
  72.    if not IsValidMatrix then
  73.    begin
  74.       Write('Element Musr Be In the Range from ', MinNumber, ' to ', MaxNumber, '. Wrong Elements Are:');
  75.       Writeln(Error);
  76.    end;
  77.    ReadMatrix := IsValidMatrix;
  78.    CloseFile(UserFile);
  79. end;
  80.  
  81. procedure GoodGreatMarks(var UserFile: TextFile; var Matr: Matrix; var GoodRes, GreatRes: Integer);
  82. var i, j: Integer;
  83.     FileName: string;
  84.     IsValidInput: Boolean;
  85. begin
  86.    IsValidInput := False;
  87.    repeat
  88.       //repeat
  89.       FileName := CheckInputFile(FileName);
  90.       //until IsValidInput;
  91.       AssignFile(UserFile, FileName);
  92.       IsValidInput := ReadMatrix(UserFile, Matr);
  93.    until IsValidInput;
  94.    Reset(UserFile);
  95.    GoodRes := 0;
  96.    GreatRes := 0;
  97.    for i := 1 to Rows do
  98.       for j := 1 to Columns do
  99.       begin
  100.          Read(UserFile, Matr[i, j]);
  101.          if (Matr[i, j] > 5) then
  102.          begin
  103.             if (Matr[i, j] > 8) then
  104.             begin
  105.                ReadLn(UserFile);
  106.                inc(GreatRes);
  107.             end;
  108.             ReadLn(UserFile);
  109.             inc(GoodRes);
  110.          end;
  111.          ReadLn(UserFile);
  112.       end;
  113.       WriteLn('Amount of Students with Good Results: ', GoodRes);
  114.       WriteLn('Amount of Students with Great Results: ', GreatRes);
  115.       CloseFile(UserFile);
  116. end;
  117.  
  118. procedure OutputToFile(var OutFile: TextFile; var GoodRes, GreatRes: Integer);
  119. var
  120.    OutFileName: string;
  121.    IsCorrect: Boolean;
  122. begin
  123.    IsCorrect := False;
  124.    repeat
  125.       Write('Please, Enter the Name of Output File in Format name.txt: ');
  126.       ReadLn(OutFileName);
  127.       if FileExists(OutFileName) then
  128.          IsCorrect := True
  129.       else
  130.       begin
  131.          IsCorrect := False;
  132.          WriteLn('This File with Such Name Does Not Exist. Please, Try Again.');
  133.       end;
  134.    until IsCorrect;
  135.    AssignFile(OutFile, OutFileName);
  136.    Append(OutFile);
  137.    WriteLn(OutFile, 'Amount of Students with Good Results: ', GoodRes);
  138.    WriteLn(OutFile, 'Amount of Students with Great Results: ', GreatRes);
  139.    CloseFile(OutFile);
  140. end;
  141.  
  142. procedure Main();
  143. var
  144.    Matr: Matrix;
  145.    UserFile: TextFile;
  146.    OutFile: TextFile;
  147.    GoodRes, GreatRes: Integer;
  148. begin
  149.   GoodGreatMarks(UserFile, Matr, GoodRes, GreatRes);
  150.   OutputToFile(OutFile, GoodRes, GreatRes);
  151. end;
  152.  
  153. begin
  154.    Main();
  155.    ReadLn;
  156. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement