/*****************************************************
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>
#define N 30
#define MAL_OK 1
#define MAL_ERR 0
typedef struct content
{
char name[N];
char phone[N];
char address[N];
struct content *next;
}Con,*Link;
void display()
{
printf("****************************\n");
printf("*** ***\n");
printf(" ** WELCOME TO CONTENT ! ** \n");
printf("*** ***\n");
printf("****************************\n");
printf("****************************\n");
printf("*** 1.添加用户信息 ***\n");
printf("*** 2.显示用户信息 ***\n");
printf("*** 3.查询用户信息 ***\n");
printf("*** 4.删除用户 ***\n");
printf("*** 5.修改用户信息 ***\n");
printf("*** 6.退出系统 ***\n");
printf("****************************\n");
printf("******* Please input your choice ********\n");
}
void create_link(Link *head)
{
*head = (Link)malloc(sizeof(Con));
(*head)->next = NULL;
}
int judge_con(Link con)
{
if(con == NULL)
{
return MAL_ERR;
}
else
{
return MAL_OK;
}
}
void insert_con(Link *head,Link *con)
{
Link p = *head;
do
{
*con=(Link)malloc(sizeof(Con));
}while(judge_con(*con) == MAL_ERR);
printf("please input the name:\n");
scanf("%s",&(*con)->name);
printf("please input the phone:\n");
scanf("%s",&(*con)->phone);
while(strlen((*con)->phone) != 11)
{
printf("Error!Please input the phone again!\n");
scanf("%s",&(*con)->phone);
}
printf("please input the address:\n");
scanf("%s",&(*con)->address);
if(p->next == NULL)
{
(*head)->next = *con;
(*con)->next = NULL;
}
else
{
while(p->next != NULL)
{
p = p->next;
}
p->next = *con;
(*con)->next = NULL;
}
}
void list_con(Link head)
{
if(NULL == head->next)
{
printf("The Content is empty!\n");
}
else
{
Link p = head->next;
while(p)
{
printf("Name is :%s\nPhone is :%s\nAddress is :%s\n",p->name,p->phone,p->address);
p = p->next;
}
}
}
void search_con(Link head)
{
Link p = head->next;
char Name[N];
printf("Please input the Name you want to find:\n");
scanf("%s",Name);
while(p)
{
if(strcmp(p->name,Name) == 0)
{
printf("Name is :%s\nPhone is :%s\nAddress is :%s\n",p->name,p->phone,p->address);
break;
}
else
{
p = p->next;
}
}
if(p == NULL)
{
printf("No such person!\n");
}
}
void delete_con(Link head)
{
Link p = head->next;
Link q = head;
char Name[N];
printf("Please input the Name you want to delete:\n");
scanf("%s",Name);
if(p == NULL)
{
printf("The Content is empty!");
}
else
{
while(p)
{
if(strcmp(p->name,Name) == 0)
{
q->next = p->next;
free(p);
printf("Delete success!\n");
break;
}
else
{
q = p;
p = q->next;
if(p == NULL)
{
printf("No such person you want to delete!\n");
break;
}
}
}
}
}
void change_Name(Link q,char Name[N])
{
Link p = q->next;
char new_Name[N];
while(p)
{
if(strcmp(p->name,Name) == 0)
{
printf("The original Name is :%s\n",p->name);
printf("Input the new Name :");
scanf("%s",new_Name);
strcpy(p->name,new_Name);
printf("Change Success(%s)!",p->name);
break;
}
else
{
p = p->next;
}
}
if(p == NULL)
{
printf("No such person whose Name you want to change!");
}
}
void change_Phone(Link q,char Name[N])
{
Link p = q->next;
char new_Phone[N];
while(p)
{
if(strcmp(p->name,Name) == 0)
{
printf("The original Phone is :%s\n",p->phone);
printf("Input the new Phone :");
scanf("%s",new_Phone);
strcpy(p->phone,new_Phone);
printf("Change Success(%s)!",p->phone);
break;
}
else
{
p = p->next;
}
}
if(p == NULL)
{
printf("No such person whose Phone you want to change!");
}
}
void change_Address(Link q,char Name[N])
{
Link p = q->next;
char new_Address[N];
while(p)
{
if(strcmp(p->name,Name) == 0)
{
printf("The original Address is :%s\n",p->address);
printf("Input the new Address :");
scanf("%s",new_Address);
strcpy(p->address,new_Address);
printf("Change Success(%s)!",p->address);
break;
}
else
{
p = p->next;
}
}
if(p == NULL)
{
printf("No such person whose Address you want to change!");
}
}
void change_con(Link *head)
{
Link p = (*head)->next;
Link q = *head;
char Name[N];int choice;
if(p == NULL)
{
printf("The Content is empty!\n");
}
else
{
printf("Please input the Name whose information you want to change\n");
scanf("%s",Name);
printf("0.Exit Change\n1.Change the Name\n2.Change the Phone\nChange the Address\n");
while(1)
{
scanf("%d",&choice);
switch(choice)
{
case 1:change_Name(q,Name);
printf("***************\n");
break;
case 2:change_Phone(q,Name);
printf("***************\n");
break;
case 3:change_Address(q,Name);
printf("***************\n");
break;
case 0:
exit(0);
break;
default:
printf("Unknow Input!\n");
break;
}
}
}
}
int main()
{
Link head = NULL;
Link con = NULL;
int choice;
create_link(&head);
while(1)
{
display();
scanf("%d",&choice);
switch(choice)
{
case 1:
insert_con(&head,&con);
printf("*******************\n");
break;
case 2:
list_con(head);
printf("*******************\n");
break;
case 3:
search_con(head);
printf("*******************\n");
break;
case 4:
delete_con(head);
printf("*******************\n");
break;
case 5:
change_con(&head);
printf("*******************\n");
break;
case 6:
exit(0);
break;
default:
printf("Unknow Iput!\n");
break;
}
}
return 0;
}
链表实现通讯录1
最新推荐文章于 2023-07-09 07:41:11 发布