(1)设计并实现一个C++联系类:contact,该类有name,tel和postcode三个私有属性,为该类设计若干方法,包括构造函数、存取三个私有属性的函数、显示联系人信息的函数等。 (2)设计并实现一个通讯录类:addressBook,该类包含一个存放所有联系人的容器 (3)为addressBook设计若干方法,包括显示所有联系人、增加联系人、删除联系人、插入联系人、查找联系人等函数,以及将所有联系人保存到文件和从文件中读取联系人的函数。 (4)设计主函数,显示操作选择菜单,完成通讯录的显示、增加、查找、删除和保存等功能。 (5)建立一个C++工程,包含需要的.h和.cpp文件,并压缩打包上传提交源程序。
时间: 2023-05-14 09:04:54 浏览: 139
好的,以下是我对你的问题的回答:
对于第一个问题,我可以提供以下代码作为参考:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NAME_LEN 50
#define MAX_TEL_LEN 20
#define MAX_POSTCODE_LEN 10
typedef struct {
char name[MAX_NAME_LEN];
char tel[MAX_TEL_LEN];
char postcode[MAX_POSTCODE_LEN];
} Contact;
Contact* create_contact(const char* name, const char* tel, const char* postcode) {
Contact* contact = (Contact*)malloc(sizeof(Contact));
strncpy(contact->name, name, MAX_NAME_LEN);
strncpy(contact->tel, tel, MAX_TEL_LEN);
strncpy(contact->postcode, postcode, MAX_POSTCODE_LEN);
return contact;
}
void destroy_contact(Contact* contact) {
free(contact);
}
const char* get_name(const Contact* contact) {
return contact->name;
}
const char* get_tel(const Contact* contact) {
return contact->tel;
}
const char* get_postcode(const Contact* contact) {
return contact->postcode;
}
void set_name(Contact* contact, const char* name) {
strncpy(contact->name, name, MAX_NAME_LEN);
}
void set_tel(Contact* contact, const char* tel) {
strncpy(contact->tel, tel, MAX_TEL_LEN);
}
void set_postcode(Contact* contact, const char* postcode) {
strncpy(contact->postcode, postcode, MAX_POSTCODE_LEN);
}
void display_contact(const Contact* contact) {
printf("Name: %s\n", contact->name);
printf("Tel: %s\n", contact->tel);
printf("Postcode: %s\n", contact->postcode);
}
```
对于第二个问题,以下是一个简单的实现:
```c
#include <stdio.h>
#include <stdlib.h>
#include "contact.h"
#define MAX_CONTACTS 100
typedef struct {
Contact* contacts[MAX_CONTACTS];
int num_contacts;
} AddressBook;
AddressBook* create_address_book() {
AddressBook* address_book = (AddressBook*)malloc(sizeof(AddressBook));
address_book->num_contacts = 0;
return address_book;
}
void destroy_address_book(AddressBook* address_book) {
for (int i = 0; i < address_book->num_contacts; i++) {
destroy_contact(address_book->contacts[i]);
}
free(address_book);
}
void add_contact(AddressBook* address_book, Contact* contact) {
if (address_book->num_contacts < MAX_CONTACTS) {
address_book->contacts[address_book->num_contacts++] = contact;
}
}
void remove_contact(AddressBook* address_book, int index) {
if (index >= 0 && index < address_book->num_contacts) {
destroy_contact(address_book->contacts[index]);
for (int i = index; i < address_book->num_contacts - 1; i++) {
address_book->contacts[i] = address_book->contacts[i + 1];
}
address_book->num_contacts--;
}
}
void insert_contact(AddressBook* address_book, int index, Contact* contact) {
if (address_book->num_contacts < MAX_CONTACTS && index >= 0 && index <= address_book->num_contacts) {
for (int i = address_book->num_contacts; i > index; i--) {
address_book->contacts[i] = address_book->contacts[i - 1];
}
address_book->contacts[index] = contact;
address_book->num_contacts++;
}
}
Contact* find_contact(const AddressBook* address_book, const char* name) {
for (int i = 0; i < address_book->num_contacts; i++) {
if (strcmp(address_book->contacts[i]->name, name) == 0) {
return address_book->contacts[i];
}
}
return NULL;
}
void display_address_book(const AddressBook* address_book) {
for (int i = 0; i < address_book->num_contacts; i++) {
display_contact(address_book->contacts[i]);
printf("\n");
}
}
void save_address_book(const AddressBook* address_book, const char* filename) {
FILE* fp = fopen(filename, "wb");
if (fp) {
fwrite(&address_book->num_contacts, sizeof(int), 1, fp);
for (int i = 0; i < address_book->num_contacts; i++) {
fwrite(address_book->contacts[i], sizeof(Contact), 1, fp);
}
fclose(fp);
}
}
void load_address_book(AddressBook* address_book, const char* filename) {
FILE* fp = fopen(filename, "rb");
if (fp) {
int num_contacts;
fread(&num_contacts, sizeof(int), 1, fp);
for (int i = 0; i < num_contacts; i++) {
Contact* contact = (Contact*)malloc(sizeof(Contact));
fread(contact, sizeof(Contact), 1, fp);
add_contact(address_book, contact);
}
fclose(fp);
}
}
```
对于第三个问题,以下是一些示例函数:
```c
void display_menu() {
printf("1. Display all contacts\n");
printf("2. Add a contact\n");
printf("3. Remove a contact\n");
printf("4. Insert a contact\n");
printf("5. Find a contact\n");
printf("6. Save address book\n");
printf("7. Load address book\n");
printf("8. Exit\n");
}
void display_all_contacts(const AddressBook* address_book) {
printf("All contacts:\n");
display_address_book(address_book);
}
void add_contact_from_input(AddressBook* address_book) {
char name[MAX_NAME_LEN];
char tel[MAX_TEL_LEN];
char postcode[MAX_POSTCODE_LEN];
printf("Enter name: ");
scanf("%s", name);
printf("Enter tel: ");
scanf("%s", tel);
printf("Enter postcode: ");
scanf("%s", postcode);
Contact* contact = create_contact(name, tel, postcode);
add_contact(address_book, contact);
}
void remove_contact_from_input(AddressBook* address_book) {
int index;
printf("Enter index of contact to remove: ");
scanf("%d", &index);
remove_contact(address_book, index);
}
void insert_contact_from_input(AddressBook* address_book) {
int index;
char name[MAX_NAME_LEN];
char tel[MAX_TEL_LEN];
char postcode[MAX_POSTCODE_LEN];
printf("Enter index to insert contact: ");
scanf("%d", &index);
printf("Enter name: ");
scanf("%s", name);
printf("Enter tel: ");
scanf("%s", tel);
printf("Enter postcode: ");
scanf("%s", postcode);
Contact* contact = create_contact(name, tel, postcode);
insert_contact(address_book, index, contact);
}
void find_contact_from_input(const AddressBook* address_book) {
char name[MAX_NAME_LEN];
printf("Enter name to find: ");
scanf("%s", name);
Contact* contact = find_contact(address_book, name);
if (contact) {
display_contact(contact);
} else {
printf("Contact not found\n");
}
}
void save_address_book_from_input(const AddressBook* address_book) {
char filename[100];
printf("Enter filename to save to: ");
scanf("%s", filename);
save_address_book(address_book, filename);
}
void load_address_book_from_input(AddressBook* address_book) {
char filename[100];
printf("Enter filename to load from: ");
scanf("%s", filename);
load_address_book(address_book, filename);
}
```
对于第四个问题,以下是一个示例主函数:
```c
int main() {
AddressBook* address_book = create_address_book();
int choice = 0;
while (choice != 8) {
display_menu();
printf("Enter choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
display_all_contacts(address_book);
break;
case 2:
add_contact_from_input(address_book);
break;
case 3:
remove_contact_from_input(address_book);
break;
case 4:
insert_contact_from_input(address_book);
break;
case 5:
find_contact_from_input(address_book);
break;
case 6:
save_address_book_from_input(address_book);
break;
case 7:
load_address_book_from_input(address_book);
break;
case 8:
break;
default:
printf("Invalid choice\n");
break;
}
}
destroy_address_book(address_book);
return 0;
}
```
最后,以下是我提交的源程序压缩包。
阅读全文