/*
目的:
用〈〈数据结构〉〉中的双向链表作数据结构,结合C语言基本知识。编写一个通讯录管理系统。
功能:
1) 输入信息——enter();
2) 显示信息———display();
3) 查找以姓名作为关键字———search( );
4) 删除信息———delete();
5) 存盘———save ( );
6) 装入———load( ) ;
时间:
2017年7月20日09:50:14
版本:
1.0
环境:
gcc 7.1
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define maxNameLength 12
#define maxAddrLength 16
#define maxPhoneLength 12
#define maxCacheLength 64
///////////////////////////////////////////////////////////////////////////////////////
typedef struct person
{
char name[maxNameLength];
char address[maxAddrLength];
char phone[maxPhoneLength];
struct person *prior, *next;
} singlePerson;
typedef struct addressList
{
singlePerson *head;
int length;
} addressList;
//////////////////////////////////////////////////////////////////////////////////////
void initialization(addressList &record);
int getCommand(void);
void help(void);
void useradd(addressList &record);
void display(const addressList &record);
void showSingle(singlePerson *elem);
void userdel(addressList &record);
void search(const addressList &record);
void save(const addressList &record);
void load(addressList &record);
/////////////////////////////////////////////////////////////////////////////////////
int main(void)
{
int engine = 1;
addressList record;
initialization(record);
while (engine != -1)
{
fflush(stdin);
printf("[enter command]#");
engine = getCommand();
switch(engine)
{
case 0:
help();
break;
case 1:
useradd(record);
break;
case 2:
display(record);
break;
case 3:
search(record);
break;
case 4:
userdel(record);
break;
case 5:
save(record);
break;
case 6:
load(record);
break;
case -1:
break;
case 100:
break;
case 500:
printf("NO such command\n");
break;
default:
printf("Error\n");
}
}
return 0;
}
/////////////////////////////////////////////////////////////////////////////////////
void initialization(addressList &record)
{
record.head = NULL;
record.length = 0;
}
int getCommand(void)
{
char *cache = (char *)malloc(maxCacheLength * sizeof(char));
char temp = getchar();
int location = 0;
while (temp == ' ' || temp == '\t')
temp = getchar();
while (temp != ' '&&temp != '\t'&&temp != '\n')
{
*(cache + location++) = temp;
temp = getchar();
}
*(cache + location) = '\0';
strlwr(cache);
if (strcmp(cache, "help") == 0)
{
free(cache);
return 0;
}
else if (strcmp(cache, "useradd") == 0)//添加
{
free(cache);
return 1;
}
else if (strcmp(cache, "display") == 0)//显示
{
free(cache);
return 2;
}
else if (strcmp(cache, "search") == 0)//查找
{
free(cache);
return 3;
}
else if (strcmp(cache, "userdel") == 0)//删除
{
free(cache);
return 4;
}
else if (strcmp(cache, "save") == 0)// 保存
{
free(cache);
return 5;
}
else if (strcmp(cache, "load") == 0)// 读取
{
free(cache);
return 6;
}
else if (strcmp(cache, "exit") == 0)
{
free(cache);
return -1;
}
else if (*cache == '\0') //NULL
{
free(cache);
return 100;
}
else
{
free(cache);
return 500;
}//default
}
void useradd(addressList &record)
{
singlePerson *newNode = (singlePerson *)malloc(sizeof(singlePerson));
char temp = getchar();
int location = 0;
while (temp == ' ' || temp == '\t')
temp = getchar();
while (temp != ' '&&temp != '\t'&&temp != '\n')
{
*(newNode->name + location++) = temp;
temp = getchar();
}
*(newNode->name + location)='\0',location=0;
while (temp == ' ' || temp == '\t')
temp = getchar();
while (temp != ' '&&temp != '\t'&&temp != '\n')
{
*(newNode->address + location++) = temp;
temp = getchar();
}
*(newNode->address + location)='\0',location=0;
while (temp == ' ' || temp == '\t')
temp = getchar();
while (temp != ' '&&temp != '\t'&&temp != '\n')
{
*(newNode->phone + location++) = temp;
temp = getchar();
}
*(newNode->phone + location)='\0',location=0;
newNode->next=record.head,newNode->prior=NULL;
if(record.head!=NULL)
record.head->prior=newNode;
record.head=newNode;
record.length++;
}
void userdel(addressList &record)
{
char *cache = (char *)malloc(maxCacheLength * sizeof(char));
singlePerson *elem=record.head;
char temp = getchar();
int location = 0;
while (temp == ' ' || temp == '\t')
temp = getchar();
while (temp != ' '&&temp != '\t'&&temp != '\n')
{
*(cache + location++) = temp;
temp = getchar();
}
*(cache + location) = '\0';
strlwr(cache);
if(strcmp(cache,"all")==0)
{
if(elem==NULL)
printf("there are no contacts in the list\n");
else
{
while(elem->next!=NULL)
{
elem=elem->next;
free(elem->prior);
}
free(elem);
record.head=NULL,record.length=0;
}
}
else
{
while(elem!=NULL)
{
if(strcmp(elem->name,cache)==0)
break;
elem=elem->next;
}
if(elem!=NULL)
{
if(elem==record.head)
{
record.head=elem->next;
}
else
{
if(elem->next!=NULL)
elem->next->prior=elem->prior;
elem->prior->next=elem->next;
}
free(elem);
}
else
printf("The contact called %s was not found\n",cache);
}
free(cache);
}
void search(const addressList &record)
{
char *cache = (char *)malloc(maxCacheLength * sizeof(char));
singlePerson *elem=record.head;
char temp = getchar();
int location = 0;
while (temp == ' ' || temp == '\t')
temp = getchar();
while (temp != ' '&&temp != '\t'&&temp != '\n')
{
*(cache + location++) = temp;
temp = getchar();
}
*(cache + location) = '\0';
strlwr(cache);
while(elem!=NULL)
{
if(strcmp(elem->name,cache)==0)
break;
elem=elem->next;
}
if(elem==NULL)
printf("The contact called %s was not found\n",cache);
else
showSingle(elem);
free(cache);
}
void save(const addressList &record)
{
FILE *fileWrite;
char *cache = (char *)malloc(maxCacheLength * sizeof(char));
singlePerson *elem=record.head;
char temp = getchar();
int location = 0;
while (temp == ' ' || temp == '\t')
temp = getchar();
while (temp != ' '&&temp != '\t'&&temp != '\n')
{
*(cache + location++) = temp;
temp = getchar();
}
*(cache + location) = '\0';
strlwr(cache);
if((fileWrite=fopen(cache,"wt"))==NULL)
printf("Cant't create this file\n");
else
{
while(elem!=NULL)
{
fprintf(fileWrite,"%s %s %s\n",elem->name,elem->address,elem->phone);
elem=elem->next;
}
fclose(fileWrite);
}
free(cache);
}
void load(addressList &record)
{
FILE *fileRead;
char *cache = (char *)malloc(maxCacheLength * sizeof(char));
singlePerson *elem=NULL;
char temp = getchar();
int location = 0;
while (temp == ' ' || temp == '\t')
temp = getchar();
while (temp != ' '&&temp != '\t'&&temp != '\n')
{
*(cache + location++) = temp;
temp = getchar();
}
*(cache + location) = '\0';
strlwr(cache);
if((fileRead=fopen(cache,"rt"))==NULL)
printf("Cant't open this file\n");
else
{
temp=fgetc(fileRead);
while(feof(fileRead)==0)
{
elem=(singlePerson *)malloc(sizeof(singlePerson));
while (temp == ' ' || temp == '\t')
temp = fgetc(fileRead);
location=0;
while (temp != ' '&&temp != '\t'&&temp != '\n')
{
*(cache + location++) = temp;
temp = fgetc(fileRead);
}
*(cache + location) = '\0';
strlwr(cache);
strcpy(elem->name,cache);
while (temp == ' ' || temp == '\t')
temp = fgetc(fileRead);
location=0;
while (temp != ' '&&temp != '\t'&&temp != '\n')
{
*(cache + location++) = temp;
temp = fgetc(fileRead);
}
*(cache + location) = '\0';
strlwr(cache);
strcpy(elem->address,cache);
while (temp == ' ' || temp == '\t')
temp = fgetc(fileRead);
location=0;
while (temp != ' '&&temp != '\t'&&temp != '\n')
{
*(cache + location++) = temp;
temp = fgetc(fileRead);
}
*(cache + location) = '\0';
strlwr(cache);
strcpy(elem->phone,cache);
elem->next=record.head,elem->prior=NULL;
if(record.head!=NULL)
record.head->prior=elem;
record.head=elem;
record.length++;
temp=fgetc(fileRead);
}
fclose(fileRead);
}
free(cache);
}
void display(const addressList &record)
{
char *cache = (char *)malloc(maxCacheLength * sizeof(char));
singlePerson *elem=record.head;
char temp = getchar();
int location = 0;
while (temp == ' ' || temp == '\t')
temp = getchar();
while (temp != ' '&&temp != '\t'&&temp != '\n')
{
*(cache + location++) = temp;
temp = getchar();
}
*(cache + location) = '\0';
strlwr(cache);
if(strcmp(cache,"all")==0)
{
if(elem==NULL)
printf("there are no contacts in the list\n");
else
{
while(elem!=NULL)
{
showSingle(elem);
elem=elem->next;
}
}
}
else
{
while(elem!=NULL)
{
if(strcmp(elem->name,cache)==0)
break;
elem=elem->next;
}
if(elem!=NULL)
showSingle(elem);
else
printf("The contact called %s was not found\n",cache);
}
free(cache);
}
void showSingle(singlePerson *elem)
{
printf("name: %-10saddress: %-16sphone: %-12s\n",elem->name,elem->address,elem->phone);
}
void help(void)
{
printf("useradd:\n"
"\t功能:添加一个联系人.\n"
"\t格式:useradd name address phone.\n"
"\t例: useradd xiaoming shanghai 13590909090\n");
printf("display:\n"
"\t功能:显示联系人.\n"
"\t格式:display name 或 display all.\n"
"\t例: display xiaoming\n");
printf("search:\n"
"\t功能:查找联系人.\n"
"\t格式:search name.\n"
"\t例: search xiaoming\n");
printf("userdel:\n"
"\t功能:删除联系人.\n"
"\t格式:useradd name 或 userdel all.\n"
"\t例: userdel xiaoming\n");
printf("save:\n"
"\t功能:保存当前的通讯录.\n"
"\t格式:save path.\n"
"\t例: save d:\backup\addressList.txt\n");
printf("load:\n"
"\t功能:读取通讯录.\n"
"\t格式:load path.\n"
"\t例: load d:\backup\addressList.txt\n");
}