对上述程序给出main函数测试
时间: 2024-02-25 21:59:02 浏览: 46
好的,下面是一个简单的 main 函数,用于测试 PhoneBook 类的实现:
```cpp
int main() {
PhoneBook phoneBook;
// 添加联系人
phoneBook.addContact("Alice", "123456");
phoneBook.addContact("Bob", "234567");
phoneBook.addContact("Charlie", "345678");
// 根据姓名查找电话号码
cout << "Alice's number: " << phoneBook.findNumber("Alice") << endl;
cout << "Bob's number: " << phoneBook.findNumber("Bob") << endl;
cout << "Charlie's number: " << phoneBook.findNumber("Charlie") << endl;
// 根据电话号码查找姓名
cout << "123456's name: " << phoneBook.findName("123456") << endl;
cout << "234567's name: " << phoneBook.findName("234567") << endl;
cout << "345678's name: " << phoneBook.findName("345678") << endl;
// 删除联系人
phoneBook.removeContact("Bob");
// 根据姓名查找电话号码
cout << "Alice's number: " << phoneBook.findNumber("Alice") << endl;
cout << "Bob's number: " << phoneBook.findNumber("Bob") << endl;
cout << "Charlie's number: " << phoneBook.findNumber("Charlie") << endl;
// 根据电话号码查找姓名
cout << "123456's name: " << phoneBook.findName("123456") << endl;
cout << "234567's name: " << phoneBook.findName("234567") << endl;
cout << "345678's name: " << phoneBook.findName("345678") << endl;
// 将电话簿导出到文件
phoneBook.exportToFile("phonebook.txt");
// 从文件导入电话簿
PhoneBook phoneBook2;
phoneBook2.importFromFile("phonebook.txt");
// 根据姓名查找电话号码
cout << "Alice's number: " << phoneBook2.findNumber("Alice") << endl;
cout << "Bob's number: " << phoneBook2.findNumber("Bob") << endl;
cout << "Charlie's number: " << phoneBook2.findNumber("Charlie") << endl;
// 根据电话号码查找姓名
cout << "123456's name: " << phoneBook2.findName("123456") << endl;
cout << "234567's name: " << phoneBook2.findName("234567") << endl;
cout << "345678's name: " << phoneBook2.findName("345678") << endl;
return 0;
}
```
这个测试程序中,我们首先添加了三个联系人,然后测试了根据姓名查找电话号码和根据电话号码查找姓名的功能。接着,我们删除了一个联系人,再次测试了查找功能。最后,我们将电话簿导出到文件,并从文件中导入电话簿,测试了导入导出功能。你可以根据自己的需求来编写测试程序。
阅读全文