C语言通讯录管理系统
时间: 2023-12-26 16:05:25 浏览: 210
以下是一个简单的C语言通讯录管理系统的代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_CONTACTS 100
typedef struct {
char name[50];
char phone[20];
} Contact;
Contact contacts[MAX_CONTACTS];
int numContacts = 0;
void addContact() {
if (numContacts >= MAX_CONTACTS) {
printf("通讯录已满,无法添加新联系人。\n");
return;
}
Contact newContact;
printf("请输入联系人姓名:");
scanf("%s", newContact.name);
printf("请输入联系人电话:");
scanf("%s", newContact.phone);
contacts[numContacts] = newContact;
numContacts++;
printf("联系人添加成功。\n");
}
void displayContacts() {
if (numContacts == 0) {
printf("通讯录为空。\n");
return;
}
printf("通讯录中的联系人:\n");
for (int i = 0; i < numContacts; i++) {
printf("姓名:%s,电话:%s\n", contacts[i].name, contacts[i].phone);
}
}
void searchContact() {
if (numContacts == 0) {
printf("通讯录为空。\n");
return;
}
char searchName[50];
printf("请输入要查找的联系人姓名:");
scanf("%s", searchName);
int found = 0;
for (int i = 0; i < numContacts; i++) {
if (strcmp(contacts[i].name, searchName) == 0) {
printf("联系人姓名:%s,电话:%s\n", contacts[i].name, contacts[i].phone);
found = 1;
break;
}
}
if (!found) {
printf("未找到该联系人。\n");
}
}
int main() {
int choice;
while (1) {
printf("\n通讯录管理系统\n");
printf("1. 添加联系人\n");
printf("2. 显示所有联系人\n");
printf("3. 查找联系人\n");
printf("4. 退出\n");
printf("请选择操作:");
scanf("%d", &choice);
switch (choice) {
case 1:
addContact();
break;
case 2:
displayContacts();
break;
case 3:
searchContact();
break;
case 4:
printf("感谢使用通讯录管理系统,再见!\n");
exit(0);
default:
printf("无效的选择,请重新输入。\n");
}
}
return 0;
}
```
阅读全文