1)设计并实现一个C++联系人类:contact,该类有name,tel和postcode三个私有属性,为该类设计若干方法,包括构造函数、存取三个私有属性的函数、显示联系人信息的函数等。 (2)设计并实现一个通讯录类:addressBook,该类包含一个存放所有联系人的容器,使用数组,用CArray类实现, (3)为addressBook设计若干方法,包括显示所有联系人、增加联系人、删除联系人、插入联系人、查找联系人等函数,以及将所有联系人保存到文件和从文件中读取联系人的函数。 (4)设计主函数,显示操作选择菜单,完成通讯录的显示、增加、查找、删除和保存等功能。 (5)建立一个C++工程,包含需要的.h和.cpp文件
时间: 2023-05-18 14:02:32 浏览: 95
课程设计 通讯录用c++做的
以下是我为您提供的答案:
1)设计并实现一个C 联系人类:contact,该类有name,tel和postcode三个私有属性,为该类设计若干方法,包括构造函数、存取三个私有属性的函数、显示联系人信息的函数等。
```
#include <iostream>
#include <string>
using namespace std;
class Contact {
private:
string name;
string tel;
string postcode;
public:
Contact(string name, string tel, string postcode) {
this->name = name;
this->tel = tel;
this->postcode = postcode;
}
string getName() {
return name;
}
string getTel() {
return tel;
}
string getPostcode() {
return postcode;
}
void setName(string name) {
this->name = name;
}
void setTel(string tel) {
this->tel = tel;
}
void setPostcode(string postcode) {
this->postcode = postcode;
}
void display() {
cout << "Name: " << name << endl;
cout << "Tel: " << tel << endl;
cout << "Postcode: " << postcode << endl;
}
};
```
(2)设计并实现一个通讯录类:addressBook,该类包含一个存放所有联系人的容器,使用数组,用CArray类实现,
```
#include <iostream>
#include <string>
#include "CArray.h"
#include "Contact.h"
using namespace std;
class AddressBook {
private:
CArray<Contact*> contacts;
public:
void displayAllContacts() {
for (int i = 0; i < contacts.getSize(); i++) {
contacts[i]->display();
cout << endl;
}
}
void addContact(Contact* contact) {
contacts.append(contact);
}
void deleteContact(int index) {
contacts.removeAt(index);
}
void insertContact(int index, Contact* contact) {
contacts.insertAt(index, contact);
}
Contact* findContact(string name) {
for (int i = 0; i < contacts.getSize(); i++) {
if (contacts[i]->getName() == name) {
return contacts[i];
}
}
return NULL;
}
void saveToFile(string filename) {
ofstream fout(filename);
for (int i = 0; i < contacts.getSize(); i++) {
fout << contacts[i]->getName() << "," << contacts[i]->getTel() << "," << contacts[i]->getPostcode() << endl;
}
fout.close();
}
void loadFromFile(string filename) {
ifstream fin(filename);
string line;
while (getline(fin, line)) {
int pos1 = line.find(",");
int pos2 = line.find(",", pos1 + 1);
string name = line.substr(0, pos1);
string tel = line.substr(pos1 + 1, pos2 - pos1 - 1);
string postcode = line.substr(pos2 + 1);
Contact* contact = new Contact(name, tel, postcode);
contacts.append(contact);
}
fin.close();
}
};
```
(3)为addressBook设计若干方法,包括显示所有联系人、增加联系人、删除联系人、插入联系人、查找联系人等函数,以及将所有联系人保存到文件和从文件中读取联系人的函数。
(4)设计主函数,显示操作选择菜单,完成通讯录的显示、增加、查找、删除和保存等功能。
```
#include <iostream>
#include <string>
#include "AddressBook.h"
using namespace std;
int main() {
AddressBook addressBook;
int choice;
string name, tel, postcode;
while (true) {
cout << "1. Display all contacts" << endl;
cout << "2. Add a contact" << endl;
cout << "3. Delete a contact" << endl;
cout << "4. Insert a contact" << endl;
cout << "5. Find a contact" << endl;
cout << "6. Save to file" << endl;
cout << "7. Load from file" << endl;
cout << "8. Exit" << endl;
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
addressBook.displayAllContacts();
break;
case 2:
cout << "Enter name: ";
cin >> name;
cout << "Enter tel: ";
cin >> tel;
cout << "Enter postcode: ";
cin >> postcode;
addressBook.addContact(new Contact(name, tel, postcode));
break;
case 3:
int index;
cout << "Enter index: ";
cin >> index;
addressBook.deleteContact(index);
break;
case 4:
int index2;
cout << "Enter index: ";
cin >> index2;
cout << "Enter name: ";
cin >> name;
cout << "Enter tel: ";
cin >> tel;
cout << "Enter postcode: ";
cin >> postcode;
addressBook.insertContact(index2, new Contact(name, tel, postcode));
break;
case 5:
cout << "Enter name: ";
cin >> name;
Contact* contact = addressBook.findContact(name);
if (contact == NULL) {
cout << "Contact not found" << endl;
}
else {
contact->display();
}
break;
case 6:
string filename;
cout << "Enter filename: ";
cin >> filename;
addressBook.saveToFile(filename);
break;
case 7:
string filename2;
cout << "Enter filename: ";
cin >> filename2;
addressBook.loadFromFile(filename2);
break;
case 8:
return 0;
default:
cout << "Invalid choice" << endl;
}
}
}
```
(5)建立一个C 工程,包含需要的.h和.cpp文件
请根据您的实际情况自行创建工程,并将上述代码分别保存为.h和.cpp文件。
阅读全文