Advertisement
venik2405

lab2_5

Nov 22nd, 2020
314
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.62 KB | None | 0 0
  1. Program lab2_5;
  2.  
  3. {$APPTYPE CONSOLE}
  4.  
  5. uses
  6. System.SysUtils;
  7.  
  8. Type
  9. TMassivs = array of array of Integer;
  10.  
  11. Function InputFileLocation(): string;
  12. Var
  13. IsCorrect: Boolean;
  14. Location: String;
  15. Begin
  16. Repeat
  17. IsCorrect := false;
  18. WriteLn('Enter file location:');
  19. ReadLn(Location);
  20. If FileExists(Location) then
  21. IsCorrect := true
  22. Else
  23. Begin
  24. WriteLn('Please enter the correct location');
  25. WriteLn('');
  26. End;
  27. Until IsCorrect;
  28. InputFileLocation := Location;
  29. End;
  30.  
  31.  
  32. Function ChooseInput(): Integer;
  33. Var
  34. Line: String;
  35. IsCorrect: Boolean;
  36. Begin
  37. Repeat
  38. IsCorrect := true;
  39. WriteLn('Do you want to input from file? (y/n)');
  40. ReadLn(line);
  41. Line := Line.ToLower();
  42. If(Line <> '') and (Line <> 'y') and (Line <> 'n') then
  43. Begin
  44. IsCorrect := false;
  45. WriteLn('Enter valid answer');
  46. End;
  47. Until IsCorrect;
  48. If (Line = '') or (Line = 'y') then
  49. ChooseInput := 0
  50. Else
  51. ChooseInput := 1;
  52. End;
  53.  
  54. Procedure Print(Triangle: TMassivs; Size: Integer);
  55. Var
  56. I, J, Temp : Integer;
  57. Begin
  58. Temp := Size - 1;
  59. For I := 0 to Temp do
  60. Begin
  61. For J := 0 to I do
  62. Write(Triangle[I,J], ' ');
  63. writeln;
  64. End;
  65. End;
  66.  
  67. Function GetSizeFromFile(Size : Integer): Integer;
  68. Var
  69. TxtFile: TextFile;
  70. Begin
  71. AssignFile(TxtFile, InputFileLocation());
  72. Reset(TxtFile);
  73. Read(TxtFile, Size);
  74. ReadLn(TxtFile);
  75. CloseFile(TxtFile);
  76. GetSizeFromFile := Size;
  77. End;
  78.  
  79.  
  80. Procedure OutputToFile(Triangle: TMassivs; Size: Integer);
  81. Var
  82. I, J, Temp: Integer;
  83. TxtFile: TextFile;
  84. Line: String;
  85. Begin
  86. Temp := Size - 1;
  87. AssignFile(TxtFile, InputFileLocation());
  88. Rewrite(TxtFile);
  89. Begin
  90. For I := 0 to Temp do
  91. Begin
  92. For J := 0 to I do
  93. Write(TxtFile, Triangle[I,J], ' ');
  94. writeln(TxtFile);
  95. End;
  96. CloseFile(TxtFile);
  97. End;
  98.  
  99. End;
  100.  
  101. procedure CreateTriangle(Size: Integer ; Triangle: TMassivs);
  102. Var
  103. N, K:Integer;
  104. Begin
  105. N := 0;
  106. K := 0;
  107. for n := 0 to size do
  108. Begin
  109. triangle[n,0] := 1;
  110. triangle[n,n] := 1;
  111. for k := 1 to n do
  112. triangle[n][k] := triangle[n - 1][k - 1] + triangle[n - 1][k];
  113. End;
  114. End;
  115.  
  116.  
  117.  
  118. Function GetSizeFromConsole(IsNatural: Boolean = false): Integer;
  119. Var
  120. IsCorrect: Boolean;
  121. Size: Integer;
  122. Begin
  123. Size := 0;
  124. WriteLn('Enter the matrix size');
  125. Repeat
  126. IsCorrect := false;
  127. Try
  128. ReadLn(Size);
  129. IsCorrect := true;
  130. Except
  131. WriteLn('Enter the Number')
  132. End;
  133. If IsCorrect and IsNatural and (Size < 1) then
  134. Begin
  135. IsCorrect := false;
  136. WriteLn('Please enter a natural value');
  137. End;
  138. Until IsCorrect ;
  139. GetSizeFromConsole := Size;
  140. End;
  141.  
  142. Procedure Main();
  143. Var
  144. ChosenInput: Integer;
  145. Triangle: TMassivs;
  146. Size : Integer;
  147. Begin
  148. Size := 0;
  149. WriteLn('Удалить строки массива , содержащие нулевые элементы.');
  150. ChosenInput := ChooseInput();
  151. If (ChosenInput = 0) then
  152. Size := GetSizeFromFile(size)
  153. Else
  154. Size := GetSizeFromConsole(true);
  155. writeln('Pascal`s Triangle');
  156. SetLength(Triangle, Size + 1, Size + 1);
  157. CreateTriangle(Size, Triangle);
  158. Print(Triangle, Size);
  159. WriteLn;
  160. OutputToFile(Triangle, Size);
  161. ReadLn;
  162. End;
  163.  
  164. Begin
  165. Main();
  166. End.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement