C++快速实现简易增删改查系统

C++快速实现简易增删改查系统

#include<iostream>
//#include<algorithm>
//#include<functional>
#include<list>
#include<string>
using namespace std;
struct info
{
	string name;
	int age;
	string route;
	string team;
};
ostream& operator<<(ostream& out, info Object)
{
	out << Object.name << "\t" << Object.age << "\t" << Object.route << "\t" << Object.team << endl;
	return out;
}
istream& operator>>(istream& in, info& Object)//这里要修改Object中的信息,因而要传引用
{
	cout << "请输入游客相关信息:" << endl;
	cout << "name" << "\t" << "age" << "\t" << "route" << "\t" << "team" << endl;
	in >> Object.name >> Object.age >> Object.route >> Object.team;
	return in;
}
//采用类去写
class travelSystem
{
public:
	//增删改查
	void inputInfo(info Object);		//输入
	void outputInfo();					//输出
	void searchInfo(int age);			//通过年龄进行查找	
	void searchInfo(string name);		//通过名字进行查找
	void modifyInfoByName(string name);	//修改
	int deleteInfoByName(string name);	//删除
	void printStyle();					//抬头样式
protected:
	list<info> myList;
};
void travelSystem::inputInfo(info Object)
{
	myList.push_back(Object);
}
void travelSystem::outputInfo()
{
	list<info>::iterator iter;
	printStyle();
	for (iter = myList.begin(); iter != myList.end(); iter++)
	{
		cout << *iter;
	}
}
int travelSystem::deleteInfoByName(string name)
{
	//一定要判断存在才能做删除
	int flag = 0;
	list<info>::iterator iter;
	for (iter = myList.begin(); iter != myList.end(); iter++)
	{
		//自定义类型,用迭代器遍历剥洋葱去做删除
		if (iter->name == name)
		{
			myList.erase(iter);
			flag = 1;
			return flag;		//只删除一次,删除完就返回			若在这里想多次删除,不进行返回
		}
	}
	return flag;		//返回0,未删除 
}
void travelSystem::modifyInfoByName(string name)//删除原有信息,原有信息不存在则添加新信息
{
	//删除原来的信息,再输入,再插入
	info newData;
	while (deleteInfoByName(name));		//删除成功返回1,继续进行删除操作。直到全部名字为name的信息删除
	cout << "输入新的信息:" << endl;
	cin >> newData;
	inputInfo(newData);
}
void travelSystem::printStyle()
{
	cout << "name" << "\t" << "age" << "\t" << "route" << "\t" << "team" << endl;
}
void travelSystem::searchInfo(int age)
{
	int flag = 0;
	list<info>::iterator iter;
	printStyle();
	for (iter = myList.begin(); iter != myList.end(); iter++)
	{
		if (iter->age == age)
		{
			cout << *iter;
			flag = 1;
		}
	}
	if (flag == 0)
	{
		cout << "无相关信息" << endl;
	}
}
void travelSystem::searchInfo(string name)
{
	int flag = 0;
	list<info>::iterator iter;
	printStyle();
	for (iter = myList.begin(); iter != myList.end(); iter++)
	{
		if (iter->name == name)
		{
			cout << *iter;
			flag = 1;
		}
	}
	if (flag == 0)
	{
		cout << "无相关信息" << endl;
	}
}
void makeMenu()//制作菜单
{
	cout << "-----------------------------------------------" << endl;
	cout << "\t\t旅游管理系统" << endl;
	cout << "\t\t0.退出系统" << endl;
	cout << "\t\t1.插入信息" << endl;
	cout << "\t\t2.浏览信息" << endl;
	cout << "\t\t3.删除信息" << endl;
	cout << "\t\t4.修改信息" << endl;
	cout << "\t\t5.查找信息" << endl;
	cout << "-----------------------------------------------" << endl;
	cout << "请输入(0—5):" << endl;
}

void keyDown(travelSystem* mySystem)//按键响应
{
	int userKey;
	cin >> userKey;
	info tempData;
	switch (userKey)
	{
	case 0:
		cout << "正常退出" << endl;
		system("pause");
		exit(0);
		break;
	case 1:
		cin >> tempData;
		mySystem->inputInfo(tempData); 
		break;
	case 2:
		mySystem->outputInfo();
		break;
	case 3:
		cout << "请输入要删除的人员姓名:" << endl;
		cin >> tempData.name;
		if (mySystem->deleteInfoByName(tempData.name))
		{
			cout << "删除成功" << endl;
		}
		else
		{
			cout << "不存在相关信息,无法删除" << endl;
		}
		break;
	case 4:
		cout << "请输入要修改人员的的姓名:";
		cin >> tempData.name;
		mySystem->modifyInfoByName(tempData.name);
		break;
	case 5:
		cout << "1.按年龄查找\t2.按姓名查找" << endl;
		cout << "请输入查找方式(1or2)" << endl;
		cin >> userKey;
		if (userKey == 1)
		{
			cout << "请输入年龄:" << endl;
			cin >> tempData.age;
			mySystem->searchInfo(tempData.age);
		}
		else if (userKey == 2)
		{
			cout << "请输入姓名:" << endl;
			cin >> tempData.name;
			mySystem->searchInfo(tempData.name);
		}
		else
		{
			cout << "输入错误,无效操作" << endl;
			break;
		}
	default:
		cout << "输入错误!,请重新操作" << endl;
		break;
	}
}
int main()
{
	travelSystem* Object = new travelSystem;
	while (1)
	{
		makeMenu();
		keyDown(Object);
		cout << "选择你的操作(0-5)" << endl;
		system("pause");
		system("cls");
	}

	return 0;
}
发布了57 篇原创文章 · 获赞 12 · 访问量 3293

猜你喜欢

转载自blog.csdn.net/weixin_44795839/article/details/104228250