欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C++结构体与Delphi结构体相互传参,结构体中包含结构体的嵌套,数组指针

程序员文章站 2022-03-10 12:45:54
//结构体的声明 typedef struct Mwinddirectbaseline { char* p; int s; int i; }Mwinddirectbaseline; typedef struct F { char* p; int s; int i; }F; typedef struc ......
//结构体的声明


typedef struct mwinddirectbaseline {
    char* p;
    int s;
    int i;
}mwinddirectbaseline;


typedef struct f {
    char* p;
    int s;
    int i;
}f;

typedef struct h {
    char* p;
    int s;
    int i;
}h;



typedef struct coordinate1 {
    char* p;
    int s;
    int i;
}coordinate1;

typedef struct mwinddirectbar {
    f f;
    h h;
}mwinddirectbar;


typedef struct huxianarray {
    float* params;
}huxianarray;

typedef struct hengxianarray {
    float* params;
}hengxianarray;


typedef struct windspeedline {
    char* types;
    coordinate1** coordinates;
}windspeedline;


typedef struct resultwindspeedgrid {
    int huxianshu;
    int hengxianshu;
    huxianarray* huxianarrays;
    hengxianarray* hengxianarrays;
}resultwindspeedgrid;


typedef struct coordinate {
    mwinddirectbaseline *mwinddirectbaseline;
    mwinddirectbar *mwinddirectbars;
}coordinate;


typedef struct winddirectline {
    coordinate* coordinates;
}winddirectline;

//typedef struct structtest {
//    int a;
//    int b;
//    int* arr;
//}structtest;


typedef struct rootobject {
    char* gridstarttime;
    char* gridendtime;
    char* resultstarttime;
    char* resultendtime;
    int gridmethod;
    char* winddirectborder;
    winddirectline winddirectline;
    char* windspeedborder;
    windspeedline windspeedline;
    /*void* linedatetimerelations;
    void* ratioconverts;*/
    //void* 不确定类型
    char* resultwinddirectgrid;
    resultwindspeedgrid resultwindspeedgrid;


}rootobject;
__declspec(dllexport) rootobject __stdcall refrootobjectvalues(char* path)
{
    ifstream t(path);
    rootobject root;
    string str((istreambuf_iterator<char>(t)), istreambuf_iterator<char>());
    cjsonobject objson = cjsonobject(str);
    string strsss;

    objson.get("gridstarttime", strsss);
    root.gridstarttime = returnchararr(strsss);

    objson.get("gridendtime", strsss);
    root.gridendtime = returnchararr(strsss);

    objson.get("resultstarttime", strsss);
    root.resultstarttime = returnchararr(strsss);

    objson.get("resultendtime", strsss);
    root.resultendtime = returnchararr(strsss);

    objson.get("windspeedborder", strsss);
    root.windspeedborder = returnchararr(strsss);

    objson.get("winddirectborder", strsss);
    root.winddirectborder = returnchararr(strsss);
    

    objson.get("resultwinddirectgrid", strsss);
    root.resultwinddirectgrid = returnchararr(strsss);


    int nums = objson["winddirectline"]["coordinates"].getarraysize();

    root.winddirectline.coordinates = (struct coordinate *)malloc(nums * sizeof(struct coordinate));
    /*coordinate* coor=new coordinate [nums];*/
    /*由于school_t中定义的student是一个结构体指针,必须要对其进行分配内存*/
    //root.winddirectline->coordinates = (coordinates *)malloc(sizeof(student_t));

    for (int i = 0; i < nums; i++)
    {
        int mwinddirectbaseline_length = objson["winddirectline"]["coordinates"][i]["mwinddirectbaseline"].getarraysize();
        root.winddirectline.coordinates[i].mwinddirectbaseline = (struct mwinddirectbaseline *)malloc(nums * sizeof(struct mwinddirectbaseline));
        for (int j = 0; j < mwinddirectbaseline_length; j++)
        {
            string str1;
            objson["winddirectline"]["coordinates"][i]["mwinddirectbaseline"][j].get("p", str1);
            root.winddirectline.coordinates[i].mwinddirectbaseline[j].p = returnchararr(str1);
            objson["winddirectline"]["coordinates"][i]["mwinddirectbaseline"][j].get("i", root.winddirectline.coordinates[i].mwinddirectbaseline[j].i);
            objson["winddirectline"]["coordinates"][i]["mwinddirectbaseline"][j].get("s", root.winddirectline.coordinates[i].mwinddirectbaseline[j].s);
            root.winddirectline.coordinates[i].mwinddirectbaseline++;
        }

        int mwinddirectbars_length = objson["winddirectline"]["coordinates"][i]["mwinddirectbars"].getarraysize();
        root.winddirectline.coordinates[i].mwinddirectbars = (struct mwinddirectbar *)malloc(nums * sizeof(struct mwinddirectbar));
        for (int k = 0; k < mwinddirectbars_length; k++)
        {
            string str2;
            f f;
            h h;
            objson["winddirectline"]["coordinates"][i]["mwinddirectbars"][k]["f"].get("p", str2);
            f.p = returnchararr(str2);
            objson["winddirectline"]["coordinates"][i]["mwinddirectbars"][k]["f"].get("s", f.s);
            objson["winddirectline"]["coordinates"][i]["mwinddirectbars"][k]["f"].get("i", f.i);
            str2 = "";
            objson["winddirectline"]["coordinates"][i]["mwinddirectbars"][k]["h"].get("p", str2);
            h.p = returnchararr(str2);
            objson["winddirectline"]["coordinates"][i]["mwinddirectbars"][k]["h"].get("s", h.s);
            objson["winddirectline"]["coordinates"][i]["mwinddirectbars"][k]["h"].get("i", h.i);
            root.winddirectline.coordinates[i].mwinddirectbars[k].f = f;
            root.winddirectline.coordinates[i].mwinddirectbars[k].h = h;
            root.winddirectline.coordinates[i].mwinddirectbars++;
        }
        root.winddirectline.coordinates++;
    }

    return root;
    //strs->winddirectline.coordinates = coor;
    //strs->winddirectline->coordinates = coor;
    //strs->gridmethod = 50;

}
 type
    huxianarray = record
      params:^ double;
    end;

  type
    hengxianarray  = record
      params:^ double;
    end;

   type
    resultwindspeedgrid = record
       huxianshu:integer;
       huxianarrays:^ huxianarray;
       hengxianshu:integer;
       hengxianarrays:^ hengxianarray;
    end;

   type
    coordinate1 = record
       p:pansichar;
       s:integer;
       i:integer;
    end;

   type
    windspeedline =  record
         types:pansichar;
         coordinates:^ coordinate1;
    end;

    type
    h  = record
        p:pansichar;
        s:integer;
        i:integer;
    end;

   type
    f =  record
        p:pansichar;
        s:integer;
        i:integer;
    end;


   type
    mwinddirectbar = record
        f:f;
        h:h;
    end;
    tmyarraybar = array of mwinddirectbar;
    pmyarraybar = ^tmyarraybar;

    type
    mwinddirectbaseline  = record
       p:pansichar;
       s:integer;
       i:integer;
    end;


    type
    coordinate = record
//    tcoordinate = record
         mwinddirectbaseline: ^mwinddirectbaseline;
         mwinddirectbars: ^mwinddirectbar;
    end;



   type
    winddirectline  = record
      coordinates: ^coordinate;
    end;

     type
    rootobject = record
         gridstarttime:pansichar;
         gridendtime:pansichar;
         resultstarttime:pansichar;
         resultendtime:pansichar;
         gridmethod:integer;
         winddirectborder:pansichar;
         winddirectline:winddirectline;
         windspeedborder:pansichar;
         windspeedline:windspeedline;
//         linedatetimerelations:pointer;
//         ratioconverts:pointer;
         resultwinddirectgrid:pansichar;
         resultwindspeedgrid:resultwindspeedgrid;
    end;
function retrootobjectvalues(b:pansichar;var n1:pansichar;var mu:integer):rootobject;stdcall;external 'f:\资料文档\20190227\测试文件夹\win32dll\debug\win32dll.dll';

procedure tform5.button4click(sender: tobject);
var
root:rootobject;
path,res:pansichar;
mu,i,hsf,hsh:integer;
nums:mwinddirectbaseline;
resd:coordinate;
mwind: mwinddirectbaseline;
mwbar:mwinddirectbar;
bol,bolbar:boolean;
fs:f;
hs:h;
begin
     root.gridmethod:=0;
     path:=  pansichar(ansistring('e:\123.txt'));
     root:= retrootobjectvalues(path,res,mu);
//     resd:= root.winddirectline.coordinates^  ;
//     mwind:=  root.winddirectline.coordinates^.mwinddirectbaseline^;
//     inc(root.winddirectline.coordinates);
         bol:=true;

            while(bol) do
            begin
               resd:= root.winddirectline.coordinates^  ;
               try
                 mwind:= root.winddirectline.coordinates^.mwinddirectbaseline^;
                 mwbar:= root.winddirectline.coordinates^.mwinddirectbars^;
                 fs:=mwbar.f;
                 hs:=mwbar.h;
                 hsf:= fs.s;
                 hsh:=hs.s;

                 bolbar:=true;
                 mu:= mwind.s;

                 while((mu<>-1414812757)) do
                 begin
                  inc(root.winddirectline.coordinates^.mwinddirectbaseline); {到下一个}
                  mwind:=  root.winddirectline.coordinates^.mwinddirectbaseline^;
                  mu:= mwind.s;
                 end;

                 while((hsf<>-1414812757) and (hsh<>-1414812757) ) do
                 begin
                   inc(root.winddirectline.coordinates^.mwinddirectbars); {到下一个}

                      mwbar:= root.winddirectline.coordinates^.mwinddirectbars^;
                      fs:=mwbar.f;
                      hs:=mwbar.h;
                      hsf:= fs.s;
                      hsh:=hs.s;

                 end;
               except
               begin
                  bol:=false;
               end;
               end;
               inc(root.winddirectline.coordinates); {到下一个}
            end;


//     resd^:=root.winddirectline.coordinates;
//     mwind[0]:=  resd.mwinddirectbaseline;
//     mwbar:=   resd.mwinddirectbars;
//      for i := 0 to 3 do
//      begin
//       resd:= root.winddirectline.coordinates;
//
//      end;


     showmessage(root.gridstarttime);


end;

 其中json文件的读取运用了c++ json解析cjsonobject的详细使用

代码中只展示了部分参数的赋值