请用c语言写一个通讯录
时间: 2023-04-04 07:02:04 浏览: 91
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NAME_LEN 20
#define MAX_PHONE_LEN 11
#define MAX_EMAIL_LEN 30
#define MAX_CONTACTS 100
typedef struct {
char name[MAX_NAME_LEN];
char phone[MAX_PHONE_LEN];
char email[MAX_EMAIL_LEN];
} Contact;
Contact contacts[MAX_CONTACTS];
int numContacts = ;
void addContact() {
if (numContacts >= MAX_CONTACTS) {
printf("通讯录已满,无法添加新联系人!\n");
return;
}
Contact newContact;
printf("请输入联系人姓名:");
scanf("%s", newContact.name);
printf("请输入联系人电话:");
scanf("%s", newContact.phone);
printf("请输入联系人邮箱:");
scanf("%s", newContact.email);
contacts[numContacts++] = newContact;
printf("联系人已添加成功!\n");
}
void searchContact() {
char name[MAX_NAME_LEN];
printf("请输入要查找的联系人姓名:");
scanf("%s", name);
for (int i = ; i < numContacts; i++) {
if (strcmp(name, contacts[i].name) == ) {
printf("联系人姓名:%s\n", contacts[i].name);
printf("联系人电话:%s\n", contacts[i].phone);
printf("联系人邮箱:%s\n", contacts[i].email);
return;
}
}
printf("未找到该联系人!\n");
}
void listContacts() {
printf("通讯录中共有 %d 个联系人:\n", numContacts);
for (int i = ; i < numContacts; i++) {
printf("%d. 姓名:%s,电话:%s,邮箱:%s\n", i + 1, contacts[i].name, contacts[i].phone, contacts[i].email);
}
}
void deleteContact() {
char name[MAX_NAME_LEN];
printf("请输入要删除的联系人姓名:");
scanf("%s", name);
for (int i = ; i < numContacts; i++) {
if (strcmp(name, contacts[i].name) == ) {
for (int j = i; j < numContacts - 1; j++) {
contacts[j] = contacts[j + 1];
}
numContacts--;
printf("联系人已删除成功!\n");
return;
}
}
printf("未找到该联系人!\n");
}
int main() {
int choice;
while (1) {
printf("请选择要进行的操作:\n");
printf("1. 添加联系人\n");
printf("2. 查找联系人\n");
printf("3. 列出所有联系人\n");
printf("4. 删除联系人\n");
printf("5. 退出程序\n");
printf("请输入操作编号:");
scanf("%d", &choice);
switch (choice) {
case 1:
addContact();
break;
case 2:
searchContact();
break;
case 3:
listContacts();
break;
case 4:
deleteContact();
break;
case 5:
printf("感谢使用通讯录程序,再见!\n");
exit();
default:
printf("无效的操作编号,请重新输入!\n");
}
}
return ;
}
阅读全文