C++基础语法练习实例--通讯录

C++学习基本语法后,用于小白练手的一个简单项目,稍作修改可用于c语言课设,不含面向对象编程,基本功能:增删改查通讯录信息,退出通讯录,清空通讯录。使用ifstream和ofstream函数实现对数据的长期保存。

#include <iostream>
#include <fstream>  // 头文件,文件流的相关函数
#include <string>
#include <vector>

using namespace std;

// 定义通讯录联系人结构体
struct Contact {
    string name;      // 姓名
    string phoneNum;  // 电话号码
};

// 定义通讯录向量,存储所有联系人
vector<Contact> contacts;

// 添加联系人
void addContact() {
    Contact contact;
    cout << "请输入姓名: ";
    cin >> contact.name;
    cout << "请输入电话号码: ";
    cin >> contact.phoneNum;
    contacts.push_back(contact);
    cout << "添加成功!" << endl;
}

// 删除联系人
void delContact() {
    string name;
    cout << "请输入要删除的联系人姓名: ";
    cin >> name;
    /*
    for(vector<Contcat>::iterator it = contacts.begin(); it != contacts.end(); it++){
        if(it->name == name){
            contactc.erase(it);
            cout << "" << endl;
            return ;
        }
    }
    */
    for (vector<Contact>::iterator it = contacts.begin(); it != contacts.end(); it++) {
        if (it->name == name) {
            contacts.erase(it);
            cout << "删除成功!" << endl;
            return;
        }
    }
    cout << "没有找到联系人,删除失败!" << endl;
}

// 修改联系人
void modifyContact() {
    string name;
    cout << "请输入要修改的联系人姓名: ";
    cin >> name;
    for (vector<Contact>::iterator it = contacts.begin(); it != contacts.end(); it++) {
        if (it->name == name) {
            cout << "请输入新的电话号码: ";
            cin >> it->phoneNum;
            cout << "修改成功!" << endl;
            return;
        }
    }
    cout << "没有找到联系人,修改失败!" << endl;
}

// 查找联系人
void searchContact() {
    string name;
    cout << "请输入要查找的联系人姓名: ";
    cin >> name;
    for (vector<Contact>::iterator it = contacts.begin(); it != contacts.end(); it++) {
        if (it->name == name) {
            cout << "姓名: " << it->name << "\t电话号码: " << it->phoneNum << endl;
            return;
        }
    }
    cout << "没有找到联系人!" << endl;
}

// 清空联系人
void clearContacts() {
    contacts.clear();
    cout << "通讯录已清空!" << endl;
}

// 从文件中读取通讯录数据
void readFromFile() {
    ifstream fin("contacts.txt");  // 打开文件
    if (fin.is_open()) {           // 判断文件是否打开成功
        while (!fin.eof()) {       // 读取文件内容
            Contact contact;
            fin >> contact.name;          //通过fin对象输出到contact上
            fin >> contact.phoneNum;
            contacts.push_back(contact);
        }
        cout << "通讯录已读取完毕!" << endl;
        fin.close();  // 关闭文件
    } else {
        cout << "读取通讯录失败!" << endl;
    }
}

// 将通讯录数据存储到文件中
void writeToFile() {
    ofstream fout("contacts.txt");  // 打开文件
    if (fout.is_open()) {           // 判断文件是否打开成功
        for (vector<Contact>::iterator it = contacts.begin(); it != contacts.end(); it++) {
            fout << it->name << " " << it->phoneNum << endl;
        }
        cout << "通讯录已保存到文件中!" << endl;
        fout.close();  // 关闭文件
    } else {
        cout << "保存通讯录失败!" << endl;
    }
}

// 显示菜单
void showMenu() {
    readFromFile();  // 从文件中读取通讯录数据
    while (true) {
        cout << "===================通讯录===================" << endl;
        cout << "请选择要执行的操作: " << endl;
        cout << "1. 添加联系人" << endl;
        cout << "2. 删除联系人" << endl;
        cout << "3. 修改联系人" << endl;
        cout << "4. 查找联系人" << endl;
        cout << "5. 清空联系人" << endl;
        cout << "6. 退出通讯录系统" << endl;
        cout << "请输入操作编号: ";
        int choice;
        cin >> choice;
        switch (choice) {
            case 1:
                addContact();
                break;
            case 2:
                delContact();
                break;
            case 3:
                modifyContact();
                break;
            case 4:
                searchContact();
                break;
            case 5:
                clearContacts();
                break;
            case 6:
                writeToFile();  // 将通讯录数据存储到文件中
                cout << "谢谢使用通讯录系统,再见!" << endl;
                return;
            default:
                cout << "输入有误,请重新输入!" << endl;
        }
    }
}

int main() {
    showMenu();  // 显示菜单
    return 0;
}

makefile文件内容:

txl: txl.o

    g++ -o txl txl.o

txl.o: txl.cpp

    g++ -c txl.cpp

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值