以下是使用C++类实现的个人通讯录程序:

#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <vector>

using namespace std;

class Contact {
private:
    string name;
    string phone;
    string email;

public:
    Contact(string name="", string phone="", string email="") {
        this->name = name;
        this->phone = phone;
        this->email = email;
    }

    string getName() const {
        return name;
    }

    string getPhone() const {
        return phone;
    }

    string getEmail() const {
        return email;
    }

    void setName(string name) {
        this->name = name;
    }

    void setPhone(string phone) {
        this->phone = phone;
    }

    void setEmail(string email) {
        this->email = email;
    }

    string toString() const {
        stringstream ss;
        ss << name << "," << phone << "," << email;
        return ss.str();
    }
};

class ContactList {
private:
    vector<Contact> contacts;
    string filename;

public:
    ContactList(string filename="contacts.txt") {
        this->filename = filename;
        load();
    }

    void add(Contact contact) {
        contacts.push_back(contact);
        save();
    }

    Contact findByName(string name) const {
        for (Contact contact : contacts) {
            if (contact.getName() == name) {
                return contact;
            }
        }
        return Contact();
    }

    Contact findByPhone(string phone) const {
        for (Contact contact : contacts) {
            if (contact.getPhone() == phone) {
                return contact;
            }
        }
        return Contact();
    }

    void save() {
        ofstream fout(filename);
        for (Contact contact : contacts) {
            fout << contact.toString() << endl;
        }
        fout.close();
    }

    void load() {
        ifstream fin(filename);
        string line;
        while (getline(fin, line)) {
            stringstream ss(line);
            string name, phone, email;
            getline(ss, name, ',');
            getline(ss, phone, ',');
            getline(ss, email, ',');
            Contact contact(name, phone, email);
            add(contact);
        }
        fin.close();
    }
};

int main() {
    ContactList contacts;
    while (true) {
        cout << "1. Add Contact" << endl;
        cout << "2. Find Contact by Name" << endl;
        cout << "3. Find Contact by Phone" << endl;
        cout << "4. Exit" << endl;
        int choice;
        cin >> choice;
        if (choice == 1) {
            string name, phone, email;
            cout << "Enter Name: ";
            cin >> name;
            cout << "Enter Phone: ";
            cin >> phone;
            cout << "Enter Email: ";
            cin >> email;
            Contact contact(name, phone, email);
            contacts.add(contact);
        }
        else if (choice == 2) {
            string name;
            cout << "Enter Name: ";
            cin >> name;
            Contact contact = contacts.findByName(name);
            if (contact.getName() == "") {
                cout << "Contact not found" << endl;
            }
            else {
                cout << "Name: " << contact.getName() << endl;
                cout << "Phone: " << contact.getPhone() << endl;
                cout << "Email: " << contact.getEmail() << endl;
            }
        }
        else if (choice == 3) {
            string phone;
            cout << "Enter Phone: ";
            cin >> phone;
            Contact contact = contacts.findByPhone(phone);
            if (contact.getName() == "") {
                cout << "Contact not found" << endl;
            }
            else {
                cout << "Name: " << contact.getName() << endl;
                cout << "Phone: " << contact.getPhone() << endl;
                cout << "Email: " << contact.getEmail() << endl;
            }
        }
        else if (choice == 4) {
            break;
        }
    }
    return 0;
}

该程序通过命令行窗口与用户进行交互。用户可以选择添加联系人、根据姓名查找联系人或根据电话号码查找联系人。程序将联系人信息保存在文件中,每次启动时自动从文件中读取已保存的联系人信息

使用文本命令行窗口设计个人通信录程序。要求能设置并保存若干姓名、联系电话和电邮地址。能够根据输入的姓名在已经保存的信息中查询相应的联系电话和电邮地址。能够根据输入的电话号码在已经保存的信息中查询相应的联系人姓名。用c++的类形式写

原文地址: https://www.cveoy.top/t/topic/hwkb 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录