2013-06-03 39 views
1

我在二叉樹上有一個函數的問題。樹中有客戶端結構,其中包含一個ID號和一個日期字段。我需要做3個函數,2個find_client函數,一個使用節點的id號進行搜索,一個使用日期,他們都會將地址返回到包含所有匹配節點的新樹。在這兩個函數上面有一個find_client函數來決定根據用戶輸入調用哪個函數,我試圖使用函數指針來工作,但是我遇到了一個問題。首先,該結構:如何使用函數指針?

typedef struct date{ 
int day; 
int month; 
int year; 
}date; 

typedef struct time{ 
    int hours; 
    int minutes; 
}time; 

typedef struct client{ 
    char *first_name; 
    char *sur_name; 
    unsigned int id; 
    unsigned long reg_num; 
    date rent_date; 
    time rent_time; 
    int price_for_day; 
}client; 

typedef struct client_tree { 
    struct client c; 
    struct client_tree *left, *right; 
} clientTree; 
typedef union clientData{ 
    unsigned int id; 
    date date; 
}clientData; 

現在的功能我用:

clientTree* findClient(clientTree* t){ 
    clientTree* n=NULL; 
    int i=0; 
    clientData u; 
    while(i!=1 && i!=2){ 
     printf("\nPlease enter 1 to search via I.D. number, or press 2 to search by date: "); 
     scanf("%d", &i); 
     __fpurge(stdin); 
     if(i==1){ 
      printf("\nEnter the id number please: "); 
      scanf("%u", &u.id); 
      __fpurge(stdin); 
     } 
     else if (i==2){ 
      printf("\nEnter the day please: "); 
      scanf("%d", &u.date.day); 
      __fpurge(stdin); 
      printf("\nEnter the month please: "); 
      scanf("%d", &u.date.month); 
      __fpurge(stdin); 
      printf("\nEnter the year please: "); 
      scanf("%d", &u.date.year); 
      __fpurge(stdin); 
     } 
     else 
      printf("\nNot valid, try again."); 
    } 
    clientTree* (*pt2Function)(clientTree*, clientData) = NULL; 
    pt2Function=GetPtr2(i); 
    n= (*pt2Function)(t, u); 
    return n; 
} 


clientTree* findClientDate(clientTree* t, clientData u){ 
    if(!t) 
     return NULL; 

    if(t->c.rent_date.day==u.date.day && t->c.rent_date.month==u.date.month && t->c.rent_date.year==u.date.year){ 
     clientTree* n = createClientTree(); 
     treeAddClient(n,t->c); 
     n->left=findClientDate(t->left, u); 
     n->right=findClientDate(t->right, u); 
     return n; 
    } 
    return NULL; 
} 
clientTree* findClientId(clientTree* t, clientData u){ 
    if(!t) 
     return NULL; 

    if(t->c.id==u.id){ 
     clientTree *n = createClientTree(); 
     treeAddClient(n,t->c); 
     n->left=findClientId(t->left, u); 
     n->right=findClientId(t->right, u); 
     return n; 
    } 
    return NULL; 
} 


clientTree*(*GetPtr2(int opCode))(clientTree*, clientData){ 
if(opCode == 1) 
    return &findClientId; 
else 
    return &findClientDate; 
} 

我得到一個錯誤:「衝突的類型‘GetPtr2’」 我不是很方便用函數指針,有什麼建議?

P.S.同時這兩種功能要求:

clientTree* treeAddClient(clientTree* root, client c){ 
    if (!root) { 
     root=createClientTree(); 
     root->c=c; 
     return root; 
    } 
    if (c.id > root->c.id) 
     root->right = treeAddClient(root->right, c); 
    else if (c.id < root->c.id) 
     root->left = treeAddClient(root->left, c); 
    else 
     return NULL; 
    return root; 
} 

clientTree* createClientTree(){ 
    clientTree *t; 
    t=ALLOC(clientTree, 1); 
    return t; 
} 
+0

剛讀函數指針今天這個教程:函數指針教程(http://www.beningo.com/index.php/軟件的技術/ 139-一個引入到功能pointers.html)。它可能有幫助。 –

回答

1
clientTree* (*pt2Function)(clientTree*, clientData) = NULL; 

在這裏,您intialized pt2Function爲NULL。並且ptrFunction是一個函數指針,它可以指向一個函數,該函數可以接收2個參數,類型爲clientTree*, clientData,返回類型爲clientTree*

所以,在你的榜樣,你可以像使用 -

pt2Function = findClientDate; 

現在,你可以通過pt2Function像調用函數findClientDate -

(*findClientDate)(t,u); 

所以,在你的例子,你應該改變的簽名功能clientTree*(*GetPtr2(int opCode))(clientTree*, clientData)。它應該是 -

clientTree* GetPtr2(int opCode); 

現在你可以聲明一個函數指針像 -

clientTree* (*fPtr)(int opCode) = NULL; 
fPtr = GetPtr2; 
1

你GetPtr2的聲明看起來是正確的......但如果是它第一次被宣佈?這可能是編譯器報告的衝突的來源。

此外,可以考慮使用一個typedef使事情變得更簡單:

typedef clientTree *(* MyFuncPtr)(clientTree *, clientData); 

MyFuncPtr GetPtr2(int opCode);