Advertisement
Guest User

Untitled

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