从c++到Delphi的转换(简单)

Conversion from C++ to Delphi (simple)

本文关键字:简单 转换 c++ Delphi      更新时间:2023-10-16

我在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接口。Delphi的等效物是TStream。代码假设流已打开以供读取(使用适当的参数创建-d),并且当前流指针指向anncord的一个数字(dim),就像c++代码一样。

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

更新

正如Rob Kennedy指出的那样,in >> myDouble从输入流中读取双精度,但该流被解释为文本流,而不是二进制,即:

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可以用于流。我只能在一个新闻组帖子里找到一个版本。

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