#include
#include
#include
#include
#include
#define
TRUE
1
#define
FALSE
0
#define
MAX_ID_LEN
10
#define
MAX_NAME_LEN
30
#define
FILE_NAME
"data"
typedef int BOOL;
typedef struct _DATE{
int
year;
// 年
int
month;
// 月
int
day;
// 日
}DATE_T;
typedef struct _STUDENT{
char
id[MAX_ID_LEN];
// 学号
char
name[MAX_NAME_LEN];
// 姓名
DATE_T
birthday;
// 生日
int
height;
// 身高
struct
_STUDENT *
next;
// 链表指针
}STUDENT_T;
// 字符串转成日期,成功返回TRUE,否则返回FALSE
BOOL toDate(DATE_T * date, char * szBuf)
{
//
printf("step1\n");
int year,month,day;
if(sscanf(szBuf,"M----",&year,&month,&day)!=3)//日期分割函数
{
//
printf("step2\n");
return FALSE;
}
if(month>12||month<=0)
{
//
printf("step3\n");
return FALSE;
}
int
dayMax=30;
switch(month)
{
case 1:
dayMax=31;
break;
case 2:
dayMax=28;
break;
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
dayMax=31;
break;
}
if(((year%4==0 && year0!=0) ||
(year@0==0))&&month==2)
{
//
printf("step4\n");
dayMax=29;
}
if(day<=dayMax)
{
printf("the date is ok\n");
date->year=year;
date->month=month;
date->day=day;
return TRUE;
}
}
// 在链表中插入一个节点,保持学号从小到大的顺序
void insert(STUDENT_T ** head, STUDENT_T * data)
{
if(strcmp((*head)->id ,
data->id)>0)
{
data->next=*head;
*head=data;
}
else
{
//printf("out of
insert22\n");
STUDENT_T *p=*head;
while(p!=NULL)
{
if(p->next==NULL || strcmp(p->id ,
data->id)<0
&&
strcmp(p->next->id,data->id)>0
)
{
data->next=p->next;
p->next=data;
break;
}
else
{
p=p->next;
}
}
head=&p;
//printf("out of
insert\n");
}
}
// 保存学生信息到宏FILE_NAME定义的文件中
void save(STUDENT_T * head)
{
//printf("in save\n");
int
fd;
fd=open(FILE_NAME,O_CREAT|O_RDWR|O_TRUNC,0644);
if(fd==-1)
{
perror("open failed");
exit(EXIT_FAILURE);
}
else
{
while(head!=NULL)
{
if(write(fd,head ,sizeof(STUDENT_T)-sizeof(void *))==-1)
{
perror("write failed");
exit(1);
}
else
{
head=head->next;
}
}
close(fd);
}
}
// 从宏FILE_NAME定义的文件中读取学生信息
void load(STUDENT_T ** head)
{
STUDENT_T
*node=NULL,*node1=NULL,*node2=NULL;
int
fd;
int
flag=0;
fd=open(FILE_NAME,O_RDONLY);
if(fd)
{
//printf("open is ok\n");
node = (STUDENT_T *)malloc(sizeof(STUDENT_T));
while(read(fd,node ,sizeof(STUDENT_T)-sizeof(void
*))>0)
{
//printf("load is ok\n");
//printf("%s\n",node->id);
node->next=NULL;
if(flag==0)
{
flag=1;
node1=node;
node2=node1;
}
else
{
node1->next=node;
node1=node1->next;
}
node = (STUDENT_T *)malloc(sizeof(STUDENT_T));
}
free(node);
*head=node2;
close(fd);
}
else
{
perror("open failed");
exit(EXIT_FAILURE);
}
}
// 输出学生信息
void output(STUDENT_T * head)
{
STUDENT_T
*p=head;
printf("%-10s\t%-30s\t%-8s\t%-s\n","id","name","birthday","height");
while(head!=NULL)
{
printf("%-10s\t%-30s\tM-%d-%d\t%-d\n",head->id,head->name,head->birthday.year,head->birthday.month,head->birthday.day,head->height);
sleep(1);
head=head->next;
}
//printf("in output\n");
head=p;
}
// 释放学生信息链表
void release(STUDENT_T * head)
{
//printf("in release\n");
STUDENT_T
*temp=head;
while(head!=NULL)
{
temp=head->next;
free(head);
head=temp;
}
}
// 创建学生信息链表
STUDENT_T * create()
{
STUDENT_T
*head = NULL, *p = NULL;
char
szBuf[1024];
while(1)
{
p = (STUDENT_T *)malloc(sizeof(STUDENT_T));
if(!p)
{
printf("Memory
overflow!\n");
break;
}
printf("Please input id (0 for exit):
");
scanf("%s",
p->id);
if(strcmp(p->id,
"0") == 0)
{
break;
}
printf("Please input name:
");
scanf("%s",
p->name);
do
{
printf("Please input date (format of yyyy-mm-dd):
");
scanf("%s", szBuf);
if(toDate(&p->birthday,
szBuf))
{
break;
}
else
{
printf("Invalid date
format\n");
}
}while(1);
printf("Please input height (unit of cm):
");
scanf("%d",
&p->height);
if(!head)
{
head = p;
}
else
{
insert(&head, p);
}
}
return
head;
}
int main()
{
STUDENT_T *
head = create();
if(!head)
{
printf("Create list
failed!\n");
return -1;
}
printf("The student info created:
\n");
output(head);
save(head);
release(head);
//printf("----------ylp--------------\n");
load(&head);
printf("The student info loaded:
\n");
output(head);
release(head);
}