通讯录项目我是用单链表写的,这个项目整体比较简单,除了一些基本的增、删、改、查等功能外,由于后面学到了文件IO和标准IO方面的知识
我又添加了保存信息和下载信息两个功能通过fopen函数创建文件,fwrite将信息写入文件,fread从文件中读取信息输出打印
头文件代码:
#ifndef _STRUCT_H_
#define _STRUCT_H_
#define ERROR 1000001
#define MALLOC_ERROR 1000002
#define OK 1000003
typedef char ElemType;
struct address
{
int id;
ElemType name[10];
ElemType homeadd[20];
ElemType telephone[20];
ElemType homephone[20];
struct address *next;
};
typedef struct address Add;
typedef Add *add;
int Add_contact(add L);
void List_contact(add L);
add Find_contact(add L,ElemType *a);
int Delete_contact(add L,ElemType *b);
void Change_contact(add L,ElemType *c);
void Save_contact(add L);
void Load_contact(add L);
#endif
主函数文件代码:
#include <stdio.h>
#include <stdlib.h>
#include "Struct.h"
#include <string.h>
int main()
{
char choice[10] = {0};
char name[10] = {0};
show();
add user = (add)malloc(sizeof(Add));
if(user == NULL)
{
return ERROR;
}
user->next = NULL; //设置空链表
while(1)
{ PrintInfo();
scanf("%s",choice);
switch(atoi(&choice[0]))
{
case 1:
printf("\n");
printf("Please input friends infomation:\n");
if(Add_contact(user) != OK)
{
return ERROR;
}
printf("\n Friends infomation add finish!\n");
break;
case 2:
List_contact(user);
break;
case 3:
printf("Please input the name you want to find:\n ");
scanf("%s",name);
add P = Find_contact(user,name);
if (NULL == P)
{
printf("Sorry,there is no this friend!\n");
}
else
{
printf("id:%d name:%s homeadd:%s telephone:%s homephone:%s",P->id,P->name,P->homeadd,P->telephone,P->homephone);
}
break;
case 4:
printf("Please input the name you want to delete:\n");
scanf("%s",name);
if( Delete_contact(user,name) != OK)
{
printf("DELETE ERROR!\n");
return ERROR;
}
printf("DELETE SUCCESS!\n");
List_contact(user);
break;
case 5:
printf("Please input the whose information you want to change:\n");
scanf("%s",name);
Change_contact(user,name);
break;
case 6:
Save_contact(user);
printf("Save Success!\n");
break;
case 7:
Load_contact(user);
printf("Load Success!\n");
break;
case 8:
exit(0);
default:
printf("unkown input!\n");
break;
}
}
return 0;
}
接口函数文件代码:
#include <stdio.h>
#include <stdlib.h>
#include "Struct.h"
#include <string.h>
void show()
{
system("clear");
printf("**************************************************\n\n");
printf("********WELCOME TO ADDRESS LIST SYSTEM************\n\n");
printf("**************************************************\n\n");
sleep(2);
system("clear");
}
void PrintInfo()
{
printf("****************************************************************\n\n");
printf("****1.Add user infomation 2.List friends infomation******\n");
printf("****3.Find friends infomation 4.Delete friends infomation****\n");
printf("****5.Change friends infomation 6.Save the information*********\n");
printf("****7.Load the information 8.exit*************************\n");
printf("****************************************************************\n\n");
printf("Please input your choice :\n");
}
int Add_contact(add L)
{
add p1 = L->next;
add temp = L;
if(L == NULL)
{
return ERROR;
}
add p = (add)malloc(sizeof(Add));
if (p == NULL)
{
return MALLOC_ERROR;
}
printf("Please input id :\n");
scanf("%d",&p->id);
printf("Please input name:\n");
scanf("%s",p->name);
while(p1)
{
if(strcmp(p->name,p1->name) > 0)
{
temp = p1;
p1 = p1->next;
}
else
{
break;
}
}
p->next = p1;
temp->next = p;
printf("Please input homeadd:\n");
scanf("%s",p->homeadd);
printf("Please input telephone:\n");
scanf("%s",p->telephone);
while(strlen(p->telephone) != 11)
{
printf("Please input 11 numbers:\n");
scanf("%s",p->telephone);
}
printf("Please input homephone:\n");
scanf("%s",p->homephone);
while(strlen(p->homephone) != 8)
{
printf("Please input 8 numbers:\n");
scanf("%s",p->homephone);
}
return OK;
}
void List_contact(add L)
{
if(L == NULL)
{
printf("List ERROR!\n");
return ;
}
add p = L->next;
while(p)
{
printf("id:%d name:%s homeadd:%s telephone:%s homephone:%s\n",p->id,p->name,p->homeadd,p->telephone,p->homephone);
p = p->next;
}
}
add Find_contact(add L,ElemType *a)
{
if (NULL == L)
{
printf("Find ERROR!\n");
return NULL ;
}
add p = L->next;
while(p)
{
if(strcmp(p->name,a) == 0)
{
return p;
}
p = p->next;
}
return NULL;
}
int Delete_contact(add L, ElemType *b)
{
add temp;
if (NULL == L)
{
return ERROR;
}
add p = L;
while(p->next)
{
if (strcmp(p->next->name,b) == 0)
{
temp = p->next;
p->next = temp->next;
free(temp);
return OK;
}
p = p->next;
}
return ERROR;
}
void Change_contact(add L,ElemType *c)
{
if(L == NULL)
{
printf("CHANGE ERROR");
return ;
}
add p = L->next;
while(p)
{
if (strcmp(p->name,c) == 0)
{
printf("id : ");
scanf("%d",&p->id);
printf("name : ");
scanf("%s",p->name);
printf("homeadd : ");
scanf("%s",p->homeadd);
printf("telephone : ");
scanf("%s",p->telephone);
printf("homephone : ");
scanf("%s",p->homephone);
}
p = p->next;
}
}
void Save_contact(add L)
{
FILE *fp;
fp = fopen("contact.txt","w");
if (fp == NULL)
{
perror("fopen");
exit(1);
}
add p = L->next;
while(p)
{
fwrite(p,1,sizeof(Add),fp);
p = p->next;
}
getchar();
fclose(fp);
}
void Load_contact(add L)
{
add p = L;
L->next = NULL;
add temp = L;
FILE *fp;
fp = fopen("contact.txt","r");
if (fp == NULL)
{
perror("fopen");
exit(1);
}
while(feof(fp) == 0)
{
p = (add)malloc(sizeof(Add));
if (p == NULL)
{
printf("Malloc Failure!\n");
return ;
}
if (fread(p,1,sizeof(Add),fp) == 0)
{
break;
}
else
{
p->next = NULL;
temp->next = p;
temp = p;
}
}
getchar();
fclose(fp);
}