通讯录管理系统(C语言)

一、设计目的与内容

问题描述:设计一个操作简便、功能实用的个人通信信息管理系统。

  1. 基本要求
  1. 存储内容:姓名、手机、家庭电话、办公电话、电子邮箱、地址等信息;
  2. 具备创建、添加、删除、查询、修改、排序、显示等功能;
  3. 界面美观、操作简便。
  1. 重点、难点

重点:结构体数组的增、删、改、查等操作。

难点:文件的读写操作。

二、算法的基本思想

通讯录管理系统(C语言)_第1张图片

通讯录管理系统(C语言)_第2张图片

三、主要功能设计与实现

1.创建通讯录

txl Create(){
	txl head = (txl)malloc(sizeof(Node));
	head->count = 0;
	head->next = NULL;
	return head;
}

2.插入信息

void Insert(txl &head){
	printf("\n\t\t请依次输入姓名、手机、家庭电话、办公电话、电子邮箱、地址\n\t");
	data d;
	scanf("%s%s%s%s%s%s",d.name,d.phone,d.hometel,d.worktel,d.email,d.adress);
	txl p = (txl)malloc(sizeof(Node));
	txl r = head;
	p->d = d;
	while(r->next){
		r = r->next;
	}
	r->next = NULL;
	p->next = r->next;
	r->next = p;
	head->count++;
	printf("\n\t\t是否继续添加?(Y/N):");
	if (getch()=='y'||getch()=='Y') 
		Insert(head); 
	return;
}

3.显示全部信息

void showAll(txl head){

         printf("%d\n",head->count);

         txl p = head->next;

         while(p){

                  printf("\n\t\t姓    名:%s",p->d.name);

                  printf("\n\t\t手机号码:%s",p->d.phone);

                  printf("\n\t\t家庭电话:%s",p->d.hometel);

                  printf("\n\t\t工作电话:%s",p->d.worktel);

                  printf("\n\t\t电子邮件:%s",p->d.email);

                  printf("\n\t\t家庭住址:%s",p->d.adress);

                  printf("\n\t\t____________________\n");

                  p = p->next;

         }

}

4.按姓名查询(按手机号码查询与此类似)

void searchByName(txl head){

         txl p = head->next;

         int mark = 0;

         printf("\n\t\t***************** 按姓名查找 *******************");

         char name[20];

         printf("\n\t\t请输入姓名:");

         scanf("%s",name);

         printf("\n\t\t************* 您查找的信息将在下面显示 ***********");

         while(p!=NULL){

                  if(strcmp(p->d.name,name)==0){

                          printf("\n\t\t姓    名:%s",p->d.name);

                          printf("\n\t\t手机号码:%s",p->d.phone);

                          printf("\n\t\t家庭电话:%s",p->d.hometel);

                          printf("\n\t\t工作电话:%s",p->d.worktel);

                          printf("\n\t\t电子邮件:%s",p->d.email);

                          printf("\n\t\t家庭住址:%s",p->d.adress);

                          printf("\n\t\t************************************************");

                          mark++;

                  }

                  p = p->next;

         }

         if(!mark){

                  printf("\n\t\t您的通讯录中没有与此姓名匹配的联系人");

         }else printf("\n\t\t一共为您找到%d个与之匹配的联系人",mark);

         printf("\n\t\t按任意键返回主菜单");

         getch();

         return;

}

5.按姓名删除(按手机号码删除与此类似)

void deleteByName(txl &head){

         txl p = head->next,q;

         printf("\n\t\t**************** 按姓名删除 **********************");

         char name[20];

         int mark = 0;

         printf("\n\t\t请输入手机号码:");

         scanf("%s",name);

         if(p->next==NULL){

                  if(strcmp(p->d.name,name)==0){

                          free(p);

                          head->next = NULL;

                          head->count--;

                          mark++;
                  }

         }else{

                  while(p->next){

                          if(strcmp(p->next->d.name,name))

                                   p = p->next;

                          else {
                                   q = p->next;

                                   p->next = p->next->next;

                                   free(q);

                                   head->count--;

                                   mark++;

                                   break;
                          }
                  }
         }

         if(!mark)

                  printf("\n\t\t未找到此联系人");

         else printf("\n\t\t删除成功");
                 

         printf("\n\t\t按任意键返回主菜单");

         getch();

         return;

}

6.文件读写操作

bool Readtxl(txl &head){

         FILE *fp;

         txl p = (txl)malloc(sizeof(Node));

         txl r = head;

         if((fp = fopen("txl.txt","r"))==NULL){

                  printf("文件读入失败!\n");

                  return false;

         }else{

                  while(fread(p,sizeof(Node),1,fp)==1){   //如果读到数据,就显示;否则退出

                          head->count++;

                          r->next = NULL;

                          p->next = r->next;

                          r->next = p;

                          r = r->next;

                          p = (txl)malloc(sizeof(Node));

                  }

                  r->next = NULL;

                  fclose(fp);

                  return true;

         }

        

}

bool Writetxl(txl head){

         FILE *fp;txl p = head->next;

         if((fp = fopen("txl.txt","w"))==NULL){

                  printf("文件写入失败!\n");

                  return false;

         }else {

                  for(int i = 0; i < head->count;i++){

                          fwrite(p,sizeof(Node),1,fp);

                          p = p->next;

                  }

                  fclose(fp);

                  printf("通讯录保存成功\n");

                  return true;

         }

}

上面给出了部分核心代码,下面是完整的程序代码

https://download.csdn.net/download/qq_26154945/10906061

你可能感兴趣的:(通讯录管理系统(C语言))