gpt4 book ai didi

c++ - C++ 到 Delphi 的转换(简单)

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:57:52 27 4
gpt4 key购买 nike

我在 C++ 中有一个函数,我试图在 delphi 中复制它:

typedef double  ANNcoord;        // coordinate data type
typedef ANNcoord* ANNpoint; // a point
typedef ANNpoint* ANNpointArray; // an array of points

bool readPt(istream &in, ANNpoint p) // read point (false on EOF)
{
for (int i = 0; i < dim; i++) {
if(!(in >> p[i])) return false;
}
return true;
}

在 Delphi 中,我相信我已经正确地声明了数据类型..(我可能是错的):

type
IPtr = ^IStream; // pointer to Istream
ANNcoord = Double;
ANNpoint = ^ANNcoord;

function readPt(inpt: IPtr; p: ANNpoint): boolean;
var
i: integer;
begin

for i := 0 to dim do
begin

end;

end;

但我不知道如何模仿 C++ 函数中的行为(可能是因为我不了解位移运算符)。

此外,我需要最终弄清楚如何将一组点从 Zeos TZQuery 对象传输到相同的数据类型 - 所以如果有人对此有任何意见,我将非常感激。

最佳答案

尝试:

type
ANNcoord = Double;
ANNpoint = ^ANNcoord;

function readPt(inStr: TStream; p: ANNpoint): boolean;
var
Size: Integer; // number of bytes to read
begin
Size := SizeOf(ANNcoord) * dim;
Result := inStr.Read(p^, Size) = Size;
end;

无需单独阅读每个 ANNcoord。请注意,istream 是 C++ 中的流类,而不是 IStream 接口(interface)。 Delphi 的等价物是 TStream。代码假定流已打开以供读取(使用适当的参数创建-d)并且当前流指针指向一个数字(dim)的 ANNcoords,就像 C++ 代码一样。

FWIW in >> p[i] 从输入流 in 中读取一个 ANNcoordp[i],将 p 解释为指向 ANNcoords 数组的指针。

更新

正如 Rob Kennedy 所指出的,in >> myDouble 从输入流中读取一个 double,但该流被解释为 text 流,不是二进制的,即它看起来像:

1.345 3.56845 2.452345
3.234 5.141 3.512
7.81234 2.4123 514.1234

etc...

据我所知,在 Delphi 中没有针对流的等效方法或操作。只有 System.ReadSystem.Readln 用于此目的。显然 Peter Below 曾经写过一个 unit StreamIO,这使得使用 System.ReadSystem.Readln 成为可能溪流。我只能找到 one version , 在新闻组帖子中。

为可以从文本表示中读取 double 、整数、单数等的流编写一个包装器可能是有意义的。我还没有看到。

关于c++ - C++ 到 Delphi 的转换(简单),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7086417/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com