#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);
}
}
基于容器对文件进行增删改查
最新推荐文章于 2024-04-30 20:06:58 发布