1. 程式人生 > >通訊錄管理系統(C語言)

通訊錄管理系統(C語言)

一、設計目的與內容

問題描述:設計一個操作簡便、功能實用的個人通訊資訊管理系統。

  1. 基本要求
  1. 儲存內容:姓名、手機、家庭電話、辦公電話、電子郵箱、地址等資訊;
  2. 具備建立、新增、刪除、查詢、修改、排序、顯示等功能;
  3. 介面美觀、操作簡便。
  1. 重點、難點

重點:結構體陣列的增、刪、改、查等操作。

難點:檔案的讀寫操作。

二、演算法的基本思想

三、主要功能設計與實現

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