Advertisement
daniv1

Untitled

Nov 12th, 2017
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 2.90 KB | None | 0 0
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
  9.   Grids;
  10.  
  11. type
  12.   Matr = Array[1..5,1..5] of Real;
  13. Vec = Array[1..5] of Real;
  14.   { TForm1 }
  15.  
  16.   TForm1 = class(TForm)
  17.     Button1: TButton;
  18.     Button2: TButton;
  19.     Button3: TButton;
  20.     Edit1: TEdit;
  21.     Label1: TLabel;
  22.     Label2: TLabel;
  23.     Label3: TLabel;
  24.     Label4: TLabel;
  25.     StringGrid1: TStringGrid;
  26.     StringGrid2: TStringGrid;
  27.     StringGrid3: TStringGrid;
  28.     procedure Button1Click(Sender: TObject);
  29.     procedure Button2Click(Sender: TObject);
  30.     procedure Button3Click(Sender: TObject);
  31.     procedure FormCreate(Sender: TObject);
  32.   private
  33.     { private declarations }
  34.   public
  35.     { public declarations }
  36.   end;
  37.  
  38. var
  39.   Form1: TForm1;
  40.  
  41. implementation
  42.  
  43. {$R *.lfm}
  44.  
  45. { TForm1 }
  46.  
  47. procedure TForm1.FormCreate(Sender: TObject);
  48. begin
  49.  
  50. end;
  51.  
  52. procedure TForm1.Button3Click(Sender: TObject);
  53. begin
  54.   close
  55. end;
  56.  
  57. procedure TForm1.Button1Click(Sender: TObject);
  58. Var N: Integer;
  59. Begin
  60. N := StrToInt(Edit1.Text);
  61. StringGrid1.ColCount := N;
  62. StringGrid1.RowCount := N;
  63. StringGrid2.rowCount := N;
  64. StringGrid3.rowCount := N;
  65. end;
  66.  
  67.   procedure TForm1.Button2Click(Sender: TObject);
  68.   Var A : Matr;
  69.   b, x : Vec;
  70.   i, j, k, p, n: integer;
  71.   m, S, t: real;
  72.   begin
  73.   N := StrToInt(Edit1.Text);
  74.   For i:=1 to N do
  75.   For j:=1 to N do
  76.   Begin
  77.   a[i,j] := StrToFloat(StringGrid1.Cells[j - 1,i - 1]);
  78.   b[j] := StrToFloat(StringGrid2.Cells[0,j - 1]);
  79.   End;
  80. begin
  81. k:= 1;
  82. while true do
  83. begin
  84. i:= k + 1;
  85. if (a[k, k] = 0) then
  86. begin
  87. {перестановка}
  88. p:= k;
  89. while true do
  90. begin
  91. if abs(a[i, k]) > abs(a[p, k]) then p:= i;
  92. if i = n then break;
  93. i:= i + 1;
  94. continue;
  95. end;
  96. if p= k then i:= k + 1
  97. else
  98. begin
  99. j:= k;
  100. while true do
  101. begin
  102. t:= a[k, j];
  103. a[k, j]:= a[p, j];
  104. a[p, j]:= t;
  105. if j = n then break;
  106. j:= j + 1;
  107. continue;
  108. end;
  109. t:= b[k];
  110. b[k]:= b[p];
  111. b[p]:= t;
  112. end;
  113. end; // кінець перестановки
  114. while true do
  115. begin
  116. m:=a[i, k] / a[k, k];
  117. a[i, k]:= 0;
  118. j:= k + 1;
  119. while true do
  120. begin
  121. a[i, j]:= a[i, j] - m * a[k, j];
  122. if j = n then break;
  123. j:= j + 1;
  124. continue;
  125. end;
  126. b[i]:= b[i] - m * b[k];
  127. if i = n then break;
  128. i:= i + 1;
  129. continue;
  130. end;
  131. if k= n - 1 then break;
  132. k:= k + 1;
  133. continue;
  134. end;
  135. {Перевіряємо чи існує корінь}
  136. if a[n, n] <> 0 then
  137. begin
  138. x[n]:= b[n] / a[n, n];
  139. i:= n - 1;
  140. while true do
  141. begin
  142. j:= i + 1;
  143. S:= 0;
  144. while true do
  145. begin
  146. S:= S - a[i, j] * x[j];
  147. if j = n then break;
  148. j:= j + 1;
  149. continue;
  150. end;
  151. x[i]:= (b[i] + S) / a[i, i];
  152. if i = 1 then break;
  153. i:= i - 1;
  154. continue;
  155. end;
  156.   For i:=1 to N do
  157.   Begin
  158.   StringGrid3.Cells[0,i-1]:=FloatToStr(x[i]);
  159.   End;
  160. end
  161. else
  162. if b[n] = 0 then
  163. ShowMessage('Systema rivnyannya ne maye rozv''yazkiv')
  164. else
  165. ShowMessage('bezlich rozvyazkiv');
  166. ShowMessage('press any key');
  167. end;
  168. end;
  169. end.
  170.  
  171. end;
  172.  
  173. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement