用c++写一个可以增删查改的通讯录
时间: 2023-12-04 15:02:30 浏览: 34
很抱歉,作为AI语言模型,我无法提供完整的代码。以下是一个通讯录的基本框架,仅供参考。
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NAME_LENGTH 20
#define MAX_PHONE_LENGTH 11
#define MAX_EMAIL_LENGTH 50
#define MAX_ENTRIES 100
typedef struct {
char name[MAX_NAME_LENGTH];
char phone[MAX_PHONE_LENGTH];
char email[MAX_EMAIL_LENGTH];
} Entry;
Entry entries[MAX_ENTRIES];
int num_entries = 0;
void add_entry() {
if (num_entries < MAX_ENTRIES) {
printf("Enter name: ");
scanf("%s", entries[num_entries].name);
printf("Enter phone number: ");
scanf("%s", entries[num_entries].phone);
printf("Enter email address: ");
scanf("%s", entries[num_entries].email);
num_entries++;
printf("Entry added successfully.\n");
} else {
printf("Error: Maximum number of entries reached.\n");
}
}
void print_entry(Entry entry) {
printf("Name: %s\n", entry.name);
printf("Phone number: %s\n", entry.phone);
printf("Email address: %s\n", entry.email);
}
void list_entries() {
if (num_entries == 0) {
printf("No entries found.\n");
} else {
for (int i = 0; i < num_entries; i++) {
printf("Entry #%d:\n", i+1);
print_entry(entries[i]);
printf("\n");
}
}
}
void search_entries() {
char search_name[MAX_NAME_LENGTH];
printf("Enter name to search for: ");
scanf("%s", search_name);
int found = 0;
for (int i = 0; i < num_entries; i++) {
if (strcmp(entries[i].name, search_name) == 0) {
printf("Entry found:\n");
print_entry(entries[i]);
printf("\n");
found = 1;
}
}
if (!found) {
printf("No entry found for %s.\n", search_name);
}
}
void edit_entry() {
char edit_name[MAX_NAME_LENGTH];
printf("Enter name of entry to edit: ");
scanf("%s", edit_name);
int found = 0;
for (int i = 0; i < num_entries; i++) {
if (strcmp(entries[i].name, edit_name) == 0) {
printf("Enter new phone number: ");
scanf("%s", entries[i].phone);
printf("Enter new email address: ");
scanf("%s", entries[i].email);
printf("Entry edited successfully.\n");
found = 1;
}
}
if (!found) {
printf("No entry found for %s.\n", edit_name);
}
}
void delete_entry() {
char delete_name[MAX_NAME_LENGTH];
printf("Enter name of entry to delete: ");
scanf("%s", delete_name);
int found = 0;
for (int i = 0; i < num_entries; i++) {
if (strcmp(entries[i].name, delete_name) == 0) {
for (int j = i; j < num_entries-1; j++) {
entries[j] = entries[j+1];
}
num_entries--;
printf("Entry deleted successfully.\n");
found = 1;
}
}
if (!found) {
printf("No entry found for %s.\n", delete_name);
}
}
int main() {
int choice;
do {
printf("1. Add entry\n");
printf("2. List entries\n");
printf("3. Search entry\n");
printf("4. Edit entry\n");
printf("5. Delete entry\n");
printf("6. Quit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch(choice) {
case 1:
add_entry();
break;
case 2:
list_entries();
break;
case 3:
search_entries();
break;
case 4:
edit_entry();
break;
case 5:
delete_entry();
break;
case 6:
printf("Goodbye!\n");
break;
default:
printf("Invalid choice.\n");
break;
}
} while (choice != 6);
return 0;
}
```
相关推荐
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)