21xrx.com
2024-09-20 00:04:24 Friday
登录
文章检索 我的文章 写文章
C++大作业:通讯录的设计
2023-06-26 00:17:59 深夜i     --     --
C++ 大作业 通讯录 设计 编程

随着现代科技的发展,人们之间的交流变得更加方便快捷。而通讯录作为联系人信息的集合,也成为了现代生活中不可或缺的一部分。本文将介绍一个使用C++编程语言设计的通讯录程序。

首先,我们需要确定通讯录中需要保存的信息。通讯录通常包含联系人姓名、电话号码、电子邮件地址等信息,因此我们需要创建一个类来描述联系人的基本信息。这个类包含以下几个基本的成员变量:


class Contact {

private:

  std::string name;   // 联系人姓名

  std::string phone;  // 联系人电话

  std::string email;  // 联系人邮箱

public:

  // 构造函数

  Contact(const std::string& name, const std::string& phone, const std::string& email);

  // 拷贝构造函数

  Contact(const Contact& other);

  // 修改联系人信息

  void setName(const std::string& name);

  void setPhone(const std::string& phone);

  void setEmail(const std::string& email);

  // 获取联系人信息

  std::string getName() const;

  std::string getPhone() const;

  std::string getEmail() const;

};

接下来,我们需要设计一个通讯录类。这个类可以用来添加、删除、修改和查询联系人信息。同时,我们需要使用STL容器vector来管理联系人信息。


class AddressBook {

private:

  std::vector<Contact> contacts;  // 联系人信息

public:

  // 添加联系人

  void addContact(const Contact& contact);

  // 删除联系人

  void removeContact(const Contact& contact);

  // 修改联系人信息

  void modifyContact(const Contact& old_contact, const Contact& new_contact);

  // 查询联系人

  std::vector<Contact> searchContact(const std::string& name) const;

};

整个通讯录程序的源代码如下:


#include <iostream>

#include <string>

#include <vector>

class Contact {

private:

  std::string name;   // 联系人姓名

  std::string phone;  // 联系人电话

  std::string email;  // 联系人邮箱

public:

  // 构造函数

  Contact(const std::string& name, const std::string& phone, const std::string& email) :

    name(name), phone(phone), email(email)

  

  // 拷贝构造函数

  Contact(const Contact& other) :

    name(other.name), phone(other.phone), email(other.email)

  

  // 修改联系人信息

  void setName(const std::string& name)

    this->name = name;

  

  void setPhone(const std::string& phone)

    this->phone = phone;

  

  void setEmail(const std::string& email)

    this->email = email;

  

  // 获取联系人信息

  std::string getName() const

    return name;

  

  std::string getPhone() const

    return phone;

  

  std::string getEmail() const

    return email;

  

};

class AddressBook {

private:

  std::vector<Contact> contacts;  // 联系人信息

public:

  // 添加联系人

  void addContact(const Contact& contact) {

    contacts.push_back(contact);

    std::cout << "联系人 " << contact.getName() << " 已添加" << std::endl;

  }

  // 删除联系人

  void removeContact(const Contact& contact) {

    for (auto it = contacts.begin(); it != contacts.end(); it++) {

      if (it->getName() == contact.getName()) {

        contacts.erase(it);

        std::cout << "联系人 " << contact.getName() << " 已删除" << std::endl;

        break;

      }

    }

  }

  // 修改联系人信息

  void modifyContact(const Contact& old_contact, const Contact& new_contact) {

    for (auto& contact : contacts) {

      if (contact.getName() == old_contact.getName()) {

        contact = new_contact;

        std::cout << "联系人 " << old_contact.getName() << " 已修改" << std::endl;

        break;

      }

    }

  }

  // 查询联系人

  std::vector<Contact> searchContact(const std::string& name) const {

    std::vector<Contact> result;

    for (auto& contact : contacts) {

      if (contact.getName() == name) {

        result.push_back(contact);

      }

    }

    return result;

  }

};

int main() {

  AddressBook addressBook;

  // 添加联系人

  Contact contact1("张三", "13888888888", "zhangsan@example.com");

  addressBook.addContact(contact1);

  // 修改联系人

  Contact contact2("李四", "13999999999", "lisi@example.com");

  Contact new_contact2("李四", "14000000000", "lisi@example.com");

  addressBook.addContact(contact2);

  addressBook.modifyContact(contact2, new_contact2);

  // 删除联系人

  Contact contact3("王五", "13666666666", "wangwu@example.com");

  addressBook.addContact(contact3);

  addressBook.removeContact(contact3);

  // 查询联系人

  Contact contact4("赵六", "13555555555", "zhaoliu@example.com");

  Contact contact5("赵六", "13666666666", "zhaoliu@example.com");

  addressBook.addContact(contact4);

  addressBook.addContact(contact5);

  std::vector<Contact> results = addressBook.searchContact("赵六");

  for (const auto& result : results) {

    std::cout << result.getName() << "\t" << result.getPhone() << "\t" << result.getEmail() << std::endl;

  }

  return 0;

}

通过本文的介绍,我们可以看到使用C++实现通讯录程序是非常简单的。如果需要更加复杂的功能,可以根据需要进行扩展和修改。

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复