Advertisement
Guest User

Untitled

a guest
Oct 19th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.59 KB | None | 0 0
  1. program Project7;
  2.  
  3. {$APPTYPE CONSOLE}
  4.  
  5. {$R *.res}
  6.  
  7. uses
  8. System.SysUtils;
  9.  
  10. type
  11. matrixInteger = Array of array of integer;
  12. const
  13. SenseMessage = 'Данная программа находит сумму элементов, расположенных в заштрихованной части матрицы';
  14. InputMessage = 'Пожалуйста, введите имя файла, из которого будут считываться данные';
  15. OutputMessage = 'Сумма элементов, расположенных в заштрихованной части матрицы';
  16. MistakeFoundMessage = 'Ошибка! Файл с таким именем не найден. Повторите опытку.';
  17. MistakeOpenMessage = 'Ошибка! Невозможно открыть данный файл. Пожалуйста, повторите попытку';
  18. InputExample = 'Например, "C:\Users\Ольга\Desktop\LabWork\Unit2\Lab 4\input.txt"';
  19. OutputExample = 'Например, "C:\Users\Ольга\Desktop\LabWork\Unit2\Lab 4\output.txt"';
  20.  
  21.  
  22. function LoadMatrixFromFile(var InF: TextFile) : matrixInteger;
  23. var I, J, N: Integer;
  24. NNMatrix: matrixInteger;
  25. begin
  26. Read(InF, N);
  27. SetLength(NNMatrix, N, N);
  28. for I := 0 to N - 1 do
  29. for J := 0 to N - 1 do
  30. Read(InF, NNMatrix [I][J]);
  31. LoadMatrixFromFile := NNMatrix;
  32. end;
  33.  
  34. function IsElementInArea(I,J,N: Integer) : Boolean;
  35. begin
  36. if J < N / 2 then
  37. if (I > J + 1) and (I < N - J) then
  38. Result := True
  39. else
  40. Result := False;
  41. end;
  42.  
  43. function CalculateSum(var OutF : TextFile; NNMatrix : matrixInteger; N: integer) : Integer;
  44. var Sum, I, J : Integer;
  45. begin
  46. Sum := 0;
  47. for J := 0 to N div 2 do
  48. for I := J to N - J - 1 do
  49. begin
  50. writeln(NNMatrix[I][J]);
  51. Sum := Sum + NNMatrix[I][J];
  52. end;
  53.  
  54. WriteLn('---------------------------------');
  55. for I := 0 to N - 1 do
  56. begin
  57. for J := 0 to N - 1 do
  58. if IsElementInArea(I,J,N) then
  59. Write(NNMatrix[I][J]);
  60. WriteLn;
  61. end;
  62. WriteLn('---------------------------------');
  63. WriteLn(OutF,Sum);
  64. Result := Sum;
  65. end;
  66.  
  67. procedure Main();
  68. var InF, OutF : TextFile;
  69. NNMatrix : matrixInteger;
  70. N, i, j : integer;
  71. begin
  72. AssignFile(InF, 'D:\University\ola\input.txt');
  73. AssignFile(OutF, 'D:\University\ola\output.txt');
  74. Reset(InF);
  75. Rewrite(OutF);
  76. NNMatrix := LoadMatrixFromFile(InF);
  77. N := length(NNMatrix);
  78. WriteLn(CalculateSum(OutF,NNMatrix,N));
  79. Close(InF);
  80. Close(OutF);
  81. Readln;
  82. end;
  83.  
  84. begin
  85. Main();
  86. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement