C语言练习:通讯录的实现-创新互联
文章目录
网页名称:C语言练习:通讯录的实现-创新互联
转载来源:http://scjbc.cn/article/cedsoe.html
- 1.test.c
- 2.Contacts.c
- 3.Contacts.h
作用:函数的测试
#define _CRT_SECURE_NO_WARNINGS 1
#include"Contacts.h"
void menu() {printf("*********************************************\n");
printf("******** 1.add 2.del ********\n");
printf("******** 3.query 4.modif ********\n");
printf("******** 5.show 6.empty ********\n");
printf("******** 7.sort 0.exit ********\n");
printf("********************************************\n");
}
int main() {int input = 0;
Contacts con;
initContaces(&con);
do {menu();
scanf("%d", &input);
switch (input) {case 1:
addContacts(&con);
break;
case 2:
delContacts(&con);
break;
case 3:
queryContacts(&con);
break;
case 4:
modifContacts(&con);
break;
case 5:
showContacts(&con);
break;
case 6:
emptyContacts(&con);
break;
case 7:
sortContacts(&con);
break;
case 0:
printf("退出通讯录\n");
break;
default:
printf("输入错误\n");
break;
}
} while (input);
return 0;
}
2.Contacts.c作用:函数体的实现
#define _CRT_SECURE_NO_WARNINGS 1
#include"Contacts.h"
void initContaces(Contacts* con) {assert(con);
con->sz = 0;
memset(con->date, 0, sizeof(con->date));
}
void addContacts(Contacts* con) {assert(con);
if (con->sz == 100) {printf("通讯录已满无法添加\n");
return;
}
printf("请输入要添加联系人姓名:>");
scanf("%s", con->date[con->sz].name);
printf("请输入要添加联系人年龄:>");
scanf("%d", &(con->date[con->sz].age));
printf("请输入要添加联系人性别:>");
scanf("%s", con->date[con->sz].sex);
printf("请输入要添加联系人地址:>");
scanf("%s", con->date[con->sz].addr);
printf("请输入要添加联系人电话号:>");
scanf("%s", con->date[con->sz].tel);
con->sz++;
}
void showContacts(Contacts* con) {assert(con);
int i = 0;
printf("%-20s\t%-4s\t%-5s\t%-30s\t%-12s\n ", "姓名","年龄","性别","地址","电话号");
for (i = 0; i< con->sz; i++) {printf("%-20s\t%-4d\t%-5s\t%-30s\t%-12s\n ", con->date[i].name,
con->date[i].age,
con->date[i].sex,
con->date[i].addr,
con->date[i].tel);
}
}
int findContacts(Contacts* con, char* name) {int i = 0;
int ret = 0;
for (i = 0; i< con->sz; i++) {if (strcmp(name, con->date[i].name) == 0) { ret = i;
return ret;
}
}
return -1;
}
void delContacts(Contacts* con) {assert(con);
char name[20];
printf("请输入要删除联系人的姓名\n");
scanf("%s", name);
int temp=findContacts(con, name);
if (-1 == temp) {printf("找不到姓名为%s的联系人\n", name);
return;
}
else {for (int i = temp; i< con->sz - 1; i++) { con->date[i] = con->date[i + 1];
}
con->sz--;
}
}
void queryContacts(Contacts* con) {printf("请输入要查找的联系人的姓名\n");
char name[20];
scanf("%s", name);
int temp = findContacts(con, name);
if (-1 == temp) {printf("找不到姓名为%s的联系人\n", name);
return;
}
else {printf("%-20s\t%-4s\t%-5s\t%-30s\t%-12s\n ", "姓名", "年龄", "性别", "地址", "电话号");
printf("%-20s\t%-4d\t%-5s\t%-30s\t%-12s\n ", con->date[temp].name,
con->date[temp].age,
con->date[temp].sex,
con->date[temp].addr,
con->date[temp].addr);
}
}
void modifContacts(Contacts* con) {printf("请输入要修改的联系人的姓名\n");
char name[20];
scanf("%s", name);
int temp = findContacts(con, name);
if (-1 == temp) {printf("找不到姓名为%s的联系人\n", name);
return;
}
else {printf("请输入修改后联系人姓名:>");
scanf("%s", con->date[temp].name);
printf("请输入修改后联系人年龄:>");
scanf("%d", &(con->date[temp].age));
printf("请输入修改后联系人性别:>");
scanf("%s", con->date[temp].sex);
printf("请输入修改后联系人地址:>");
scanf("%s", con->date[temp].addr);
printf("请输入修改后联系人电话号:>");
scanf("%s", con->date[temp].tel);
}
}
int cmp(const void* e1, const void* e2) {Contacts* c1 = (Contacts*)e1;
Contacts* c2 = (Contacts*)e2;
return strcmp(c1->date->name, c2->date->name);
}
void sortContacts(Contacts* con) {qsort(con, con->sz, sizeof(con->date[0]), cmp);
}
void emptyContacts(Contacts* con) {con->sz = 0;
}
3.Contacts.h作用:函数的声明
#pragma once
#include#include#include
#define MAX 100
#define NAME_MAX 20
#define SEX_MAX 5
#define ADDR_MAX 30
#define TEL_MAX 12
typedef struct people {char name[NAME_MAX];
int age;
char sex[SEX_MAX];
char addr[ADDR_MAX];
char tel[TEL_MAX];
}people;
typedef struct Contacts {people date[MAX];
int sz;
}Contacts;
//初始化结构体
void initContaces(Contacts* con);
//添加
void addContacts(Contacts* con);
//展示
void showContacts(Contacts* con);
//删除
void delContacts(Contacts* con);
//查询
void queryContacts(Contacts* con);
//修改
void modifContacts(Contacts* con);
//排序
void sortContacts(Contacts* con);
void emptyContacts(Contacts* con);
你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧
网页名称:C语言练习:通讯录的实现-创新互联
转载来源:http://scjbc.cn/article/cedsoe.html