目录
背景:VS2019编译器
创建文件:
contact.h代码:
test.c代码:
contact.c代码:
背景:VS2019编译器
创建文件:
contact.h代码:
#pragma once#include <string.h>
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include <windows.h>//类型声明
typedef struct PeoInfo
{char name[20];int age;char sex[10];char tele[12];char addr[30];
}PeoInfo;//通讯录
typedef struct Contact
{PeoInfo data[100];//存放人的信息int count; //记录当前通讯录中实际存储人的信息个数。
}Contact;//初始化通讯录
void InitContact(Contact* pc);//增加通讯录信息
void AddContact(Contact* pc);//显示通讯录信息
void ShowContact(Contact* pc);//删除通讯录信息
void DelContact(Contact* pc);//查找通讯录信息
void SearchContact(Contact* pc);//修改通讯录信息
void ModifyContact(Contact* pc);//排序通讯录信息
void SortContact(Contact* pc);
test.c代码:
#define _CRT_SECURE_NO_WARNINGS
//实现通讯录
//测试文件
#include "contact.h"
void menu()
{printf("*************************************\n");printf("****** 1. add 2. del ******\n");printf("****** 3. search 4. modify ******\n");printf("****** 5. show 6. sort ******\n");printf("****** 0. exit ******\n");printf("*************************************\n");
}
int main()
{int input = 0;Contact con; //通讯录//初始化通讯录InitContact(&con);do{menu();printf("请选择:>");scanf("%d", &input);switch (input){case 1: //增加AddContact(&con);break;case 2: //删除DelContact(&con);break;case 3: //查找SearchContact(&con);break; case 4: //修改ModifyContact(&con);break;case 5: //显示ShowContact(&con);break;case 6: //排序SortContact(&con);break;case 0:printf("退出通讯录\n");break;default:printf("选择错误,请重新输入\n");break;}} while(input);return 0;
}
contact.c代码:
#define _CRT_SECURE_NO_WARNINGS
//通讯录模块文件
#include "contact.h"#define MAX 100void InitContact(Contact* pc)
{assert(pc);pc->count = 0;memset(pc->data, 0, sizeof(pc->data));
}void AddContact(Contact* pc)
{assert(pc);if (pc->count == MAX){printf("通讯录已满,无法添加\n");return;}//printf("请输入名字:>");scanf("%s", pc->data[pc->count].name);printf("请输入年龄:>");scanf("%d", &pc->data[pc->count].age);printf("请输入性别:>");scanf("%s", pc->data[pc->count].sex);printf("请输入电话:>");scanf("%s", pc->data[pc->count].tele);printf("请输入地址:>");scanf("%s", pc->data[pc->count].addr);pc->count++;printf("增加成功\n");
}void ShowContact(Contact* pc)
{assert(pc);printf("%-20s\t%-5s\t%-5s\t%-12s\t%-30s\n", "姓名", "年龄", "性别", "电话", "地址");int i = 0;for (i = 0;i < pc->count;i++){printf("%-20s\t", pc->data[i].name);printf("%-5d\t", pc->data[i].age);printf("%-5s\t", pc->data[i].sex);printf("%-12s\t", pc->data[i].tele);printf("%-30s\n", pc->data[i].addr);}
}static int FindName(Contact* pc,char name[])
{assert(pc);int i = 0;for (i = 0;i < pc->count;i++){if (0 == strcmp(pc->data[i].name, name)){return i;}}return -1;
}void DelContact(Contact* pc)
{assert(pc);printf("请输入要删除的人名:>");char name[20] = { 0 };scanf("%s", name);int flag = FindName(pc, name);if (flag == -1){printf("要删除的信息不存在\n");return;}else{int i = 0;for (i = flag;i < pc->count - 1;i++){pc->data[i] = pc->data[i + 1];}pc->count--;printf("删除成功。\n");}}void SearchContact(Contact* pc)
{assert(pc);printf("请输入要查找人的名字:");char name[20] = { 0 };scanf("%s", name);//查找int flag = FindName(pc, name);if (flag == -1){printf("找不到该信息。\n");return;}else{printf("%-20s\t%-5d\t%-5s\t%-12s\t%-30s\n",pc->data[flag].name,pc->data[flag].age, pc->data[flag].sex, pc->data[flag].tele, pc->data[flag].addr);}
}void ModifyContact(Contact* pc)
{assert(pc);printf("请输入要修改的人名:");char name[20] = { 0 };scanf("%s", name);//查找int flag = FindName(pc, name);if (flag == -1){printf("找不到该信息。\n");return;}else{printf("请输入修改后的名字:>");scanf("%s", pc->data[flag].name);printf("请输入修改后的年龄:>");scanf("%d", &pc->data[flag].age);printf("请输入修改后的性别:>");scanf("%s", pc->data[flag].sex);printf("请输入修改后的电话:>");scanf("%s", pc->data[flag].tele);printf("请输入修改后的地址:>");scanf("%s", pc->data[flag].addr);printf("修改成功\n");}
}void SortContact(Contact* pc)
{assert(pc);//冒泡排序,按名字升序int i = 0;int j = 0;PeoInfo C = { 0 };for (i = 0;i < pc->count;i++){for (j = 1;j < pc->count - i;j++){if (strcmp(pc->data[j - 1].name, pc->data[j].name) > 0){C = pc->data[j - 1];pc->data[j - 1] = pc->data[j];pc->data[j] = C;}}}printf("排序成功。");
}