用单链表编写简单的通讯录
发布网友
发布时间:2022-05-13 15:38
我来回答
共1个回答
热心网友
时间:2023-11-05 19:28
声明: 非原创
/*个人通讯录*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct dataWab{
char name[8];
char telephone[16];
char mobile[16];
char address[16];
char unit[16];
struct dataWab *nextPtr;
}; /*自引用结构,用作通讯录的数据结构*/
typedef struct dataWab DATAWAB;
typedef DATAWAB *DATAWABPTR;
void insertWab(DATAWABPTR*,char[],char[],char[],char[],char[]); /*插入函数*/
void menuWab(void); /*菜单函数*/
void printWab(DATAWABPTR); /*打印所有记录函数*/
void seekWab(DATAWABPTR,char[]); /*查找函数*/
void deleteWab(DATAWABPTR,char[]); /*删除函数*/
void modifyWab(DATAWABPTR,char[]); /*修改函数*/
int test(DATAWABPTR); /*测试函数,测试通讯录内是否有记录*/
void printone(DATAWABPTR); /*打印单条记录函数*/
int controlWab(void); /*程序分支控制函数*/
int main()
{
DATAWABPTR startPtr=NULL;
DATAWABPTR tempPtr;
FILE *fileWab;
char menu; /*菜单操作控制变量*/
char insert; /*插入确认控制变量*/
char modifyName[8]; /*查找时的临时姓名存储*/
char deleteName[8]; /*删除时的临时姓名存储*/
char tempName[8]; /*临时姓名存储*/
char tempTelephone[16]; /*临时电话存储*/
char tempMobile[16]; /*临时手机存储*/
char tempAddress[16]; /*临时地址存储*/
char tempUnit[16]; /*临时单位存储*/
char seekName[8]; /*查找时的临时姓名存储*/
if(!(fileWab=fopen("wab.dat","rb")))
return 0;
tempPtr=(DATAWABPTR)malloc(sizeof(DATAWAB));
while(fread(tempPtr,sizeof(DATAWAB),1,fileWab)==1){
tempPtr->nextPtr=startPtr;
startPtr=tempPtr;
tempPtr=(DATAWABPTR)malloc(sizeof(DATAWAB));
}
fclose(fileWab);
puts("\n\n\t\t欢迎使用个人通讯录1.0\n");
menuWab();
menu=getch();
while(menu!='6'){
switch(menu){
case '1':
printWab(startPtr);
break;
case '2':
while(controlWab()){
printf("\n查找姓名:\n#>");
scanf("%s",seekName);
seekWab(startPtr,seekName);
}
break;
case '3':
while(controlWab()){
puts("\n录入个人信息\n");
printf("姓名:");
scanf("%s",tempName);
printf("\n电话:");
scanf("%s",tempTelephone);
printf("\n手机:");
scanf("%s",tempMobile);
printf("\n地址:");
scanf("%s",tempAddress);
printf("\n单位:");
scanf("%s",tempUnit);
printf("录入正确请按 1 \n#>");
insert=getch();
if(insert=='1')
insertWab(&startPtr,tempName,tempTelephone,tempMobile,
tempAddress,tempUnit);
}
break;
case '4':
while(controlWab()){
printf("\n删除姓名\n#>");
scanf("%s",deleteName);
deleteWab(startPtr,deleteName);
}
break;
case '5':
while(controlWab()){
printf("\n修改姓名\n#>");
scanf("%s",modifyName);
modifyWab(startPtr,modifyName);
}
break;
default:
puts("\n\n输入选项错误,请重新输入\n");
break;
}
menuWab();
menu=getch();
}
if((fileWab=fopen("wab.dat","wb"))==NULL)
return 0;
while(startPtr!=NULL){
fwrite(startPtr,sizeof(DATAWAB),1,fileWab);
startPtr=startPtr->nextPtr;
}
puts("\n\n\t\t谢谢使用个人通讯录1.0,再见\n");
}
void menuWab(void) /*菜单函数*/
{
puts("\t\t\t主菜单");
puts("**********************************************************");
puts("**********************************************************");
puts("**\t<<-1->>所有记录\t\t<<-2->>查找记录\t\t**");
puts("**\t<<-3->>增加记录\t\t<<-4->>删除记录\t\t**");
puts("**\t<<-5->>修改记录\t\t<<-6->>退出程序\t\t**");
puts("**********************************************************");
puts("**********************************************************");
puts("\n请选择 1-6");
printf("#>");
}
void insertWab(DATAWABPTR *tempPtr,char temp2Name[],char temp2Telephone[],
char temp2Mobile[],char temp2Address[],char temp2Unit[])
/*插入函数*/
{
DATAWABPTR newPtr;
newPtr=(DATAWABPTR)malloc(sizeof(DATAWAB));
strcpy(newPtr->name,temp2Name);
strcpy(newPtr->telephone,temp2Telephone);
strcpy(newPtr->mobile,temp2Mobile);
strcpy(newPtr->address,temp2Address);
strcpy(newPtr->unit,temp2Unit);
newPtr->nextPtr=*tempPtr;
*tempPtr=newPtr;
}
void printWab(DATAWABPTR printPtr) /*打印函数*/
{
if(!(test(printPtr))){
printf("\n%8s%16s%16s%16s%16s\n","姓名","电话","手机","住址","单位");
while(printPtr!=NULL){
printf("%8s%16s%16s%16s%16s\n",printPtr->name,printPtr->telephone,
printPtr->mobile,printPtr->address,printPtr->unit);
printPtr=printPtr->nextPtr;
}
}
}
void seekWab(DATAWABPTR seekPtr,char tempName[])
/*查找函数*/
{
if(!(test(seekPtr))){
while(seekPtr!=NULL){
if(!(strcmp(tempName,seekPtr->name))){
printone(seekPtr);
seekPtr=seekPtr->nextPtr;
}
else
seekPtr=seekPtr->nextPtr;
}
}
}
void deleteWab(DATAWABPTR deletePtr,char deleteName[])
/*删除函数*/
{
DATAWABPTR tempPtr,forePtr;
forePtr=deletePtr;
tempPtr=deletePtr->nextPtr;
while(tempPtr!=NULL){
if(!(strcmp(tempPtr->name,deleteName))){
forePtr->nextPtr=tempPtr->nextPtr;
free(tempPtr);
break;
}
else{
forePtr=tempPtr;
tempPtr=tempPtr->nextPtr;
}
}
}
void modifyWab(DATAWABPTR modifyPtr,char modifyName[])
/*修改函数*/
{
if(!(test(modifyPtr))){
while(modifyPtr!=NULL){
if(!(strcmp(modifyName,modifyPtr->name))){
printf("请输入新姓名:");
scanf("%s",modifyPtr->name);
printf("\n请输入新电话:");
scanf("%s",modifyPtr->telephone);
printf("\n请输入新手机:");
scanf("%s",modifyPtr->mobile);
printf("\n请输入新地址:");
scanf("%s",modifyPtr->address);
printf("\n请输入新单位:");
scanf("%s",modifyPtr->unit);
break;
}
else
modifyPtr=modifyPtr->nextPtr;
}
}
}
int test(DATAWABPTR testPtr)
{
if(testPtr==NULL)
puts("通讯录内没有任何记录");
else
return 0;
return 1;
}
void printone(DATAWABPTR printone)
{
printf("\n%8s%16s%16s%16s%16s\n","姓名","电话","手机","住址","单位");
printf("%8s",printone->name);
printf("%16s",printone->telephone);
printf("%16s",printone->mobile);
printf("%16s",printone->address);
printf("%16s\n",printone->unit);
}
int controlWab(void)
{
puts("继续执行请按 1");
if(getch()=='1')
return 1;
return 0;
}