C語言實現通訊錄小功能

本文實例為大傢分享瞭C語言實現通訊錄功能的具體代碼,供大傢參考,具體內容如下

頭文件contact.h

#pragma once
#define  _CRT_SECURE_NO_WARNINGS 1
 
//#define max 1000
 
#define fault 3
#define max_name 20
#define max_telephone 12
#define max_sex 5
#define max_address 30
 
#include<stdio.h>
#include<string.h>
#include<assert.h>
#include<stdlib.h>
#include<errno.h>
 
enum option
{
 EXIT,
 ADD,
 DEL,
 SEARCH,
 MODIFY,
 SHOW,
 SORT,
 SAVE
};
 
struct people_information
{
 char name[max_name];
 char telephone[max_telephone];
 char sex[max_sex];
 char address[max_address];
 int age;
};
 
struct contact
{
 //struct people_information data[max];
 struct people_information *data;//動態空間
 int size;//記錄當前有多少信息
 int capacity;//當前最大容量
};
 
void initcontact(struct contact* ps);//初始化函數
void add(struct contact* ps);
void my_delete(struct contact* ps);
void search(const struct contact* ps);
void modify(struct contact* ps);//修改指定聯系人
void show(const struct contact* ps);//展示全部聯系人
void sort(struct contact* ps);
void destroy(struct contact* ps);
void save(struct contact* ps);

分裝函數contact.c

#define  _CRT_SECURE_NO_WARNINGS 1
 
#include"contact.h"
 
 
void initcontact(struct contact* ps)
{
 //memset(ps->data, 0, sizeof(ps->data));
 //ps->size = 0;//初始化通訊錄中隻有0個信息
 ps->data = (struct people_information*)malloc(fault * sizeof(struct people_information));
 if (ps->data == NULL)
 {
  return;
 }
 ps->size = 0;
 ps->capacity = fault;
}
 
void checkcapacity(struct contact* ps)
{
 if (ps->size == ps->capacity)
 {
  struct people_information * ptr=realloc(ps->data, (ps->capacity + 2)*sizeof(struct people_information));
  if (ptr != NULL)
  {
   ps->data = ptr;
   ps->capacity += 2;
   printf("增容成功\n");
  }
  else
  {
   printf("增容失敗\n");
  }
 }
}
 
void add(struct contact* ps)
{
 /*if (ps->size == max)
 {
  printf("通訊錄已滿,無法增加\n");
 }
 else
 {
  printf("請輸入姓名:>");
  scanf("%s", ps->data[ps->size].name);
  printf("請輸入電話:>");
  scanf("%s", ps->data[ps->size].telephone);
  printf("請輸入性別:>");
  scanf("%s", ps->data[ps->size].sex);
  printf("請輸入住址:>");
  scanf("%s", ps->data[ps->size].address);
  printf("請輸入年齡:>");
  scanf("%d", &(ps->data[ps->size].age));
  ps->size++;
  printf("添加成功!\n");
 }*/
 //檢測當前通訊錄的容量
 checkcapacity(ps);
 printf("請輸入姓名:>");
 scanf("%s", ps->data[ps->size].name);
 printf("請輸入電話:>");
 scanf("%s", ps->data[ps->size].telephone);
 printf("請輸入性別:>");
 scanf("%s", ps->data[ps->size].sex);
 printf("請輸入住址:>");
 scanf("%s", ps->data[ps->size].address);
 printf("請輸入年齡:>");
 scanf("%d", &(ps->data[ps->size].age));
 ps->size++;
 printf("添加成功!\n");
}
 
void show(const struct contact* ps)
{
 if (ps->size == 0)
 {
  printf("通訊錄為空\n");
 }
 else
 {
  int i = 0;
  for (i = 0; i < ps->size; i++)
  {
   printf("%-20s\t%-12s\t%-5s\t%-30s\t%-4s\n",
    "姓名", "電話", "性別", "住址", "年齡");
   printf("%-20s\t%-12s\t%-5s\t%-30s\t%-4d\n",
    ps->data[i].name, 
    ps->data[i].telephone,
    ps->data[i].sex,
    ps->data[i].address,
    ps->data[i].age);
  }
 }
}
 
static int FindByName(const struct contact* ps, char name[max_name])//冗餘函數、查找函數
{ 
 //找到瞭返回名字所在元素的下標,找不到返回-1
 int i = 0;
 for (i = 0; i < ps->size; i++)
 {
  if (0 == strcmp(ps->data[i].name, name))
  {
   return i;
  }
 }
 return -1;
}
 
void my_delete(struct contact* ps)
{
 printf("請輸入您要刪除聯系人的姓名:");
 char name[max_name] ;
 scanf("%s", name);
 int pos = FindByName(ps, name);
 if (pos==-1)
 {
  printf("您要刪除的人不存在\n");
 }
 else
 {
  //刪除數據
  int j = 0;
  for (j = pos; j < ps->size - 1; j++)
  {
   ps->data[j] = ps->data[j + 1];
  }
  ps->size--;
  printf("刪除成功!\n");
 }
}
 
void search( const struct contact* ps)
{
 printf("請輸入您要查找聯系人的姓名:");
 char name[max_name];
 scanf("%s", name);
 int pos = FindByName(ps, name);
 if (pos == -1)
 {
  printf("您要查找的聯系人不存在\n");
 }
 else
 {
  printf("%-20s\t%-12s\t%-5s\t%-30s\t%-4s\n",
   "姓名", "電話", "性別", "住址", "年齡");
  printf("%-20s\t%-12s\t%-5s\t%-30s\t%-4d\n",
   ps->data[pos].name,
   ps->data[pos].telephone,
   ps->data[pos].sex,
   ps->data[pos].address,
   ps->data[pos].age);
 }
}//完成
 
void modify(struct contact* ps)
{
 printf("請輸入您要修改聯系人的姓名:");
 char name[max_name];
 scanf("%s", name);
 int pos = FindByName(ps, name);
 if (pos == -1)
 {
  printf("您要修改的聯系人不存在\n");
 }
 else
 {
  printf("新的姓名:");
  scanf("%s", ps->data[pos].name);
  printf("新的電話:");
  scanf("%s", ps->data[pos].telephone);
  printf("新的性別:");
  scanf("%s", ps->data[pos].sex);
  printf("新的住址:");
  scanf("%s", ps->data[pos].address);
  printf("新的年齡:");
  scanf("%d", &(ps->data[pos].age));
 
  printf("修改完成!\n");
 }
}
 
void sort(struct contact* ps)
{
 assert(ps);
 if (ps->size == 0)
 {
  printf("通訊錄為空,無需排序\n");
 }
 else if (ps->size == 1)
 {
  printf("您的通訊錄中隻有一個人,無需排序\n");
  printf("%-20s\t%-12s\t%-5s\t%-30s\t%-4s\n",
   "姓名", "電話", "性別", "住址", "年齡");
  printf("%-20s\t%-12s\t%-5s\t%-30s\t%-4d\n",
   ps->data[0].name,
   ps->data[0].telephone,
   ps->data[0].sex,
   ps->data[0].address,
   ps->data[0].age);
 }
 else
 {
  int i = 0;
  for (i = 0; i < ps->size; i++)
  {
   int j = 0;
   for (j = 0; j < ps->size - i - 1; j++)
   {
    int ret = strcmp(ps->data[j].name, ps->data[j + 1].name);
    if (ret > 0)
    {
     char tmp1[max_name] = { 0 };
     char tmp2[max_telephone] = { 0 };
     char tmp3[max_sex] = { 0 };
     char tmp4[max_address] = { 0 };
     int  tmp5 = 0;
 
     strcpy(tmp1, ps->data[j].name);
     strcpy(tmp2, ps->data[j].telephone);
     strcpy(tmp3, ps->data[j].sex);
     strcpy(tmp4, ps->data[j].address);
     tmp5 = ps->data[j].age;//交給臨時變量
 
     strcpy(ps->data[j].name, ps->data[j+1].name);
     strcpy(ps->data[j].telephone, ps->data[j+1].telephone);
     strcpy(ps->data[j].sex, ps->data[j+1].sex);
     strcpy(ps->data[j].address, ps->data[j+1].address);
     ps->data[j].age= ps->data[j+1].age;
 
     strcpy( ps->data[j + 1].name,tmp1);
     strcpy( ps->data[j + 1].telephone,tmp2);
     strcpy( ps->data[j + 1].sex,tmp3);
     strcpy( ps->data[j + 1].address,tmp4);
     ps->data[j + 1].age=tmp5;
    }
   }
  }
 }
 printf("排序完成!\n");
}
 
void destroy(struct contact* ps)
{
 free(ps->data);
 ps->data = NULL;
}
 
void save(struct contact* ps)
{
 FILE* pfwrite = fopen("contact.dat", "wb");
 if (pfwrite == NULL)
 {
  printf("%s\n", strerror(errno));
  return ;
 }
 //寫通訊錄中的數據到文件中
 
 int i = 0;
 for (i = 0; i < ps->size; i++)
 {
  fwrite(&(ps->data[i]), sizeof(struct people_information), 1, pfwrite);
 }
 
 fclose(pfwrite);
 pfwrite = NULL;
}

源文件test.c

#define  _CRT_SECURE_NO_WARNINGS 1
 
//功能:
//一、存放1000個系人信息,信息包括:姓名、電話、性別、住址、年齡
//二、增加聯系人信息
//三、刪除指定聯系人信息
//四、查找聯系人信息
//五、修改聯系人信息
//六、顯示全部聯系人信息
//七、排序
 
#include<stdio.h>
#include"contact.h"
 
void menu()
{
 printf("******************************\n");
 printf("****1.add         2.delete****\n");
 printf("****3.search      4.modify****\n");
 printf("****5.show        6.sort  ****\n");
 printf("****7.save        0.exit  ****\n");
 printf("******************************\n");
}
 
int main()
{ 
 int input = 0;
 //創建通訊錄
 struct contact con;//con就是通訊錄,存放1000個信息
 
 
 //初始化通訊錄
 initcontact(&con);
 do
 {
  menu();
  printf("請選擇:>");
  scanf("%d", &input);
  switch (input)
  {
  case ADD:
   add(&con);//增加聯系人信息
   break;
  case DEL:
   my_delete(&con);//刪除指定聯系人信息
   break;
  case SEARCH:
   search(&con);//查找聯系人信息
   break;
  case MODIFY:
   modify(&con);//修改聯系人信息
   break;
  case SHOW:
   show(&con);//顯示全部聯系人信息
   break;
  case SORT:
   sort(&con);//排序
   break;
  case EXIT:
   printf("退出通訊錄\n");
   //銷毀通訊錄
   destroy(&con);
   break;
  case SAVE:
   save(&con);
  default:
   printf("選擇錯誤\n");
   break;
  }
 } while(input);
 return 0;
}

以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。

推薦閱讀: