c语言通讯录系统程序的功能,求助用C语言编程序通讯录管理系统实训题目

#include #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");

break;

default:

printf("Error");

}

}

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 == '' '')

temp = getchar();

while (temp != '' ''&&temp != '' ''&&temp != '''')

{

*(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 == '' '')

temp = getchar();

while (temp != '' ''&&temp != '' ''&&temp != '''')

{

*(newNode->name + location++) = temp;

temp = getchar();

}

*(newNode->name + location)=''\0'',location=0;

while (temp == '' '' || temp == '' '')

temp = getchar();

while (temp != '' ''&&temp != '' ''&&temp != '''')

{

*(newNode->address + location++) = temp;

temp = getchar();

}

*(newNode->address + location)=''\0'',location=0;

while (temp == '' '' || temp == '' '')

temp = getchar();

while (temp != '' ''&&temp != '' ''&&temp != '''')

{

*(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 == '' '')

temp = getchar();

while (temp != '' ''&&temp != '' ''&&temp != '''')

{

*(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");

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",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 == '' '')

temp = getchar();

while (temp != '' ''&&temp != '' ''&&temp != '''')

{

*(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",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 == '' '')

temp = getchar();

while (temp != '' ''&&temp != '' ''&&temp != '''')

{

*(cache + location++) = temp;

temp = getchar();

}

*(cache + location) = ''\0'';

strlwr(cache);

if((fileWrite=fopen(cache,"wt"))==NULL)

printf("Cant''t create this file");

else

{

while(elem!=NULL)

{

fprintf(fileWrite,"%s %s %s",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 == '' '')

temp = getchar();

while (temp != '' ''&&temp != '' ''&&temp != '''')

{

*(cache + location++) = temp;

temp = getchar();

}

*(cache + location) = ''\0'';

strlwr(cache);

if((fileRead=fopen(cache,"rt"))==NULL)

printf("Cant''t open this file");

else

{

temp=fgetc(fileRead);

while(feof(fileRead)==0)

{

elem=(singlePerson *)malloc(sizeof(singlePerson));

while (temp == '' '' || temp == '' '')

temp = fgetc(fileRead);

location=0;

while (temp != '' ''&&temp != '' ''&&temp != '''')

{

*(cache + location++) = temp;

temp = fgetc(fileRead);

}

*(cache + location) = ''\0'';

strlwr(cache);

strcpy(elem->name,cache);

while (temp == '' '' || temp == '' '')

temp = fgetc(fileRead);

location=0;

while (temp != '' ''&&temp != '' ''&&temp != '''')

{

*(cache + location++) = temp;

temp = fgetc(fileRead);

}

*(cache + location) = ''\0'';

strlwr(cache);

strcpy(elem->address,cache);

while (temp == '' '' || temp == '' '')

temp = fgetc(fileRead);

location=0;

while (temp != '' ''&&temp != '' ''&&temp != '''')

{

*(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 == '' '')

temp = getchar();

while (temp != '' ''&&temp != '' ''&&temp != '''')

{

*(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");

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",cache);

}

free(cache);

}

void showSingle(singlePerson *elem)

{

printf("name: %-10saddress: %-16sphone: %-12s",elem->name,elem->address,elem->phone);

}

void help(void)

{

printf("useradd:"

" 功能:添加一个联系人."

" 格式:useradd name address phone."

" 例: useradd xiaoming shanghai 13590909090");

printf("display:"

" 功能:显示联系人."

" 格式:display name 或 display all."

" 例: display xiaoming");

printf("search:"

" 功能:查找联系人."

" 格式:search name."

" 例: search xiaoming");

printf("userdel:"

" 功能:删除联系人."

" 格式:useradd name 或 userdel all."

" 例: userdel xiaoming");

printf("save:"

" 功能:保存当前的通讯录."

" 格式:save path."

" 例: save d:\backup\addressList.txt");

printf("load:"

" 功能:读取通讯录."

" 格式:load path."

" 例: load d:\backup\addressList.txt");

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值