#include <stdio.h>
#include <stdlib.h>
#define OK 0
#define ERROR -1
#define MALLOC_ERROR -2
int temp_ID = 1;
typedef struct node
{
int ID; // 结点的数据
char name[21];
char person_phone[21];
char office_phone[21];
char address[21];
struct node *next; // 结点指针
}t;
typedef struct node *PNode; // 重命名结点指针类型
void zhujiemian(void)
{
printf("\t##########################################################\n");
printf("\t## 通讯录 ##\n");
printf("\t## INSERT ---------------------添加好友信息 ##\n");
printf("\t## DISPLAY---------------------列表好友信息 ##\n");
printf("\t## SEARCH ---------------------搜索好友 ##\n");
printf("\t## DELETE ---------------------删除好友 ##\n");
printf("\t## ##\n");
printf("\t## 作者:陈志辉 ##\n");
printf("\t##########################################################\n");
}
void cho1(int *p1, int *p2, int *p3, int *p4) //判断输入口令
{
char a[40];
char bijiao[4][11]={"INSERT","DISPLAY","SEARCH","DELETE"};
int i;
int j = 0;
int temp=1;//检测标志位
printf("\t请输入操作命令:");
while(temp)
{
i = 0;
scanf("%s", a);//输入口令 字符长度检测
for(i=0; i<4; i++)//校验命令
{
if(a[0] == bijiao[i][0])
{
for(j=1; j<10; j++)
{
if(a[j] != bijiao[i][j])//校验1
{
// i++;
break;
}
else if(a[j] == '\0' && i == 0)
{
*p1 = 1;
break;
}
if(a[j] != bijiao[i][j])//校验2
{
// i++;
break;
}
else if(a[j] == '\0' && i == 1)
{
*p2 = 1;
break;
}
if(a[j] != bijiao[i][j])//校验3
{
break;
}
else if(a[j] == '\0' && i == 2)
{
*p3 = 1;
break;
}
if(a[j] != bijiao[i][j])//校验4
{
break;
}
else if(a[j] == '\0' && i == 3)
{
*p4 = 1;
break;
}
}
if(*p1 || *p2 || *p3 ||*p4)
{
temp=0;
break;
}
}
if (temp == 0)
{
break;
}
}
if(temp)
{
printf("\t口令错误,请重新输入!\n\n\t请输入操作命令:");
}
}
}
//INSERT————添加通讯录好友 尾插法
int creat_list_tail(PNode h)
{
if (h == NULL)
{
return ERROR;
}
PNode node = (PNode)malloc(sizeof(t)/sizeof(char));
if (node == NULL)
{
return MALLOC_ERROR;
}
printf("\t \n");
printf("\t新建联系人: \n");
char name[21];
char person_phone[21];
char office_phone[21];
char address[21];
printf("\t\t请输入姓名: ");
scanf("%s",name);
printf("\t\t请输入手机号码:");
scanf("%s",person_phone);
printf("\t\t请输入家庭住址:");
scanf("%s",address);
printf("\t\t请输入公司电话:");
scanf("%s",office_phone);
node->next = NULL;
node->ID = temp_ID++;
int i;
for(i = 0; i < 21; i++)
{
node->name[i] = name[i];
}
for(i = 0; i < 21; i++)
{
node->person_phone[i] = person_phone[i];
}
for(i = 0; i < 21; i++)
{
node->address[i] = address[i];
}
for(i = 0; i < 21; i++)
{
node->office_phone[i] = office_phone[i];
}
PNode temp = h;
while (temp->next)
{
temp = temp->next;
}
temp->next = node;
return OK;
}
void display(PNode h)
{
if (h == NULL)
{
return;
}
PNode temp = h->next; // 链表第一个结点指针
while (temp)
{
printf("\tID:");
printf("\t\t%08d\n",temp->ID);
printf("\t姓名");
printf("\t\t%s\n",temp->name);
printf("\t手机号码:");
printf("\t%s\n",temp->person_phone);
printf("\t家庭住址:");
printf("\t%s\n",temp->address);
printf("\t公司电话:");
printf("\t%s\n",temp->office_phone);
temp = temp->next;
}
}
int search(PNode h, char *p)
{
if (h == NULL)
{
return ERROR;
}
PNode temp = h->next;
// printf("%c",*p);
int i;
int flag = 1;
while(temp)
{
for(i = 0; i < 21; i++)
{
if(*(p+i) != temp->name[i])
{
break;
}
else if( (*(p+i) == '\0') && (temp->name[i] == '\0') )
{
flag = 0;
printf("\tID:");
printf("\t%08d\n",temp->ID);
printf("\t姓名");
printf("\t%s\n",temp->name);
printf("\t手机号码:");
printf("\t%s\n",temp->person_phone);
printf("\t家庭住址:");
printf("\t%s\n",temp->address);
printf("\t公司电话:");
printf("\t%s\n",temp->office_phone);
printf("\n");
break;
}
}
temp = temp->next;
}
if (flag)
{
printf("\t没有此联系人!\n");
}
return OK;
}
int del(PNode h, char *p)
{
//1搜索联系人
if (h == NULL)
{
return ERROR;
}
PNode temp = h->next;
int i;
int name_count = 0;
int a[20];
while(temp)
{
for(i = 0; i < 21; i++)
{
if(*(p+i) != temp->name[i])
{
break;
}
else if( (*(p+i) == '\0') && (temp->name[i] == '\0') )
{
a[name_count] = temp->ID;
name_count++;
break;
}
}
temp = temp->next;
}
//2删除联系人
PNode front = NULL;
if (name_count == 0)
{
printf("\t没有此联系人!\n");
}
else
{
printf("\t查找到联系人%d个\n",name_count);
printf("\t选择联系人删除:\n");
i = 0;
while(i < name_count )
{
printf("\t\tID%d: %08d\n",i+1,a[i]);
i++;
}
int choose;
printf("\t输入要删除好友ID:");
scanf("%d",&choose);
temp = h->next;
if(temp->next == NULL)
{
free(temp);
h->next = NULL;
}
else
{
while(temp)
{
if(temp->ID == choose)
{
PNode m = front->next;
front->next = m->next;
free(m);
m=NULL;
}
front=temp;
temp = temp->next;
}
}
}
return OK;
}
int main()
{
//链表初始化,建立空链表
PNode head_node = (PNode)malloc(sizeof(t)/sizeof(char));
if (head_node == NULL)
{
return ERROR;
}
head_node->next = NULL; // 空链表
//功能标志位
int INSERT=0;
int *p1 = &INSERT;
int DISPLAY=0;
int *p2 = &DISPLAY;
int SEARCH=0;
int *p3 = &SEARCH;
int DELETE=0;
int *p4 = &DELETE;
//开始装逼
printf("\t通讯录载入中...\n");
sleep(1);
system("clear");
while(1)
{
//显示主界面
zhujiemian();
//功能选择
while(!(INSERT || DISPLAY || SEARCH || DELETE))
{
//判断输入口令
cho1(p1, p2, p3, p4);
}
sleep(1);
//功能选择INSERT————添加通讯录好友
while(INSERT)
{
system("clear");
printf("\tINSERT模式 ---------------------添加好友信息\n");
if(creat_list_tail(head_node) != OK)
{
printf("\t添加好友失败!\n");
return ERROR;
}
else
{
printf("\n\t添加好友成功!\n");
}
printf("\t返回主菜单...\n\n");
sleep(1);
INSERT = 0;
// back_zhujiemian(p1);
}
while(DISPLAY)//显示通讯录好友
{
system("clear");
printf("\t进入DISPLAY模式---------------------列表好友信息\n\n");
display(head_node);
printf("\n\t返回主菜单...\n\n");
sleep(1);
DISPLAY = 0;
}
while(SEARCH)//搜索通讯录好友
{
system("clear");
printf("\t进入SEARCH模式 ---------------------搜索好友\n\n");
printf("\t请输入联系人姓名:");
char name[21];
scanf("%s",name);
if(search(head_node,name) != OK)
{
return ERROR;
// printf("#1");
}
printf("\n\t返回主菜单...\n\n");
sleep(1);
SEARCH = 0;
}
while(DELETE)//删除通讯录好友
{
system("clear");
printf("\t进入DELETE模式 ---------------------删除好友\n\n");
printf("\t请输入联系人姓名:");
char name[21];
scanf("%s",name);
if(del(head_node,name) != OK)
{
return ERROR;
}
printf("\n\t返回主菜单...\n\n");
sleep(1);
DELETE = 0;
}
}
return 0;
}
#include <stdlib.h>
#define OK 0
#define ERROR -1
#define MALLOC_ERROR -2
int temp_ID = 1;
typedef struct node
{
int ID; // 结点的数据
char name[21];
char person_phone[21];
char office_phone[21];
char address[21];
struct node *next; // 结点指针
}t;
typedef struct node *PNode; // 重命名结点指针类型
void zhujiemian(void)
{
printf("\t##########################################################\n");
printf("\t## 通讯录 ##\n");
printf("\t## INSERT ---------------------添加好友信息 ##\n");
printf("\t## DISPLAY---------------------列表好友信息 ##\n");
printf("\t## SEARCH ---------------------搜索好友 ##\n");
printf("\t## DELETE ---------------------删除好友 ##\n");
printf("\t## ##\n");
printf("\t## 作者:陈志辉 ##\n");
printf("\t##########################################################\n");
}
void cho1(int *p1, int *p2, int *p3, int *p4) //判断输入口令
{
char a[40];
char bijiao[4][11]={"INSERT","DISPLAY","SEARCH","DELETE"};
int i;
int j = 0;
int temp=1;//检测标志位
printf("\t请输入操作命令:");
while(temp)
{
i = 0;
scanf("%s", a);//输入口令 字符长度检测
for(i=0; i<4; i++)//校验命令
{
if(a[0] == bijiao[i][0])
{
for(j=1; j<10; j++)
{
if(a[j] != bijiao[i][j])//校验1
{
// i++;
break;
}
else if(a[j] == '\0' && i == 0)
{
*p1 = 1;
break;
}
if(a[j] != bijiao[i][j])//校验2
{
// i++;
break;
}
else if(a[j] == '\0' && i == 1)
{
*p2 = 1;
break;
}
if(a[j] != bijiao[i][j])//校验3
{
break;
}
else if(a[j] == '\0' && i == 2)
{
*p3 = 1;
break;
}
if(a[j] != bijiao[i][j])//校验4
{
break;
}
else if(a[j] == '\0' && i == 3)
{
*p4 = 1;
break;
}
}
if(*p1 || *p2 || *p3 ||*p4)
{
temp=0;
break;
}
}
if (temp == 0)
{
break;
}
}
if(temp)
{
printf("\t口令错误,请重新输入!\n\n\t请输入操作命令:");
}
}
}
//INSERT————添加通讯录好友 尾插法
int creat_list_tail(PNode h)
{
if (h == NULL)
{
return ERROR;
}
PNode node = (PNode)malloc(sizeof(t)/sizeof(char));
if (node == NULL)
{
return MALLOC_ERROR;
}
printf("\t \n");
printf("\t新建联系人: \n");
char name[21];
char person_phone[21];
char office_phone[21];
char address[21];
printf("\t\t请输入姓名: ");
scanf("%s",name);
printf("\t\t请输入手机号码:");
scanf("%s",person_phone);
printf("\t\t请输入家庭住址:");
scanf("%s",address);
printf("\t\t请输入公司电话:");
scanf("%s",office_phone);
node->next = NULL;
node->ID = temp_ID++;
int i;
for(i = 0; i < 21; i++)
{
node->name[i] = name[i];
}
for(i = 0; i < 21; i++)
{
node->person_phone[i] = person_phone[i];
}
for(i = 0; i < 21; i++)
{
node->address[i] = address[i];
}
for(i = 0; i < 21; i++)
{
node->office_phone[i] = office_phone[i];
}
PNode temp = h;
while (temp->next)
{
temp = temp->next;
}
temp->next = node;
return OK;
}
void display(PNode h)
{
if (h == NULL)
{
return;
}
PNode temp = h->next; // 链表第一个结点指针
while (temp)
{
printf("\tID:");
printf("\t\t%08d\n",temp->ID);
printf("\t姓名");
printf("\t\t%s\n",temp->name);
printf("\t手机号码:");
printf("\t%s\n",temp->person_phone);
printf("\t家庭住址:");
printf("\t%s\n",temp->address);
printf("\t公司电话:");
printf("\t%s\n",temp->office_phone);
temp = temp->next;
}
}
int search(PNode h, char *p)
{
if (h == NULL)
{
return ERROR;
}
PNode temp = h->next;
// printf("%c",*p);
int i;
int flag = 1;
while(temp)
{
for(i = 0; i < 21; i++)
{
if(*(p+i) != temp->name[i])
{
break;
}
else if( (*(p+i) == '\0') && (temp->name[i] == '\0') )
{
flag = 0;
printf("\tID:");
printf("\t%08d\n",temp->ID);
printf("\t姓名");
printf("\t%s\n",temp->name);
printf("\t手机号码:");
printf("\t%s\n",temp->person_phone);
printf("\t家庭住址:");
printf("\t%s\n",temp->address);
printf("\t公司电话:");
printf("\t%s\n",temp->office_phone);
printf("\n");
break;
}
}
temp = temp->next;
}
if (flag)
{
printf("\t没有此联系人!\n");
}
return OK;
}
int del(PNode h, char *p)
{
//1搜索联系人
if (h == NULL)
{
return ERROR;
}
PNode temp = h->next;
int i;
int name_count = 0;
int a[20];
while(temp)
{
for(i = 0; i < 21; i++)
{
if(*(p+i) != temp->name[i])
{
break;
}
else if( (*(p+i) == '\0') && (temp->name[i] == '\0') )
{
a[name_count] = temp->ID;
name_count++;
break;
}
}
temp = temp->next;
}
//2删除联系人
PNode front = NULL;
if (name_count == 0)
{
printf("\t没有此联系人!\n");
}
else
{
printf("\t查找到联系人%d个\n",name_count);
printf("\t选择联系人删除:\n");
i = 0;
while(i < name_count )
{
printf("\t\tID%d: %08d\n",i+1,a[i]);
i++;
}
int choose;
printf("\t输入要删除好友ID:");
scanf("%d",&choose);
temp = h->next;
if(temp->next == NULL)
{
free(temp);
h->next = NULL;
}
else
{
while(temp)
{
if(temp->ID == choose)
{
PNode m = front->next;
front->next = m->next;
free(m);
m=NULL;
}
front=temp;
temp = temp->next;
}
}
}
return OK;
}
int main()
{
//链表初始化,建立空链表
PNode head_node = (PNode)malloc(sizeof(t)/sizeof(char));
if (head_node == NULL)
{
return ERROR;
}
head_node->next = NULL; // 空链表
//功能标志位
int INSERT=0;
int *p1 = &INSERT;
int DISPLAY=0;
int *p2 = &DISPLAY;
int SEARCH=0;
int *p3 = &SEARCH;
int DELETE=0;
int *p4 = &DELETE;
//开始装逼
printf("\t通讯录载入中...\n");
sleep(1);
system("clear");
while(1)
{
//显示主界面
zhujiemian();
//功能选择
while(!(INSERT || DISPLAY || SEARCH || DELETE))
{
//判断输入口令
cho1(p1, p2, p3, p4);
}
sleep(1);
//功能选择INSERT————添加通讯录好友
while(INSERT)
{
system("clear");
printf("\tINSERT模式 ---------------------添加好友信息\n");
if(creat_list_tail(head_node) != OK)
{
printf("\t添加好友失败!\n");
return ERROR;
}
else
{
printf("\n\t添加好友成功!\n");
}
printf("\t返回主菜单...\n\n");
sleep(1);
INSERT = 0;
// back_zhujiemian(p1);
}
while(DISPLAY)//显示通讯录好友
{
system("clear");
printf("\t进入DISPLAY模式---------------------列表好友信息\n\n");
display(head_node);
printf("\n\t返回主菜单...\n\n");
sleep(1);
DISPLAY = 0;
}
while(SEARCH)//搜索通讯录好友
{
system("clear");
printf("\t进入SEARCH模式 ---------------------搜索好友\n\n");
printf("\t请输入联系人姓名:");
char name[21];
scanf("%s",name);
if(search(head_node,name) != OK)
{
return ERROR;
// printf("#1");
}
printf("\n\t返回主菜单...\n\n");
sleep(1);
SEARCH = 0;
}
while(DELETE)//删除通讯录好友
{
system("clear");
printf("\t进入DELETE模式 ---------------------删除好友\n\n");
printf("\t请输入联系人姓名:");
char name[21];
scanf("%s",name);
if(del(head_node,name) != OK)
{
return ERROR;
}
printf("\n\t返回主菜单...\n\n");
sleep(1);
DELETE = 0;
}
}
return 0;
}