#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef int (*cmp)(void *,void *);
typedef struct book* Book;
struct book
{
int id;
char name[10];
};
extern void *max(void *array[],int len,cmp func);
extern int cmp_int(void *p,void *q);
extern int cmp_struct(void *p,void *q);
extern int insert_struct(Book *pos,int id,char *name);
extern int insert_int(int **pos,int val);
————————————————————————————————————
#include "common.h"
void *max(void *array[],int len,cmp func)
{
int i;
void tmp;
tmp=array[0];
for(i=1;i<len;i++)
{
if((*func)(tmp,array[i])==-1)
tmp=array[i];
}
return tmp;
}
——————————————————————————————————————
#include "common.h"
int insert_struct(Book *pos,int id,char *name)
{
Book p;
p=(Book)malloc(sizeof(struct book));
if(p==NULL)
{
printf("fail to malloc");
return -1;
}
p->id=id;
strcpy(p->name,name);
*pos=p;
return 0;
}
int insert_int(int **pos,int val)
{
int *p;
p=(int *)malloc(sizeof(int));
*p=val;
*pos=p;
return 0;
}
————————————————————————
#include "common.h"
#define MAX 3
int main(void)
{
Book array1[MAX];
int *array[MAX];
int i; int id; int val;
char name[10];
Book res1;
int *res2;
for(i=0;i<MAX;i++)
{
printf("input info of book \n");
scanf("%d",&id);
scanf("%s",name);
if(insert_struct(array1+i,id,name)==-1)
exit(1);
printf("input int \n");
scanf("%d",&val);
if(insert_int(array+i,val)==-1)
exit(1);
}
res1=(Book)max((void **)array1,MAX,cmp_struct);
res2=(int *)max((void**)array,MAX,cmp_int);
printf("the max of books:%d,%s\n",res1->id,res1->name);
printf("the max of int:%d \n",*res2);
return 0;
}