c语言构建简单的手机通讯录,要求最多容纳50名联系人的信息,可查询联系人和新建联系人的源代码
时间: 2023-03-24 16:04:00 浏览: 149
以下是 C 语言构建简单的手机通讯录,最多容纳50名联系人的信息,可查询联系人和新建联系人的源代码:
#include <stdio.h>
#include <string.h>
struct Contact {
char name[20];
char phone[20];
};
int main() {
struct Contact contacts[50];
int numContacts = ;
int choice;
do {
printf("\n\n");
printf("1. 添加联系人\n");
printf("2. 查询联系人\n");
printf("3. 退出\n");
printf("请选择操作:");
scanf("%d", &choice);
switch (choice) {
case 1:
if (numContacts < 50) {
printf("\n请输入联系人姓名:");
scanf("%s", contacts[numContacts].name);
printf("请输入联系人电话:");
scanf("%s", contacts[numContacts].phone);
numContacts++;
printf("\n联系人添加成功!\n");
} else {
printf("\n通讯录已满,无法添加联系人!\n");
}
break;
case 2:
if (numContacts > ) {
char searchName[20];
printf("\n请输入要查询的联系人姓名:");
scanf("%s", searchName);
int found = ;
for (int i = ; i < numContacts; i++) {
if (strcmp(searchName, contacts[i].name) == ) {
printf("\n联系人姓名:%s\n", contacts[i].name);
printf("联系人电话:%s\n", contacts[i].phone);
found = 1;
break;
}
}
if (!found) {
printf("\n未找到该联系人!\n");
}
} else {
printf("\n通讯录为空,无法查询联系人!\n");
}
break;
case 3:
printf("\n感谢使用手机通讯录!\n");
break;
default:
printf("\n无效的操作,请重新选择!\n");
break;
}
} while (choice != 3);
return ;
}
阅读全文