用c++编写一个简单的通讯录系统

一.简易通讯录的主要功能

1.添加联系人

void addContact(vector<Contact>& contacts) {
    Contact newContact;
    cout << "请输入联系人姓名: ";
    cin >> newContact.name;
    cout << "请输入联系人电话: ";
    cin >> newContact.phone;
    contacts.push_back(newContact);
    scout << "联系人已添加" << endl;

}

2.显示联系人

void displayContacts(const vector<Contact>& contacts) {
    if (contacts.empty()) {
        cout << "联系人列表为空" << endl;
    }
    else {
        cout << "联系人列表:" << endl;
        for (const auto& contact : contacts) {
            cout << "联系人姓名: " << contact.name << endl;
            cout << "联系人电话: " << contact.phone << endl;
        }
    }


}

3.修改联系人 

void modifyContact(vector<Contact>& contacts) {
    string name;
    cout << "请输入要修改的联系人姓名: ";
    cin >> name;
    bool found = false;
    for (auto& contact : contacts) {
        if (contact.name == name) {
            cout << "请输入新的联系人电话: ";
            cin >> contact.phone;
            found = true;
            break;
        }
    }
    if (!found) {
        cout << "未找到该联系人" << endl;
    }
    else {
        cout << "联系人已修改" << endl;
    }

    system("cls");//清屏

}

4.查找联系人

void deleteContact(std::vector<Contact>& contacts) {
    std::string name;
    std::cout << "请输入要删除的联系人姓名: ";
    std::cin >> name;
    bool found = false;
    for (auto it = contacts.begin(); it != contacts.end(); ++it) {
        if (it->name == name) {
            contacts.erase(it);
            found = true;
            break;
        }
    }
    if (!found) {
        std::cout << "未找到该联系人" << std::endl;
    }
    else {
        std::cout << "联系人已删除" << std::endl;
    }

    system("cls");//清屏

}

5.删除联系人

void deleteContact(std::vector<Contact>& contacts) {
    std::string name;
    std::cout << "请输入要删除的联系人姓名: ";
    std::cin >> name;
    bool found = false;
    for (auto it = contacts.begin(); it != contacts.end(); ++it) {
        if (it->name == name) {
            contacts.erase(it);
            found = true;
            break;
        }
    }
    if (!found) {
        std::cout << "未找到该联系人" << std::endl;
    }
    else {
        std::cout << "联系人已删除" << std::endl;
    }

    system("cls");//清屏

}

6.清空列表

void clearContacts(std::vector<Contact>& contacts) {
    contacts.clear();
    std::cout << "联系人列表已清空" << std::endl;
    system("cls");//清屏

}

7.退出程序

return 0;

8.需要的声明

#include <iostream>
#include <string>
#include <vector>

9.整体代码

#include <iostream>
#include <string>
#include <vector>

struct Contact {
    std::string name;
    std::string phone;
};
void addContact(std::vector<Contact>& contacts) {
    Contact newContact;
    std::cout << "请输入联系人姓名: ";
    std::cin >> newContact.name;
    std::cout << "请输入联系人电话: ";
    std::cin >> newContact.phone;
    contacts.push_back(newContact);
    std::cout << "联系人已添加" << std::endl;

    system("cls");//清屏

}
void modifyContact(std::vector<Contact>& contacts) {
    std::string name;
    std::cout << "请输入要修改的联系人姓名: ";
    std::cin >> name;
    bool found = false;
    for (auto& contact : contacts) {
        if (contact.name == name) {
            std::cout << "请输入新的联系人电话: ";
            std::cin >> contact.phone;
            found = true;
            break;
        }
    }
    if (!found) {
        std::cout << "未找到该联系人" << std::endl;
    }
    else {
        std::cout << "联系人已修改" << std::endl;
    }

    system("cls");//清屏

}
void findContact(const std::vector<Contact>& contacts) {
    std::string name;
    std::cout << "请输入要查找的联系人姓名: ";
    std::cin >> name;
    bool found = false;
    for (const auto& contact : contacts) {
        if (contact.name == name) {
            std::cout << "联系人姓名: " << contact.name << std::endl;
            std::cout << "联系人电话: " << contact.phone << std::endl;
            found = true;
            break;
        }
    }
    if (!found) {
        std::cout << "未找到该联系人" << std::endl;
    }

    system("cls");//清屏

}
void deleteContact(std::vector<Contact>& contacts) {
    std::string name;
    std::cout << "请输入要删除的联系人姓名: ";
    std::cin >> name;
    bool found = false;
    for (auto it = contacts.begin(); it != contacts.end(); ++it) {
        if (it->name == name) {
            contacts.erase(it);
            found = true;
            break;
        }
    }
    if (!found) {
        std::cout << "未找到该联系人" << std::endl;
    }
    else {
        std::cout << "联系人已删除" << std::endl;
    }

    system("cls");//清屏

}
void displayContacts(const std::vector<Contact>& contacts) {
    if (contacts.empty()) {
        std::cout << "联系人列表为空" << std::endl;
    }
    else {
        std::cout << "联系人列表:" << std::endl;
        for (const auto& contact : contacts) {
            std::cout << "联系人姓名: " << contact.name << std::endl;
            std::cout << "联系人电话: " << contact.phone << std::endl;
        }
    }

    system("cls");//清屏

}
void clearContacts(std::vector<Contact>& contacts) {
    contacts.clear();
    std::cout << "联系人列表已清空" << std::endl;
    system("cls");//清屏

}
int main() {
    std::vector<Contact> contacts;

    while (true) {
        std::cout << "请选择操作:" << std::endl;
        std::cout << "1. 添加联系人" << std::endl;
        std::cout << "2. 修改联系人" << std::endl;
        std::cout << "3. 查找联系人" << std::endl;
        std::cout << "4. 删除联系人" << std::endl;
        std::cout << "5. 显示所有联系人" << std::endl;
        std::cout << "6. 清空联系人" << std::endl;
        std::cout << "7. 退出程序" << std::endl;
        int choice;
        std::cin >> choice;

        switch (choice) {
        case 1:
            addContact(contacts);
            break;
        case 2:
            modifyContact(contacts);
            break;
        case 3:
            findContact(contacts);
            break;
        case 4:
            deleteContact(contacts);
            break;
        case 5:
            displayContacts(contacts);
            break;
        case 6:
            clearContacts(contacts);
            break;
        case 7:
            return 0;
        default:
            std::cout << "无效的选项,请重新选择" << std::endl;
            break;
        }
        system("cls");//清屏
        std::cout << std::endl;
    }
    return 0;
}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值