1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
| unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, Grids,
StdCtrls, Buttons, Spin;
type
{ TForm1 }
TForm1 = class(TForm)
BitBtn1: TBitBtn;
BitBtn2: TBitBtn;
Button1: TButton;
Memo1: TMemo;
SpinEdit1: TSpinEdit;
StringGrid1: TStringGrid;
StringGrid2: TStringGrid;
procedure BitBtn1Click(Sender: TObject);
procedure BitBtn2Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure spinEdit1Change(Sender: TObject);
procedure clean;
private
{ private declarations }
public
{ public declarations }
end;
var
Form1: TForm1;
type
TMatrix = array[0..10, 0..10 + 1] of real;
TVector = array[0..10] of real;
var
A: TMatrix;
X: TVector;
p, i, j: integer;
k, n: real;
implementation
{================ Решение системы линейных уравнений ====================}
procedure SolveSLE(n: integer; A: TMatrix; var x: TVector);
var
i, j, k, m: integer;
t: real;
begin
{прямой ход}
for i := 0 to n - 1 do
begin
{перестановка строк с максимальным A[...,i]}
t := abs(A[i, i]);
m := i;
for k := i + 1 to n - 1 do
if t < abs(A[k, i]) then
begin
m := k;
t := abs(A[k, i]);
end;
if (i <> m) then
begin
for j := 0 to n do
begin
t := A[i, j];
A[i, j] := A[m, j];
A[m, j] := t;
end;
end;
{собственно сам ход}
t := A[i, i];
for j := i to n - 1 do
A[i, j] := A[i, j] / t;
A[i, n] := A[i, n] / t;
for k := i + 1 to n - 1 do
begin
t := A[k, i];
for j := i to n - 1 do
A[k, j] := A[k, j] - t * A[i, j];
A[k, n] := A[k, n] - t * A[i, n];
end;
end;
{обратный ход}
for i := n - 1 downto 0 do
begin
x[i] := A[i, n];
for j := i + 1 to n - 1 do
x[i] := x[i] - A[i, j] * x[j];
end;
end;
{$R *.lfm}
procedure TForm1.clean;
var
n, i, j, code: integer;
begin
val(spinedit1.Text, n, code);
if code <> 0 then
begin
ShowMessage('ошибка ' + spinedit1.Text);
spinedit1.SetFocus;
exit;
end;
for i := 0 to n - 1 do
for j := 0 to n do
begin
StringGrid1.Cells[j, i] := '';
StringGrid2.Cells[j, i] := '';
end;
end;
procedure TForm1.spinEdit1Change(Sender: TObject);
begin
StringGrid1.RowCount := StrToInt(Trim(spinEdit1.Text));
StringGrid1.ColCount := StrToInt(Trim(spinEdit1.Text)) + 1;
// StringGrid3.ColCount:=StrToInt(Trim(spinEdit1.Text));
end;
procedure TForm1.BitBtn2Click(Sender: TObject);
var
i, j: integer;
begin
clean;
randomize;
for i := 0 to 10 do
for j := 0 to 10 do
begin
a[i, j] := random(50) - 25;
end;
for i := 0 to StringGrid1.RowCount - 1 do
for j := 0 to StringGrid1.ColCount - 1 do
StringGrid1.Cells[j, i] := floatToStr(a[i, j]);
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
clean;
end;
procedure TForm1.BitBtn1Click(Sender: TObject);
var
i, j, n1, code: integer;
Sum: real;
begin
val(spinedit1.Text, n1, code);
if code <> 0 then
begin
ShowMessage('ошибка ' + spinedit1.Text);
spinedit1.SetFocus;
exit;
end;
Memo1.Lines.Add(floatToStr(n1) + ' ');
for i := 0 to n1 - 1 do
for j := 0 to n1 do
a[i, j] := StrToInt(StringGrid1.Cells[j, i]);
SolveSLE(n1, a, x);
for j := 0 to n1 - 1 do
StringGrid2.Cells[1, j] := floatToStr(x[j]);
for i := 0 to n1 - 1 do
begin
Sum := 0;
for j := 0 to n1 - 1 do
Sum := Sum + a[i, j] * x[j];
StringGrid2.Cells[3, i] := floatToStr(Sum);
end;
end;
end. |