{ !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! }
{ !! Раскомментировать строку "Uses CRT" !! }
{ !! И закомментировать процедуру "Readkey" !! }
{ !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! }
program NewGaussMethod;
{$E-,N+,G+,D-}
type
Sto = array [ 1..70 ] of Extended;
var
A : array [ 1..70 ] of sto;{Массив }
B : Sto ;
i, j, k : Byte;
m, n, p : Byte;{m - количество строк , n - количество эл-в}
Function ReadKey : Byte;Assembler;
asm
mov ah, $01
int $21
end;
Label
Loop{:Word};
Procedure ScreenMattr;
var
i, j : Byte;
begin
Writeln;
for i := 1 to m do
begin
for j := 1 to n do
Write(A[i, j]:1:3,' ');
WriteLn('| ',B[i]:1:3);
end;
ReadKey;
end;
Procedure Swap(p, q: Byte);
var
j : Byte;
X : Real;
begin
for j:=1 to n do
begin
X := A[p,j];
A[p,j] := A[q,j];
A[q,j] := X;
end;
X := B[p];
B[p] := B[q];
B[q] := X;
end;
Procedure Divid(p : Byte; k : Real);
var
j : Byte;
begin
for j := 1 to n do
begin
A[p, j] := A[p, j] / k;
if Abs(A[p, j]) <1.0E-10 then {Иначе возможен неправильный результат}
A[p, j] := 0.0;
end;
B[p] := B[p] / k;
if Abs(B[p]) <1.0E-10 then
B[p] := 0.0;
end;
Procedure Sub(p, q : Byte);
var
j : Byte;
begin
for j := 1 to n do
begin
A[p, j] := A[p, j] - A[q, j];
if Abs(A[p, j]) <1.0E-10 then {Иначе возможен неправильный результат}
A[p, j] := 0.0;
end;
B[p] := B[p] - B[q];
if Abs(B[p]) <1.0E-10 then
B[p] := 0.0;
end;
Function CheckNullStr(p : Byte): Boolean;
var
i : Byte;
k : Real;
begin
k := 0;
for i := 1 to n do
k:=k + Abs (A[p, i]);
CheckNullStr := ((k=0) and (B[p]=0));
end;
Function BadStr(p : Byte) : Boolean;
var
i : Byte;
k : Real;
begin
k := 0;
for i := 1 to n do
k:=k + Abs (A[p, i]);
BadStr := ((k=0) and (B[p]<>0));
end;
Procedure KillNullStr(r : Byte);
begin
Swap(r, m);
dec(m);
end;
Function IsZeroDiag : Boolean;
var
i : Byte;
q : Boolean;
begin
q := false;
for i := 1 to m do
q := q or (A[i,i] = 0) ;
IsZeroDiag := q;
end;
Procedure FindNZD ;
var
i : Byte;
begin
for i := k+1 to m do
if A[i,k] <> 0.0 then
begin
Swap(k, i);
end;
end;
{++++++++++++++++++++++++++++++++++++++++++++++++++++++++}
Procedure ViewResult(h : Byte);
var
G, j, p : Byte;
begin
WriteLn('************* Ответы ***************');
case h of
0:
begin
Writeln('Решений нет: ');
ScreenMattr;
ReadKey;
end;
1:
begin
if CheckNullStr(1) then
begin
WriteLn('Бесконечно много решений(равенство: 0 = 0):');
ScreenMattr;
end;
if m>=n then {Вообще-то после обработки матрицы M>N уже невозможно !}
begin
ScreenMattr;
for i:=1 to m do
WriteLn('X[', i, '] = ', B[i]:1:7);
ReadKey;
end;
if m0 then
if A[G,J]<0 then
Write(' + ',Abs(A[G,J]):5:3,'*X(',J,')')
else Write(' - ',A[G,J]:5:3,'*X(',J,')');
end;
WriteLn;
p := 0;
end;
ReadKey;
end;
end;
end;{case...}
Halt;
end;
{========================================================}
begin
m:=0;
n:=0;
While (m=0) or (n=0) do
begin
Write('Введите количество уравнений (1..70) m = ');
Readln(m);
Write('Введите количество неизвестных (1..70) n = ');
Readln(n);
end;
for i := 1 to m do
begin
WriteLn('***********', i, '-ое уравнение... **************');
for j := 1 to n do
begin
Write('A [ ', i,', ', j,'] = ');
ReadLn( A [ i, j ]);
end;
Write('B [ ', i,'] = ');
ReadLn( B [ i ]);
end;
ScreenMattr;
k := 1;
Loop:
While k <= m do {???}
begin
for i := 1 to m do
if not BadStr(i) then
begin
if CheckNullStr(i) then
begin
KillNullStr(i);
goto loop;
end;
end
else ViewResult(0);
FindNZD;
p:=0;
for i := k to n do
if A[k,i] = 0 then inc(p)
else Break;
for i := 1 to m do
if A[i,k+p]<>0 then
Divid(i, A[i,k+p]);
for i :=1 to m do
if (i<>(k+p)) and (A[i,k+p]<>0.0) then
Sub(i,k+p);
inc(k);
end;
for i:=1 to m do if A[i,i]<>0 then
Divid(i,A[i,i]);
i :=1;
While i <= m do
begin
if BadStr(i) then ViewResult(0);
if CheckNullStr(i) then KillNullStr(i);
inc(i);
end;
ViewResult(1);
end.