#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#define NULL 0
#define LEN sizeof(struct student)
void save(struct node *p);
void creat();
void print();
void del();
void insert();
struct node * read();
struct student
{
char name[10];
char telephone[10];
char address[40];
char postnum[10];
};
struct node
{
struct student *stu;
struct node *next;
};
int n;
void save(struct node *p)
{
FILE *fp;
if((fp=fopen("file.txt","w"))==NULL)
printf("open file error!");
else
while(p)
{
fwrite(p->stu,LEN,1,fp);
p=p->next;
}
fclose(fp);
}
struct node * read()
{
FILE *fp;
struct node *head,*p;
struct student *student;
head=p=(struct node *)malloc(sizeof(struct node));
student=(struct student *)malloc(LEN);
if((fp=fopen("file.txt","r"))==NULL)
return NULL;
do
{
fread(student,LEN,1,fp);
p->stu=student;
p->next=(struct node *)malloc(sizeof(struct node));
student=(struct student *)malloc(LEN);
p=p->next;
}while(!feof(fp));
p=NULL;
fclose(fp);
return head;
}
void creat()
{
int n=0;
struct node *head,*p;
struct student *s;
head=p=(struct node *)malloc(sizeof(struct node));
s=(struct student *)malloc(LEN);
printf("Input students' name,telephone,address and postcode:\n");
printf("Number %d\n",n+1);
n++;
printf("name :");
getchar();gets(s->name);
if(!strcmp(s->name,"exit"))
head=NULL;
else
{
printf("telephone :");gets(s->telephone);
printf("address :");gets(s->address);
printf("postnumber:");gets(s->postnum);
p->stu=s;
while(1)
{
p=p->next=(struct node *)malloc(sizeof(struct node));
s=(struct student *)malloc(LEN);
printf("\nNumber %d\n",n+1);
printf("name :");gets(s->name);
if(!strcmp(s->name,"exit"))
break;
printf("telephone :");gets(s->telephone);
printf("address :");gets(s->address);
printf("postnumber:");gets(s->postnum);
p->stu=s;
n++;
}
p=NULL;
}
save(head);
}
void print()
{
int i=1;
struct node *p;
p=read();
printf("\nNow,these records are:\n");
printf(" Name\t telephone address postnumber\n");
if(p!=NULL)
do
{
printf("Number %d:",i++);
printf(" %-8s%-8s %-8s%-8s\n",p->stu->name,p->stu->telephone,p->stu->address,p->stu->postnum);
p=p->next;
}while(p);
}
void del()
{
struct node *head,*p1,*p2;
char name[10];
head=read();
if(head==NULL)
printf("\nlist null!\n");
else
{
p2=p1=head;
printf("input the name you want to del:");
scanf("%s",name);
while((!strcmp(name,p1->stu->name))&&(p1->next!=NULL))
{
p2=p1;p1=p1->next;
}
if(!strcmp(name,p1->stu->name))
{
if(p1==head)head=p1->next;
else p2->next=p1->next;
printf("delete:%s\n",name);
}
else printf("can't find name:%s!\n",name);
}
save(head);
}
void insert()
{
struct node *p,*head;
struct student *s;
if((head=p=read())==NULL)
printf("error!");
while(p->next!=NULL)
p=p->next;
p=p->next=(struct node *)malloc(sizeof(struct node));
s=(struct student *)malloc(LEN);
printf("input the student's Name telephone address and postcode\n");
printf("Name:");getchar();gets(s->name);
printf("telephone:");gets(s->telephone);
printf("address:");gets(s->address);
printf("postcode:");gets(s->postnum);
p->stu=s;
p->next=NULL;
save(head);
}
void printmenu()
{
printf("Input number to choose:\n");
printf("1.显示通讯录\n");
printf("2.添加成员\n");
printf("3.删除成员\n");
printf("4.退出\n");
}
void main()
{
int choose;
char c;
do
{
if(read())
{
printmenu();
scanf("%d",&choose);
switch(choose)
{
case 1:print();break;
case 2:insert();break;
case 3:del();break;
case 4:exit(0);
}
}
else
{
printf("没有通讯录,现在添加(y/n):");
scanf("%c",&c);
if(c=='y'||c=='Y')
creat();
else exit(0);
}
}while(1);
}