首页 文章

C到Delphi指针转换[关闭]

提问于
浏览
-2

我正在尝试将开源C项目转换为Delphi(柏林10.1) . 它使用一些指针,我找不到将它们转换为Delphi指针的方法 . 如何将这段代码从C翻译成Delphi?这是代码:

int SolveAll(int DragFunction, double DragCoefficient, double Vi, 
      double SightHeight, 
      double ShootingAngle, double ZAngle, double WindSpeed, double WindAngle, 
      double** Solution)
{    
    double* ptr;

    ptr = (double*)malloc(10*__BCOMP_MAXRANGE__*sizeof(double)+2048);

    double t=0;
    double dt=0.5/Vi;
    double v=0;
    double vx=0, vx1=0, vy=0, vy1=0;
    double dv=0, dvx=0, dvy=0;
    double x=0, y=0;

    double headwind=HeadWind(WindSpeed, WindAngle);
    double crosswind=CrossWind(WindSpeed, WindAngle);

    double Gy=GRAVITY*cos(DegtoRad((ShootingAngle + ZAngle)));
    double Gx=GRAVITY*sin(DegtoRad((ShootingAngle + ZAngle)));

    vx=Vi*cos(DegtoRad(ZAngle));
    vy=Vi*sin(DegtoRad(ZAngle));

    y=-SightHeight/12;

    int n=0;
    for (t=0;;t=t+dt){

        vx1=vx, vy1=vy; 
        v=pow(pow(vx,2)+pow(vy,2),0.5);
        dt=0.5/v;


        dv = retard(DragFunction,DragCoefficient,v+headwind);       
        dvx = -(vx/v)*dv;
        dvy = -(vy/v)*dv;


        vx=vx + dt*dvx + dt*Gx;
        vy=vy + dt*dvy + dt*Gy;



        if (x/3>=n){
            ptr[10*n+0]=x/3;                            
            ptr[10*n+1]=y*12;                           
            ptr[10*n+2]=-RadtoMOA(atan(y/x));           
            ptr[10*n+3]=t+dt;                           
            ptr[10*n+4]=Windage(crosswind,Vi,x,t+dt);   
            ptr[10*n+5]=RadtoMOA(atan(ptr[10*n+4]));    
            ptr[10*n+6]=v;                              
            ptr[10*n+7]=vx;                         
            ptr[10*n+8]=vy;                     
            ptr[10*n+9]=0;                              
            n++;    
        }   

        // Compute position based on average velocity.
        x=x+dt*(vx+vx1)/2;
        y=y+dt*(vy+vy1)/2;

        if (fabs(vy)>fabs(3*vx)) break;
        if (n>=__BCOMP_MAXRANGE__+1) break;
    }

    ptr[10*__BCOMP_MAXRANGE__+1]=(double)n;

    *Solution = ptr;

    return n;
}

2 回答

  • 0
    type
       TDoubleArray  = array[Word] of Double;
       PDoubleArray  = ^TDoubleArray;
    
    
    ptr: PDoubleArray;
    
    GetMem(ptr, 10*sizeof(double)+324);
    
    
    ptr[10*n+1] := y*12;   
    
    ptr[1] := n;
    

    完整代码添加后编辑:

    argument definition:  
        ... var Solution: PDoubleArray)
    in this case usage:   
        Solution := ptr;
    
  • 0
    type
     DoubleArray = Array[Word] of Double;
     PDoubleArray = ^DoubleArray;
    
    const
     __BCOMP_MAXRANGE__ = 5; // for Example
    
    var
     Ptr : ^DoubleArray;
     Solution : ^PDoubleArray;
     Y : Double;
     N : Integer;
    begin
     Ptr := AllocMem(10*SizeOf(Double)+324); // Initialize with Zero
     // OR
     GetMem(Ptr, 10*SizeOf(Double)+324); // Without Initializing
     ...
     Ptr^[10*N+1] := Y*12;
     ...
     Ptr^[10*__BCOMP_MAXRANGE__ + 1] := N;
     ...
     Solution := @Ptr;
    
     ShowMessage(FloatToStr(Solution^^[1])); // Example for using 'Solution'
     ...
    
     // Free Memory using FreeMem(Ptr) at the End
    end;
    

相关问题