Advertisement
Andrik0099

laba_2_3 (Delphi)

Oct 20th, 2018
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 6.60 KB | None | 0 0
  1. program laba_2_3;
  2.  
  3. {$APPTYPE CONSOLE}
  4.  
  5. uses
  6.     SysUtils;
  7.  
  8. type
  9.     TShortIntMatrix = array of array of ShortInt;
  10.  
  11. const
  12.     MinNumber: ShortInt = -128;
  13.     MaxNumber: ShortInt = 127;
  14.     MinSize: ShortInt = 2;
  15.     MaxSize: ShortInt = 127;
  16.  
  17. procedure GetOutputToConsole(Sum: Integer);
  18. begin
  19.     WriteLn('The sum is:', Sum);
  20. end;
  21.  
  22. function GetAnswer(): Char;
  23. var
  24.     Answer: Char;
  25.     IsCorrect: Boolean;
  26. begin
  27.     repeat
  28.         ReadLn(Answer);
  29.         Answer := UpCase(Answer);
  30.         if (Answer = 'Y') or (Answer = 'N') then
  31.             IsCorrect := true
  32.         else
  33.         begin
  34.             IsCorrect := false;
  35.             WriteLn('Incorrect input. Enter Y(Yes) or N(No):');
  36.         end;
  37.     until IsCorrect;
  38.     GetAnswer := Answer;
  39. end;
  40.  
  41. procedure GetOutputToFile(Sum: Integer);
  42. var
  43.     IsCorrect: Boolean;
  44.     NewFile: TextFile;
  45.     NameOfFile: String;
  46. begin
  47.     WriteLn('Enter the name of file Name.txt:');
  48.     IsCorrect := false;
  49.     repeat
  50.         ReadLn(NameOfFile);
  51.         if (not FileExists(NameOfFile)) then
  52.             WriteLn('File does not exist. Try again:')
  53.         else
  54.         begin
  55.             IsCorrect := true;
  56.             Assign(NewFile, NameOfFile);
  57.             writeln('Would you like to rewrite the file? Press Y(Yes) or N(No):');
  58.             if GetAnswer = 'Y' then
  59.                 try
  60.                     Rewrite(NewFile);
  61.                     WriteLn(NewFile, 'The sum is:', Sum);
  62.                 except
  63.                     WriteLn('Access is not allowed. Try again:');
  64.                     IsCorrect:= false;
  65.                 end
  66.             else
  67.                 try
  68.                     Append(NewFile);
  69.                     WriteLn(NewFile, 'The sum is:', Sum);
  70.                 except
  71.                     WriteLn('Access is not allowed. Try again:');
  72.                     IsCorrect:= false;
  73.                 end;
  74.         end;
  75.     until IsCorrect;
  76.     Close(NewFile);
  77. end;
  78.  
  79.  
  80. function GetSum(Matrix: TShortIntMatrix; Size: ShortInt): Integer;
  81. var
  82.     Sum: Integer;
  83.     i, j: ShortInt;
  84. begin
  85.     Sum := 0;
  86.     for j := 0 to Size do
  87.         for i := j + 1 to Size do
  88.             if (Matrix[i, j] > 0) then
  89.                     Sum := Sum + Matrix[i, j];
  90.     GetSum := Sum;
  91. end;
  92.  
  93. function CheckInput(Min, Max: ShortInt): ShortInt;
  94. var
  95.     IsCorrect: Boolean;
  96.     Number: ShortInt;
  97. begin
  98.     IsCorrect := false;
  99.     repeat
  100.         try
  101.             ReadLn(Number);
  102.             if (Number >= min) and (Number <= max) then
  103.                 IsCorrect := true
  104.             else
  105.                 WriteLn('Enter number from interval ', Min, '..', Max, ':');
  106.         except
  107.             WriteLn('Check entered data. Enter number from interval ', Min, '..', Max, ':');
  108.         end;
  109.     until IsCorrect;
  110.     CheckInput := Number;
  111. end;
  112.  
  113. function GetMatrixConsole(var Size: ShortInt): TShortIntMatrix;
  114. var
  115.     i, j: ShortInt;
  116.     Matrix: TShortIntMatrix;
  117.     IsCorrect: Boolean;
  118. begin
  119.     WriteLn('Enter size of matrix ', MinSize, '..', MaxSize, ':');
  120.     Size := CheckInput(MinSize, MaxSize);
  121.     SetLength(Matrix, Size, Size);
  122.     Dec(Size);
  123.     for i := 0 to Size do
  124.         for j := 0 to Size do
  125.         begin
  126.             WriteLn('Enter [', i, '][', j, '] number of matrix ', MinNumber, '..', MaxNumber, ':');
  127.             Matrix[i, j] := CheckInput(MinNumber, MaxNumber);
  128.         end;
  129.     GetMatrixConsole := Matrix;
  130. end;
  131.  
  132. function CheckInputFile(const MinNumber, MaxNumber: ShortInt; var MyFile: TextFile): Boolean;
  133. var
  134.     IsCorrect: boolean;
  135.     Number: ShortInt;
  136. begin
  137.     IsCorrect := true;
  138.     while (not SeekEof(MyFile)) and (IsCorrect) do
  139.         try
  140.             ReadLn(MyFile, Number);
  141.             if (Number <= MinNumber) or (Number >= MaxNumber) then
  142.                 IsCorrect := false;
  143.         except
  144.             IsCorrect := false;
  145.         end;
  146.     CheckInputFile := IsCorrect;
  147. end;
  148.  
  149. function ReadFromFile(var MyFile: TextFile; var Matrix: TShortIntMatrix; var Size: ShortInt): Boolean;
  150. var
  151.     Number, i, j: ShortInt;
  152. begin
  153.     if CheckInputFile(MinNumber, MaxNumber, MyFile) then
  154.     begin
  155.         reset(MyFile);
  156.         i := 0;
  157.         while not Eof(MyFile) do
  158.         begin
  159.             SetLength(Matrix, i + 1);
  160.             j := 0;
  161.             while not Eoln(MyFile) do
  162.             begin
  163.                 SetLength(Matrix[i], j + 1);
  164.                 Read(MyFile,Matrix[i,j]);
  165.                 WriteLn('Elemet [', i, '][', j, ']:', Matrix[i,j]);
  166.                 inc(j);
  167.             end;
  168.             ReadLn(MyFile);
  169.             inc(i);
  170.         end;
  171.         if (i = j) then
  172.         begin
  173.             Size := i - 1;
  174.             ReadFromFile := true;
  175.         end
  176.         else
  177.         begin                                  
  178.             WriteLn('This is not a square matrix. Try again:');
  179.             ReadFromFile := false;
  180.         end;
  181.     end
  182.     else
  183.     begin
  184.         CloseFile(MyFile);
  185.         ReadFromFile := false;
  186.         WriteLn('Check entered data. Enter number from interval ', MinNumber, '..', MaxNumber, '. Try Again:');
  187.     end;
  188. end;
  189.  
  190. function GetMatrixFile(var Size: ShortInt): TShortIntMatrix;
  191. var
  192.     IsCorrect: Boolean;
  193.     NameOfFile: String;
  194.     MyFile: TextFile;
  195.     Matrix: TShortIntMatrix;
  196. begin
  197.     WriteLn('Enter file name(Name.txt):');
  198.     repeat
  199.         ReadLn(NameOfFile);
  200.         if (not FileExists(NameOfFile)) then
  201.         begin
  202.             WriteLn('File does not exist. Try again:');
  203.             IsCorrect := false;
  204.         end
  205.         else
  206.         begin
  207.             AssignFile(MyFile, NameOfFile);
  208.             reset(MyFile);
  209.             if SeekEof(MyFile) then
  210.             begin
  211.                 WriteLn('File is empty. Try again:');
  212.                 IsCorrect := false;
  213.             end
  214.             else
  215.                 IsCorrect := ReadFromFile(MyFile, Matrix, Size);
  216.         end;
  217.     until IsCorrect;
  218.     CloseFile(MyFile);
  219.     GetMatrixFile := Matrix;
  220. end;
  221.  
  222. procedure Main();
  223. var
  224.     Matrix: TShortIntMatrix;
  225.     Size: ShortInt;
  226.     Sum: Integer;
  227. begin
  228.     WriteLn('This program calculates the sum of positive elements of a square matrix under the main diagonal.', #10, 'Would you like to open the file? Press Y(Yes) or N(No):');
  229.     if GetAnswer = 'Y' then
  230.         Matrix := GetMatrixFile(Size)
  231.     else
  232.         Matrix := GetMatrixConsole(Size);
  233.     Sum := GetSum(Matrix, Size);
  234.     WriteLn('Would you like to write down the answer to file? Press Y(Yes) or N(No):');
  235.     if GetAnswer = 'Y' then
  236.         GetOutputToFile(Sum)
  237.     else
  238.         GetOutputToConsole(Sum);
  239.     WriteLn('Press "Enter" to exit the console.');
  240.     ReadLn;
  241. end;
  242.  
  243. begin
  244.     Main();
  245. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement