一、概述
实现一个通讯录;
通讯录可以用来存储1000个人的信息,每个人的信息包括:姓名、性别、年龄、电话、住址
提供方法:
- 添加联系人信息
- 删除指定联系人信息
- 查找指定联系人信息
- 修改指定联系人信息
- 显示所有联系人信息
- 清空所有联系人
- 以名字排序所有联系人
二、定义通讯录结构体
#include<assert.h> #include<string.h> #include<stdio.h> #include<stdlib.h> #define MAX 1000 #define MAX_NAME 10 #define MAX_SEX 6 #define MAX_TELE 12 #define MAX_ADDR 15 struct PeoInfo { char name[MAX_NAME]; char sex[MAX_SEX]; int age; char tele[MAX_TELE]; char adder[MAX_ADDR]; }; struct Contact { int size; struct PeoInfo date[100]; };
三、实现通讯录的功能
void initContact(struct Contact* connect) { assert(connect); connect->size = 0; memset(connect->date, 0, sizeof(struct PeoInfo) * MAX); } void addContact(struct Contact* connect) { assert(connect); if (connect->size == MAX) { printf("通讯录已满,无法继续添加\n"); return; } else { printf("姓名:"); scanf("%s", connect->date[connect->size].name); printf("性别:"); scanf("%s", connect->date[connect->size].sex); printf("年龄:"); scanf("%d", &(connect->date[connect->size].age)); printf("电话号码:"); scanf("%s", connect->date[connect->size].tele); printf("地址:"); scanf("%s", connect->date[connect->size].adder); connect->size++; printf("添加成功!\n"); } } int findContact(const struct Contact* connect, char name[]) { for (int i = 0; i < connect->size; i++) { if (0==strcmp(connect->date[i].name, name)) { return i; } } return -1; } void deleteContact(struct Contact* connect) { char name[MAX_NAME]; printf("请输入要删除信息人的姓名:"); scanf("%s", name); int j = findContact(connect, name); if (j >= 0) { for (int i = j; i < connect->size - 1; i++) { connect->date[j] = connect->date[j + 1]; } connect->size -= 1; printf("删除成功!\n"); } else { printf("通讯录中不存在要删除的人"); } } void searchContact(const struct Contact* connect) { char name[MAX_NAME]; printf("请输入要查找信息人的姓名:"); scanf("%s", name); int i = findContact(connect, name); if (i!=-1) { printf("%-10s\t%-6s\t%-6s\t%-12s\t%-15s\n", "姓名", "性别", "年龄", "电话", "地址"); printf("%-10s\t%-6s\t%-6d\t%-12s\t%-15s\n", connect->date[i].name, connect->date[i].sex, connect->date[i].age, connect->date[i].tele, connect->date[i].adder); } else { printf("通讯录中不存在所查找的人\n"); } } void modifyContact(struct Contact* connect) { char name[MAX_NAME]; printf("请输入要修改信息人的姓名:"); scanf("%s", name); int i = findContact(connect, name); if (i!=-1) { printf("姓名:"); 3; scanf("%s",connect->date[i].name); printf("性别:"); scanf("%s", connect->date[i].sex); printf("年龄:"); scanf("%d", &(connect->date[i].age)); printf("电话号码:"); scanf("%s", connect->date[i].tele); printf("地址:"); scanf("%s", connect->date[i].adder); printf("修改成功!\n"); } else { printf("通讯录中不存在要修改信息的人\n"); } } void listContact(const struct Contact* connect) { printf("%-10s\t%-6s\t%-6s\t%-12s\t%-15s\n", "姓名", "性别", "年龄", "电话", "地址"); for (int i = 0; i < connect->size; i++) { printf("%-10s\t%-6s\t%-6d\t%-12s\t%-15s\n", connect->date[i].name, connect->date[i].sex, connect->date[i].age, connect->date[i].tele, connect->date[i].adder); } } int CmpByName(const void* e1, const void* e2) { return ((struct PeoInfo*)e1)->name - ((struct PeoInfo*)e2)->name; } //按照年龄来排序 void sortContact(struct Contact* connect) { qsort(connect->date, connect->size, sizeof(struct PeoInfo), CmpByName); printf("对姓名排序成功!\n"); listContact(connect); } void emptyContact(struct Contact* connect) { assert(connect); memset(connect->date, 0, sizeof(struct PeoInfo) * MAX); printf("清空成功!\n"); }
四、测试通讯录
#define _CRT_SECURE_NO_WARNINGS 1 #include"contact.h" void menu() { printf("--------------------------------------------------------------------\n"); printf("-----------1.add 2.delete--------------------------\n"); printf("-----------3.search 4.modify--------------------------\n"); printf("-----------5.sort 6.list----------------------------\n"); printf("-----------7.empty 0.exit----------------------------\n"); printf("--------------------------------------------------------------------\n"); } int main() { int input=0; struct Contact connect; initContact(&connect); do { menu(); printf("请输入您要进行的操作:"); scanf("%d", &input); switch (input) { case 1: addContact(&connect); break; case 2: deleteContact(&connect); break; case 3: searchContact(&connect); break; case 4: modifyContact(&connect); break; case 5: sortContact(&connect); break; case 6: listContact(&connect); break; case 7: emptyContact(&connect); break; case 0: printf("退出成功!\n"); break; default: printf("您的输入有误!"); break; } } while (input); return 0; }
对通讯录功能做了简单的实现,希望可以帮助到大家!