基于容器对文件进行增删改查

#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

struct Point
{
    int x, y, w;
    Point() : x(0), y(0), w(0) {}                    // 无参初始化
    Point(int x, int y, int w) : x(x), y(y), w(w) {} // 无参初始化
};

// 选项
enum
{
    EXIT,
    ADD,
    DEL,
    UPDATE,
    QUERY
};

// 添加
void add(vector<Point *> &ve, Point *p)
{
    ve.push_back(p);
    cout << "add successfully" << endl;
}

// 删除
void del(vector<Point *> &ve, int u)
{
    for (vector<Point *>::iterator it = ve.begin(); it != ve.end(); it++)
    {
        if ((*it)->x == u)
        {
            ve.erase((it));
            cout << "delete successfully" << endl;
            break;
        }
    }
}

// 修改
void update(vector<Point *> &ve, int u, int ww)
{
    for (vector<Point *>::iterator it = ve.begin(); it != ve.end(); it++)
    {
        if ((*it)->x == u)
        {
            (*it)->w = ww;
            cout << "update successfully" << endl;
            break;
        }
    }
}

// 功能菜单
void menu()
{
    cout << "======功能菜单=======" << endl;
    cout << "1.添加\n";
    cout << "2.删除\n";
    cout << "3.修改\n";
    cout << "4.显示\n";
    cout << "0.退出\n";
    cout << "请选择功能:";
}

// 显示
void show(vector<Point *> &ve)
{
    for (vector<Point *>::iterator it = ve.begin(); it != ve.end(); it++)
    {
        cout << (*it)->x << " " << (*it)->y << " " << (*it)->w << endl;
    }
}

void read(vector<Point *> &ve)
{
    ifstream fin("data.txt", ios::in);
    while (!fin.eof())
    {
        auto p2 = new Point();
        fin >> p2->x >> p2->y >> p2->w;
        ve.push_back(p2);
        while (fin.peek() == '\n')
            fin.get();
    }
    fin.close();
}

void save(vector<Point *> &ve)
{
    ofstream fout("data.txt", ios::out);
    for (vector<Point *>::iterator it = ve.begin(); it != ve.end(); it++)
    {
        fout << (*it)->x << " " << (*it)->y << " " << (*it)->w << "\n";
    }
    fout.close();
}

int main()
{
    vector<Point *> ve;
    while (true)
    {
        system("CLS");
        ve.clear();
        read(ve);
        menu();
        int ch;
        cin >> ch;
        switch (ch)
        {
        case ADD:
        {
            add(ve, new Point(1, 2, 3));
            break;
        }
        case DEL:
        {
            del(ve, 1);
            break;
        }
        case UPDATE:
        {
            update(ve, 1, 22);
            break;
        }
        case QUERY:
        {
            show(ve);
            break;
        }
        case EXIT:
        {
            cout << "exit system successfully" << endl;
            system("PAUSE");
            exit(0);
        }
        default:
        {
            cout << "input error! please input again!" << endl;
        }
        }
        system("PAUSE");
        save(ve);
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Demo2021

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值