300分 仅转换一个c语言函数为delphi 致谢!(300)

fanronghua

New Member
Member
#2
procedure cheby(x :do
uble;
y: array ofdo
uble;
nC,n : integer;
var r : array ofdo
uble);
var i,j,k : integer;
s,v : array[0..20-1] ofdo
uble;
x0 :do
uble;
begin
x0 := 2;
if x0 <> x then
begin
x0 := x;
s[0]:= 1.0;
s[1]:= x;
for i := 2 to 20 -1do
s := 2*x*s[i-1] - s[i-2];
v[0] := 0.0;
// T0' = 0 v[1] := 1.0;
// T1' = 1 for i:=2 to 20 -1do
v := 2*x*v[i-1] + 2*s[i-1] - v[i-2];
end;
for i:=0 to nC -1do
begin
// k := i+nC, r=r[k] = 0;;?? k:= i +nC;
if r[k] = 0.0 then
r:= 1.0 else
r:= 0.0;
for j:= 0 to n -1 do
begin
r :=r+ s[j] * y[i*n+j];
r[k] :=r[k]+ v[j] * y[i*n+j];
end;
end;
end;
 

heroicdragon

New Member
Member
#3
他在过程内使用了 static 相当于,使用了全局变量了。 所以我觉得只要把, staticdo
uble s[20],v[20];
staticdo
uble x0=2;
具体如下:var s, v: array[0..19] ofdo
uble;
x0:do
uble = 2;procedure Cheby(x:do
uble;
y: PDouble;
nc, n: Integer;
r: PDouble);var i, j, k: Integer;
begin
if x0 <> x then
begin
x0 := x;
s[0] := 1.0;
x[1] := x;
for i := 2 to 20 - 1do
s := 2 * x * s[i - 1] - s[i - 2];
v[0] := 0.0;
v[1] := 1.0;
for i := 2 to 20 - 1do
v := 2 * x * v[i - 1] + 2 * s[i - 1] - v[i - 2];
end;
for i := 0 to nc - 1do
begin
k := i + nc;
r[k] := 0;
r := r[k];
for j := 0 to n - 1do
begin
r := r + s[j] * y[i * n + j];
r[k] := r[k] + v[j] * y[i * n + j];
end;
end;
end;
 

冰力不足

New Member
Member
#4
十分感谢 fanronghua帮忙翻译! 感谢 heroicdragon 教了我一招, 我自己试了一下, 果真是这样:#include <stdio.h>#include <stdlib.h>void cheby(){ static int x0=2; x0++; printf("%d/n", x0);}int main(int argc, char *argv[]){ cheby();
// 显示结果为 3 cheby();
// 显示结果为 4 system("PAUSE"); return 0;}但我有个2个疑问还要请教:1.heroicdragon翻译成procedure Cheby(x:do
uble;
y: PDouble;
nc, n: Integer;
r: PDouble);为什么y和r不加 var?即:procedure cheby(x:do
uble;
nC, n: integer;
var y, r: PDouble);2.另外就是do
uble *r翻译为 var r : array ofdo
uble 正确些 还是 r: PDouble 正确些? 谢谢了!