C++学习之简易的学生管理系统

翔翔

C++学习之简易的学生管理系统

目录

1. 功能介绍
2. 功能代码
3. 外部连接库

1.功能介绍

主要实现的功能点包括增加学生信息、删除学生信息、查询学生信息、修改学生信息。数据保存在json文件中,读取和写入json的时候需要第三方的库。

2.功能代码

首先需要准备Student的基础类,该类主要包括学生的信息,学生号(ID)、学生名字(Name)、学生性别(Sex)、政治面貌(PoliticalStatu)等。此类中需要重载"==",以便后续用作查询时的操作。如果不重载该方法,会直接报错。可参考博客:https://blog.csdn.net/K777Z333/article/details/81358200
其次要准备StudentManage的类,学生管理类主要包括查询、增加、删除、保存。
首先我们将原先存储在json文件中的学生读取出来,如果没有该json则新建一个空的json文件。主要代码如下:

	string fullPath = RootPath();
	fullPath += filePath;
	if (!FileExist(fullPath)) {
		ofstream fout(fullPath);
		if (fout) {
			std::cout << "ok" << endl;
		}
		fout.close();
	}
	Json::Reader reader;
	Json::Value root;
	ifstream in(fullPath, ios::binary);
	if (in.is_open()) {
		if (reader.parse(in, root)) {
			for (Json::Value::ArrayIndex i = 0; i != root.size(); i++) {
				Json::Value temp = root[i];
				Student ss;
				ss.ID = temp["id"].asInt();
				ss.Name = temp["name"].asString();
				ss.Address = temp["address"].asString();
				ss.Sex = temp["sexs"] == 0 ? man : woman;
				ss.PoliticalStatu = temp["politivalstatu"] == 0 ? ZGDY : (temp["politivalstatu"] == 1 ? ZGYBDY : GQTY);
				Students.push_back(ss);
			}
		}
	}

从json中读取出来的Student是存储在Students的顺序容器(vector)中。

  • 增加学生信息

初始化一个Student类,然后给变量赋值。最后通过方法Add添加到Students中。此时学生号根据Students中已有学生自动增加1 。代码如下:

void StudentManage::Add(Student &student)
{
	student.ID = Students.size() == 0 ? 1 : Students[Students.size() - 1].ID;
	Students.push_back(student);
}
  • 查询学生信息

查询学生信息可根据学生名字、学号和全部信息来查询,不同查询返回的结果不一样。

  • 根据学生名字查询

由于学生有可能存在重名的情况,此时返回的是一个数组。

/*按照名字查询*/
vector<Student> StudentManage::Query(string &name)
{
	return Query(Students.begin(), name);
}

/*按照名字查询,将名字一样的全部查询出来*/
vector<Student> StudentManage::Query(vector<Student>::iterator begin, const string & name)
{
	vector<Student> results;
	vector<Student>::iterator location_index = find(begin, Students.end(), name);
	if (location_index == Students.end()) {
		return results;
	}
	else
	{
		results.push_back(*location_index);
		vector<Student> temp = Query(location_index + 1, name);
		if (temp.size() > 0) {
			results.insert(results.end(), temp.begin(), temp.end());
		}
	}

	return results;
}

此时需要在Student类中重载 “==”,重载的代码如下:

bool Student::operator==(const string & name)
{
	return this->Name == name;
}
  • 根据学生号查询

在学校中学号是学生的唯一id,此时返回唯一值。

/*根据学生号查询,学生号也是唯一的。*/
vector<Student>::iterator StudentManage::Query(int &id)
{
	return find(Students.begin(), Students.end(), id);
}

此时需要在Student类中重载 “==”,重载的代码如下:

bool Student::operator==(const int & id)
{
	return this->ID == id;
}
  • 根据完整信息查询

此查询唯一的,返回的值也是唯一。

/*根据学生类查询,返回唯一对应的学生*/
vector<Student>::iterator StudentManage::Query(Student &student)
{
	return find(Students.begin(), Students.end(), student);
}

此时需要在Student类中重载 “==”,重载的代码如下:

bool Student::operator==(const Student &student)
{
	return this->ID == student.ID && this->Name == student.Name && this->Address == student.Address && this->PoliticalStatu == student.PoliticalStatu;
}
  • 修改学生信息

修改学生信息主要修改名字、性别、地址和政治面貌等,修改的主要代码如下:

/*传入待修改的Student(olds),修改信息Student(news)*/
void StudentManage::Modify(Student&olds, Student&news)
{
	vector<Student>::iterator index = Query(olds);
	(*index).Name = news.Name;
	(*index).Sex = news.Sex;
	(*index).Address = news.Address;
	(*index).PoliticalStatu = news.PoliticalStatu;
}
  • 删除学生信息

将指定的Student删除,由于指定的Student唯一所以只会删除一个,如果要批量删除那么请循环使用该方法。

/*删除指定的学生*/
void StudentManage::Delete(Student &student)
{
	vector<Student>::iterator index = Query(student);
	Students.erase(index);
}
  • 保存数据到本地

前面做的增加、查询、修改和删除的这些方法都没有保存数据到本地,如果需要每次做了查询后都用方法Save保存到本地。保存时,会将原来的json文件里面的数据给全部覆盖。OsWrite方法强制覆盖。

void StudentManage::Save(void)
{
	Json::StyledWriter sw;

	string fullPath = RootPath();
	fullPath += filePath;

	Json::Reader reader;
	Json::Value root;
	for (auto s : Students) {
		Json::Value value;
		value["name"] = Json::Value(s.Name);
		value["id"] = Json::Value(s.ID);
		value["address"] = Json::Value(s.Address);
		value["sexs"] = Json::Value(s.Sex);
		value["politivalstatu"] = Json::Value(s.PoliticalStatu);
		root.append(value);
	}
	ofstream OsWrite(fullPath);
	OsWrite << sw.write(root);
	OsWrite.close();
}
  • 3.外部连接库

该程序中用到解析json的库叫jsoncpp,该库可以在gitghub上下载使用:https://github.com/open-source-parsers/jsoncpp
jsoncpp的api说明:jsoncpp api说明

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值