c++学生兴趣管理系统(仅作学术探讨,请勿搬运抄袭)

学生兴趣管理系统。

码这个还是相当辛苦的,因为想着赶紧码完,连着构思到最后码完测试后通过,就花了三天,每天呆在床上(床上舒服)码代码,思路阻塞就出去散步,一边走一边疏通思路。还好也算是挺顺利的,中间也曾为一个返回参数的问题,debug了四五个小时,现在想起来也只有苦笑啊2333333。说实话,我自己还是挺享受一个人随心所欲自由自在做自己想做的事的,不然凭我的意志断然是不能一连高度集中注意力工作这么久的。

其实我写这个博客,最初是想,肯定有很多人要做兴趣管理系统,只要我抓紧写一篇,就肯定有人会进来看,这样不就能涨粉了么哈哈哈哈,不过最后也释然了,有没有人看也无所谓了,有没有人点赞也还好,也不是说想要见证一下我的成长历史。只是单纯的想写一篇,我热爱编程,热爱现在努力做自己想做的事的自己,所以我做了。


回归正传,谈点关于代码的事。由于界面我想用QT做(我知道QT药丸了),最开始想用uwp的,但是老师的电脑都装着xp,我也很绝望的。由于想用QT做界面,所以所有查找失败的这些异常我统统没有处理,全部是写了注释后就返回false,因为我想的是用弹窗来处理异常的。另外一点,我个人代码风格就是名称都写的特别长,刚看起来特别费劲,但是名字是很容易理解的,这让我的记忆负担小了很多。期间遇到了很多问题,不一一赘述,文档我也不打算写了,注释够详细了,主要是分享一下思路吧。


说一点整体结构的问题,学生兴趣管理系统,要求可以删改学生信息,可以删改兴趣类和兴趣。最开始我大脑的想法就是用继承,兴趣继承兴趣类,然后具体实现,但是马上就否决了。其实只要是读过课本的都知道,像兴趣,兴趣类这种都是被拿来当作继承的范本来讲的,但是这里不同,如果把每一个兴趣类当作一个类,除非用template,但是那样很无趣,一是复杂,二是无用,因为兴趣不会很多,主要是要存储人的信息。所以我果断选择了管理类-》兴趣类-》兴趣这样的包含结构,即兴趣作为兴趣类的一个属性,然后取声明对象,给不同的对象命名来达到不同的兴趣。从效果来看我的想法应该是没错的。下面给出思路图和代码。思路图是最开始写好的,后面编程的时候又加了很多方法进去




先把兴趣类和学生类的头文件放上来,我都给了注释,我觉得已经够清晰了,所以就不写文档了,本来兴致勃勃,想的是第一个完整软件,想写文档,但是奈何太累了,就算了。

语言:c++,编程环境:vs2017

#ifndef STUDENTMANA
#define STUDENTMANA

#include<iostream>
#include<string>
#include<Windows.h>
#include<deque>
#include<list>
#include<vector>
#include<map>
#include<fstream>
#include<sstream>
using namespace std;

#endif // !STUDENTMANA

///<summary>
/// 功能    :学生信息管理系统文件,之前头文件和cpp是分开的,后来出了些问题,就干脆放一起了,包括学生信息增加修改,和创建唯一编码
/// 更新人  :林夕
/// 更新时间:2018年
///</summary> 

struct inteNumber {//由于兴趣管理采用的时三层封装,兴趣作为兴趣类的一个属性的方式,所以单独为兴趣类和兴趣做了一个结构体,
					//重载了==符号

	int inteClass;//兴趣类别编号
	int inte;//具体兴趣编号
	bool operator==(const inteNumber& cmp);
	inteNumber(int inteClass, int inte);
};




class student {
private:
	long id;
	string name;
	string sex;
	int age;
	int department;//专业,在父类学生管理类设置Map做转化 这里我最开始的设想,由于string比int多占用太多存储空间,人数过多的话会造成大量空间浪费
					//所以我就想将专业和兴趣设为整形,然后通过map做匹配,在需要的时候进行转换,当时想的很简单,实际上后来操作起来还是比较麻烦的
	string otherNote;
	int splNumber;//专一编码,考虑到名字可能重名,就干脆再设置一个内部编码,保证每个人编号不一样,可以区分
public:
	vector<inteNumber> inteNum; //设置inteNumber形式的vector,用来保存每个学生的兴趣项
	student(long id, string name, string sex, int age, int department, string othernote);//构造函数
	student(const student &b);
	void setSplNumber();//设置特殊编码
	int static getSplNumberByName(string);//此函数设置为静态的,可以方便的通过姓名获取专一编码
	inline int getDepartmentNum() { return department; }
	inline student* getStuPtr() {  return this; }//返回该对象的指针
	inline int getSplNumber() { return splNumber; }
	inline string getName() { return this->name; }
	inline long getId() { return this->id; }
	inline string getSex() { return this->sex; }
	inline int getAge() { return this->age; }
	inline string getOtherNote() { return this->otherNote; }
	bool modifyInfo(long id, string name, string sex, int age, int department, string othernote);
	bool addInte(int inteClass,int inte);//增加兴趣
	bool delInte(int inteClass,int inte);
};


class studentMana {
private:

public:
	//属性
	int static sumDepartment;//总的专业数量
	int static sumStudent;//总人数
	vector<student> stu;//将所有学生用数组存起来,方便查询
	map<int, string> numDepartmentMap;//设置map,说实话,这一点没有python好,map不能方便的获取value的集合,只能通过迭代器遍历,我就干脆设置了一正一反两个map结合起来用,牺牲空间复杂换时间复杂度
	map<string, int> departmentNumMap;
	map<int, student*> stuMap;//老出问题,问题还没找到,先弃之不用,等整体完成了再来修补好了。不过百分之八十是弃之不用了
								//这个传指针总是传成了一个随机指针,我很苦恼,等我哪天发神经,我会回来debug的
	//方法							 				
	studentMana();
	bool setDepartment(string departmentName);
	bool addStudent(long id, string name, string sex, int age, string departmentName, string othernote);//后期改进的时候可以添加重复学生检测
	bool delStudentByName(string name);
	student* searchStudentByName(string);//后期改进代码时,考虑学生可能有重名的
	student* searchStudentById(int);
	vector<student> searchStudentByDepartmentName(string);
	bool modifyStudentInfo(int splNumber);
	bool addInte(string name, int inteClass,int inte);
	bool delInte(string name, int inteClass, int inte);
	void getDataFromFile();//建立本机文件,获取数据
	void setData2File();//将数据重新存储
	void showAllInfo();
};



#ifndef STUDENTMANA
#define STUDENTMANA

#include<iostream>
#include<string>
#include<Windows.h>
#include<deque>
#include<list>
#include<vector>
#include<map>
#include<fstream>
#include<sstream>
using namespace std;

#endif // STUDENTMANA


class inte {
private:
	string inteName;
	int inteSplNum;//inteSplNum=>interesting Special Number兴趣特殊编码,后面的inteClassSplNum就是兴趣类的特殊编码,和学生一样,为了节约空间,设置的特殊编码
	int sumInteStudent;
	vector<int> studentSplNumVector;//这个用来存储选择该兴趣项的所有学生的特殊编码,一样的为了节省空间设置为int型
public:
	
	inte();
	bool addStudentNum(int);
	inline vector<int> getStudentSplNum() { return this->studentSplNumVector; }
	void setInteName(string);
	inline string getInteName() { return inteName; }
	bool delStudentNum(int);
	void setInteSplNum(int);
	bool operator == (const inte& cmp);//重载==运算符,这个不要轻易重载,由于我只在迭代器时要用,所以问题不大
	bool operator == (const int);
	bool operator == (const string);
	inline int getInteSplNum() { return this->inteSplNum; }//获取该兴趣的特殊编码
	inline inte* getIntePtr() { return this; }//获取该对象的指针,实践证明,比引用好用多了
};

class inteClass {
private:
	string inteClassName;//兴趣类的名称
	int inteClassSplNum;
	int sumInte;//该兴趣类下的兴趣总数
public:
	vector<inte> inteVector;//将该兴趣类下的兴趣用数组全部存储下来
	map<int, string>splNumInteMap;//map,用来将兴趣的特殊编码和名字之间进行转换
	map<string, int>InteSplNumMap;//名字的意思解释一下:inteSplNumMap=>interesting Special Number Map兴趣转特殊编码map
	inteClass();
	void setInteClassName(string);
	inline string getInteClassName() { return this->inteClassName; }
	void setInteClassSplNum(int);
	inline int getInteClassSplNum() { return this->inteClassSplNum; }
	inte* searchInteByInteSplNum(int);//通过特殊编码来获得兴趣的指针
	inte* searchInteByInteName(string);
	bool addInte(string);//增加一个兴趣
	bool delInteBySplNum(int inteSplNum);
	bool delInteByName(string inteName);
	vector<vector<int>> getInteClassStudentNum();//获取该兴趣类下的所有学生,这个结构很复杂,虽然实现了,但后来并没有用这个
	inline vector<inte> getInteVector() { return inteVector; }
	inline inteClass* getInteClassPtr() { return this; }
	bool operator == (const inteClass& cmp);//一样只用在迭代器里,所以就重载了
	bool operator == (const int);
	bool operator == (const string);
	bool addStudentNum(string inteName, int splNum);//为一个兴趣项添加学生编码
	bool delStudentNum(string inteName, int splNum);
};


class inteClassMana {
public:
	vector<inteClass> inteClassVector;//存储所有兴趣类
	int sumInteClass;//所有兴趣类的总数
	inteClassMana();
	map<int, string>splNumInteClassMap;//兴趣类的特殊编码到名称的转换map
	map<string, int>inteClassSplNumMap;

	void getDataFromFile();//从文件中载入数据
	void setData2File();//保存数据
	int getInteSplNumByName(string inteClassName, string inteName);//通过兴趣的名字获取特殊编码
	string getInteNameBySplNum(int inteClassSplNum, int inteSplNum);//通过兴趣的特殊编码获取名字
	int getInteClassSplNumByName(string inteClassName);//通过兴趣类的名字获取特殊编码
	string getInteClassNameBySplNum(int inteClassSplNum);//通过兴趣类的特殊编码获取名字
	bool addInte(string inteClassName,string intName);//添加兴趣
	bool delInteClassByName(string);//通过名字删除兴趣类
	bool delInteClassByInteClassSplNum(int);//通过兴趣类的特殊编码删除兴趣类
	bool delInteByName(string inteClassName, string inteName);
	vector<vector<vector<int>>> getInteClassManaStudentNum();//获取所有学生的特殊编码,我真的不知道自己脑子是咋想的,整出个这么复杂的结构,我自己些迭代器都理了好半天
	inline vector<inteClass> getInteClassVector() { return inteClassVector; }//获取兴趣类的数组
	inteClass* searchInteClassByInteClassSplNum(int);//通过兴趣类的特殊编码找到兴趣类
	inteClass* searchInteClassByInteClassName(string);
	bool addStudentNum(string inteClassName, string inteName, int studentSplNum);//为一个兴趣类下的某个兴趣添加一个学生
	bool delStudentNum(string inteClassName, string inteName, int studentSplNum);
	void showAllInfo();//展示所有信息,测试用
};


///<summary>
/// 功能    :从中午回来就一直在码代码,由于之前思路已经很清晰了,过程还算顺利,晚上七点前完成了兴趣管理部分的代码
 /// 更新人  :林夕
/// 更新时间:2018年2月28晚10:45
///</summary> 

///<summary>
/// 功能    :又坐在床上一天(屁股坐痛了)搞定了所有代码,接下来只要完成界面再结合起来就好了,本兴趣管理项目历时三天,自我感觉非常棒,我乐在其中
/// 更新人  :林夕
/// 更新时间:2018年3月1日
///</summary>   
				

头文件实现:

#include"Student.h"

bool inteNumber::operator==(const inteNumber& cmp)
{
	if (this->inte == cmp.inte&&this->inteClass == cmp.inteClass)
		return true;
	return false;
}

inteNumber::inteNumber(int inteClass, int inte)
{
	this->inteClass = inteClass;
	this->inte = inte;
}

void student::setSplNumber()
{
	int s = 0;
	int m = 0;
	if (name != "") {
		for (int i = 0; i < name.length(); i += 2)
		{
			unsigned char a = name[i];
			unsigned char b = name[i + 1];
			m = (a - 176) * 94 + (b - 161);
			s *= 10;
			s += m;
		}
	}//每个学生有一个特殊编码,这个特殊编码是用一个哈希函数用名字生成的,这个哈希函数时网上搜的,据说很牛逼
	this->splNumber = s;

}//设置特殊编码

int student::getSplNumberByName(string name)
{
	int s = 0;
	int m = 0;
	if (name != "") {
		for (int i = 0; i < name.length(); i += 2)
		{
			unsigned char a = name[i];
			unsigned char b = name[i + 1];
			m = (a - 176) * 94 + (b - 161);
			s *= 10;
			s += m;
		}
	}//静态函数生成特殊编码
	return s;
}

student::student(long id, string name, string sex, int age, int department, string othernote)
{
	this->id = id;
	this->name = name;
	this->sex = sex;
	this->age = age;
	this->department = department;
	this->otherNote = othernote;
	setSplNumber();
}

student::student(const student &stu)
{
	this->id = stu.id;
	this->name = stu.name;
	this->sex = stu.sex;
	this->age = stu.age;
	this->department = stu.department;
	this->otherNote = stu.otherNote;
	this->inteNum = stu.inteNum;
	setSplNumber();
}//拷贝构造,老实说,如果不涉及指针应该不需要拷贝构造的

bool student::modifyInfo(long id, string name, string sex, int age, int department, string othernote)
{
	this->id = id;
	this->name = name;
	this->sex = sex;
	this->age = age;
	this->department = department;
	this->otherNote = othernote;
	return true;
}//整体修改学生信息

bool student::addInte(int inteClass,int inte)
{
	vector<inteNumber>::iterator ite;
	inteNumber temp(inteClass, inte);
	ite = find(inteNum.begin(), inteNum.end(), temp);

	if (ite != inteNum.end())
	{
		//该学生已经有该兴趣,添加失败
		return false;
	}
	else
	{
		inteNum.push_back(temp);
		return true;
	}

}//增加一个兴趣


bool student::delInte(int inteClass,int inte)
{
	inteNumber temp(inteClass, inte);
	vector<inteNumber>::iterator ite;
	ite = find(inteNum.begin(), inteNum.end(), temp);
	if (ite == inteNum.end())
	{
		//没有该兴趣项,不用删除
		return false;
	}
	else
	{
		inteNum.erase(ite);
		return true;
	}
	
}//删除一个兴趣项



int studentMana::sumStudent = 0;
int studentMana::sumDepartment = 0;

void studentMana::getDataFromFile()
{
	ifstream ifs("StudentsInterestingManageSystem-Data.txt");
	char str[100];
	string s;
	long id; string name; string sex; int age; string departmentName; string othernote;
	int count = 0;
	while (ifs.getline(str, 100))
	{
		istringstream _is(str);
		_is >> id >> name >> sex >> age >> departmentName >> othernote;
		addStudent(id, name, sex, age, departmentName, othernote);
		int inteClass;
		int inte;
		while(_is >> inteClass >> inte)
			addInte(name, inteClass, inte);
		
	}
	ifs.close();
}

void studentMana::setData2File()
{
	ofstream ofs("StudentsInterestingManageSystem-Data.txt");
	vector<student>::iterator ite;
	map<int, string>::iterator ite1;
	for (ite = stu.begin(); ite != stu.end(); ite++)
	{
		ite1 = numDepartmentMap.find(ite->getDepartmentNum());
		ofs << ite->getId() << " " << ite->getName() << " " << ite->getSex() << " " << ite->getAge() << " " << ite1->second << " " << ite->getOtherNote();
		vector<inteNumber>::iterator inteIte;
		for (inteIte = ite->inteNum.begin(); inteIte != ite->inteNum.end(); inteIte++)
		{
			ofs << inteIte->inteClass << " " << inteIte->inte << " ";
		}
		ofs << endl;
	}
	ofs.close();
}

studentMana::studentMana()
{
	//先留着,没想到要做什么事
	//那你留着干嘛
	//不知道,反正懒得删
	//那你打这么多字
	//要你管,你是谁啊
	//我是精分啊
}

bool studentMana::setDepartment(string departmentName)
{
	numDepartmentMap.insert(map<int, string>::value_type(sumDepartment, departmentName));
	departmentNumMap.insert(map<string, int>::value_type(departmentName, sumDepartment++));
	return true;
}

bool studentMana::addStudent(long id,string name,string sex,int age,string departmentName,string othernote)
{
	/*添加学生专业*/
	map<string, int>::iterator ite;
	ite = departmentNumMap.find(departmentName);
	int departmentNum;
	if (ite != departmentNumMap.end())
	{
		 departmentNum = ite->second;
	}
	else
	{
		setDepartment(departmentName);
		ite = departmentNumMap.find(departmentName);
		 departmentNum = ite->second;
	}//在字典中查找专业名称,如果有,则返回专业代码,没有则插入专业并返回专业代码。
	
	/*创建学生信息*/
	student a(id, name, sex, age, departmentNum, othernote);
	map<int, student*>::iterator ite1;
	
	stu.push_back(a);
	stuMap.insert(map<int, student*>::value_type(stu[0].getSplNumber(), stu[0].getStuPtr()));//在map中插入学生信息

	sumStudent++;
	return true;
}

bool studentMana::delStudentByName(string name)
{
	int splNum = student::getSplNumberByName(name);
	map<int, student*>::iterator ite;
	vector<student>::iterator iteStu;
	for (iteStu = stu.begin(); iteStu != stu.end();iteStu++)//从stu vector中删除这个学生的信息。
	{

		if (iteStu->getName()==name)
		{
			sumStudent--;
			stu.erase(iteStu);
			return true;
		}
	}

	return true;
}

student* studentMana::searchStudentById(int _id)
{
	vector<student>::iterator ite;
	for (ite = stu.begin(); ite != stu.end();ite++)
	{
		if (ite->getId() == _id)
		{
			return ite->getStuPtr();
		}
	}
	//没有找到该学生
	return nullptr;
}

student* studentMana::searchStudentByName(string _name)
{
	vector<student>::iterator ite;
	for (ite = stu.begin(); ite != stu.end(); ite++)
	{
		if (ite->getName() == _name)
		{
			return ite->getStuPtr();
		}
	}
	//没有找到该学生
	return nullptr;
}

vector<student> studentMana::searchStudentByDepartmentName(string _departmentName)
{
	vector<student> stuTemp;
	int _departmentNum;
	cout << _departmentName;
	map<string, int>::iterator iteDep = departmentNumMap.find(_departmentName);
	if (iteDep == departmentNumMap.end())
	{
		
		//没找到
		return stuTemp;
	}
	else
	{
		_departmentNum = iteDep->second;
	}
	
	vector<student>::iterator ite;
	for (ite = stu.begin(); ite != stu.end(); ite++)
	{
		if (ite->getDepartmentNum() == _departmentNum)
		{
			stuTemp.push_back(*ite);
		}
	}//找到了返回所有为该专业的人的指针
	return stuTemp;
}

bool studentMana::modifyStudentInfo(int _splNumber)
{

	long id; string name; string sex; int age; string departmentName; string othernote;
	cin >> id >> name >> sex >> age >> departmentName >> othernote;
	/*添加学生专业*/
	map<string, int>::iterator ite;
	ite = departmentNumMap.find(departmentName);
	int departmentNum;
	if (ite != departmentNumMap.end())
	{
		departmentNum = ite->second;
	}
	else
	{
		setDepartment(departmentName);
		ite = departmentNumMap.find(departmentName);
		departmentNum = ite->second;
	}//在字典中查找专业名称,如果有,则返回专业代码,没有则插入专业并返回专业代码。


	vector<student>::iterator iteStu;
	for (iteStu = stu.begin(); iteStu != stu.end(); ite++)
	{
		if (iteStu->getSplNumber() == _splNumber)
		{
			iteStu->modifyInfo(id, name, sex, age, departmentNum, othernote);
			return true;
		}
	}
	//没查找到,修改不成功
	return false;
}

bool studentMana::addInte(string name, int inteClass, int inte)
{
	student* temp;
	temp = searchStudentByName(name);
	if (temp == nullptr)
	{
		//寻找失败,没有此人
		return false;
	}
	else
	{
		temp->addInte(inteClass, inte);
		return true;
	}
}

bool studentMana::delInte(string name, int inteClass, int inte)
{
	student* temp;
	temp = searchStudentByName(name);
	if (temp == nullptr)
	{
		//寻找失败,没有此人
		return false;
	}
	else
	{
		temp->delInte(inteClass, inte);
		return true;
	}
}

void studentMana::showAllInfo()
{
	vector<student>::iterator ite;
	for (ite = stu.begin(); ite != stu.end(); ite++)
	{
		map<int, string>::iterator iteDep;
		iteDep = numDepartmentMap.find(ite->getDepartmentNum());
		cout << endl << "id:" << ite->getId() << " name:" << ite->getName() << " age:" << ite->getAge() << " department:" << iteDep->second << " notes:" << ite->getOtherNote();
		vector<inteNumber>::iterator _ite;
		for (_ite = ite->inteNum.begin(); _ite != ite->inteNum.end(); _ite++)
		{
			cout << endl << "\tinteClass:" << _ite->inteClass << " inte" << _ite->inte;
		}
	}


	map<int, string>::iterator ite1;
	for (ite1 = numDepartmentMap.begin(); ite1 != numDepartmentMap.end(); ite1++)
	{
		cout << endl << "ite1DepartNUM:" << ite1->first << "  ite1Name:" << ite1->second;
	}
	cout << endl << " sumStudents:" << sumStudent << " sumDepartment:" << sumDepartment;
}



/*
	//测试用例:
	studentMana _s;
	cout << "1:";
	_s.addStudent();
	cout << endl << "2:";
	_s.addStudent();
	cout << endl << "3:";
	_s.addStudent();
	_s.showAllInfo();
	cout << " departmentNUM:" << _s.sumDepartment << " sumStudents:" << _s.sumStudent;
	//_s.delStudentByName("邓");//测试通过
	_s.showAllInfo();
	cout << " departmentNUM:" << _s.sumDepartment << " sumStudents:" << _s.sumStudent;
	
	//suPtr = _s.searchStudentByDepartmentName("信息安全");//测试通过

	student* ptr;
	//ptr = _s.searchStudentById(11);
	//ptr = _s.searchStudentByName("邓");//测试通过
	cout<<ptr->getAge();
	
	
	studentMana _s;

	_s.getDataFromFile();
	_s.showAllInfo();

	_s.addInte("邓", 01, 01);
	_s.showAllInfo();

	return 0;

	//_s.showAllInfo();
	//_s.setData2File();//写入文件,读取文件测试通过
	//return 0;
}
*/

///<summary>
/// 功能    :完成学生信息的修改工作,类返回指针还存在问题,这个问题肯定非常幼稚,因为我半天找不出来,下一步着手做界面和兴趣管理部分,测试全部通过
 /// 更新人  :林夕
/// 更新时间:2018年2月27日晚
///</summary>   


///<summary>
/// 功能    :unsigned char a =c[0];
//   unsigned char b = c[1];
//   int m = (a - 176) * 94 + (b - 161);
 /// 更新人  :林夕
/// 更新时间:2018年
///</summary>   

#include"InterestMana.h"


void inte::setInteName(string na)
{
	this->inteName = na;
	
}

void inte::setInteSplNum(int splNum)
{
	this->inteSplNum = splNum;
	
}

bool inte::addStudentNum(int studentSplNum)
{
	vector<int>::iterator ite;
	ite = find(studentSplNumVector.begin(), studentSplNumVector.end(), studentSplNum);
	if (ite != studentSplNumVector.end())
	{
		//该学生已经在里面了,不用添加
		return false;
	}
	else
	{
		studentSplNumVector.push_back(studentSplNum);
		sumInteStudent++;
		return true;
	}
}

bool inte::delStudentNum(int studentSplNum)
{
	vector<int>::iterator ite;
	ite = find(studentSplNumVector.begin(), studentSplNumVector.end(), studentSplNum);
	if (ite == studentSplNumVector.end())
	{
		//该学生已经不在里面了,不用删除
		return false;
	}
	else
	{
		studentSplNumVector.erase(ite);
		sumInteStudent--;
		return true;
	}
}

bool inte::operator==(const inte& cmp)
{
	if (cmp.inteName == this->inteName)
		return true;
	return false;
}

bool inte::operator==(const int inteSplNum)
{
	if (this->inteSplNum == inteSplNum)
		return true;
	return false;
}

bool inte::operator==(const string inteName)
{
	if (this->inteName == inteName)
		return true;
	return false;
}

inte::inte()
{
	inteSplNum = 0;
	sumInteStudent = 0;
}

inteClass::inteClass()
{
	inteClassName = "";
	inteClassSplNum = 0;
	sumInte = 0;
}

void inteClass::setInteClassName(string na)
{
	this->inteClassName = na;
}

void inteClass::setInteClassSplNum(int num)
{
	this->inteClassSplNum = num;
}

bool inteClass::addStudentNum(string inteName, int studentSplNum)
{
	inte* temp = searchInteByInteName(inteName);
	if (temp != nullptr)
	{//找到了该兴趣
		if (temp->addStudentNum(studentSplNum))
			return true;//添加成功
		else
			return false;//添加失败
	}
	return false;//没找到该兴趣

}

bool inteClass::delStudentNum(string inteName, int studentSplNum)
{
	inte* temp = searchInteByInteName(inteName);
	if (temp != nullptr)
	{//找到了该兴趣
		if (temp->delStudentNum(studentSplNum))
			return true;//删除成功
		else
			return false;//删除失败
	}
	return false;//没找到该兴趣
}

bool inteClass::addInte(string inteName)
{
	vector<inte>::iterator ite;
	ite = find(inteVector.begin(), inteVector.end(), inteName);
	if (ite != inteVector.end())
	{
		//已经有该兴趣向了
		return false;
	}
	else
	{
		inte temp;
		temp.setInteName(inteName);
		temp.setInteSplNum(inteVector.size());
		
		splNumInteMap.insert(map<int, string>::value_type(temp.getInteSplNum(), temp.getInteName()));
		InteSplNumMap.insert(map<string, int>::value_type(temp.getInteName(), temp.getInteSplNum()));
		
		inteVector.push_back(temp);
		sumInte++;
		return true;
	}

}

bool inteClass::delInteBySplNum(int inteSplNum)
{
	vector<inte>::iterator ite;
	ite = find(inteVector.begin(), inteVector.end(), inteSplNum);
	if (ite == inteVector.end())
	{
		//没有此兴趣项
		return false;
	}
	else
	{
		inteVector.erase(ite);
		sumInte--;

		map<string, int>::iterator ite1;
		for (ite1 = InteSplNumMap.begin(); ite1 != InteSplNumMap.end(); ite1++)
		{
			if (ite1->second == inteSplNum)
			{
				InteSplNumMap.erase(ite1);
				break;
			}
		}
		splNumInteMap.erase(inteSplNum);

		return true;
	}
}

bool inteClass::delInteByName(string inteName)
{
	vector<inte>::iterator ite;

	ite = find(inteVector.begin(), inteVector.end(), inteName);
	if (ite == inteVector.end())
	{
		//没有此兴趣项
		return false;
	}
	else
	{
		//找到此兴趣项,删除
		inteVector.erase(ite);
		sumInte--;

		map<int, string>::iterator ite1;
		for (ite1 = splNumInteMap.begin(); ite1 != splNumInteMap.end(); ite1++)
		{
			if (ite1->second == inteName)
			{

				splNumInteMap.erase(ite1);
				break;
			}
		}
		InteSplNumMap.erase(inteName);


		return true;
	}
}

inte* inteClass::searchInteByInteName(string inteName)
{
	inte* temp;
	vector<inte>::iterator ite;
	ite = find(inteVector.begin(), inteVector.end(), inteName);
	if (ite == inteVector.end())
	{
		temp = nullptr;
	}
	else
	{
		temp = ite->getIntePtr();
	}
	return temp;
}

inte* inteClass::searchInteByInteSplNum(int inteSplNum)
{
	inte* temp;
	vector<inte>::iterator ite;
	ite = find(inteVector.begin(), inteVector.end(), inteSplNum);
	if (ite == inteVector.end())
	{
		//没有此兴趣项
		temp = nullptr;
	}
	else
	{
		temp = ite->getIntePtr();
	}
	return temp;
}

bool inteClass::operator==(const inteClass& cmp)
{
	if (this->inteClassName == cmp.inteClassName&&this->inteClassSplNum == cmp.inteClassSplNum)
		return true;
	return false;
}
bool inteClass::operator==(const int inteClassSplNum)
{
	if (this->inteClassSplNum == inteClassSplNum)
		return true;
	return false;
}
bool inteClass::operator==(const string inteClassName)
{
	if (this->inteClassName == inteClassName)
		return true;
	return false;
}

vector<vector<int>> inteClass::getInteClassStudentNum()
{
	vector<vector<int>> temp;
	vector<inte>::iterator ite;
	for (ite = inteVector.begin(); ite != inteVector.end(); ite++)
	{
		temp.push_back(ite->getStudentSplNum());
	}
	return temp;
}

inteClassMana::inteClassMana()
{
	sumInteClass = 0;
}

int inteClassMana::getInteClassSplNumByName(string inteClassName)
{
	map<string, int>::iterator ite;
	ite = inteClassSplNumMap.find("inteClassName");
	if (ite != inteClassSplNumMap.end())
	{
		//找到了该兴趣类名
		return inteClassSplNumMap[inteClassName];
	}
	return NULL;
}

int inteClassMana::getInteSplNumByName(string inteClassName, string inteName)
{
	inteClass* tempPtr = searchInteClassByInteClassName(inteClassName);
	if (tempPtr == nullptr)
	{
		//没找到
		return NULL;
	}
	else
	{
		map<string, int>::iterator ite;
		ite = tempPtr->InteSplNumMap.find(inteName);
		if (ite != tempPtr->InteSplNumMap.end())
			return tempPtr->InteSplNumMap[inteName];
		else
			return NULL;
	}
}

string inteClassMana::getInteNameBySplNum(int inteClassSplNum, int inteSplNum)
{
	inteClass* tempPtr = searchInteClassByInteClassSplNum(inteClassSplNum);
	if (tempPtr == nullptr)
	{
		//没找到
		return "";
	}
	else
	{
		map<int, string>::iterator ite;
		ite = tempPtr->splNumInteMap.find(inteSplNum);
		if (ite != tempPtr->splNumInteMap.end())
			return tempPtr->splNumInteMap[inteSplNum];
		else
			return "";
	}
}

string inteClassMana::getInteClassNameBySplNum(int inteClassSplNum)
{
	map<int, string>::iterator ite;
	ite = splNumInteClassMap.find(inteClassSplNum);
	if (ite != splNumInteClassMap.end())
	{
		return splNumInteClassMap[inteClassSplNum];
	}
	return "";
}

bool inteClassMana::addInte(string inteClassName,string inteName)
{
	vector<inteClass>::iterator ite;
	ite = find(inteClassVector.begin(), inteClassVector.end(), inteClassName);
	if (ite != inteClassVector.end())
	{
		vector<inte>::iterator ite1;
		ite1 = find(ite->inteVector.begin(), ite->inteVector.end(), inteName);
		if (ite1 != ite->inteVector.end())
		{
			//已有该兴趣类和兴趣项
			return false;
		}
		else
		{
			ite->addInte(inteName);
			return true;
		}

	}
	else
	{
		inteClass temp;
		temp.setInteClassName(inteClassName);
		temp.setInteClassSplNum(inteClassVector.size());
		temp.addInte(inteName);
		inteClassVector.push_back(temp);
		inteClassSplNumMap.insert(map<string, int>::value_type(temp.getInteClassName(), temp.getInteClassSplNum()));
		splNumInteClassMap.insert(map<int, string>::value_type(temp.getInteClassSplNum(), temp.getInteClassName()));
		sumInteClass++;
		return true;
	}
}

bool inteClassMana::addStudentNum(string inteClassName, string inteName, int studentSplNum)
{
	inteClass* temp = searchInteClassByInteClassName(inteClassName);
	if (temp != nullptr)
	{
		if (temp->addStudentNum(inteName, studentSplNum))
			return true;//添加成功
		else
			return false;//添加失败
	}
	else
		return false;//没找到该兴趣类
}
bool inteClassMana::delStudentNum(string inteClassName, string inteName, int studentSplNum)
{
	inteClass* temp = searchInteClassByInteClassName(inteClassName);
	if (temp != nullptr)
	{
		if (temp->delStudentNum(inteName, studentSplNum))
			return true;//删除成功
		else
			return false;//删除失败
	}
	else
		return false;//没找到该兴趣类
}

bool inteClassMana::delInteClassByName(string inteClassName)
{
	vector<inteClass>::iterator ite;
	ite = find(inteClassVector.begin(), inteClassVector.end(), inteClassName);
	if (ite == inteClassVector.end())
	{
		//没有这个兴趣类
		return false;
	}
	else
	{
		inteClassVector.erase(ite);
		sumInteClass--;

		//删除map里的键值
		map<int, string>::iterator ite1;
		for (ite1 = splNumInteClassMap.begin();ite1 != splNumInteClassMap.end(); ite1++)
		{
			if (ite1->second == inteClassName)
			{
				splNumInteClassMap.erase(ite1);
				break;
			}
		}

		inteClassSplNumMap.erase(inteClassName);
		return true;
	}

}

bool inteClassMana::delInteByName(string inteClassName, string inteName)
{
	vector<inteClass>::iterator ite;
	ite = find(inteClassVector.begin(), inteClassVector.end(), inteClassName);
	if (ite == inteClassVector.end())
	{
		//没有这个兴趣类
		return false;
	}
	else
	{
		if(ite->delInteByName(inteName))
			return true;
		return false;
	}
}

bool inteClassMana::delInteClassByInteClassSplNum(int inteClassSplNum)
{
	vector<inteClass>::iterator ite;

	ite=find(inteClassVector.begin(), inteClassVector.end(), inteClassSplNum);
	if (ite==inteClassVector.end())
	{
		//没有这个兴趣类
		return false;
	}
	else
	{

		inteClassVector.erase(ite);
		sumInteClass--;

		//删除字典中键对值
		map<string, int>::iterator ite1;
		for (ite1 = inteClassSplNumMap.begin(); ite1 != inteClassSplNumMap.end(); ite1++)
		{
			if (ite1->second == inteClassSplNum)
			{
				inteClassSplNumMap.erase(ite1);
				break;
			}
		}
		splNumInteClassMap.erase(inteClassSplNum);
		return true;
	}
	
}
inteClass* inteClassMana::searchInteClassByInteClassSplNum(int inteClassSplNum)
{
	inteClass* temp;
	vector<inteClass>::iterator ite;
	ite = find(inteClassVector.begin(), inteClassVector.end(), inteClassSplNum);
	if (ite == inteClassVector.end())
	{
		temp = nullptr;
	}
	else
	{
		temp = ite->getInteClassPtr();
	}
	return temp;
}

inteClass* inteClassMana::searchInteClassByInteClassName(string inteClassName)
{
	inteClass* temp;
	vector<inteClass>::iterator ite;
	ite = find(inteClassVector.begin(), inteClassVector.end(), inteClassName);
	if (ite == inteClassVector.end())
	{
		temp = nullptr;
	}
	else
	{
		temp = ite->getInteClassPtr();
	}
	return temp;
}

vector<vector<vector<int>>> inteClassMana::getInteClassManaStudentNum()
{
	vector<vector<vector<int>>> temp;
	vector<inteClass>::iterator ite;
	for (ite = inteClassVector.begin(); ite != inteClassVector.end(); ite++)
	{
		temp.push_back(ite->getInteClassStudentNum());
	}
	return temp;
}

void inteClassMana::showAllInfo()
{
	vector<inte> _inteVector;
	vector<inteClass> _inteClassVector=getInteClassVector();
	vector<int> _studentVector;

	vector<inte>::iterator _inteIte;
	vector<inteClass>::iterator _inteClassIte;

	for (_inteClassIte = _inteClassVector.begin(); _inteClassIte != _inteClassVector.end(); _inteClassIte++)
	{
		cout << endl << _inteClassIte->getInteClassName() << ":";
		_inteVector = _inteClassIte->getInteVector();
		for (_inteIte = _inteVector.begin(); _inteIte != _inteVector.end(); _inteIte++)
		{
			cout << endl <<"\t"<< _inteIte->getInteName() << ":";
			_studentVector = _inteIte->getStudentSplNum();
			for (int i=0;i<_studentVector.size();i++)
			{
				cout << endl << "\t\tstudents:" << _studentVector[i];
			}
		}
	}
	
	//下面代码为测试兴趣类是否争取存储的代码
	/*vector<inteClass>::iterator _ite;
	for (_ite = inteClassVector.begin(); _ite != inteClassVector.end(); _ite++)
	{
		cout << _ite->getInteClassName() << "类有:\n";
		vector<inte>::iterator _ite1;
		for (_ite1 = _ite->inteVector.begin(); _ite1 != _ite->inteVector.end(); _ite1++)
		{
			cout << _ite1->getInteName() << endl;
		}
		cout << endl;
	}*/
}

void inteClassMana::getDataFromFile()
{
	ifstream ifs("InteData.txt");
	char str[1000];
	string s;
	string inteClassName;
	string inteName;
	int studentSplNum;
	while (ifs.getline(str, 1000))
	{
		istringstream _is(str);
		_is >> inteClassName >> inteName;
		addInte(inteClassName, inteName);
		while (_is >> studentSplNum)
		{
			addStudentNum(inteClassName, inteName, studentSplNum);
		}
	}
	ifs.close();
}

void inteClassMana::setData2File()
{
	ofstream ofs("InteData.txt");
	vector<inteClass>::iterator _inteClassIte;
	vector<inte>::iterator _inteIte;
	
	vector<inteClass> _inteClassVector = getInteClassVector();
	vector<inte> _inteVector;

	for (_inteClassIte = _inteClassVector.begin(); _inteClassIte != _inteClassVector.end(); _inteClassIte++)
	{

		_inteVector = _inteClassIte->getInteVector();
		for (_inteIte = _inteVector.begin(); _inteIte != _inteVector.end(); _inteIte++)
		{
			ofs << _inteClassIte->getInteClassName() << " " << _inteIte->getInteName() << " ";
			vector<int> _studentVector=_inteIte->getStudentSplNum();
			for (int i=0;i<_studentVector.size();i++)
			{
				ofs << _studentVector[i] << " ";
			}
			ofs << endl;
		}
		
	}
	ofs.close();
}

/*
	//下面为测试用例:
	inteClassMana _m;
	cout << " ";
	/*_m.addInte("球类", "羽毛球");
	_m.addInte("球类", "乒乓球");
	_m.addInte("田径", "跑步");
	_m.addInte("田径", "跳远");
	_m.addInte("其他", "游泳");
	_m.addInte("其他", "跳高");

	_m.addStudentNum("球类", "羽毛球", 5555);
	_m.addStudentNum("田径", "跑步", 1529);
	_m.setData2File();

	_m.getDataFromFile();
	_m.addStudentNum("球类", "羽毛球", 101);
	_m.showAllInfo();
	_m.addStudentNum("田径", "跑步", 24824564);
	_m.addStudentNum("其他", "跳高", 852);

	_m.showAllInfo();


	_m.delStudentNum("田径", "跑步", 1529);
	_m.showAllInfo();
	/*cout << "-----------------------\n";
	_m.delInteByName("球类", "羽毛球");
	_m.showAllInfo();
	cout << "-----------------------\n";
	_m.delInteClassByName("田径");
	_m.showAllInfo();
	cout << "-----------------------\n";
	int inteSplNum = InteSplNumMap["跳高"];
	inteClass* tempPtr = _m.searchInteClassByInteClassName("其他");
	tempPtr->delInteBySplNum(inteSplNum);
	_m.showAllInfo();
	cout << "-----------------------\n";
	int splNum = _m.inteClassSplNumMap["其他"];
	_m.delInteClassByInteClassSplNum(splNum);
	_m.showAllInfo();
	cout << "-----------------------\n";
	*/


最后加了一个管理类,方便互相调用


#include"Student.h"
#include"InterestMana.h"


int main()
{
	studentMana _s;
	inteClassMana _i;
	_i.addInte("球类", "羽毛球");
	_i.addInte("球类", "乒乓球");
	_s.addStudent(10, "邓", "男", 10, "信息安全", "没什么特别的");

	_i.addStudentNum("球类", "羽毛球", student::getSplNumberByName("邓"));
	_s.addInte("邓", _i.getInteClassSplNumByName("球类"), _i.getInteSplNumByName("球类","羽毛球"));

	cout << _i.searchInteClassByInteClassSplNum(_s.searchStudentByName("邓")->inteNum[0].inteClass)->getInteClassName();
	
	_i.showAllInfo();
	_s.showAllInfo();
	cout << "---------------";
	

	_s.getDataFromFile();
	_s.showAllInfo();
	return 0;
}


///<summary>
/// 功能    :新增一个管理cpp,用来将两个文件连起来,方便测试,测试全部通过
 /// 更新人  :林夕
/// 更新时间:2018年3月1日
///</summary>   


如果有人想用,自己写一个界面,套上我的源码就可以用了。看我多贴心,不点个赞么2333

  • 50
    点赞
  • 58
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值