/*****************************************************
copyright (C), 2014-2015, Lighting Studio. Co., Ltd.
File name:
Author:Jerey_Jobs Version:0.1 Date:
Description:
Funcion List:
*****************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct txl Node; //将结构体换一个名字为Node
typedef struct txl * Link; //定义结构体指针
struct txl
{
char name[10];
int year;
char sex; //定义结构体内部的值域
long telephone;
long QQnumber;
char address[100];
Link next;
};
//构造界面
void welcome()
{
printf("********************************************************************\n");
printf("********************************************************************\n");
printf("********************************************************************\n");
printf("********************* Welcome! *****************************\n");
printf("*** 1.Incret the information 2.Print the informatioan ***\n");
printf("*** 3.Delet the information 4.Modify the information ***\n");
printf("*** 5.Find the information 6.Release the informatioan ***\n");
printf("------Welcome you to use the address book----\n");
printf("------Please choose the function you want to------\n");
}
//给裢表的节点分配空间
void creat_node(Link *node)
{
*node = (Link)malloc(sizeof(Node));
if(*node == NULL)
{
printf("malloc error!\n"); //判断malloc函数是否成功分配空间
exit(-1);
}
}
//创建链表的表头
void creat_link(Link *head)
{
creat_node(head);
(*head)->next = NULL;
}
//将节点插入链表,边插边边排序
void incret_node(Link head,Link new_node)
{
Link p,q;
p = q = head->next;
if(head->next == NULL)
{
new_node->next = head->next;
head->next = new_node;
}
else
{
while(p != NULL && strcmp(p->name,new_node->name) < 0)
{
q = p;
p = p->next;
}
q->next = new_node; //排序的依据是根据名字的顺序
new_node->next = p;
}
}
//检查电话和QQ号码的长度是否合格
int check_tel_QQ(Link new_node)
{
int k = 0,t = 0;
long i,j;
int flag = 0;
i = new_node->telephone;
j = new_node->QQnumber;
while(i)
{
i = i / 10; //输入的电话号码的长度
t++;
}
while(j)
{
j = j / 10; //QQ的长度
k++;
}
if(t == 11 || k <= 10)
{
return flag = 1; //作为判断是否合格的依据
}
else
{
printf("The telephone is rong or QQnumber is rong!\n");
return flag = 0;
}
}
//删除所需要删除的信息
void delet_node(Link head,char n[10])
{
Link p,q;
q = p = head->next;
if(strcmp(head->next->name,n) == 0)
{
head->next = p->next;
}
else
{
while(p != NULL && strcmp(p->name,n) !=0) //根据提供的名字的信息进 行查找所要删除的信息的位置, 当然还有通过电话,QQ等我就不 一一写了
{
q = p;
p = p->next;
}
if(p == NULL)
{
printf("No found!\n");
}
else
{
q->next = p->next;
free(p);
}
}
}
//修改信息
void quity_node(Link head,char qn[],char y)
{
Link p;
p = head->next;
while(p != NULL && strcmp(p->name,qn) != 0) //这边通过需要修改的人的名 字进行查找
{
p = p->next;
}
if(p == NULL)
{
printf("No found!\n");
}
else
{
while(y != 'n' && y != 'N')
{
printf("Do you want to quity the name:(y/n)");
scanf(" %c",&y); //这边再次确认是否要修改
if(y == 'y' || y == 'Y')
{
scanf(" %s",&p->name);
}
printf("Do you want to quity the sex:(y/n)");
scanf(" %c",&y); //这边询问是否要修改性别
if(y == 'Y' || y == 'y')
{
scanf(" %c",&p->sex);
}
printf("Do you want to quity the telephone:(y/n)");
scanf(" %c",&y); //这边询问是否要修改电话号码
if(y == 'Y' || y == 'y')
{
scanf(" %ld",&p->telephone);
}
printf("Do you want to quity the QQnumber:(y/n)");
scanf(" %c",&y); //这边询问是否要修改QQ
if(y == 'Y' || y == 'y') //判断选择
{
scanf(" %ld",&p->QQnumber); //修改信息
}
printf("Do you want to quity the address:(y/n)");
scanf(" %c",&y); //这边询问是否要修改地址
if(y == 'y' || y == 'Y')
{
scanf(" %s",&p->address[100]);
}
printf("Do you want to contuine:(y/n)"); //询问是否要继续
scanf(" %c",&y);
}
}
}
//查找信息
void find_node(Link head,char nm[])
{
Link p,q;
char y;
long m,n;
q = p = head->next;
while(p != NULL && strcmp(p->name,nm) != 0) //方法一通过名字
{
p = p->next;
}
if(p == NULL)
{
printf("No found!\n"); //没找到
printf("Do you continue wanting to find:(y/n)"); //给予机会
scanf(" %c",&y);
while(y != 'n' && y != 'N')
{
p = head->next;
printf("Please input the name again!"); //重新输入查找人姓名
scanf(" %s",nm);
while(p != NULL && strcmp(p->name,nm) != 0)
{
p = p->next;
}
if(p == NULL)
{
printf("No found!\n");
}
else //找到了就输出信息
{
printf("name=%s,year=%d,sex=%c,telephone=%ld,QQnumber=%ld, address=%s\n",p->name,p->year,p->sex,p->telephone, p->QQnumber,p->address);
}
printf("Do you continue wanting to find:(y/n)");
scanf(" %c",&y); //询问是否要继续通过姓名查找
}
printf("Do you want to change the way to find(y/n):");
scanf(" %c",&y); //询问是否要更换查找方法
if(y == 'y' || y == 'Y')
{
printf("Please input the telephone you want to find:");
scanf(" %ld",&m); // 输入要查找的电话
p = head->next;
while(p != NULL && p->telephone != m)
{
p = p->next;
}
if(p == NULL)
{
printf("No found!\n");
printf("Do you continue wanting to find:(y/n)");
scanf(" %c",&y); // 询问是否要继续查找号码
while(y != 'n' && y != 'N')
{
p = head->next;
printf("Please input the telephone again:");
scanf(" %ld",&m); //重新输入
while(p != NULL && p->telephone != m)
{
p = p->next;
}
if(p == NULL)
{
printf("No found!\n");
}
else //找到就输出信息
{
printf("name=%s,year=%d,sex=%c,telephone=%ld, QQnumber=%ld,address=%s\n",p->name,p->year,p->sex, p->telephone,p->QQnumber,p->address);
}
printf("Do you continue wanting to find:(y/n)");
scanf(" %c",&y);
}
}
else
{
printf("name=%s,year=%d,sex=%c,telephone=%ld,QQnumber=%ld, address=%s\n",p->name,p->year,p->sex,p->telephone, p->QQnumber,p->address);
}
}
printf("Do you want to change the way to find(y/n):");
scanf(" %c",&y); //是否要更换查找方式
if(y == 'y' || y == 'Y') // 通过QQ号码进行查找
{
p = head->next;
printf("Please input the QQnumber you want to find:");
scanf("%ld",&n);
while(p != NULL && p->QQnumber != n)
{
p = p->next;
}
if(p == NULL)
{
printf("No found!\n");
printf("Do you continue wanting to find:(y/n)");
scanf(" %c",&y);
while(y == 'y' && y == 'Y')
{
p = head->next;
printf("Please input the QQnumber again:");
scanf(" %ld",&n);
while(p != NULL && p->QQnumber != n)
{
p = p->next;
}
if(p == NULL)
{
printf("No found!\n");
}
else
{
printf("name=%s,year=%d,sex=%c,telephone=%ld, QQnumber=%ld,address=%s\n",p->name,p->year,p->sex, p->telephone,p->QQnumber,p->address);
}
printf("Do you continue wanting to find:(y/n)");
scanf(" %c",&y);
}
}
else
{
printf("name=%s,year=%d,sex=%c,telephone=%ld,QQnumber=%ld, address=%s\n",p->name,p->year,p->sex,p->telephone, p->QQnumber,p->address);
}
}
}
else
{
printf("name=%s,year=%d,sex=%c,telephone=%ld,QQnumber=%ld,address=%s \n",p->name,p->year,p->sex,p->telephone,p->QQnumber,p->address);
}
}
//格式化通讯录
void empty_node(Link head)
{
Link p;
p = head->next;
while(head->next != NULL)
{
head->next = p->next;
free(p);
p = head->next;
}
}
//打印通讯录
void display_node(Link head)
{
Link p;
if(head->next == NULL)
{
printf("The address book is empty!\n");
return;
}
p = head->next;
while(p != NULL)
{
printf("name=%s,year=%d,sex=%c,telephone=%ld,QQnumber=%ld,address=%s \n",p->name,p->year,p->sex,p->telephone,p->QQnumber,p->address);
p = p->next;
}
}
//功能一储存信息
void function1(char y,int flag,Link new_node,Link head)
{
printf("Are you sure continue:(y/n)");
scanf(" %c",&y);
while(y != 'n' && y != 'N')
{
printf("Please input the name,year,sex,telephone,QQnumber,address:\n ");
creat_node(&new_node);
scanf(" %s %d %c %ld %ld %s",&new_node->name,&new_node->year, &new_node->sex,&new_node->telephone,&new_node->QQnumber, &new_node->address); //输入信息
flag = check_tel_QQ(new_node); //进行进查电话,QQ是否合格
if(flag == 1)
{
incret_node(head,new_node);
}
else
{
printf("Please reinput!\n");
scanf("%ld%ld",&new_node->telephone,&new_node->QQnumber);
incret_node(head,new_node);
}
printf("Do you want to continue:(y/n)");
scanf(" %c",&y); //询问是否继续储存信息
}
}
//功能二打印出通讯录
void function2(char y,Link head)
{
printf("Are you sure continue:(y/n)");
scanf(" %c",&y);
while(y != 'N' && y != 'n')
{
display_node(head);
printf("Do you want to continue:(y/n)");
scanf(" %c",&y);
}
}
//功能三删除联系人
void function3(char y,Link head,char n[])
{
printf("Are you sure continue:(y/n)");
scanf(" %c",&y);
while(y != 'n' && y != 'N')
{
printf("Please input who you want to delet:");
scanf(" %s",n);
delet_node(head,n);
printf("Do you want to continue:(y/n)");
scanf(" %c",&y);
}
}
//功能四修改联系人信息
void function4(char y,Link head)
{
printf("Are you sure continue:(y/n)");
scanf(" %c",&y);
char qn[10];
char rn[10];
while(y != 'n' && y != 'N')
{
printf("Please input the name you want to quity:");
scanf(" %s",qn);
quity_node(head,qn,y);
printf("Do you want to continue:(y/n)");
scanf(" %c",&y);
}
}
//功能五查找联系人
void function5(char y,Link head)
{
printf("Are you sure continue:(y/n)");
scanf(" %c",&y);
char nm[10];
while(y != 'n' && y != 'N')
{
printf("Please input who you want to find:");
scanf("%s",nm);
find_node(head,nm);
printf("Do you want to continue:(y/n)");
scanf(" %c",&y);
}
}
//功能六格式化通讯录
void function6(char y,Link head)
{
printf("Are you sure continue:(y/n)");
scanf(" %c",&y);
while(y != 'n' && y != 'N')
{
empty_node(head);
display_node(head);
printf("Do you want to continue:(y/n)");
scanf(" %c",&y);
}
}
int main()
{
Link head;
Link new_node;
int num;
char y = '\0';
int flag;
char n[10];
creat_link(&head);
system("reset"); //清屏
welcome(); //界面
while(1) //循环
{
printf("Please the function you want to choose:");
scanf("%d",&num);
switch(num) //使用switch语句实现功能的选择
{
case 1:
function1(y,flag,new_node,head);
break;
case 2:
function2(y,head);
break;
case 3:
function3(y,head,n);
break;
case 4:
function4(y,head);
break;
case 5:
function5(y,head);
break;
case 6:
function6(y,head);
break;
default:
printf("Data error!\n");
break;
}
}
return 0;
}
结果示范: