我正在Pascal编写一个多项式数据类型的赋值。我遇到的最大问题是当我尝试使用过程setCoeff(var p:poly; n:integer; coeff:real)时,它返回一个新的多边形对象,其系数仅针对索引n或x ^ n。如何保持其他值,并在程序中设置新系数?我已经尝试了许多解决方法,但这些是我使用的两种方法:
procedure setCoeff(var p : poly; n : integer; coeff : real);
{ prints error and halts if n < 0 or n > MAX_DEGREE; setting the coefficient of a term to zero where n >= 0 and n <= MAX_DEGREE will result in that term becoming or remaining non-existent }
var i : integer;
Begin
if n < 0 then
Begin
writeln('Error - illegal coefficient; program halted');
Halt(0);
End;
if n > MAX_DEGREE then
Begin
writeln('Error - illegal coefficient; program halted');
Halt(0);
End;
p[n] := coeff;
End;
End;
- 和 -
procedure setCoeff(var p : poly; n : integer; coeff : real);
{ prints error and halts if n < 0 or n > MAX_DEGREE; setting the coefficient of a term to zero where n >= 0 and n <= MAX_DEGREE will result in that term becoming or remaining non-existent }
var i : integer;
Begin
if n < 0 then
Begin
writeln('Error - illegal coefficient; program halted');
Halt(0);
End;
if n > MAX_DEGREE then
Begin
writeln('Error - illegal coefficient; program halted');
Halt(0);
End;
p[n] := coeff;
for i := MAX_DEGREE downto 0 do
Begin
getCoeff(p,i,coeff);
if coeff <> 0.0 then
p[i] := coeff;
End;
End;
我知道数组是通过引用传递的,但是每次运行setCoeff(...)时它似乎都会重置数组的值。这是我数据类型的最重要部分,包括如何声明:
unit
polynomial;
interface
uses
Math;
const
MAX_DEGREE = 1000;
type
poly = Array[0..MAX_DEGREE] of Real;
var
coeff: Real;
degree : Integer;