通讯录
实现一个通讯录
通讯录可以用来存储1000个人的信息,每个人的信息包括:姓名、性别、年龄、电话、住址
提供方法
添加联系人信息
删除指定联系人信息
查找指定联系人信息
修改指定联系人信息
显示所有联系人信息
清空所有联系人
以名字排序所有联系人
定义结构体
typedef struct contactNode{ //定义联系人结构体
char name[15];
char sex[15];
int age;
char number[20];
char address[100];
};
contactNode nodes[1000]; //全局定义1000个联系人的数组用于存放联系人
int countIndex = 0; //用于控制数组下标
交互界面
void menu(){
while (1){
printf("\n==========通讯录==========\n||");
printf("0.退出系统 |\n||");
printf("1.添加联系人信息 |\n||");
printf("2.删除指定联系人信息 |\n||");
printf("3.查找指定联系人信息 |\n||");
printf("4.修改指定联系人信息 |\n||");
printf("5.显示所有联系人信息 |\n||");
printf("6.清空所有联系人 |\n||");
printf("7.以名字排序所有联系人 |\n");
printf("==========================\n");
int choice;
printf("\n请输入您的选择:");
scanf("%d", &choice);
switch (choice)
{
default:
printf("输入不合法!\n");
break;
case 0:
printf("byebye!!\n");
return;
case 1:
addContactInformation();
break;
case 2:
deleteSpecifiedContactInformation();
break;
case 3:
findSpecifiedContactInformation();
break;
case 4:
modifySpecifiedContactInformation();
break;
case 5:
displayAllContactInformation();
break;
case 6:
emptyAllContacts();
break;
case 7:
sortContactsByName();
break;
}
}
}
1.添加联系人信息
void addContactInformation(){
printf("请输入你要添加的联系人信息\n");
printf("姓名:");
scanf("%s", &nodes[countIndex].name);
printf("性别:");
scanf("%s", &nodes[countIndex].sex);
printf("年龄:");
scanf("%d", &nodes[countIndex].age);
printf("电话号码:");
scanf("%s", &nodes[countIndex].number);
printf("地址:");
scanf("%s", &nodes[countIndex].address);
countIndex++;
printf("联系人添加成功!\n");
}
2.删除指定联系人信息
void deleteSpecifiedContactInformation(){
printf("请输入你要删除的联系人信息\n");
char deleteName[15] ; //用于存储需要删除的联系人姓名
char deleteNumber[20]; //用于存储需要删除的联系人电话号码
printf("姓名:");
scanf("%s", &deleteName);
printf("电话号码:");
scanf("%s",&deleteNumber);
int tempIndex = -1; //用于存储需要删除的联系人在数组中的的下标
int i;
for (i = 0; i < countIndex; i++) //寻找需要删除的联系人
{
if (strcmp(deleteName, nodes[i].name) == 0 && strcmp(deleteNumber, nodes[i].number) == 0){
tempIndex = i;
break;
}
}
if (tempIndex == -1){
printf("无此联系人!\n");
}
else{
for (int j = tempIndex; j < countIndex - 1; j++){ //将该联系人之后所有联系人前移一个位置,将需要删除的联系人覆盖
strcpy(nodes[j].name, nodes[j + 1].name);
strcpy(nodes[j].sex, nodes[j + 1].sex);
nodes[j].age = nodes[j + 1].age;
strcpy(nodes[j].number, nodes[j + 1].number);
strcpy(nodes[j].address, nodes[j + 1].address);
}
countIndex--; //联系人总数减1
printf("删除成功!");
}
}
3.查找指定联系人信息
void findSpecifiedContactInformation(){
printf("请输入你要查询的联系人信息\n");
char findName[15]; //用于存储需要查询的联系人姓名
char findNumber[20]; //用于存储需要查询的联系人电话号码
printf("姓名:");
scanf("%s", &findName);
printf("电话号码:");
scanf("%s", &findNumber);
int tempIndex = -1; //用于存储需要删除的联系人在数组中的的下标
int i;
for (i = 0; i < countIndex; i++) //寻找需要删除的联系人
{
if (strcmp(findName, nodes[i].name) == 0 && strcmp(findNumber, nodes[i].number) == 0){
tempIndex = i;
break;
}
}
if (tempIndex == -1){
printf("无此联系人!\n");
}
else{
printf("=========================================================================================\n");
printf("|Name\t|Sex\t|Age\t|Tel\t\t\t|Address\t\t\t\n");
printf("-----------------------------------------------------------------------------------------\n");
printf("|%s\t|%s\t|%d\t|%s\t\t\t|%s\t\t\t\n", nodes[tempIndex].name, nodes[tempIndex].sex, nodes[tempIndex].age, nodes[tempIndex].number, nodes[tempIndex].address);
printf("-----------------------------------------------------------------------------------------\n");
printf("=========================================================================================\n");
printf("查询成功!\n");
}
}
4.修改指定联系人信息
void modifySpecifiedContactInformation(){
printf("请输入你要修改的联系人信息\n");
char findName[15]; //用于存储需要查询的联系人姓名
char findNumber[20]; //用于存储需要查询的联系人电话号码
printf("待修改联系人姓名:");
scanf("%s", &findName);
printf("待修改联系人电话号码:");
scanf("%s", &findNumber);
int tempIndex = -1; //用于存储需要修改的联系人在数组中的的下标
int i;
for (i = 0; i < countIndex; i++) //寻找需要修改的联系人
{
if (strcmp(findName, nodes[i].name) == 0 && strcmp(findNumber, nodes[i].number) == 0){
tempIndex = i;
break;
}
}
if (tempIndex == -1){
printf("无此联系人!\n");
}
else{
printf("修改后的姓名:");
scanf("%s", &nodes[tempIndex].name);
printf("修改后的性别:");
scanf("%s", &nodes[tempIndex].sex);
printf("修改后的年龄:");
scanf("%d", &nodes[tempIndex].age);
printf("修改后的电话号码:");
scanf("%s", &nodes[tempIndex].number);
printf("修改后的地址:");
scanf("%s", &nodes[tempIndex].address);
printf("修改成功!");
}
}
5.显示所有联系人信息
void displayAllContactInformation(){
printf("=========================================================================================\n");
printf("|Name\t|Sex\t|Age\t|Tel\t\t\t|Address\t\t\t\n");
printf("-----------------------------------------------------------------------------------------\n");
for (int i = 0; i < countIndex; i++)
{
printf("|%s\t|%s\t|%d\t|%s\t\t\t|%s\t\t\t\n", nodes[i].name, nodes[i].sex, nodes[i].age, nodes[i].number, nodes[i].address);
printf("-----------------------------------------------------------------------------------------\n");
}
printf("=========================================================================================\n");
}
6.清空所有联系人
void emptyAllContacts(){
countIndex = 0;
printf("通讯录已清空!\n");
}
清空后的通讯录:
7.以名字排序所有联系人
void sortContactsByName(){
for (int i = 0; i < countIndex; i++)
{
for (int j = i; j < countIndex; j++){
if (strcmp(nodes[i].name, nodes[j].name) > 0){
contactNode tempnode;
strcpy(tempnode.name, nodes[i].name);
strcpy(tempnode.sex, nodes[i].sex);
tempnode.age = nodes[i].age;
strcpy(tempnode.number, nodes[i].number);
strcpy(tempnode.address, nodes[i].address);
strcpy(nodes[i].name, nodes[j].name);
strcpy(nodes[i].sex, nodes[j].sex);
nodes[i].age = nodes[j].age;
strcpy(nodes[i].number, nodes[j].number);
strcpy(nodes[i].address, nodes[j].address);
strcpy(nodes[j].name, tempnode.name);
strcpy(nodes[j].sex, tempnode.sex);
nodes[j].age = tempnode.age;
strcpy(nodes[j].number, tempnode.number);
strcpy(nodes[j].address, tempnode.address);
}
}
}
printf("已升序排列!\n");
}
main函数
int main(){
menu();
system("pause");
return 0;
}
所有源代码
#include <stdio.h>
#include <string.h>
#include <Windows.h>
#pragma warning(disable:4996)
typedef struct contactNode{ //定义联系人结构体
char name[15];
char sex[15];
int age;
char number[20];
char address[100];
};
contactNode nodes[1000]; //全局定义1000个联系人的数组用于存放联系人
int countIndex = 0; //用于控制数组下标
void addContactInformation(){
printf("请输入你要添加的联系人信息\n");
printf("姓名:");
scanf("%s", &nodes[countIndex].name);
printf("性别:");
scanf("%s", &nodes[countIndex].sex);
printf("年龄:");
scanf("%d", &nodes[countIndex].age);
printf("电话号码:");
scanf("%s", &nodes[countIndex].number);
printf("地址:");
scanf("%s", &nodes[countIndex].address);
countIndex++;
printf("联系人添加成功!\n");
}
void deleteSpecifiedContactInformation(){
printf("请输入你要删除的联系人信息\n");
char deleteName[15] ; //用于存储需要删除的联系人姓名
char deleteNumber[20]; //用于存储需要删除的联系人电话号码
printf("姓名:");
scanf("%s", &deleteName);
printf("电话号码:");
scanf("%s",&deleteNumber);
int tempIndex = -1; //用于存储需要删除的联系人在数组中的的下标
int i;
for (i = 0; i < countIndex; i++) //寻找需要删除的联系人
{
if (strcmp(deleteName, nodes[i].name) == 0 && strcmp(deleteNumber, nodes[i].number) == 0){
tempIndex = i;
break;
}
}
if (tempIndex == -1){
printf("无此联系人!\n");
}
else{
for (int j = tempIndex; j < countIndex - 1; j++){ //将该联系人之后所有联系人前移一个位置,将需要删除的联系人覆盖
strcpy(nodes[j].name, nodes[j + 1].name);
strcpy(nodes[j].sex, nodes[j + 1].sex);
nodes[j].age = nodes[j + 1].age;
strcpy(nodes[j].number, nodes[j + 1].number);
strcpy(nodes[j].address, nodes[j + 1].address);
}
countIndex--; //联系人总数减1
printf("删除成功!");
}
}
void findSpecifiedContactInformation(){
printf("请输入你要查询的联系人信息\n");
char findName[15]; //用于存储需要查询的联系人姓名
char findNumber[20]; //用于存储需要查询的联系人电话号码
printf("姓名:");
scanf("%s", &findName);
printf("电话号码:");
scanf("%s", &findNumber);
int tempIndex = -1; //用于存储需要删除的联系人在数组中的的下标
int i;
for (i = 0; i < countIndex; i++) //寻找需要删除的联系人
{
if (strcmp(findName, nodes[i].name) == 0 && strcmp(findNumber, nodes[i].number) == 0){
tempIndex = i;
break;
}
}
if (tempIndex == -1){
printf("无此联系人!\n");
}
else{
printf("=========================================================================================\n");
printf("|Name\t|Sex\t|Age\t|Tel\t\t\t|Address\t\t\t\n");
printf("-----------------------------------------------------------------------------------------\n");
printf("|%s\t|%s\t|%d\t|%s\t\t\t|%s\t\t\t\n", nodes[tempIndex].name, nodes[tempIndex].sex, nodes[tempIndex].age, nodes[tempIndex].number, nodes[tempIndex].address);
printf("-----------------------------------------------------------------------------------------\n");
printf("=========================================================================================\n");
printf("查询成功!\n");
}
}
void modifySpecifiedContactInformation(){
printf("请输入你要修改的联系人信息\n");
char findName[15]; //用于存储需要查询的联系人姓名
char findNumber[20]; //用于存储需要查询的联系人电话号码
printf("待修改联系人姓名:");
scanf("%s", &findName);
printf("待修改联系人电话号码:");
scanf("%s", &findNumber);
int tempIndex = -1; //用于存储需要修改的联系人在数组中的的下标
int i;
for (i = 0; i < countIndex; i++) //寻找需要修改的联系人
{
if (strcmp(findName, nodes[i].name) == 0 && strcmp(findNumber, nodes[i].number) == 0){
tempIndex = i;
break;
}
}
if (tempIndex == -1){
printf("无此联系人!\n");
}
else{
printf("修改后的姓名:");
scanf("%s", &nodes[tempIndex].name);
printf("修改后的性别:");
scanf("%s", &nodes[tempIndex].sex);
printf("修改后的年龄:");
scanf("%d", &nodes[tempIndex].age);
printf("修改后的电话号码:");
scanf("%s", &nodes[tempIndex].number);
printf("修改后的地址:");
scanf("%s", &nodes[tempIndex].address);
printf("修改成功!");
}
}
void emptyAllContacts(){
countIndex = 0;
printf("通讯录已清空!\n");
}
void displayAllContactInformation(){
printf("=========================================================================================\n");
printf("|Name\t|Sex\t|Age\t|Tel\t\t\t|Address\t\t\t\n");
printf("-----------------------------------------------------------------------------------------\n");
for (int i = 0; i < countIndex; i++)
{
printf("|%s\t|%s\t|%d\t|%s\t\t\t|%s\t\t\t\n", nodes[i].name, nodes[i].sex, nodes[i].age, nodes[i].number, nodes[i].address);
printf("-----------------------------------------------------------------------------------------\n");
}
printf("=========================================================================================\n");
}
void sortContactsByName(){
for (int i = 0; i < countIndex; i++)
{
for (int j = i; j < countIndex; j++){
if (strcmp(nodes[i].name, nodes[j].name) > 0){
contactNode tempnode;
strcpy(tempnode.name, nodes[i].name);
strcpy(tempnode.sex, nodes[i].sex);
tempnode.age = nodes[i].age;
strcpy(tempnode.number, nodes[i].number);
strcpy(tempnode.address, nodes[i].address);
strcpy(nodes[i].name, nodes[j].name);
strcpy(nodes[i].sex, nodes[j].sex);
nodes[i].age = nodes[j].age;
strcpy(nodes[i].number, nodes[j].number);
strcpy(nodes[i].address, nodes[j].address);
strcpy(nodes[j].name, tempnode.name);
strcpy(nodes[j].sex, tempnode.sex);
nodes[j].age = tempnode.age;
strcpy(nodes[j].number, tempnode.number);
strcpy(nodes[j].address, tempnode.address);
}
}
}
printf("已升序排列!\n");
}
void menu(){
while (1){
printf("\n==========通讯录==========\n||");
printf("0.退出系统 |\n||");
printf("1.添加联系人信息 |\n||");
printf("2.删除指定联系人信息 |\n||");
printf("3.查找指定联系人信息 |\n||");
printf("4.修改指定联系人信息 |\n||");
printf("5.显示所有联系人信息 |\n||");
printf("6.清空所有联系人 |\n||");
printf("7.以名字排序所有联系人 |\n");
printf("==========================\n");
int choice;
printf("\n请输入您的选择:");
scanf("%d", &choice);
switch (choice)
{
default:
printf("输入不合法!\n");
break;
case 0:
printf("byebye!!\n");
return;
case 1:
addContactInformation();
break;
case 2:
deleteSpecifiedContactInformation();
break;
case 3:
findSpecifiedContactInformation();
break;
case 4:
modifySpecifiedContactInformation();
break;
case 5:
displayAllContactInformation();
break;
case 6:
emptyAllContacts();
break;
case 7:
sortContactsByName();
break;
}
}
}
int main(){
menu();
system("pause");
return 0;
}