64,654
社区成员
发帖
与我相关
我的任务
分享
#ifndef PEOPLE_CLASS
#define PEOPLE_CLASS
/*
*人员类
*操作单个人
*只提够默认构造函数,重载了输入输出
*/
class People
{
public:
//重载输入操作
friend std::istream& operator>>(std::istream &in, People &people);
//重载输出操作
friend std::ostream& operator<<(std::ostream &os, People &people);
People();
People(const People &people);
People& operator=(const People &people);
//查找是否存在某个关键字,查询失败返回NULL
People* find(std::string strFind);
private:
std::string strNumber; //学号
std::string strName; //姓名
std::string strClass; //班级
std::string strCollege; //学院
std::string strPho; //电话
std::string strQQ; //QQ号码
};
#endif
#include <iostream>
#include <string>
#include "People.h"
People::People():strNumber(""), strName(""), strClass(""), strCollege(""), strPho(""), strQQ("")
{}
People::People(const People &people):strNumber(people.strNumber), strName(people.strName), strClass(people.strClass),strCollege(people.strCollege), strPho(people.strPho), strQQ(people.strQQ)
{}
People& People::operator =(const People &people)
{
strNumber = people.strNumber;
strName = people.strName;
strClass = people.strClass;
strCollege = people.strCollege;
strPho = people.strPho;
strQQ = people.strQQ;
return *this;
}
std::istream& operator>>(std::istream& in, People &people)
{
std::cout << "学号:";
in >> people.strNumber;
std::cout << "姓名:";
in >> people.strName;
std::cout << "班级:";
in >> people.strClass;
std::cout << "学院:";
in >> people.strCollege;
std::cout << "电话:";
in >> people.strPho;
std::cout << "Q Q :";
in >> people.strQQ;
return in;
}
std::ostream& operator<<(std::ostream& os, People &people)
{
os << "学号:" << people.strNumber << "\t姓名:" << people.strName
<< "\t班级:" << people.strClass << "\t学院:" << people.strCollege
<< "\t电话:" << people.strPho << "\tQQ:" << people.strQQ;
return os;
}
People* People::find(const std::string strFind)
{
if (strNumber == strFind || strName == strFind ||
strClass == strFind || strCollege == strFind || strPho == strFind || strQQ == strFind)
return this;
else
return NULL;
}
#ifndef MANAGEPEOPLE_CLASS
#define MANAGEPEOPLE_CLASS
#include "People.h"
/*
* 管理People,对多个People的封装
* 对管理系统的封装
*/
class ManagePeople
{
public:
friend std::istream& operator>>(std::istream &is, ManagePeople &people);
friend std::ostream& operator<<(std::ostream &os, const ManagePeople &people);
ManagePeople();
ManagePeople(const ManagePeople& people);
ManagePeople& operator=(const ManagePeople &people);
void show();
private:
//显示菜单
void showMenu();
//显示所有学生信息
void showStudent();
//增加学生信息
void addStudent();
//删除学生信息
void delStudent();
//查找学生信息,成功查找返回指向改学生信息的迭代器
std::vector<People>::iterator find();
//销毁容器
void destroyManage();
private:
std::vector<People> all_people; //存储所有人信息
int index; //存储你输入的选项
};
#endif
#include <iostream>
#include <vector>
#include <string>
#include <windows.h>
#include "ManagePeople.h"
#include "People.h"
ManagePeople::ManagePeople():all_people()
{}
ManagePeople::ManagePeople(const ManagePeople &people):all_people(people.all_people)
{}
ManagePeople& ManagePeople::operator=(const ManagePeople &people)
{
this->all_people = people.all_people;
return *this;
}
void ManagePeople::showMenu()
{
system("cls");
std::cout << "\t\t\t\t学生管理系统\n" << std::endl;
std::cout << "\t\t\t 1.显示所有学生信息" << std::endl;
std::cout << "\t\t\t 2.添加学生信息" << std::endl;
std::cout << "\t\t\t 3.删除学生信息" << std::endl;
std::cout << "\t\t\t 4.初始化系统(慎用)" << std::endl;
std::cout << "\t\t\t 5.退出系统" << std::endl;
std::cout << "\n\t\t\t请选择(1,2,3,4,5):";
std::cin >> index;
}
std::vector<People>::iterator ManagePeople::find()
{
system("cls");
std::string strFind;
std::cout << "请输入查找关键字:" << std::endl;
std::cin >> strFind;
for (std::vector<People>::iterator iter = all_people.begin(); iter != all_people.end(); ++iter)
{
if (iter->find(strFind))
return iter;
}
return all_people.end();
}
void ManagePeople::delStudent()
{
std::vector<People>::iterator iter = find();
if (iter == all_people.end())
{
std::cout << "没有找到相关信息" << std::endl;
}
else
{
all_people.erase(iter);
}
}
void ManagePeople::showStudent()
{
system("cls");
for (std::vector<People>::iterator iter = all_people.begin(); iter != all_people.end(); ++iter)
{
std::cout << *iter << std::endl;
}
}
void ManagePeople::addStudent()
{
People people;
std::cin >> people;
all_people.push_back(people);
}
void ManagePeople::destroyManage()
{
system("cls");
std::string str;
std::cout << "确认初始化系统?(Y/N)" << std::endl;
std::cin >> str;
if (str == "n" || str == "N")
return;
std::cout << "\n强制关闭系统可结束系统初始化\n" << std::endl;
while (index++ != 83)
{
Sleep(90);
std::cout << "<";
}
all_people.erase(all_people.begin(), all_people.end());
std::cout << "\t\t\t\t系统初始化成功" << std::endl;
system("pause");
}
void ManagePeople::show()
{
while (true)
{
showMenu();
switch(index)
{
case 1:
showStudent();
system("pause");
break;
case 2:
addStudent();
break;
case 3:
delStudent();
break;
case 4:
destroyManage();
break;
case 5:
exit(0);
}
}
}