J
juli
Unregistered / Unconfirmed
GUEST, unregistred user!
代码:
//多项式ADT单元,使用数组方法
unit Polynomial_liner;
interface
type
array1 = record //存储多项式数据
p: array[1..30] of Integer;
//系数
e: array[1..30] of Integer;
//指数
num: byte;
//多项式项数
end;
Tpoly = class
data: array1;
public
constructor Create;virtual;
procedure Attach(p: Integer;
e: Integer);virtual;
//增加数据
procedure clear;virtual;
//清空
function Empty:Boolean;
//判断是否为空
procedure Add(B: Tpoly;var Result: Tpoly);
//多项式相加
procedure Sub(B: Tpoly;var Result: Tpoly);
//多项式相减
procedure Mult(B: Tpoly;var Result: Tpoly);
//多项式相乘
function Calc(X: Real): Real;
//多项式求值
end;
implementation
constructor Tpoly.Create;
begin
data.num:=0;
end;
procedure Tpoly.Attach(p: Integer;
e: Integer);
begin
inc(data.num);
data.p[data.num]:=p;
data.e[data.num]:=e;
end;
function Tpoly.Empty: Boolean;
begin
Result:=(data.num<1);
end;
procedure Tpoly.Add(B: TPoly;var Result: Tpoly );
//Result=self+B
var
i,j,k:Integer;
begin
i:=1;
j:=1;
while (i<=data.num) and (j<=B.data.num)do
if data.e[i]<B.data.e[j] then
begin
inc(Result.data.num);
Result.data.p[Result.data.num]:=data.p[i];
Result.data.e[Result.data.num]:=data.e[i];
inc(i);
end
else
if data.e[i]=B.data.e[j] then
begin
if (data.p[i]+B.data.p[j])<>0 then
begin
inc(Result.data.num);
Result.data.p[Result.data.num]:=data.p[i]+B.data.p[j];
Result.data.e[Result.data.num]:=data.e[i];
end;
inc(i);
inc(j);
end
else
begin
inc(Result.data.num);
Result.data.p[Result.data.num]:=B.data.p[j];
Result.data.e[Result.data.num]:=B.data.e[j];
inc(j);
end;
if j<=B.data.num then
for k:=j to B.data.numdo
begin
inc(Result.data.num);
Result.data.p[Result.data.num]:=B.data.p[k];
Result.data.e[Result.data.num]:=B.data.e[k];
end;
if i<=data.num then
for k:=i to data.numdo
begin
inc(Result.data.num);
Result.data.p[Result.data.num]:=data.p[k];
Result.data.e[Result.data.num]:=data.e[k];
end;
end;
procedure Tpoly.Sub(B: Tpoly;var Result: Tpoly);
//Result=self-B
var
A:Tpoly;
i:Integer;
begin
A:=Tpoly.Create;
for i:=1 to B.data.numdo
//A:=-B
begin
A.data.p[i]:=-B.data.p[i];
A.data.e[i]:=B.data.e[i];
A.data.num:=B.data.num;
end;
Add(A,Result);
//Result=self-B=self+A
end;
procedure Tpoly.Mult(B: Tpoly;var Result: Tpoly);
//Result=self*B
var
i,j,t:Integer;
A:array[1..10] of Tpoly;
begin
Result.Create;
if data.num<B.data.num then
//用短的多项式乘长的
begin
for i:=1 to data.numdo
begin
A[i]:=Tpoly.Create;
for j:=1 to B.data.numdo
begin
A[i].data.p[j]:=data.p[i]*B.data.p[j];
A[i].data.e[j]:=data.e[i]+B.data.e[j];
end;
A[i].data.num:=B.data.num;
end;
t:=data.num;
end
else
begin
for i:=1 to B.data.numdo
begin
A[i]:=Tpoly.Create;
for j:=1 to data.numdo
begin
A[i].data.p[j]:=B.data.p[i]*data.p[j];
A[i].data.e[j]:=B.data.e[i]+data.e[j];
end;
A[i].data.num:=data.num;
end;
t:=B.data.num;
end;
for i:=1 to tdo
//问题出在这里,但是我不知道怎么解决
Result.Add(A[i],Result);
end;
function Tpoly.Calc(X: Real): Real;
var
i:Integer;
begin
Result:=0;
for i:=1 to data.numdo
Result:=Result+data.p[i]*Exp(data.e[i]*Ln(X));
end;
procedure Tpoly.clear;
begin
data.num:=0;
end;
end.