typedef struct PersonInfo
{
int num;
char name[30];
char ID[30];
}PerInfo;
typedef struct _PerInfoNode
{
PerInfo data;
struct _PerInfoNode *next;
}PerInfoNode;
void initDataFile(char *filePath)
{
FILE *pf;
PerInfo s[4] = { 1, "liudehua", "0123456789",
2, "zhangxueyou", "0123456789",
3, "liming", "0123456789",
4, "guofucheng", "0123456789"
};
pf = fopen(filePath,"w");
fwrite((void*)s,sizeof(s),1,pf);
fclose(pf); return ;
}
PerInfoNode *createListFromFile(char *filePath)
{
FILE *pf;
PerInfoNode *head;
PerInfoNode *cur;
pf = fopen(filePath,"r");
head = (PerInfoNode *)malloc(sizeof(PerInfoNode));
head->next = NULL;
cur = (PerInfoNode *)malloc(sizeof(PerInfoNode));
while(fread((void*)&cur->data,sizeof(PerInfo),sizeof(PerInfo)/sizeof(PerInfo),pf))
{
cur->next = head->next;
head->next = cur;
cur = (PerInfoNode *)malloc(sizeof(PerInfoNode));
}
free(cur);
return head;
}
void traversePerInfoList(PerInfoNode * head)
{
head = head->next;
while(head)
{
head = head->next;
}
}
void addListPerInfo(PerInfoNode *head)
{
PerInfoNode * cur = (PerInfoNode *)malloc(sizeof(PerInfoNode));
printf("num :");
scanf("%d",&cur->data.num);
printf("name :");
scanf("%s",cur->data.name);
printf("ID :");
scanf("%s",&cur->data.ID);
cur->next = head->next;
head->next = cur;
}
PerInfoNode * searchListPerInfo(PerInfoNode *head)
{
char name[30];
printf("pls input your search name:");
scanf("%s",name);
head = head->next;
while(head)
{
if(strcmp(head->data.name,name) == 0)
break;
head = head->next;
}
return head;
}
void deleteListNodePerInfo(PerInfoNode *head)
{
PerInfoNode *pfind = searchListPerInfo(head);
if(pfind == NULL)
{
printf("您要删除的人不存在\n");
return ;
}
while(head->next != pfind)
head = head->next;
head->next = pfind->next;
free(pfind);
return ;
}
int lenListPerInfo(PerInfoNode *head)
{
int len =0;
head = head->next;
while(head)
{
len++;
head = head->next;
}
return len;
}
void sortListPerInfo(PerInfoNode *head)
{
PerInfoNode *prep,*p,*q;
int len,i,j;
len = lenListPerInfo(head);
for(i=0; i<len-1; i++)
{
prep = head;
p = prep->next;
q = p->next;
for(j=0; j<len-1-i; j++)
{
if(p->data.num > q->data.num)
{
prep->next = q;
p->next = q->next;
q->next = p;
prep = q;
q = p->next;
continue;
}
prep = prep->next;
p = p->next;
q = p->next;
}
}
}
void saveListPerInfoFile(PerInfoNode *head,char *filePath)
{
FILE* pf = fopen(filePath,"w");
head = head->next;
while(head)
{
fwrite((void*)&head->data,sizeof(PerInfo),1,pf);
head = head->next;
}
fclose(pf);
}
void destroyListPerInfo(PerInfoNode *head)
{
PerInfoNode *t;
while(head)
{
t = head;
head = head->next;
free(t);
}
}
int IDCardManagement(void)
{
int choice;
uint8_t result;
PerInfoNode *pfind;
PerInfoNode *head;
result = finit("S0:\\PerInfo.data");
if(result != NULL)
{
printf("挂载文件系统失败 (%s)\r\n");
}
else
{
printf("挂载文件系统成功 (%s)\r\n");
}
initDataFile("S0:\\PerInfo.data");
head =createListFromFile("S0:\\PerInfo.data");
while(1)
{
traversePerInfoList(head);
printf("1->add\t 2->search 3->delete 4->sort 5->exit\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
addListPerInfo(head);
break;
case 2:
if(pfind = searchListPerInfo(head))
{
printf("You find data is in database\n");
printf("\t%d\t\t%s\t\t%s\n", pfind->data.num, pfind->data.name,pfind->data.ID);
}
else
printf("查无此人\n");
break;
case 3:
deleteListNodePerInfo(head);
break;
case 4:
sortListPerInfo(head);
break;
case 5:
saveListPerInfoFile(head,"S0:\\PerInfo.data");
destroyListPerInfo(head);
return 0;
default:
printf("您输错了\n");
}
}
return 0;
}