algorytm.org

Implementacja w Delphi/Pascal



Baza Wiedzy
wersja offline serwisu przeznaczona na urządzenia z systemem Android
Darowizny
darowiznaWspomóż rozwój serwisu
Nagłówki RSS
Artykuły
Implementacje
Komentarze
Forum
Bookmarki






Sonda
Implementacji w jakim języku programowania poszukujesz?

Algorytm Hornera (pochodne) - Implementacja w Delphi/Pascal
Ocena użytkownikóww: *****  / 2
SłabyŚwietny
Nadesłany przez Tomasz Lubiński, 08 sierpnia 2005 01:00
Kod przedstawiony poniżej przedstawia główną część rozwiązania problemu.
Pobierz pełne rozwiązanie.

Jeżeli nie odpowiada Ci sposób formatowania kodu przez autora skorzystaj z pretty printer'a i dostosuj go automatycznie do siebie.

hornerp_d/Algorytm.pas:
//www.algorytm.org
//Tomasz Lubiński (c)2001
//Algorytmy numeryczne - Algorytm Hornera - obliczanie wartosci znormalizowanych pochodnych wielomianu

unit Algorytm;
interface
uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ComCtrls, Grids, Buttons, Math;
type
  TForm1 = class(TForm)
    Edit1: TEdit;
    StringGrid1: TStringGrid;
    Label1: TLabel;
    BitBtn1: TBitBtn;
    BitBtn3: TBitBtn;
    Edit2: TEdit;
    Label2: TLabel;
    Button1: TButton;
    Button2: TButton;
    Label3: TLabel;
    Edit3: TEdit;
    Label6: TLabel;
    Label4: TLabel;
    Edit4: TEdit;
    procedure Edit1Change(Sender: TObject);
    procedure w_lewo(Sender: TObject);
    procedure w_prawo(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure StringGrid1SelectCell(Sender: TObject; ACol, ARow: Integer;
      var CanSelect: Boolean);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
var
  Form1: TForm1;
  n: Integer;
  prawo: Integer=1;
  X: Real;
  A,B,C: Array of real;

implementation

{$R *.DFM}

function wart(k:Integer):Real;   //wartość wielomianu
begin
if k=n then wart:=B[k] else wart:=wart(k+1)*x+B[k]
end;

function wspol(k:Integer):Real;  //współczynniki wielmianu po podzieleniu
begin
if k=n then begin wspol:=B[k]; C[k-1]:=B[k]; end
else begin wspol:=wspol(k+1)*x+B[k]; if k>0 then C[k-1]:=wspol(k+1)*x+B[k]; end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
StringGrid1.ColCount:=1;
StringGrid1.RowCount:=2;
Edit1Change(Form1);
end;

//Zrobienie siatki i ustawienie tablic dynamicznych
procedure TForm1.Edit1Change(Sender: TObject);
var i:Integer;
begin
n:=StrToInt(Edit1.Text);
StringGrid1.ColCount:=n+1;
SetLength(A, n+1);  //tablica ze współczynnikami wielomianu
SetLength(B, n+1);
SetLength(C, n+1);
for i:=0 to n+1 do StringGrid1.Cells[i,1]:='';
for i:=0 to n do StringGrid1.Cells[i,0]:='x^'+IntToStr(n-i);
end;

//Nawigacja
procedure TForm1.w_lewo(Sender: TObject);
begin
if prawo>0 then
 begin
 prawo:=prawo-1;
 StringGrid1.Col:=prawo;
 end;
end;
procedure TForm1.w_prawo(Sender: TObject);
begin
if prawo<n then
 begin
  prawo:=prawo+1;
  StringGrid1.Col:=prawo;
 end;
end;
procedure TForm1.StringGrid1SelectCell(Sender: TObject; ACol,
  ARow: Integer; var CanSelect: Boolean);
begin
  prawo:=ACol;
end;

//wpis
procedure TForm1.Button1Click(Sender: TObject);
var i:Integer;
begin
Val(Edit2.Text,A[n-prawo],i);
if i<>0 then ShowMessage('Blad podczas wpisu') Else StringGrid1.Cells[prawo,1]:=Edit2.Text;
end;

procedure TForm1.Button2Click(Sender: TObject);
var i,j,s: Integer;
begin
Val(Edit3.Text,x,i);
if i<>0 then begin ShowMessage('Źle wpisane x'); exit; end;
s:=StrToInt(Edit4.Text);
if (s<0) or (s>n) then begin ShowMessage('Źle wpisany stopien pochodnej'); exit; end;
for i:=0 to n do B[i]:=A[i];
for j:=1 to s do
 begin
  wspol(0);
  for i:=0 to n do B[i]:=C[i];
 end;
label6.caption:='Wartość pochodnej znormalizowanej '+IntToStr(s)+' stopnia wynosi: '+FloatToStr(wart(0));
end;
end.
Dodaj komentarz