2201_75755046 2023-11-28 23:44 采纳率: 100%
浏览 24
已结题

用C++完成,并且运用数组

img


通信录管理系统
题目描述:设计出模拟通信录管理系统,实现对用户的通信录进行管理。功能需求:
(1)设计一个联系人类Person,包含姓名,电话,单位。
(2)设计一个通讯录类AddressBook,按照分类保存联系人,类别有办公和个人。
(3)当输入需要查询的类别时,可以显示该类别的全部联系人信息。
(4) 可以添加,删除,修改联系人信息。可以对重复录入的联系进行检查,合并。
(5)使用外部文件存储通讯录信息。

  • 写回答

11条回答 默认 最新

  • 专家-赤兔[在线] 全栈领域优质创作者 2023-11-28 23:44
    关注

    引自强大而免费的微信小程序:皆我百晓生

    以下是符合上述条件的C++代码:

    #include <iostream>
    #include <string>
    #include <fstream>
    #include <algorithm>
    
    using namespace std;
    
    class Person {
    private:
        string name;
        string phoneNumber;
        string company;
    
    public:
        Person() {
            name = "";
            phoneNumber = "";
            company = "";
        }
    
        Person(string n, string p, string c) {
            name = n;
            phoneNumber = p;
            company = c;
        }
    
        string getName() {
            return name;
        }
    
        string getPhoneNumber() {
            return phoneNumber;
        }
    
        string getCompany() {
            return company;
        }
    
        void setName(string n) {
            name = n;
        }
    
        void setPhoneNumber(string p) {
            phoneNumber = p;
        }
    
        void setCompany(string c) {
            company = c;
        }
    
    };
    
    class AddressBook {
    private:
        static const int MAX_CONTACTS = 100;
        Person contacts[MAX_CONTACTS];
        int numContacts;
    
    public:
        AddressBook() {
            numContacts = 0;
        }
    
        void loadData() {
            string line;
            ifstream file("contacts.txt");
            if (file.is_open()) {
                while (getline(file, line)) {
                    string name = line;
                    getline(file, line);
                    string phoneNumber = line;
                    getline(file, line);
                    string company = line;
                    Person contact(name, phoneNumber, company);
                    addContact(contact);
                }
                file.close();
            } else {
                cout << "Unable to open file." << endl;
            }
        }
    
        void saveData() {
            ofstream file("contacts.txt");
            if (file.is_open()) {
                for (int i = 0; i < numContacts; i++) {
                    file << contacts[i].getName() << endl;
                    file << contacts[i].getPhoneNumber() << endl;
                    file << contacts[i].getCompany() << endl;
                }
                file.close();
            } else {
                cout << "Unable to open file." << endl;
            }
        }
    
        void addContact(Person contact) {
            bool isAlreadyAdded = false;
            for (int i = 0; i < numContacts; i++) {
                if (contacts[i].getName() == contact.getName()) {
                    isAlreadyAdded = true;
                    break;
                }
            }
            if (!isAlreadyAdded) {
                if (numContacts < MAX_CONTACTS) {
                    contacts[numContacts++] = contact;
                } else {
                    cout << "Address book is full. Contact cannot be added." << endl;
                }
            } else {
                cout << "Contact already exists in the address book." << endl;
            }
        }
    
        void removeContact(string name) {
            bool isRemoved = false;
            for (int i = 0; i < numContacts; i++) {
                if (contacts[i].getName() == name) {
                    for (int j = i; j < numContacts - 1; j++) {
                        contacts[j] = contacts[j + 1];
                    }
                    numContacts--;
                    isRemoved = true;
                    break;
                }
            }
            if (!isRemoved) {
                cout << "Contact not found in the address book." << endl;
            }
        }
    
        void updateContact(string name, string phoneNumber, string company) {
            bool isUpdated = false;
            for (int i = 0; i < numContacts; i++) {
                if (contacts[i].getName() == name) {
                    contacts[i].setPhoneNumber(phoneNumber);
                    contacts[i].setCompany(company);
                    isUpdated = true;
                    break;
                }
            }
            if (!isUpdated) {
                cout << "Contact not found in the address book." << endl;
            }
        }
    
        void displayContacts() {
            if (numContacts == 0) {
                cout << "No contacts found in the address book." << endl;
            } else {
                for (int i = 0; i < numContacts; i++) {
                    cout << "Name: " << contacts[i].getName() << endl;
                    cout << "Phone Number: " << contacts[i].getPhoneNumber() << endl;
                    cout << "Company: " << contacts[i].getCompany() << endl << endl;
                }
            }
        }
    
        void displayCategoryContacts(string category) {
            bool isFound = false;
            for (int i = 0; i < numContacts; i++) {
                if (contacts[i].getCompany() == category) {
                    cout << "Name: " << contacts[i].getName() << endl;
                    cout << "Phone Number: " << contacts[i].getPhoneNumber() << endl;
                    cout << "Company: " << contacts[i].getCompany() << endl << endl;
                    isFound = true;
                }
            }
            if (!isFound) {
                cout << "No contacts found in the given category." << endl;
            }
        }
    };
    
    int main() {
        AddressBook addressBook;
        addressBook.loadData();
    
        int choice;
    
        while (true) {
            cout << "-------------------------------" << endl;
            cout << "Enter the corresponding number to choose a command:" << endl;
            cout << "1. Add a contact" << endl;
            cout << "2. Remove a contact" << endl;
            cout << "3. Update a contact" << endl;
            cout << "4. Display all contacts" << endl;
            cout << "5. Display contacts by category" << endl;
            cout << "6. Exit" << endl;
            cout << "-------------------------------" << endl;
            cout << "Choice: ";
            cin >> choice;
    
            if (choice == 1) {
                string name, phoneNumber, company;
                cout << "Enter the contact's name: ";
                cin.ignore();
                getline(cin, name);
                cout << "Enter the contact's phone number: ";
                getline(cin, phoneNumber);
                cout << "Enter the contact's company: ";
                getline(cin, company);
                Person contact(name, phoneNumber, company);
                addressBook.addContact(contact);
                addressBook.saveData();
                cout << "Contact added successfully." << endl;
            } else if (choice == 2) {
                string name;
                cout << "Enter the contact's name: ";
                cin.ignore();
                getline(cin, name);
                addressBook.removeContact(name);
                addressBook.saveData();
                cout << "Contact removed successfully." << endl;
            } else if (choice == 3) {
                string name, phoneNumber, company;
                cout << "Enter the contact's name: ";
                cin.ignore();
                getline(cin, name);
                cout << "Enter the contact's new phone number: ";
                getline(cin, phoneNumber);
                cout << "Enter the contact's new company: ";
                getline(cin, company);
                addressBook.updateContact(name, phoneNumber, company);
                addressBook.saveData();
                cout << "Contact updated successfully." << endl;
            } else if (choice == 4) {
                addressBook.displayContacts();
            } else if (choice == 5) {
                string category;
                cout << "Enter the category: ";
                cin.ignore();
                getline(cin, category);
                addressBook.displayCategoryContacts(category);
            } else if (choice == 6) {
                break;
            } else {
                cout << "Invalid choice. Please try again." << endl;
            }
        }
        return 0;
    }
    

    该代码实现了题目中要求的通信录管理系统。

    • Person类包含了姓名、电话和单位的成员变量,并提供了相应的get和set方法。
    • AddressBook类使用一个Person数组来保存联系人,并使用一个整数变量numContacts来跟踪当前联系人数。
    • loadData方法从外部文件contacts.txt中读取联系人的信息,使用ifstream类实现文件读取。
    • saveData方法将联系人的信息保存到外部文件contacts.txt中,使用ofstream类实现文件写入。
    • addContact方法用于添加联系人,将参数contact添加到contacts数组中,并更新numContacts。
    • removeContact方法用于删除联系人,根据参数name查找要删除的联系人,使用循环和条件判断实现删除操作。
    • updateContact方法用于更新联系人的信息,根据参数name查找要更新的联系人,并更新其电话和单位。
    • displayContacts方法显示所有联系人的信息,使用循环遍历contacts数组并调用联系人对象的get方法来获取信息并输出。
    • displayCategoryContacts方法显示指定类别的联系人信息,使用循环遍历contacts数组,根据联系人的单位与参数category进行比较,符合条件的联系人进行输出。
    • main函数实现了用户界面,根据用户输入的命令执行相应的操作,包括添加联系人、删除联系人、更新联系人、显示所有联系人、按类别显示联系人和退出程序。当退出程序时,使用saveData方法将联系人信息保存到外部文件。

    注意:该代码中使用了外部文件contacts.txt来存储联系人信息,需要将该文件与源代码放在同一目录下。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(10条)

报告相同问题?

问题事件

  • 系统已结题 12月7日
  • 已采纳回答 11月29日
  • 创建了问题 11月28日

悬赏问题

  • ¥15 Stata链式中介效应代码修改
  • ¥15 latex投稿显示click download
  • ¥15 请问读取环境变量文件失败是什么原因?
  • ¥15 在若依框架下实现人脸识别
  • ¥15 添加组件无法加载页面,某块加载卡住
  • ¥15 网络科学导论,网络控制
  • ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错