C++ 通讯录系统:添加联系人并根据首字母搜索
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
class Contact {
protected:
string name;
string pinyin;
string nickname;
string address;
string birthday;
string lunarBirthday;
string relationship;
string note;
string company;
string position;
string phoneNumber;
string occupation;
public:
Contact(string n) : name(n) {
pinyin = "";
nickname = "";
address = "";
birthday = "";
lunarBirthday = "";
relationship = "";
note = "";
company = "";
position = "";
phoneNumber = "";
occupation = "";
}
virtual void display() {
cout << "姓名:" << name << endl;
cout << "姓名拼音:" << pinyin << endl;
cout << "即时信息:" << nickname << endl;
cout << "地址:" << address << endl;
cout << "生日:" << birthday << endl;
cout << "农历生日:" << lunarBirthday << endl;
cout << "关系:" << relationship << endl;
cout << "备注:" << note << endl;
cout << "公司:" << company << endl;
cout << "职位:" << position << endl;
cout << "手机号码:" << phoneNumber << endl;
cout << "工作:" << occupation << endl;
}
string getName() const {
return name;
}
void setPinyin(string py) {
pinyin = py;
}
void setNickname(string nn) {
nickname = nn;
}
void setAddress(string addr) {
address = addr;
}
void setBirthday(string bday) {
birthday = bday;
}
void setLunarBirthday(string lbday) {
lunarBirthday = lbday;
}
void setRelationship(string rel) {
relationship = rel;
}
void setNote(string nt) {
note = nt;
}
void setCompany(string cmp) {
company = cmp;
}
void setPosition(string pos) {
position = pos;
}
void setPhoneNumber(string phone) {
phoneNumber = phone;
}
void setOccupation(string occ) {
occupation = occ;
}
bool operator<(const Contact& other) const {
return pinyin < other.pinyin;
}
};
class RelativeContact : public Contact {
public:
RelativeContact(string n) : Contact(n) {}
void display() {
cout << "---亲属联系人---" << endl;
Contact::display();
}
};
class ColleagueContact : public Contact {
public:
ColleagueContact(string n) : Contact(n) {}
void display() {
cout << "---同事联系人---" << endl;
Contact::display();
}
};
class ClassmateContact : public Contact {
public:
ClassmateContact(string n) : Contact(n) {}
void display() {
cout << "---同学联系人---" << endl;
Contact::display();
}
};
class TeacherContact : public Contact {
public:
TeacherContact(string n) : Contact(n) {}
void display() {
cout << "---教师联系人---" << endl;
Contact::display();
}
};
class AddressBook {
private:
vector<Contact*> contacts;
public:
AddressBook() {}
~AddressBook() {
for (auto contact : contacts) {
delete contact;
}
}
void addContact(Contact* contact) {
contacts.push_back(contact);
}
void displayContacts() {
cout << "---通讯录---" << endl;
for (auto contact : contacts) {
contact->display();
cout << "--------------------------------------" << endl;
}
}
void sortByPinyin() {
sort(contacts.begin(), contacts.end(), [](Contact* a, Contact* b) {
return a->getName() < b->getName();
});
}
void searchByInitial(char initial) {
cout << "---以 " << initial << " 开头的联系人---" << endl;
for (auto contact : contacts) {
if (toupper(contact->getName()[0]) == toupper(initial)) {
contact->display();
cout << "--------------------------------------" << endl;
}
}
}
};
int main() {
AddressBook addressBook;
string contactName;
cout << "请输入联系人姓名:";
getline(cin, contactName);
char contactType;
cout << "请选择联系人类型:(R-亲属, C-同事, M-同学, T-教师) ";
cin >> contactType;
cin.ignore();
Contact* newContact;
if (contactType == 'R') {
newContact = new RelativeContact(contactName);
} else if (contactType == 'C') {
newContact = new ColleagueContact(contactName);
} else if (contactType == 'M') {
newContact = new ClassmateContact(contactName);
} else if (contactType == 'T') {
newContact = new TeacherContact(contactName);
} else {
cout << "无效的联系人类型" << endl;
return 0;
}
cout << "是否添加联系人属性?(Y/N) ";
char addProperty;
cin >> addProperty;
cin.ignore();
if (addProperty == 'Y') {
string pinyin;
cout << "请输入姓名拼音:";
getline(cin, pinyin);
newContact->setPinyin(pinyin);
string nickname;
cout << "请输入即时信息:";
getline(cin, nickname);
newContact->setNickname(nickname);
string address;
cout << "请输入地址:";
getline(cin, address);
newContact->setAddress(address);
// 添加其他属性的输入和设置,例如生日、公司等
}
addressBook.addContact(newContact);
addressBook.sortByPinyin();
addressBook.displayContacts();
char searchInitial;
cout << "请输入要搜索的联系人姓名的首字母:";
cin >> searchInitial;
addressBook.searchByInitial(searchInitial);
return 0;
}
在这个更新的代码中,我们添加了用户输入联系人姓名和类型的功能。用户可以选择亲属(R)、同事(C)、同学(M)或教师(T)作为联系人类型。然后,根据用户选择是否添加联系人的其他属性。用户可以根据首字母搜索联系人。
请根据您的需求和具体情况进一步修改和扩展代码。如果有任何问题,请随时提问。
原文地址: https://www.cveoy.top/t/topic/Tjx 著作权归作者所有。请勿转载和采集!