#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#include <windows.h>
typedef struct node
{
char name[20]; /*姓名*/
char address[40]; /*住址*/
char phone[15]; /*电话*/
struct node *next;
}add_list;
struct person
{
char name[20];
char address[40];
char phone[15];
};
add_list *tail,*head; /*定义链表尾结点指针和头指针*/
FILE *fp; /*文件指针*/
char filename[] =”address.txt";
char name[20];
char phone[15];
char c;
int pass() /*密码登录模块*/
{
char password[] = "jike0802"; /*通讯录密码*/
char inputPassword[100] = ""; /*用来存储输入的密码*/
printf("请输入系统登录密码:");
scanf("%s",inputPassword);
if ( !strcmp(password,inputPassword) )
return 1;
printf("密码错误!你有三次尝试机会,已尝试过一次\n请输入系统登录密码:");
scanf("%s",inputPassword);
if ( !strcmp(password,inputPassword) )
return 1;
printf("密码错误!你有三次尝试机会,已尝试过两次\n请输入系统登录密码:");
scanf("%s",inputPassword);
if ( !strcmp(password,inputPassword) )
return 1;
printf("密码错误!自动退出系统\n");
getchar();
return 0;
}
add_list *load(char filename[]) /*从文件载入通讯录*/
{
add_list *new,*head;
struct person t;
head=(add_list *)malloc(sizeof(add_list));
tail=head=NULL;
if((fp=fopen(filename,"rb"))==NULL)
return head;
else
if(!feof(fp))
if(fread(&t,sizeof(struct person),1,fp)==1)
{
new=(add_list *)malloc(sizeof(add_list)); /*连入链表第一个结点*/
strcpy(new->name,t.name);
strcpy(new->address,t.address);
strcpy(new->phone,t.phone);
head=tail=new;
new->next=NULL;
/*连入链表其余结点*/
while(!feof(fp))
{
if(fread(&t,sizeof(struct person),1,fp)==1)
{
new=(add_list *)malloc(sizeof(add_list));
strcpy(new->name,t.name);
strcpy(new->address,t.address);
strcpy(new->phone,t.phone);
tail->next=new;
new->next=NULL;
tail=new;
}
}
}
fclose(fp);
return head;
}
int delete(add_list **head)
{
add_list *p,*q,*t;
char name[20],c;
int flag=0;
printf("请输入要删除人的姓名:\n");
getchar();
gets(name);
q=p=*head;
while(p!=NULL)
{
if(strcmp(name,p->name)==0)
{
printf("姓名:%s\t住址:%s\n",p->name,p->address);
printf("电话:%s\n",p->phone);
printf("真的要删除吗?\n删除请输入(Y)取消请输入(N)-->");
c=getchar();
getchar();
if(c=='y'||c=='Y')
{
if(p==*head)
*head=p->next;
else
q->next=p->next;
t=p;
p=p->next;
free(t);
flag=1;
printf("已删除确认项!按回车键继续!\n");
}
else
{
q=p;
p=p->next;
}
}
else
{
q=p;
p=p->next;
}
}
return flag;
}
int search(add_list *head)
{
add_list *p;
p = head;
printf("查看所有输入1,根据姓名查询输入2,根据号码查询输入3-->");
getchar();
c = getchar();
getchar();
switch (c)
{
case '1':
while(p!=NULL)
{
printf("************************通讯成员*********************************\n");
printf("姓名:%-20s\t住址:%-40s\n",p->name,p->address);
printf("电话:%-15s\n",p->phone);
printf("*****************************************************************\n");
p=p->next;
}
printf("按回车键继续!");
getchar();
return 1;
break;
case '2':
printf("输入要查找人的姓名:");
gets(name);
while(p!=NULL)
{
if(strcmp(name,p->name)==0)
{
printf("姓名:%s\t住址:%s\n",p->name,p->address);
printf("电话:%s\n",p->phone);
printf("按回车键继续!");
getchar();
return 1;
}
p=p->next;
}
getchar();
return 0;
break;
case '3':
printf("输入要查找人的号码:");
gets(phone);
while(p!=NULL)
{
if(strcmp(phone,p->phone)==0)
{
printf("姓名:%s\t住址:%s\n",p->name,p->address);
printf("电话:%s\n",p->phone);
printf("按回车键继续!");
getchar();
return 1;
}
p=p->next;
}
getchar();
return 0;
break;
default:
return 0;
}
return 0;
}
void insert(add_list **head)
{
add_list *new;
new=(add_list *)malloc(sizeof(add_list));
printf("请输入姓名:"); getchar();gets(new->name);
printf("请输入住址:"); gets(new->address);
printf("请输入电话:"); scanf("%s",new->phone);
if(*head==NULL)
{
*head=new;
new->next=NULL;
tail=new;
}
else
{
tail->next=new;
new->next=NULL;
tail=new;
}
getchar();
}
void add()
{
c = 'y';
while ( c=='y' || c=='Y')
{
insert(&head);
printf("\n是否继续?\n继续添加输入(Y)停止添加输入(N)-->");
c = getchar();
getchar();
}
}
void save(add_list *head,char filename[])
{
add_list *p;
struct person t;
if((fp=fopen(filename,"wb"))==NULL)
{
printf("error:cannot open file %s\n",filename);
getchar();
}
else
{
p=head;
while(p!=NULL)
{
strcpy(t.name,p->name);
strcpy(t.address,p->address);
strcpy(t.phone,p->phone);
fwrite(&t,sizeof(struct person),1,fp);
p=p->next;
}
}
fclose(fp);
}
void view()
{
printf("=====================================================\n");
printf("=== 欢迎使用pp通讯录 ===\n");
printf("=== 制作 : 计科0802班 - 第十编程小组 ===\n");
printf("=====================================================\n");
}
int theUI() /*界面模块*/
{
int t;
while(1)
{
view();
printf("=== 1 增加通讯成员 ===\n");
printf("=== 2 删除通讯成员 ===\n");
printf("=== 3 查询通讯成员 ===\n");
printf("=== 4 插入通讯成员 ===\n");
printf("=== 5 保存通讯文本 ===\n");
printf("=== 0 退出本次操作 ===\n");
printf("=====================================================\n");
printf("请输入您选择的操作,输入选项编号然后按回车键:");
c=getchar();
switch(c)
{
case '1':
add(&head);
printf("请尽快保存!按回车键继续!");
getchar();
system("cls");
break;
case '2':
t=delete(&head);
if(!t)
{
printf("查无此人按回车键继续!");
getchar();
}
system("cls");
break;
case '3':
t=search(head);
if(!t)
{
printf("查询失败按回车键继续!");
getchar();
}
system("cls");
break;
case '4':
insert(&head);
printf("请尽快保存!按回车键继续!");
getchar();
system("cls");
break;
case '5':
save(head,filename);
printf("保存成功!按回车键继续!");
getchar();
getchar();
system("cls");
break;
case '0':
printf("推出前是否保存?保存请输入(Y),不保存输入其它值-->");
c = getchar();
c = getchar();
if ( c=='Y' || c=='y')
{
save(head,filename);
printf("保存成功!按回车键继续!");
getchar();
}
exit(0);
default:
printf("输入指令无效!按回车键继续!");
getchar();
system("cls");
}
}
}
void main()
{
view();
if ( pass() )
{
system("cls");
getchar();
head=load(filename);
theUI();
}
else
exit(0);
}