azard
Зарегистрирован: 26.11.2005 Сообщения: 1
|
Добавлено: Сб Ноя 26 2005 16:19 Заголовок сообщения: Нужна помощь (Pascal -> Delphi) |
|
|
Вот программа на паскале, которая работает!
(Решение СЛУ методом Крамера)
Код: |
Const n = 4;
Type
Equation = Array[1 .. n, 1 .. Succ(n)] Of Double;
matrix = Array[1 .. n, 1 .. n] Of Double;
Const
a: Equation = ((1, 9, 45, 4, 87), (1, 2, 7, 2, 0), (-6, 1, 6, 7, 0), (1, 2, 4, 7, 0));
Procedure GetMatrix(wout: Integer; Var m: matrix);
Var i, j: Integer;
Begin
For i := 1 To n Do
For j := 1 To n Do
If j <> wout Then m[i, j] := a[i, j]
Else m[i, j] := a[i, Succ(n)]
End;
Function Det(a: matrix; n: integer): Double;
Var i, j, k: Integer; d: Double;
Const
Eps = 10E-6;
Begin
For i := 1 To Pred(n) Do
Begin
If Abs(a[i, i]) < Eps Then
Begin
Det := 0.0; Exit
End;
For j := Succ(i) To n Do
Begin
d := a[j, i] / a[i, i];
For k := i To n Do
a[j, k] := a[j, k] - d * a[i, k];
End;
End;
d := 1.0;
For i := 1 To n Do
d := d * a[i, i];
Det := d
End;
Var
i: Integer; mx: matrix; Determ: Double;
begin
GetMatrix(Succ(n), mx);
Determ := Det(mx, n);
If Abs(Determ) < 1E-6 Then
Writeln( 'Matrix determinant = 0' )
Else
For i := 1 To n Do
Begin
GetMatrix(i, mx);
WriteLn( 'x(', i, ') = ', (Det(mx, n) / Determ):8:5 )
End;
readln;
end.
|
При переносе на Delphi не считает, возможно с типами и объявлениями переменных что-то напутал.
Код: |
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
Const
n = 4;
type
Equation = Array[1 .. n, 1 .. Succ(n)] Of Double;
matrix = Array[1 .. n, 1 .. n] Of Double;
Const
a: Equation = ((1, 9, 45, 4, 87), (1, 2, 7, 2, 0), (-6, 1, 6, 7, 0), (1, 2, 4, 7, 0));
type
TForm1 = class(TForm)
Memo1: TMemo;
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure GetMatrix(wout: Integer; m: matrix);
function Det(a: matrix; n: integer): Double;
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
Procedure TForm1.GetMatrix(wout: Integer; m: matrix);
Var i, j: Integer;
Begin
For i := 1 To n Do
For j := 1 To n Do
If j <> wout Then m[i, j] := a[i, j]
Else m[i, j] := a[i, Succ(n)]
End;
Function TForm1.Det(a: matrix; n: integer): Double;
Var i, j, k: Integer; d: Double;
Const
Eps = 10E-6;
Begin
For i := 1 To Pred(n) Do
Begin
If Abs(a[i, i]) < Eps Then
Begin
Form1.Memo1.Lines.Add(FloatToStr(a[i, i]));
Det := 0.0; Exit
End;
For j := Succ(i) To n Do
Begin
d := a[j, i] / a[i, i];
For k := i To n Do
a[j, k] := a[j, k] - d * a[i, k];
End;
End;
d := 1.0;
For i := 1 To n Do
d := d * a[i, i];
Det := d
End;
procedure TForm1.Button1Click(Sender: TObject);
VAR
i: Integer; mx: matrix; Determ: Double;
begin
GetMatrix(Succ(n), mx);
Determ := Det(mx, n);
If Abs(Determ) < (1E-6) Then
Form1.Memo1.Lines.Add( 'Matrix determinant = 0' )
Else
For i := 1 To n Do
Begin
GetMatrix(i, mx);
Form1.Memo1.Lines.Add( 'x(' + IntToStr(i) + ') = ' + FloatToStr((Det(mx, n) / Determ)) )
End;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
Close();
end;
end.
|
Помогите разобраться в чем там дело.. |
|