#include<set>头文件的用法

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是基于您的要求编写的 C++ 代码: ``` #include <iostream> #include <string> using namespace std; class Contact { protected: string name; string gender; string eyeColor; string phoneNumber; string company; public: Contact(string n, string g, string e, string p, string c) { name = n; gender = g; eyeColor = e; phoneNumber = p; company = c; } virtual void display() { cout << "Name: " << name << endl; cout << "Gender: " << gender << endl; cout << "Eye Color: " << eyeColor << endl; cout << "Phone Number: " << phoneNumber << endl; cout << "Company: " << company << endl; } string getName() { return name; } void setName(string n) { name = n; } string getGender() { return gender; } void setGender(string g) { gender = g; } string getEyeColor() { return eyeColor; } void setEyeColor(string e) { eyeColor = e; } string getPhoneNumber() { return phoneNumber; } void setPhoneNumber(string p) { phoneNumber = p; } string getCompany() { return company; } void setCompany(string c) { company = c; } }; class ContactList { private: Contact** contacts; int size; public: ContactList() { size = 0; contacts = new Contact*[100]; } ~ContactList() { for (int i = 0; i < size; i++) { delete contacts[i]; } delete[] contacts; } void addContact(Contact* contact) { contacts[size] = contact; size++; } void removeContact(string name) { for (int i = 0; i < size; i++) { if (contacts[i]->getName() == name) { delete contacts[i]; contacts[i] = contacts[size-1]; size--; cout << "Contact removed." << endl; return; } } cout << "Contact not found." << endl; } void displayContacts() { for (int i = 0; i < size; i++) { cout << "Contact " << i+1 << ":" << endl; contacts[i]->display(); cout << endl; } } void searchContact(string name) { for (int i = 0; i < size; i++) { if (contacts[i]->getName() == name) { contacts[i]->display(); return; } } cout << "Contact not found." << endl; } void sortContacts() { for (int i = 0; i < size-1; i++) { for (int j = i+1; j < size; j++) { if (contacts[i]->getName() > contacts[j]->getName()) { Contact* temp = contacts[i]; contacts[i] = contacts[j]; contacts[j] = temp; } } } cout << "Contacts sorted by name." << endl; } void saveContactsToFile(string fileName) { ofstream file; file.open(fileName); for (int i = 0; i < size; i++) { file << contacts[i]->getName() << "," << contacts[i]->getGender() << "," << contacts[i]->getEyeColor() << "," << contacts[i]->getPhoneNumber() << "," << contacts[i]->getCompany() << endl; } file.close(); cout << "Contacts saved to file." << endl; } }; int main() { ContactList contacts; while (true) { cout << "Select an option:" << endl; cout << "1. Add contact" << endl; cout << "2. Remove contact" << endl; cout << "3. Display contacts" << endl; cout << "4. Search for contact" << endl; cout << "5. Sort contacts by name" << endl; cout << "6. Save contacts to file" << endl; cout << "7. Quit" << endl; int option; cin >> option; if (option == 1) { string name, gender, eyeColor, phoneNumber, company; cout << "Enter contact name: "; cin >> name; cout << "Enter contact gender: "; cin >> gender; cout << "Enter contact eye color: "; cin >> eyeColor; cout << "Enter contact phone number: "; cin >> phoneNumber; cout << "Enter contact company: "; cin >> company; Contact* contact = new Contact(name, gender, eyeColor, phoneNumber, company); contacts.addContact(contact); } else if (option == 2) { string name; cout << "Enter contact name to remove: "; cin >> name; contacts.removeContact(name); } else if (option == 3) { contacts.displayContacts(); } else if (option == 4) { string name; cout << "Enter contact name to search for: "; cin >> name; contacts.searchContact(name); } else if (option == 5) { contacts.sortContacts(); } else if (option == 6) { string fileName; cout << "Enter file name to save contacts to: "; cin >> fileName; contacts.saveContactsToFile(fileName); } else if (option == 7) { break; } else { cout << "Invalid option. Please try again." << endl; } } return 0; } ``` 这是一个简单的通讯录应用程序,包括添加、删除、浏览和查询联系人,按姓名排序和将联系人保存到文件。在程序中,我们使用了类的继承和派生,其中 Contact 类是基类,包含联系人的姓名、性别、眼睛颜色、电话号码和公司信息。ContactList 类是派生类,将 Contact 对象存储在一个数组中,并提供了添加、删除、浏览和查询联系人、按姓名排序和将联系人保存到文件的方法。在主函数中,我们使用一个 while 循环来显示菜单选项,并根据用户的选择调用相应的方法来执行操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值