亲宝软件园·资讯

展开

C语言通讯录 C语言实现通讯录

get_it_started 人气:0

实现的功能有:

1.初始化通讯录;
2. 通讯录中增加条目;
3. 删除通讯录中的指定条目;
4. 按指定规则查找通讯录中特定条目;
5. 打印通讯录中的所有信息;
6. 修改通讯录的指定条目;
7. 销毁通讯录;
8. 将通讯录内容保存在文件;
9. 将文件中的内容加载回内存。

**Addr_Book.h**

#pragma once
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define NAME_SIZE 200 
#define PHONE_SIZE 200 
#define ADDR_SIZE 200

typedef struct PersonInfo {
  char name[NAME_SIZE];
  char phone[PHONE_SIZE];
  char addr[ADDR_SIZE];
} PersonInfo;

#define FILE_PATH "./data.txt"

enum {
  DISPLAY = 1,
  ADD = 2,
  ERASE = 3,
  FIND = 4,
  MODIFY = 5,
  EXIT = 0
};

typedef struct AddrBook {
  PersonInfo* data;//内容
  size_t size; //联系人数量
  size_t capacity; //通讯录容量
} AddrBook;


/**
* @brief 初始化通讯录
*
* @param addr_book
*/
void AddrBookInit(AddrBook* addr_book);

/**
* @brief 通讯录中增加条目
*
* @param addr_book
*/
void AddrBookAdd(AddrBook* addr_book);

/**
* @brief 删除通讯录中的指定条目
*
* @param addr_book
*/
void AddrBookErase(AddrBook* addr_book);

/**
* @brief 按指定规则查找通讯录中特定条目
*
* @param addr_book
*/
void AddrBookFind(AddrBook* addr_book);

/**
* @brief 打印通讯录中的所有信息
*
* @param addr_book
*/
void AddrBookDisplay(AddrBook* addr_book);

/**
* @brief 修改通讯录的指定条目
*
* @param addr_book
*/
void AddrBookModify(AddrBook* addr_book);

/**
* @brief 销毁通讯录
*
* @param addr_book
*/
void AddrBookDestroy(AddrBook* addr_book);

/**
* @brief 将通讯录内容保存在文件
*
* @param addr_book
*/
void AddrBookSave(AddrBook* addr_book);

/**
* @brief 将文件中的内容加载回内存.
*
* @param addr_book
*/
void AddrBookLoad(AddrBook* addr_book);

**Addr_Book.c**

#define _CRT_SECURE_NO_WARNINGS 1

#include "Addr_Book.h"

//打印通讯录
void AddrBookDisplay(AddrBook* addr_book){ 
  if (addr_book == NULL){
    return;
  }
  printf("\n====================通讯录=========================\n");
  printf("|   姓名   |   号码   |   地址   |\n");
  printf("|----------------|---------------|----------------|\n");
  size_t i = 0;
  for (; i < addr_book->size; ++i){
    printf("|   %s   |   %s   |   %s   |\n", 
      addr_book->data[i].name, addr_book->data[i].phone, addr_book->data[i].addr);
    printf("|----------------|---------------|----------------|\n");
  }
}

//初始化通讯录
void AddrBookInit(AddrBook* addr_book){ 
  if (addr_book == NULL){
    return ;
  }
  addr_book->size = 0;
  addr_book->capacity = 1;
  addr_book->data = (PersonInfo*)malloc(sizeof(PersonInfo)*addr_book->capacity);
}

//扩大内存
void AddrBookRelloc(AddrBook* addr_book){
  if (addr_book == NULL){
    return;
  }
  addr_book->capacity = addr_book->capacity * 2 + 1;//内存不够,扩大容量
  PersonInfo* new_data = (PersonInfo*)malloc(sizeof(PersonInfo)*addr_book->capacity);//开辟新的空间大小
  size_t i = 0;
  for (; i < addr_book->size; ++i){
    new_data[i] = addr_book->data[i];//将旧内存赋给新内存
  }
  free(addr_book->data);//释放旧内存
  addr_book->data = new_data;
}

//添加联系人
void AddrBookAdd(AddrBook* addr_book){ 
  if (addr_book == NULL){
    return;
  }
  if (addr_book->size >= addr_book->capacity){ //内存不够需要扩大内存
    AddrBookRelloc(addr_book);
  }
  size_t cur = addr_book->size;
  ++addr_book->size;
  printf("添加联系人\n");
  printf("输入姓名:");
  scanf("%s", addr_book->data[cur].name);
  printf("输入号码:");
  scanf("%s", addr_book->data[cur].phone);
  printf("输入地址:");
  scanf("%s", addr_book->data[cur].addr);
  printf("添加结束!\n");
}

//删除指定联系人
void AddrBookErase(AddrBook* addr_book){ 
  if (addr_book == NULL){
    return;
  }
  char delete_name[200] = {0};
  printf("输入要删除联系人姓名:");
  scanf("%s", &delete_name);
  size_t i = 0;
  for (; i <addr_book->size;++i){
    if (strcmp(addr_book->data[i].name, delete_name)){
      free(addr_book->data);
      addr_book->size--;
      printf("已删除!");
    }
    else{
      printf("删除失败,请重新输入联系人!");
    }
  }
}

//保存内容
void AddrBookSave(AddrBook* addr_book){ 
  FILE* fp = fopen(FILE_PATH, "w");
  if (fp == NULL){
    printf("打开文件失败!\n");
    return;
  }
  size_t i = 0;
  for (; i < addr_book->size; ++i){
    fprintf(fp, "|   %s   |   %s   |   %s   |\n",
      addr_book->data[i].name, addr_book->data[i].phone, addr_book->data[i].addr);
  }
  fclose(fp);
}

//按姓名查找联系人
void AddrBookFind(AddrBook* addr_book){  
  if (addr_book == NULL){
    return;
  }
  char delete_name[200] = { 0 };
  printf("输入所查找联系人姓名:");
  scanf("%s", &delete_name);
  size_t i = 0;
  for (; i <addr_book->size; ++i){
    if (strcmp(addr_book->data[i].name, delete_name)){
      printf("找到了,该联系人信息为:姓名:%s,号码:%s,地址:%s\n",
        addr_book->data[i].name, addr_book->data[i].phone, addr_book->data[i].addr);    
    }
    else{
      printf("此人不存在!\n");
    }
  }
}

//修改联系人信息
void AddrBookModify(AddrBook* addr_book){
  if (addr_book == NULL){
    return;
  }
  char name[200] = { 0 };
  printf("输入要修改的联系人名字:");
  scanf("%s", &name);
  size_t i = 0;
  for (; i < addr_book->size; ++i)
  {
    if (strcmp(addr_book->data[i].name, name) == 0)
    {
      printf("这是第%lu个联系人:\n", i + 1);          
      printf("此人信息为:\n 姓名:%s ,电话:%s ,地址:%s\n",
        addr_book->data[i].name,
        addr_book->data[i].phone,
        addr_book->data[i].addr);
    }
  }
  size_t num = 0;
  printf("输入要修改的联系人编号:");       
  scanf("%lu", &num);
  char name2[200];
  char phone[200];
  char addr[200];
  printf("请输入新的姓名:");
  scanf("%s", name2);
  strcpy(addr_book->data[num - 1].name, name2);
  printf("请输入新的电话号码:");
  scanf("%s", phone);
  strcpy(addr_book->data[num - 1].phone, phone);
  printf("请输入新的地址:");
  scanf("%s", addr);
  strcpy(addr_book->data[num - 1].addr, addr);
  printf("修改成功!\n");
  return;

}

//销毁通讯录
void AddrBookDestroy(AddrBook* addr_book){
  if (addr_book == NULL)            
  {
    printf("addr_book is null\n");
    return;
  }
  else
  {
    addr_book->size = 0;
    addr_book->capacity = 0;
    free(addr_book->data);
    printf("销毁成功!\n");
  }

}

//将文件中的内容加载回内存
void AddrBookLoad(AddrBook* addr_book){
  if (addr_book = NULL){
    printf("通讯录为空!\n");
    return;
  }
  FILE* fp = fopen(FILE_PATH,"r");
  if (fp = NULL){
    printf("打开文件失败!\n");
    return;
  }
  while (!feof(fp)){
    if (addr_book->size >= addr_book->capacity){
      AddrBookRelloc(addr_book);
    }
    size_t cur = addr_book->size;
    fscanf(fp, "%s%s%s\n", addr_book->data[i].name, addr_book->data[i].phone, addr_book->data[i].addr);
    addr_book->size++;
  }
  fclose(fp);
}



//===============
//以下为测试代码
//===============

void menu(){
  AddrBook addr_book;
  AddrBookInit(&addr_book);
  AddrBookLoad(&addr_book);

  while (1){
    printf("============1.打印通讯录=============\n");
    printf("============2.增加联系人=============\n");
    printf("============3.删除联系人=============\n");
    printf("============4.查找联系人=============\n");
    printf("============5.修改联系人=============\n");
    printf("============6.销毁通讯录=============\n");
    printf("============0.退出通讯录=============\n");
    int choice = 0;
    printf("请输入所需功能:");
    scanf("%d", &choice);
    switch (choice){
    case DISPLAY:
      AddrBookDisplay(&addr_book);
      break;
    case ADD:
      AddrBookAdd(&addr_book);
      AddrBookSave(&addr_book);
      break;
    case ERASE:
      AddrBookErase(&addr_book);
      break;
    case FIND:
      AddrBookFind(&addr_book);
      break;
    case MODIFY:
      AddrBookModify(&addr_book);
      AddrBookSave(&addr_book);
      break;
    case DESTROY:
      AddrBookDestroy(&addr_book);
      break;
    case EXIT:
      printf("退出通讯录!\n");
      break;
    default:
      printf("请输入正确选择:");
      break;
    }
  }
}

int main(){
  menu();
  return 0;
}

加载全部内容

相关教程
猜你喜欢
用户评论