1)设计并实现一个C++联系人类:contact,该类有name,tel和postcode三个私有属性,为该类设计若干方法,包括构造函数、存取三个私有属性的函数、显示联系人信息的函数等。 (2)设计并实现一个通讯录类:addressBook,该类包含一个存放所有联系人的容器,使用数组,用CArray类实现,写出CArray代码 (3)为addressBook设计若干方法,包括显示所有联系人、增加联系人、删除联系人、插入联系人、查找联系人等函数,以及将所有联系人保存到文件和从文件中读取联系人的函数。 (4)设计主函数,显示操作选择菜单,完成通讯录的显示、增加、查找、删除和保存等功能。 (5)建立一个C++工程,包含需要的.h和.cpp文件
时间: 2023-05-20 18:01:09 浏览: 78
1)以下是C++代码实现:
class Contact {
private:
string name;
string tel;
string postcode;
public:
Contact(string n, string t, string p) {
name = n;
tel = t;
postcode = p;
}
string getName() {
return name;
}
string getTel() {
return tel;
}
string getPostcode() {
return postcode;
}
void setName(string n) {
name = n;
}
void setTel(string t) {
tel = t;
}
void setPostcode(string p) {
postcode = p;
}
void display() {
cout << "Name: " << name << endl;
cout << "Tel: " << tel << endl;
cout << "Postcode: " << postcode << endl;
}
};
2)以下是CArray类的代码实现:
template <typename T>
class CArray {
private:
T* arr;
int size;
public:
CArray() {
arr = NULL;
size = 0;
}
CArray(int s) {
arr = new T[s];
size = s;
}
CArray(CArray& a) {
arr = new T[a.size];
size = a.size;
for (int i = 0; i < size; i++) {
arr[i] = a.arr[i];
}
}
~CArray() {
if (arr) {
delete[] arr;
}
}
void push_back(T& t) {
T* tmp = new T[size + 1];
for (int i = 0; i < size; i++) {
tmp[i] = arr[i];
}
tmp[size] = t;
size++;
if (arr) {
delete[] arr;
}
arr = tmp;
}
int getSize() {
return size;
}
T& operator[](int i) {
return arr[i];
}
};
3)以下是addressBook类的代码实现:
class AddressBook {
private:
CArray<Contact> contacts;
public:
void displayAll() {
for (int i = 0; i < contacts.getSize(); i++) {
contacts[i].display();
cout << endl;
}
}
void addContact(Contact& c) {
contacts.push_back(c);
}
void deleteContact(int i) {
for (int j = i; j < contacts.getSize() - 1; j++) {
contacts[j] = contacts[j + 1];
}
contacts[contacts.getSize() - 1] = Contact("", "", "");
contacts = CArray<Contact>(contacts.getSize() - 1);
}
void insertContact(Contact& c, int i) {
contacts.push_back(Contact("", "", ""));
for (int j = contacts.getSize() - 1; j > i; j--) {
contacts[j] = contacts[j - 1];
}
contacts[i] = c;
}
Contact* findContact(string n) {
for (int i = 0; i < contacts.getSize(); i++) {
if (contacts[i].getName() == n) {
return &contacts[i];
}
}
return NULL;
}
void saveToFile(string filename) {
ofstream fout(filename.c_str());
for (int i = 0; i < contacts.getSize(); i++) {
fout << contacts[i].getName() << endl;
fout << contacts[i].getTel() << endl;
fout << contacts[i].getPostcode() << endl;
}
fout.close();
}
void loadFromFile(string filename) {
ifstream fin(filename.c_str());
string n, t, p;
while (getline(fin, n)) {
getline(fin, t);
getline(fin, p);
Contact c(n, t, p);
contacts.push_back(c);
}
fin.close();
}
};
4)以下是主函数的代码实现:
int main() {
AddressBook ab;
int choice;
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:
ab.displayAll();
break;
case 2: {
string n, t, p;
cout << "Enter name: ";
cin >> n;
cout << "Enter tel: ";
cin >> t;
cout << "Enter postcode: ";
cin >> p;
Contact c(n, t, p);
ab.addContact(c);
break;
}
case 3: {
int i;
cout << "Enter index: ";
cin >> i;
ab.deleteContact(i);
break;
}
case 4: {
int i;
string n, t, p;
cout << "Enter index: ";
cin >> i;
cout << "Enter name: ";
cin >> n;
cout << "Enter tel: ";
cin >> t;
cout << "Enter postcode: ";
cin >> p;
Contact c(n, t, p);
ab.insertContact(c, i);
break;
}
case 5: {
string n;
cout << "Enter name: ";
cin >> n;
Contact* c = ab.findContact(n);
if (c) {
c->display();
}
else {
cout << "Contact not found" << endl;
}
break;
}
case 6: {
string filename;
cout << "Enter filename: ";
cin >> filename;
ab.saveToFile(filename);
break;
}
case 7: {
string filename;
cout << "Enter filename: ";
cin >> filename;
ab.loadFromFile(filename);
break;
}
case 8:
return 0;
default:
cout << "Invalid choice" << endl;
}
}
}
5)建立一个C++工程,包含需要的.h和.cpp文件。
阅读全文