У меня проблема с функцией на двоичном дереве. Дерево создает клиентские структуры, которые, среди прочего, имеют идентификационный номер и поле даты. Мне нужно сделать 3 функции, 2 функции find_client, один поиск с использованием номера идентификатора узла и один с использованием даты, они оба возвращают адрес в новое дерево, содержащее все соответствующие узлы в порядке. Выше этих двух функций функция 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. Кроме того, эти 2 функции призваны:
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;
}
Просто прочтите это руководство на указатели на функции сегодня: [Функция Pointer Учебник] (http://www.beningo.com/index.php/ пО-технологии/139-ан-введение-к-функции-pointers.html). Это может помочь. –