数据结构通讯录源代码

相关主题
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

#include
#include
#include
#include

struct record
{
char name[20];
char street[20];
char city[20];
char eip[20]; //邮编
char state[20];
}people[500];//500个记录,可修改

struct pnode
{
record data;
struct pnode *next, *prior;//双循环链表
};
typedef pnode * linklist;

linklist l;
int len=0;//链表长度
FILE *fp; //文件指针

void mainmenu();//主菜单
void searchmenu();//查询菜单
void enter();//添加新纪录
void search();//按条件搜索记录
void display();//显示全部记录
void load();//载入文件内容
void save();//写入文件
void del();//删除记录
void listinsert();//插入结点函数
void initlist();//初始化链表函数

void main() //主函数
{
initlist();
load();
listinsert();
while (1)
mainmenu(); //进入主菜单,有非法输入仍停留在主菜单
}

void initlist()//链表初始化函数
{
l=(linklist)malloc(sizeof(pnode));//动态申请内存
l->next=l;
l->prior=l;
}


void load()//装载已有文件信息
{
if((fp=fopen("txl.txt","rb"))==NULL)
{
printf("\n\t\t通讯录文件不存在");
if ((fp=fopen("txl.txt","wb"))==NULL)
{
printf("\n\t\t建立失败");
exit(0);
}
else
{
printf("\n\t\t通讯录文件已建立");
printf("\n\t\t按任意键进入主菜单");
getch();
return;
}
exit(0);
}
//导入文件功能部分
fseek(fp,0,2);
if (ftell(fp)>0)
{
rewind(fp);
for(len=0;!feof(fp)&&fread(&people[len],sizeof(struct record),1,fp);len++);
printf("\n\t\t文件导入成功");
printf("\n\t\t按任意键返回主菜单");
getch();
return;
}
printf("\n\t\t文件导入成功");
printf("\n\t\t通讯录文件中无任何纪录");
printf("\n\t\t按任意键返回主菜单");
getch();
return;
}

void listinsert()//增加新结点
{
linklist s,p=l;
for(int i=0;i{
s=(linklist)malloc(sizeof(pnode));
strcpy(s->,people[i].name);
strcpy(s->data.city,people[i].city);
strcpy(s->data.street,people[i].street);
strcpy(s->data.eip,people[i].eip);
strcpy(s->data.state,people[i].state);
s->prior=p->prior;
s->next=p;
p->prior->next=s;
p->prior=s;
p=p->next;
}
}

void mainmenu()//主菜单
{
char ch;
system("cls");
printf("\n\t\t***************欢迎进入通讯录系统***************");
printf("\n\t\t******************1-新添纪录 ***************");
printf("\n\t\t******************2-查找联系人 ***************");
printf("\n\t\t******************3-删除联系人 ***************");
printf("\n\t\t******************4-保存 ***************");
printf("\n\t\t******************5-退出 ***************");
printf("\n\t\t************************************************");
printf("\n\t\t请选择:");

printf("%c ",ch=getch());
switch (ch)
{
case '1':enter();break;
case '2':searchmenu();break;
case '3':del();break;
case '4':save();break;
case '5':exit(0);
default:mainmenu();
}
}

void enter()//添加新纪录
{
printf("\n\t\t**************** 请输入学生信息 ****************\n");
printf("\n\t\t姓名:");
scanf("%s",&people[len].name);
printf("\n\t\t街道:");
scanf("%s",&people[len].street);
printf("\n\t\t城市:");
scanf("%s",&people[len].city);
printf("\n\t\t邮编:");
scanf("%s",&people[len].eip);
printf("\n\t\t国家:");
scanf("%s",&people[len].state);
len++;
printf("\n\t\t是否继续添加?(Y/N):");
if (getch()=='y')
enter();
return;
}

void searchmenu()//查询菜单
{
char ch;
system("cls");
printf("\n\t\t******************* 查询菜单 *******************");
printf("\n\t\t**************** 1-显示所有记录 ****************");
printf("\n\t\t**************** 2-按姓名查询 ****************");
printf("\n\t\t**************** 3-返回主菜单 ****************");
printf("\n\t\t************************************************");
printf("\n\t\t请选择:");
printf("%c",ch=getch());
switch (ch)
{
case '1':display();break;
case '2':search();break;
case '3':mainmenu();break;
}
}

void search()
{
printf("\n\t\t***************** 按姓名查找 *******************");
char name[20];
printf("\n\t\t请输入姓名:");
scanf("%s",name);
printf(" 查询到的信息:\n");
printf(" %-18s%-18s%-18s%-15s%s\n","姓名","街道","城市","邮编","国家");//格式控制输出
printf(" -----------------------------------------------------------------------------\n");
for (int i=0;i{
if(strcmp(name,people[i].name)==0)
printf(" %-18s%-18s%-18s%-15s%s\n",people[i].name,people[i].street,people[i].city,people[i].eip,people[i].state);
if (i+1}
printf(" -----------------------------------------------------------------------------\n");
printf("\n\t\t按任意键返回查询菜单");
getch();
searchmenu();
}

void display()//显示所有纪录
{
int i;
system("cls");
if(len!=0)
{
printf("\n\t\t*************** 以下为通讯录所有信息************\n\n");
printf(" %-18s%-18s%-18s%-15s%s\n","姓名","街道","城市","邮编","国家");
printf(" -----------------------------------------------------------------------------\n");
for (i=0;i{
printf(" %-18s%-18s%-18s%-15s%s\n",people[i].name,people[i].street,people[i].city,people[i].eip,people[i].state);
if (i+1}
printf(" -----------------------------------------------------------------------------\n");
}
else
printf("\n\t\t通讯录中无任何纪录");
printf("\n\t\t按任意键返回查询菜单:");
getch();
searchmenu();
}

void del() //删除纪录
{


int a=0,i,j,findmark; //findmark为查找结果标志
// int findmark=0,delmark=0;
char name[20];
printf("\n\t\t请输入要删除学生姓名:");
scanf("%s",name);
for (i=a;i{
if (findmark=strcmp(people[i].name,name)==NULL) //找到一条符合条件的记录
{
// findmark++;
printf("\n\t\t以下是您要删除的学生纪录:\n");
printf(" %-18s%-18s%-18s%-15s%s\n","姓名","街道","城市","邮编","国家");
printf(" -----------------------------------------------------------------------------\n");
printf(" %-18s%-18s%-18s%-15s%s\n",people[i].name,people[i].street,people[i].city,people[i].eip,people[i].state); printf(" -----------------------------------------------------------------------------\n");
printf("\n\t\t是否删除?(y/n)");
if (getch()=='y')
{
for (j=i;jpeople[j]=people[j+1];
len--;
i--;
// delmark++;
printf("\n\t\t删除成功");
if((i+1){
printf("\n\t\t是否继续删除相同姓名的同学信息?(y/n)"); //是否删除同名纪录
if (getch()=='y')
{
a=i;
continue; //删除上一条记录之后搜索是否有同名纪录
}
}
} //endif
if((i+1){
printf("\n\t\t是否继续删除相同姓名的同学信息?(y/n)"); //不删前一条纪录,删当前纪录
if (getch()=='y')
{
a=i;
continue;
}
}
} //endif
else
continue;
} //endfor
if(!findmark)//没有找到任何符合条件的记录
printf("\n\t\t没有符合条件的记录!");
printf("\n\t\t是否继续删除?(y/n)"); //无同名纪录,是否继续删除其他纪录
if (getch()=='y')
del(); //继续删除其他纪录
}

void save()//写入文件
{
int i;
if ((fp=fopen("txl.txt","wb"))==NULL)
{
printf("\n\t\t文件打开失败");
}
for (i=0;i{
if (fwrite(&people[i],sizeof(struct record),1,fp)!=1)
printf("\n\t\t写入文件错误!\n");
}
fclose(fp); //关闭文件指针
printf("\n\t\t通讯录文件已保存,按任意键继续...");
getch();
return;
}


相关文档
最新文档