Алгоритмы с многочленами
Пример. Разложить
многочлен
на кратные
множители.
![]()
![]()
![]()
│
![]()
![]()
![]()
![]()
│
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
│
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
│![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
│![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
│
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
│![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
│
![]()
![]()
![]()
![]()
![]()
![]()
│
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
Многочлен
имеет разложение
в виде
.
Я составила программу для разложения многочлена на кратные множители.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Grids;
type
TForm1 = class(TForm)
Edit1: TEdit;
Label1: TLabel;
SGd1: TStringGrid;
Label2: TLabel;
Button1: TButton;
Label3: TLabel;
SGd2: TStringGrid;
SGd3: TStringGrid;
SGd4: TStringGrid;
Edit6: TEdit;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
c,i,st1,st2,stiz,n_iz,n_nod,n,m,d_st,step,f:integer;
k,d,s:string;
kof1,kof2,k1,k2,izubst,a,b,a2,b2,buf,est,fxst:array[0..15] of integer;
izub,e,fx:array[0..50,0..50] of integer;
first:boolean;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var i,j,k_1,st3,l:integer;
sokr:boolean;
k2_2,k1_1:array[0..10] of integer;
begin
st1:=StrToInt(Edit1.Text);
for i:=0 to st1 do begin
SGd4.Cells[i,0]:=SGd1.Cells[i,0];
end;
repeat
n_iz:=n_iz+1;
st2:=st1-1;
for i:=0 to st1 do begin
if SGd1.Cells[i,0]<>'' then
kof1[st1-i]:=StrToInt(SGd1.Cells[i,0])
else MessageDlg('Внимание! Не введены значения коэффициентов!',mtWarning,[mbOK],0);
end;
s:='f(x)=';
for i:=st1 downto 0 do begin
if kof1[i]<>0 then begin
if(kof1[i-1]<0)or(i=0) then begin
str(kof1[i],d);
str(i,k);
s:=s+d+'x^'+k;
end
else begin
str(kof1[i],d);
str(i,k);
s:=s+d+'x^'+k+'+';
end;
end;
kof2[i-1]:=kof1[i]*i;
end;
//Edit2.Text:=s;
s:='f1(x)=';
for i:=st2 downto 0 do begin
SGd2.Cells[st2-i,0]:=inttostr(kof2[i]);
if kof2[i]<>0 then begin
if(kof2[i-1]<0)or(i=1) then begin
str(kof2[i],d);
str(i-1,k);
s:=s+d+'x^'+k;
end
else begin
str(kof2[i],d);
str(i-1,k);
s:=s+d+'x^'+k+'+';
end;
end;
//Edit3.Text:=s;
end;
for i:=0 to st1 do begin
kof1[i]:=StrToInt(SGd1.Cells[i,0]);
k1[i]:=StrToInt(SGd1.Cells[i,0]);
end;
for i:=0 to st2 do begin
kof2[i]:=StrToInt(SGd2.Cells[i,0]);
k2[i]:=StrToInt(SGd2.Cells[i,0]);
end;
while kof2[0]<>0 do begin
repeat
//Edit4.Text:='';
stiz:=0;
k_1:=k1[0];
if k1[0]<>kof2[0] then begin
if (k1[0] mod kof2[0])=0 then begin
for j:=0 to st2 do
k2[j]:=(k1[0] div kof2[0])*kof2[j];
end
else begin
if k2[0]<>1 then
for j:=0 to st1 do
k1[j]:=kof2[0]*k1[j];
if k_1<>1 then begin
for j:=0 to st2 do
k2[j]:=k_1*kof2[j];
end;
end;
end;
for i:=1 to st1 do begin
k1[i-1]:=k1[i]-k2[i];
end;
st1:=st1-1;
until st1<st2;
if k1[0]<>0 then begin //Сокращение
sokr:=true;
for i:=1 to st1 do
if k1[i]<>0 then begin
if (k1[i] mod k1[0])<>0 then sokr:=false;
end;
k_1:=k1[0];
if sokr=true then
for i:=0 to st1 do
k1[i]:=k1[i] div k_1;
end;
for i:=0 to st2 do //Замена многочленов
k2_2[i]:=kof2[i];
for i:=0 to st1 do
k1_1[i]:=k1[i];
for i:=0 to 10 do begin
SGd3.Cells[i,0]:='';
SGd1.Cells[i,0]:='';
kof1[i]:=0;
k1[i]:=0;
kof2[i]:=0;
k2[i]:=0;
izub[n_iz,i]:=0;
end;
izubst[n_iz]:=st2;
for i:=0 to st2 do begin
k1[i]:=k2_2[i];
SGd1.Cells[i,0]:=inttostr(k1[i]);
izub[n_iz,i]:=k1[i];
if k1[i]<>0 then begin
//Edit4.Text:=Edit4.Text+IntToStr(k1[i])+'x^'+IntToStr(st2-i);
end;