#include<stdio.h>
#include<stdlib.h>
#include<string.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
typedef struct telType
{
char name[20];
char number[100];
struct telType * next;
}TEL;
//全局变量
void menu();
TEL *save(TEL *head);
TEL *load();
void find();
TEL *increase();
void print(TEL *head);
void find(TEL *head,char name[20]);
void dele(TEL *head,char name[20]);
void mod(TEL *head,char name[20],char number[100]);
void insert(TEL *head);
int main(int argc, char *argv[])
{
int key;
TEL *h;//定义所需变量
char name1[20];
char number1[100];
TEL *head=NULL;
while(1)
{
system("cls");
menu();
printf("请输入数字键功能");
scanf("%d",&key);
getchar();
switch(key)
{
case 1:
h=increase();//新增一条电话记录
system("cls");
break;
case 2:
scanf("%s",name1);
find(h,name1);
//按姓名查找电话记录
break;
case 3:
scanf("%s",name1);//删除一条电话簿记录
dele(h,name1);
break;
case 4://修改记录
scanf("%s %s",name1,number1);
mod(h,name1,number1);
break;
case 5://输出所有电话簿记录
print(h);
break;
case 6:
head=h;
if(head==NULL)
printf("空记录\n");
else
save(head);
break;
case 7:
head=load();
break;
case 8:
insert(h);
break;
case 0:
printf("欢迎下次使用!\n");
return 0;
default :printf("数字功能错误,请重新输入! \n");
}
system("pause");
}
return 0;
}
void menu()
{
printf("\t\t欢迎进入迷你电话簿管理系统\n");
printf("***************************MENU*****************************\n");
printf("*** 1.添加一条电话簿记录***\n");//ok
printf("*** 2.按姓名查找电话号码***\n");//ok
printf("*** 3.删除一条电话簿记录***\n");//ok
printf("*** 4.修改一条电话簿记录***\n");
printf("*** 5.显示所有电话簿记录***\n");//ok
printf("*** 6.保存到文件***\n");//ok
printf("*** 7.打开文件***\n");//ok
printf("*** 8.添加联系人***\n");
printf("*** 0.退出");//ok
}
TEL *save(TEL *head)//保存数据到文件
{
FILE *fp;
TEL *p;
char outfile[20];
printf("Enter outfile name,for example e:\\data\n");
scanf("%s",outfile);
if((fp=fopen(outfile,"wb"))==NULL)
{
printf("Can't open the file\n'");
return head;
}
printf("\nSaving the file\n");
p=head;
while(p!=NULL)
{
fwrite(p,sizeof(TEL),1,fp);
p=p->next;
}
fclose(fp);
printf("Save the file successfully!\n");
return head;
}
TEL *load()//从文件中读取数据,以链表形式储存
{
TEL *p1,*p2,*head=NULL;
FILE *fp;
char infile[11];
printf("Enter infile name,for example e:\\data\n");
scanf("%s",infile);
if((fp=fopen(infile,"rb"))==NULL)
{
printf("Can not open the file");
return (head);
}
printf("\nLoading the file!\n");
p1=(TEL*)malloc(sizeof(TEL));
if(!p1)
{
printf("Out of memory");
return head;
}
head=p1;
while(!feof(fp))
{
if(fread(p1,sizeof(TEL),1,fp)!=1)
break;
p1->next=(TEL*)malloc(sizeof(TEL));
if(p1->next)
{
printf("\nOut of memory\n");
return head;
}
p2=p1;
p1=p1->next;
}
p2->next=NULL;
fclose(fp);
printf(" You have success to read data from the file");
return head;
}
TEL *increase()//添加函数
{
TEL *head,*p,*q;
char name2[20];
char number1[100];
int b;
head=(TEL*)malloc(sizeof(TEL));
p=head;
while(1)
{
printf("输入联系人姓名 电话号码");
scanf("%s %s",name2,number1);
b=strlen(number1);
if(b!=11)
break;
q=(TEL*)malloc(sizeof(TEL));
strcpy(q->name,name2);
strcpy(q->number,number1);
p->next=q;
p=q;
}
p->next=NULL;
return head;
}
void print(TEL *head)//遍历
{
TEL *p;
p=head->next;
if(p==NULL)
{
printf("无联系人");
}
else
{
printf("联系人有\n");
while(p!=NULL)
{
printf("%s %s\n",p->name,p->number);
p=p->next;
}
}
return;
}
void find(TEL *head,char name[20])
{
TEL *p;
p=head->next;
if(p==NULL)
printf("无联系人");
else
{
while(p!=NULL)
{
if(strcmp(p->name,name)==0)
{
printf(" 此联系人为 %s %s",p->name,p->number);
break;
}
else
p=p->next;
}
if(p==NULL)
printf("无此联系人");
}
}
void dele(TEL *head,char name[20])//删除
{
TEL *q,*p;
q=head;
p=head->next;
while(p!=NULL)
{
if(strcmp(p->name,name)==0)
{
q->next=p->next;
free(p);
break;
}
else
q=p;
p=p->next;
}
if(p==NULL)
printf("无此联系人");
return;
}
void mod(TEL *head,char name[20],char number[100])//修改
{
TEL *p,*q;
char name1[20];
scanf("%s",name1);//修改名字
q=head;
p=head->next;
if(p==NULL)
printf("还没创建");
while(p!=NULL)
{
if(strcmp(p->name,name)==0)
{
strcpy(p->name,name1);
strcpy(p->number,number);
printf("修改完成");
break;
}
else
q=p;
p=p->next;
}
if(p==NULL)
printf("无该联系人,无法修改");
return;
}
void insert(TEL *head)
{
TEL *p,*q,*r;
char name[20],number[100];
r=(TEL *)malloc(sizeof(TEL));
scanf("%s%s",name,number);
strcpy(r->name,name);
strcpy(r->number,number);
q=head;
p=head->next;
while(p!=NULL)
{
if(strcmp(r->name,p->name)>0)
{
q->next=r;
r->next=p;
break;
}
else
{
q=p;
p=p->next;
}
}
q->next=r;
r->next=p;
return;
}