myhead.h
#ifndef MYHEAD_H_
#define MYHEAD_H_
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define SUCCESS 1
#define FAILURE 0
struct hnode
{
char name[20];
char age[20];
char number[20];
char telephone[20];
struct hnode * next;
};
typedef struct hnode Hnode;
typedef Hnode * Hlink;
//enum reslt_val{RET_OK =1,RET_NO};
int is_malloc_ok( Hlink new_node);
int create_node( Hlink* new_node);
void create_link( Hlink * head);
int add(Hlink head);
int delete(Hlink head);
int see(Hlink head);
int find(Hlink head);
int insert_node(Hlink head);
int change_node(Hlink head);
int signout(Hlink head);
int length(Hlink head);
Hlink read_open();
void menu();
void welcome();
#endif
function.h
#include "../../include/myhead.h"
void welcome()
{
printf("*********************************************************************\n");
printf("******************* *******************\n");
printf("******************* Welocome *******************\n");
printf("******************* *******************\n");
printf("*********************************************************************\n");
printf("\n\n\n");
}
void menu()
{
printf("*************** welcome to use *******************\n");
printf("**1 添加通讯录成员 2 显示通讯录成员**\n");
printf("**3 删除通讯录成员 4 查询通讯录成员**\n");
printf("**5 插入通讯录成员 6 修改通讯录成员**\n");
printf("***************** 7 退出系统 ******************\n");
printf("*******************************************************\n");
}
int is_malloc_ok(Hlink new_node)
{
if(new_node == NULL)
{
return FAILURE;
}
else
{
return SUCCESS;
}
}
int create_node(Hlink * new_node)
{
*new_node = (Hlink)malloc((unsigned)sizeof(Hnode));
if(FAILURE == is_malloc_ok(*new_node))
{
return FAILURE;
}
else
{
return SUCCESS;
}
}
void create_link(Hlink * head)
{
Hlink new_node = NULL;
new_node = (Hlink)malloc(sizeof(Hnode));
if(NULL == new_node)
{
printf("malloc error\n");
}
new_node->next = NULL;
(*head) = new_node;
}
int add(Hlink head)
{
Hlink new_node;
while (1)
{
new_node = (Hlink)malloc(sizeof( Hnode));
if (NULL == new_node)
{
return FAILURE;
}
printf("please input the name\n");
getchar();
scanf("%s", new_node->name);
printf("please input the age\n");
getchar();
scanf("%s", new_node->age);
printf("please input the number\n");
getchar();
scanf("%s", new_node->number);
printf("please input the telephone\n");
getchar();
scanf("%s", new_node->telephone);
new_node->next = NULL;
while (head->next != NULL)
{
head = head->next;
}
head->next = new_node;
break;
}
return SUCCESS;
}
int delete(Hlink head)
{
char name[20];
Hlink temp;
printf("please input the name you want to delete\n");
getchar();
scanf("%s", name);
while (head != NULL)
{
if (strcmp(head->next->name, name) == 0)
{
temp = head->next;
head->next = head->next->next;
free(temp); return SUCCESS;
}
else
{
head = head->next;
}
}
head->next = NULL;
return FAILURE;
}
int see(Hlink head)
{
int count = 0;
//printf("1111111");
while (head->next != NULL)
{
count++;
printf("第%d位:\t", count);
printf("姓名:%s\t",head->next->name);
printf("年龄:%s\t",head->next->age);
printf("学号:%s\t",head->next->number);
printf("电话:%s\n",head->next->telephone);
head = head->next;
}
while(head == NULL)
{
return FAILURE;
}
return SUCCESS;
}
int find(Hlink head)
{
char name[20];
int count = 0;
printf("please input the name you want to find\n");
scanf("%s", name);
while (head->next != NULL)
{
if (strcmp(head->next->name, name) == 0)
{
count++;
printf("名字:%s\t", head->next->name);
printf("年龄:%s\t", head->next->age);
printf("学号:%s\t", head->next->number);
printf("电话:%s\n", head->next->telephone);
}
head = head->next;
}
if (count == 0)
{
return FAILURE;
}
return SUCCESS;
}
int insert_node(Hlink head)
{
char name[20];
Hlink new_node;
Hlink p = NULL;
Hlink q = NULL;
p = head->next;
q = head;
printf("Please input the name you want to insert front\n");
scanf("%s",name);
new_node = (Hlink)malloc(sizeof(Hnode));
if (NULL == new_node)
{
//printf("1");
return FAILURE;
}
while(p != NULL && strcmp(p->name, name) != 0)
{
q = p;
p = p->next;
}
printf("please input the name\n");
getchar();
scanf("%s", new_node->name);
printf("please input the age\n");
getchar();
scanf("%s", new_node->age);
printf("please input the number\n");
getchar();
scanf("%s", new_node->number);
printf("please input the telephone\n");
getchar();
scanf("%s", new_node->telephone);
/* if(p->next == NULL)
{
new_node->next = p->next;
p->next = new_node;
}
else*/
{
new_node->next = p;
q->next = new_node;
}
if(p == NULL)
{
//printf("2");
return FAILURE;
}
return SUCCESS;
}
int change_node(Hlink head)
{
char name[20];
Hlink new_node;
new_node = (Hlink)malloc(sizeof( Hnode));
if (NULL == new_node)
{
return FAILURE;
}
printf("please input the name you want to find\n");
scanf("%s", name);
while (head->next != NULL)
{
if (strcmp(head->next->name, name) == 0)
{
printf("please input the name\n");
getchar();
scanf("%s", new_node->name);
printf("please input the age\n");
getchar();
scanf("%s", new_node->age);
printf("please input the number\n");
getchar();
scanf("%s", new_node->number);
printf("please input the telephone\n");
getchar();
scanf("%s", new_node->telephone);
new_node->next = head->next->next;
head->next = new_node;break;
}
head = head->next;
}
if(head->next == NULL)
{
return FAILURE;
}
return SUCCESS;
}
Hlink read_open()
{
Hlink head;
Hlink p;
Hlink new_node;
FILE *fp;
char name[20];
char age[20];
char number[20];
char telephone[20];
fp=fopen("fuction.txt","r");
if (fp==NULL)
{
printf("通讯录打开失败!\n");
exit(-1);
}
head = (Hlink)malloc(sizeof(Hnode));
p = head;
while (fscanf(fp,"%s %s %s %s",name,age,number,telephone)!=EOF)
{
new_node = (Hlink)malloc(sizeof(Hnode));
strcpy(new_node->name,name);
strcpy(new_node->age,age);
strcpy(new_node->number,number);
strcpy(new_node->telephone,telephone);
while(p->next != NULL)
{
p = p->next;
}
new_node->next= p->next;
p->next=new_node;
}
fclose(fp);
return head;
}
int signout(Hlink head)
{
Hlink p;
FILE *fp;
fp=fopen("fuction.txt","w");
if (fp==NULL)
{
printf("通讯录打开失败!\n");
exit(-1);
}
p=head->next;
while (p != NULL)
{
fprintf(fp,"%s %s %s %s\n",p->name,p->age,p->number,p->telephone);
p=p->next;
}
fclose(fp);
return SUCCESS;
}
main.c
#include "../../include/myhead.h"
int main()
{
int num;
int result ;
Hlink head = NULL;
//create_link( &head);
welcome();
head = read_open();
while (1)
{
menu();
printf("Please select an action\n");
scanf("%d",&num);
switch(num)
{
case 1:
result = add(head);
if (result == SUCCESS )
{
printf("add success!\n");
}
else
{
printf("add failure!\n");
}
break;
case 2:
result = see(head);
if (result == SUCCESS )
{
printf(" success!\n");
}
else
{
printf("Address book is empty !\n");
}
break;
case 3:
result = delete(head);
if (result == SUCCESS )
{
printf("deldete success!\n");
}
else
{
printf("delete failure!\n");
}
break;
case 4:
result = find(head);
if (result == SUCCESS )
{
printf("find success!\n");
}
else
{
printf("no find!\n");
}
break;
case 5:
result = insert_node(head);
if (result == SUCCESS )
{
printf("insert success!\n");
}
else
{
printf("insert failure!\n");
}
break;
case 6:
result = change_node( head);
if (result == SUCCESS )
{
printf("insert success!\n");
}
else
{
printf("insert failure!\n");
}
break;
case 7:
result = signout(head);
if (result == SUCCESS )
{
printf("Exit successful!\n");
exit(1);
}
}
}
return 0;
}