c++实现学生管理系统(附源码)

目录

一、基本功能:

二、包含的模块:

三、系统介绍

1. 学生管理系统的功能:

2. 具体的需求:

3. 支持的标准:

四、系统结构功能图:

五、系统设计

1. 退出系统:

2. 增加学生:

3. 展示学生:

4. 删除学生:

5. 修改学生:

6. 查找学生:

7. 学号排序:

七、设计结果

1. 菜单页面:

2. 增加学生页面:

3. 显示学生页面:

4. 删除学生页面:

5. 修改学生页面:

6. 查找及模糊查找学生页面:

7. 学号排序页面:

八、源码

1. 学生管理系统.cpp

2. info.cpp

3. studentManager.cpp

4.  student.h

5. studentManager.h

6. info.h


学生管理系统是大一刚入学时的作品了,比较稚嫩,希望各位大佬进行批评指正。


设计说明

本系统为学生管理系统,采用c++语言在Visual Studio平台进行开发。


一、基本功能:

1:退出学生系统

2:增加学生信息

3:显示学生信息

4;删除离校学生

5:修改学生信息

6:查找学生信息

7:按照学号排序


二、包含的模块:

1:studentManager学生管理类:内设上述1-7的功能函数;

2:Student学生类:内设学生的学号、姓名等基本信息和展示学生信息的函数;

3:Info学生信息类:继承Student学生类,初始化Student类中的数据和函数;


三、系统介绍

1. 学生管理系统的功能

退出系统,增加学生,显示学生,删除学生,修改学生,查找学生,排序学生。

2. 具体的需求:

增加学生时学号不能是重复的,且必须为12位数字,年份(前4位数字)在一个合理的区间

性别只能为男或女,年龄在一个合理的区间,地址和专业可以为缺失值;

删除和修改学生时,如果输入了不存在的学号则返回错误,输入的学号存在才能正常继续操作;

查找学生可以是准确信息如广东省查找,也可以多条件查找如18岁的浙江省男生,还可以是模糊值查找,如名字里含小字的学生,这些不同类型的查找方法都可以计算符合条件的学生人数;

排序学生是按照学号从小到大进行排序。

3. 支持的标准

Windows 11 版本 21H2 或更高版本:家庭版、专业版、专业教育版、专业工作站版、企业版和教育版

Windows 10 版本 1909 或更高版本:家庭版、专业版、教育版和企业版。

Windows Server Core 2022

Windows Server Core 2019

Windows Server 核心 2016


四、系统结构功能图:


五、系统设计

1. 退出系统:

使用exit函数返回0给系统,即exit(0),以示正常退出,中止程序。并打印"您已退出系统,欢迎下次使用"以提示用户。

2. 增加学生

2.1判断学号是否存在:isStuExit(num)

先设置一个index默认为-1,for循环遍历数组内保存的学生学号,若有学号与用户输入的学号一致,则令index为该学号在数组中的位置。返回结果为,若学号已存在,返回学号在数组中的位置;若学号不存在,返回-1。

2.2 切割字符串的前n个字符:getNchar(string str, int n)

nChars变量是保存切割后的字符,参数str是要被切割的字符串,n是要切割的字符个数。首先判断n是否>0,若不大于0则提示错误;若>字符串本身则n=字符串本身长度;其他情况则for循环n次,把str的前n个字符通过push_back函数依次传到nChars内,最后返回nChars。

2.3 保存文件:saveStu()

先fstream File创建一个文件流,然后File.open(“student.txt”,ios::out)创建或打开student.txt文件,使用ios::out的方式写入文件。利用for循环遍历this->sStuArray数组中的每个对象中的每个信息sNum、sName等依次通过File << 的方式写入文件,最后用File.close()函数关闭文件,使数据真正保存到文件中。

2.4 具体设计过程及功能算法描述:

先提示用户输入想要添加的人数,将新增的个数保存到变量addCount里面,如果addCount<0,则提示错误,终止添加;如果addCount>0,则进入添加学生的程序。

先用newCount = addCount + this->sCount(即最新人数 = 新增人数 + 原本人数)来保存最新人数。

因为要创建父类指针(学生类)Student* 来指向子类指针Info(具体学生),所以用Student**指针来开辟一个新空间newSpace来保存new出来的Student*[newCount]大小。

接着判断一下原本的存放学生的数组是否为空,如果不为空,则先for循环把原本数组内的学生存到新空间内,再进行添加新学生;如果为空,则直接添加新学生。

利用for循环遍历addCount次,在内部设置学生信息的变量:Num(学号)、Name(姓名)、Sex(性别)、Age(年龄)、Adress(住址)、Major(专业)。

然后提示用户输入要添加的学生学号,对用户输入的学号进行判断,若已存在则提示该学号已存在;若不存在则继续进行其他判断:Num.length()==12(学号的长度=12)&& splitNum >= “2004” && splitNum <= ”2023”

(利用2.2的函数来截取学号的前4个字符判断是否在2004到2023的区间),任一项不满足则提示错误,都满足则添加其他信息。提示用户输入学生姓名,性别。

性别设置if(Sex==”男” || Sex==“女”)判断只能输入“男”或“女”,如输入其他字符则提示错误,输入正确则提示用户输入年龄。

If(Age >= 14 && Age <= 30)判断年龄在14和30之间,若不满足则提示错误,若满足则询问用户是否录入学生地址和专业,是则录入,否则设置Adress(地址)或Major(专业)为“ “空格。

全部信息输入完成后,新建一个Student*的指针对象stu,使stu等于new出来的Info对象,把Num,Name等学生信息传入进行初始化,然后再让newSpace的this->sCount + i个对象等于stu即录入成功。让this->sCount等于newCount更新为最新人数,输出“添加成功”提示用户。

然后delett[]释放原本的内存空间数组,再让this->sStuArray等于newSpace更新数组,最后利用2.3的函数保存文件。

2.5 流程图

3. 展示学生:

3.1变量isFileEmpty:

文件是否为空的标志,在构造函数内进行初始化。首先fstream File.open(“student.txt”, ios::in)打开文件,if(!File.is_open())即利用is_open函数判断文件是否不存在,若不存在为真则输出提示文件不存在;再char ch;File >> c,设置一个变量ch接收读取到的文件的第一个字符,if(File.eof())判断读取到的eof()是否为真。因为eof是文件尾部的标志,若读取到第一个字符就是eof,就代表文件读到头了,则文件存在但为空;其它情况则是文件存在且有数据,则进行一系列初始化。

3.2 具体设计过程及功能算法描述:

先用3.1的isFileEmpty属性判断文件是否存在,若为真,则提示“文件不存在或为空”;其它则进入循环,遍历sStuArray数组中的每个学生对象,调用每个学生对象的内置函数showInfo()展示学生信息。最后退出程序。

3.3 流程图:

4. 删除学生:

4.1 this->sCount: 学生人数

4.2 具体设计过程及功能算法描述:

首先用3.1的isFileEmpty标志判断文件是否存在,为真则提示“文件不存在或为空”,为假则提示“请输入您想要删除的学生学号”,用变量num来保存用户输入的学号。

再用2.1的isStuExit(num)函数来判断想要删除的学号是否存在,不存在则提示删除失败,存在则利用isStuExit(num)返回的index(该学号在数组中的位置)删除数组中该位置的学生。

For循环从i = index(学号在数组中的位置)开始遍历,让数组中第[i + 1]个依次覆盖第[i]个直到循环结束。

然后用4.1的this->sCount--更新学生人数,2.3的this->saveStu()保存学生信息到文件,提示“删除成功”。

4.3 流程图:

5. 修改学生:

5.1 具体设计过程及功能算法描述:

首先用3.1的isFileEmpty标志判断文件是否存在,为真则提示“文件不存在或为空”,为假则提示“请输入您想要修改的学生学号”,用变量num来保存用户输入的学号。

再用2.1的isStuExit(num)函数来判断想要修改的学号是否存在,不存在则提示删除失败,存在则利用isStuExit(num)返回的index(该学号在数组中的位置)修改数组中该位置的学生。

提示“查找成功 请输入您想要修改的学生信息”,输入1-6的数字为单独修改学生的学号、姓名等,用变量choose保存用户输入的数字。

若为1则代表要修改该学生的学号,先用2.1的isStuExit(num)函数来判断想要修改的新学号是否存在,若存在则提示“修改失败”;

若不存在则this->sStuArray[index]->sNum = newNum,即令学生数组中的第index(该学号在数组中的位置)个的学号等于用户输入的新学号,提示“修改成功”。

其他的修改情况以此类推。最后用2.3的saveStu()保存文件。

5.2 流程图:

6. 查找学生:

6.1 具体设计过程及功能算法描述:

首先用3.1的isFileEmpty标志判断文件是否存在,为真则提示“文件不存在或为空”,为假则提示“请输入您想要查找的方式”,用变量choose来保存用户输入的查找方式。输入1-6分别是按学号、姓名、性别等查找,输入7是多条件查找,输入8是模糊查找。

若输入1是按学号查找,用2.1的isStuExit(num)函数来判断想要查找的学号是否存在,不存在则提示查找失败,存在则利用isStuExit(num)返回的index(该学号在数组中的位置)展示数组中该位置的学生信息;

若输入2是按姓名查找,提示用户输入想查询的姓名,用变量name保存用户输入的姓名。先设置一个flag = false表示默认未找到,count = 0表示符合条件的学生人数默认为0,然后进入循环,遍历学生数组。如果第i个学生名字与用户输入的name相匹配,则调用showInfo展示该学生信息,且令flag = true(找到学生),count++(符合条件的学生人数+1)。如果flag = false,说明没找到,提示查找失败。其他类型的查找以此类推;

若输入7是多条件查找,根据用户输入的查找条件,遍历学生数组,如多条件都能匹配则输出该学生信息;

若输入8是模糊查找,输入名字中的一个字可以找到名字中有这个字的所有学生。利用外层for循环(i < this->sCount)遍历学生数组,内层for循环(j < this->sStuArray[i]->sName.length())遍历学生数组中每个学生的名字,if(this->sStuArray[i]->sName[j] == name)即如果名字中有任何一个字与用户输入的字符匹配上了,则输出该学生的信息。

6.2 流程图:(由于有的功能太过相似所以只展示了个别流程)

7. 学号排序:

7.1 具体设计过程及功能算法描述:

首先对学号前4位数(年份)进行排序,按照外层循环,从i=0开始,利用2.2的切割字符串的前n个字符函数getNchar(this->sStuArray[i]->sNum, 4),得到第i个学号的前4位数,并设置当前最小值min = i。

再内层循环j=i+1开始,利用getNchar(this->sStuArray[j]->sNum, 4)得到第i+1个学号的前4位数,将两者对比,若第j个比第i个小,则令最小值变更为j,即min = j;

然后判断,如果min != i,即最小值改变了,则更换第i和第j个的位置,即第i个和第min个位置。然后进行整个学号this->sStuArray[i]->sNum的排序,排序原理与上述过程相同。

7.2 流程图:


七、设计结果

1. 菜单页面:

说明:根据提示输入0-6分别可以进入退出程序,增加学生信息,显示学生信息等页面,调试无误。

2. 增加学生页面:

    

说明:首先提示输入想要添加的人数,输入1就是添加单个学生,输入大于1的数字则为批量添加学生;若输入2004—2023区间以外的学号会提示错误,输入已经存在的学号也会提示错误;输入男女以外的性别会提示错误;输入14—30以外的年龄会提示错误;住址和专业可以选择1录入,选择2不录入。最后输出添加成功。

3. 显示学生页面:

说明:按照提示输入2即可显示所有学生信息,并且具有缺失值的学生信息如最后一行也可以显示;在第二步刚刚添加上的202211701303的学生也可以立即显示。调试无误。

4. 删除学生页面:

说明:根据提示输入3进入删除学生页面,若输入的想要删除的学号不存在,则提示错误;若存在,则提示删除成功。再回到输入2,进入显示学生页面,刚刚删除的202211701303学生已经不存在了,删除成功。调试无误。

5. 修改学生页面:

说明:根据提示输入4进入修改学生页面,若输入的想要修改的学号不存在,则提示错误;若存在,则根据提示输入想要修改的信息。输入1进入修改学号页面,若修改的新学号已存在,提示错误,若不存在,则修改成功。进入2显示学生页面查看,202211701304号学生的学号已经修改为新学号202211701919了,说明修改成功。调试无误。

6. 查找及模糊查找学生页面:

说明:根据提示输入5进入查找学生页面,输入1-8分别是按学号查找,按姓名查找等。输入2,按姓名查找,若查找的姓名不存在,则提示错误;若存在,则展示符合该条件的学生信息和人数;若输入7,多条件查找,则可以输入1-6的数字组合来组合条件,若查找不到提示错误;若查找到了,则展示符合该条件的学生信息和人数;若输入8,模糊查找,则可输入想要查找的学生的名字中的一个字,若查找不到提示错误;若查找到了,则展示符合该条件的学生信息和人数;

7. 学号排序页面:

说明:根据提示输入6进入学号排序页面,图1为原本的顺序,图2为输入6后进行排序后的结果,按照学号从小到大排序成功。调试无误。


八、源码

1. 学生管理系统.cpp

#include <iostream>;
using namespace std;
#include "studentManager.h"
#include "student.h"
#include "info.h"


int main() {
	studentManager st;
	int choice = 0; // 用户的选择

	while (true) {
		st.showMenu(); // 展示菜单界面
		cout << "请输入你的选择:" << endl;
		cin >> choice;

		switch (choice) {
		case 0: // 退出系统
			st.backsystem();
			break;
		case 1: // 增加学生
			st.addStu();
			break;
		case 2: // 显示学生
			st.showStu();
			break;
		case 3: // 删除学生
			st.deletStu();
			break;
		case 4: // 修改学生
			st.editStu();
			break;
		case 5: // 查找学生
			st.findStu();
			break;
		case 6: // 排序学生
			st.sortStu();
			break;
		default: // 输入其他数字-清空屏幕
			system("cls");
			break;
		}
	}

	system("pause");
	return 0;
}

2. info.cpp

#include "info.h"

Info::Info(string num, string name, string sex, int age, string adress, string major) {
	this->sNum = num;
	this->sName = name;
	this->sSex = sex;
	this->sAge = age;
	this->sAdress = adress;
	this->sMajor = major;
}

void Info::showInfo() {
	cout << this->sNum << " "
		 << this->sName << " "
		 << this->sSex << " "
		 << this->sAge << " "
		 << this->sAdress << " "
		 << this->sMajor<< endl;
}

3. studentManager.cpp

#include "studentManager.h";

// 构造函数
studentManager::studentManager() { // 初始化
	fstream File;
	File.open("student.txt", ios::in);

	// 1 文件不存在的初始化
	if (!File.is_open()) {
		cout << "文件不存在" << endl; 
		this->sCount = 0; // 初始化学生人数和数组
		this->sStuArray = NULL;
		this->isFileEmpty = true; // 文件为空为真
		File.close();

		return;
	}

	// 2 文件存在但为空
	char ch;
	File >> ch;
	if (File.eof()) {
		cout << "文件为空" << endl;
		this->sCount = 0; 
		this->sStuArray = NULL;
		this->isFileEmpty = true; 
		File.close();
		return;
	}

	// 3 文件存在 并记录数据
	int count = this->getCount(); // 获取学生人数
	this->sCount = count; // 更新学生人数
	this->sStuArray = new Student * [this->sCount];
	this->initStu(); // 保存文件中数据到数组中
}

// 展示菜单
void studentManager::showMenu() {
	cout << "********************************************" << endl;
	cout << "**********  欢迎使用学生管理系统! *********" << endl;
	cout << "*************  0.退出管理程序  *************" << endl;
	cout << "*************  1.增加学生信息  *************" << endl;
	cout << "*************  2.显示学生信息  *************" << endl;
	cout << "*************  3.删除离校学生  *************" << endl;
	cout << "*************  4.修改学生信息  *************" << endl;
	cout << "*************  5.查找学生信息  *************" << endl;
	cout << "*************  6.按照学号排序  *************" << endl;
	cout << "********************************************" << endl;
	cout << endl;
}

// 退出系统
void studentManager::backsystem() {
	cout << "您已退出系统,欢迎下次使用" << endl;
	system("pause");
	exit(0); // 退出程序
}

// 添加学生
void studentManager::addStu() {
	cout << "请输入您想添加的学生人数" << endl;
	int addCount = 0;
	cin >> addCount;

	if (addCount > 0) {
		int newCount = this->sCount + addCount; // 最新人数 = 原本人数 + 新增人数
		Student** newSpace = new Student * [newCount]; // 新数组空间

		if (this->sStuArray != NULL) { // 添加原来的职工
			for (int i = 0; i < this->sCount; i++) {
				newSpace[i] = this->sStuArray[i];
			}
		}


		for (int i = 0; i < addCount; i++) {
			string Num; // 学号
			string Name; // 姓名
			string Sex; // 性别
			int Age; // 年龄
			string Adress; // 住址
			string Major; // 专业

			cout << "请输入要添加的第" << i + 1 << "个学生的学号" << endl;
			cin >> Num;
			
			int res = this->isStuExit(Num); // 判断该学号是否已存在

			if (res == -1) { // 学号不存在 可以添加

				string splitNum; // 要切割出来的学号的前n个数字
				splitNum = getNchar(Num, 4);  // 保存学号的前4个数字(年份)

				if (Num.length() == 12 && splitNum >= "2004" && splitNum <= "2023") {
					cout << "请输入要添加的第" << i + 1 << "个学生的姓名" << endl;
					cin >> Name;
					cout << "请输入要添加的第" << i + 1 << "个学生的性别" << endl;
					cin >> Sex;

					if (Sex == "男" || Sex == "女") {
						cout << "请输入要添加的第" << i + 1 << "个学生的年龄" << endl;
						cin >> Age;

						if (Age >= 14 && Age <= 30) {
							cout << "您是否要录入该学生的住址" << endl;
							cout << "1 是" << endl;
							cout << "2 否" << endl;

							int choose;
							cin >> choose;

							if (choose == 1) {
								cout << "请输入要添加的第" << i + 1 << "个学生的住址" << endl;
								cin >> Adress;

								cout << "您是否要录入该学生的专业" << endl;
								cout << "1 是" << endl;
								cout << "2 否" << endl;

								int choose;
								cin >> choose;

								if (choose == 1) {
									cout << "请输入要添加的第" << i + 1 << "个学生的专业" << endl;
									cin >> Major;
								} 
								else {
									Major = " ";
								}
							}	
							else {
								Adress = " ";

								cout << "您是否要录入该学生的专业" << endl;
								cout << "1 是" << endl;
								cout << "2 否" << endl;

								int choose;
								cin >> choose;

								if (choose == 1) {
									cout << "请输入要添加的第" << i + 1 << "个学生的专业" << endl;
									cin >> Major;
								}
								else {
									Major = " ";
								}
							}

							/*fstream File;
							File.open("student.txt", ios::out | ios::app);
							File << Num << " " << Name << " " << Sex << " " << Age << " " << Adress << " " << Major << " " << endl;
							File.close();*/

							Student* stu = NULL;
							stu = new Info(Num, Name, Sex, Age, Adress, Major);
							newSpace[this->sCount + i] = stu;
							this->sCount = newCount; // 更新人数为最新人数
							this->isFileEmpty = false; // 文件为空的标志为假
							cout << "添加成功" << endl;
							delete[] this->sStuArray;
							this->sStuArray = newSpace;
							this->saveStu();
						}
						else {
							cout << "您输入的年龄有误 请重新输入" << endl;
							break;
						}
					}
					else {
						cout << "性别只能输入男或女 请重新输入" << endl;
						break;
					}
				}
				else {
					cout << "您输入的学号有误 请重新输入" << endl;
					break;
				}
			}
			else {
				cout << "该学号已存在 请重新输入" << endl;
			}

			
		}
	}
	else {
		cout << "您输入的数字有误,请重新输入" << endl;
		system("pause");
		system("cls");
	}

	system("pause");
	system("cls");
}

// 返回字符串的前n个字符
string studentManager::getNchar(string str, int n) {
	string nChars;

	if (n < 0) {
		n = 0;
		cout << "输入有误, 请输入大于0的n" << endl;
	}

	if (n > str.size()) {
		n = str.size();
	}

	for (size_t i = 0; i < n; i++) {
		nChars.push_back(str[i]);
	}

	return nChars;
}

// 保存学生
void studentManager::saveStu() {
	fstream File;
	File.open("student.txt", ios::out);

	for (int i = 0; i < this->sCount; i++) {
		File << this->sStuArray[i]->sNum << " "
			 << this->sStuArray[i]->sName << " "
			 << this->sStuArray[i]->sSex << " "
			 << this->sStuArray[i]->sAge << " "
			 << this->sStuArray[i]->sAdress << " "
			 << this->sStuArray[i]->sMajor << " " << endl;
	}

	File.close();
}

// 获取学生人数
int studentManager::getCount() {
	fstream File;
	File.open("student.txt", ios::in);

	string Num; // 学号
	string Name; // 姓名
	string Sex; // 性别
	int Age; // 年龄
	string Adress; // 住址
	string Major; // 专业

	int Count = 0; // 学生人数

	while (File >> Num && File >> Name && File && File >> Sex && File >> Age && File >> Adress && File >> Major) {
		Count++; // 读到一条就加一条
	}
	File.close();

	return Count;
}

// 初始化学生数组
void studentManager::initStu() {
	fstream File;
	File.open("student.txt", ios::in);

	string Num; // 学号
	string Name; // 姓名
	string Sex; // 性别
	int Age; // 年龄
	string Adress; // 住址
	string Major; // 专业

	int index = 0;
	while (File >> Num && File >> Name && File && File >> Sex && File >> Age && File >> Adress && File >> Major) {
		Student* stu = NULL;
		stu = new Info(Num, Name, Sex, Age, Adress, Major);
		this->sStuArray[index] = stu;
		index++;
	}
}

// 展示学生信息
void studentManager::showStu() {
	if (this->isFileEmpty) {
		cout << "文件不存在或为空" << endl;
	}
	else {
		for (int i = 0; i < this->sCount; i++) {
			this->sStuArray[i]->showInfo();
		}
	}

	system("pause");
	system("cls");
}

// 判断学号是否存在 存在则返回学生在数组中位置 不存在则返回-1
int studentManager::isStuExit(string num) {
	int index = -1; // 默认为-1

	for (int i = 0; i < this->sCount; i++) {
		if (this->sStuArray[i]->sNum == num) {
			index = i;
			break;
		}
	}

	return index;
}

// 删除学生
void studentManager::deletStu() {
	if (this->isFileEmpty) { // 文件不存在或无数据
		cout << "文件不存在或为空" << endl;
	}
	else { // 文件有数据
		cout << "请输入您想要删除的学生学号" << endl;
		string num;
		cin >> num;

		int index = this->isStuExit(num); // 判断该学号学生是否存在

		if (index != -1) { // 学生存在 删除index位置的学生
			for (int i = index; i < this->sCount - 1; i++) {
				// index后一个覆盖前一个依次类推
				this->sStuArray[i] = this->sStuArray[i + 1];
			}
			this->sCount--; // 更新学生人数
			this->saveStu(); // 更新学生信息到文件
			cout << "成功删除学号为" << num << "的学生" << endl;
		}
		else { // 学生不存在
			cout << "删除失败 该学号学生不存在" << endl;
		}
	}

	system("pause");
	system("cls");
}

// 修改学生
void studentManager::editStu() {
	if (this->isFileEmpty) { // 文件不存在或无数据
		cout << "文件不存在或为空" << endl;
	}
	else { // 有数据
		cout << "请输入您要修改的学生学号" << endl;
		string num;
		cin >> num;

		int index = this->isStuExit(num); // 判断学生是否存在

		if (index != -1) { // 学生存在 进行修改

			string newNum; // 学号
			string newName; // 姓名
			string newSex; // 性别
			int newAge; // 年龄
			string newAdress; // 住址
			string newMajor; // 专业

			cout << "查找成功 请输入您要修改的学生信息" << endl;
			cout << "1 修改学号" << endl;
			cout << "2 修改姓名" << endl;
			cout << "3 修改性别" << endl;
			cout << "4 修改年龄" << endl;
			cout << "5 修改住址" << endl;
			cout << "6 修改专业" << endl;

			int choose;
			cin >> choose;

			switch(choose) {
			case 1: 
			{
				cout << "请输入您要修改的新学号" << endl;
				cin >> newNum;

				int res = this->isStuExit(newNum); // 判断要修改的新学号是否存在

				if (res == -1) { // 学号不存在 可以修改
					this->sStuArray[index]->sNum = newNum;
					cout << "修改成功" << endl;
					break;
				}
				else {
					cout << "修改失败 该学号已存在" << endl;
					break;
				}
			}
			case 2:
			{
				cout << "请输入您要修改的新姓名" << endl;
				cin >> newName;
				this->sStuArray[index]->sName = newName;
				cout << "修改成功" << endl;
				break;
			}
			case 3:
			{
				cout << "请输入您要修改的新性别" << endl;
				cin >> newSex;
				this->sStuArray[index]->sSex = newSex;
				cout << "修改成功" << endl;
				break;
			}
			case 4:
			{
				cout << "请输入您要修改的新年龄" << endl;
				cin >> newAge;
				this->sStuArray[index]->sAge = newAge;
				cout << "修改成功" << endl;
				break;
			}
			case 5:
			{
				cout << "请输入您要修改的新住址" << endl;
				cin >> newAdress;
				this->sStuArray[index]->sAdress = newAdress;
				cout << "修改成功" << endl;
				break;
			}
			case 6:
			{
				cout << "请输入您要修改的新专业" << endl;
				cin >> newMajor;
				this->sStuArray[index]->sMajor = newMajor;
				cout << "修改成功" << endl;
				break;
			}
			default: // 输入其他数字-清空屏幕
				system("cls");
				break;
			}
			this->saveStu(); // 更新数组到文件中
		}
		else {
			cout << "修改失败 该学号学生不存在" << endl;
		}
	}

	system("pause");
	system("cls");
}

// 查询学生
void studentManager::findStu() {
	if (this->isFileEmpty) {
		cout << "文件不存在或为空" << endl;
	}
	else {
		cout << "请输入您想要查找的方式" << endl;
		cout << "1 按学号查找" << endl;
		cout << "2 按姓名查找" << endl;
		cout << "3 按性别查找" << endl;
		cout << "4 按年龄查找" << endl;
		cout << "5 按住址查找" << endl;
		cout << "6 按专业查找" << endl;

		cout << "7 多条件查找" << endl;
		cout << "8 模糊查找" << endl;

		int choose;
		cin >> choose;

		if (choose == 1) { // 按学号查找
			cout << "请输入您要查找的学号" << endl;
			string num;
			cin >> num;

			this->findNum(num);
		}
		else if (choose == 2) { // 按姓名查找
			cout << "请输入您要查找的姓名" << endl;
			string name;
			cin >> name;

			this->findName(name);
		}
		else if (choose == 3) { // 按性别查找
			cout << "请输入您要查找的性别" << endl;
			string sex;
			cin >> sex;

			this->findSex(sex);
		}
		else if (choose == 4) { // 按年龄查找
			cout << "请输入您要查找的年龄" << endl;
			int age;
			cin >> age;

			this->findAge(age);
		}
		else if (choose == 5) { // 按住址查找
			cout << "请输入您要查找的住址" << endl;
			string adress;
			cin >> adress;

			this->findAdress(adress);
		}
		else if (choose == 6) { // 按专业查找
			cout << "请输入您要查找的专业" << endl;
			string major;
			cin >> major;

			this->findMajor(major);
		}
		else if (choose == 7) { // 多条件查找
			cout << "请输入2-7的数字组合 如按姓名+性别查找 输入23" << endl;
			int choose;
			cin >> choose;

			if (choose == 23) { // 按姓名+性别查找
				cout << "请输入您要查找的姓名" << endl;
				string name;
				cin >> name;
				cout << "请输入您要查找的性别" << endl;
				string sex;
				cin >> sex;

				int count = 0;
				for (int i = 0; i < this->sCount; i++) {
					if (this->sStuArray[i]->sName == name && this->sStuArray[i]->sSex == sex) {
						this->sStuArray[i]->showInfo();
						count++;
					}
				}

				cout << "符合条件的人数为" << count << endl;
			}

			if (choose == 345) { // 性别+年龄+住址
				cout << "请输入您要查找的性别" << endl;
				string sex;
				cin >> sex;
				cout << "请输入您要查找的年龄" << endl;
				int age;
				cin >> age;
				cout << "请输入您要查找的住址" << endl;
				string adress;
				cin >> adress;

				int count = 0;
				for (int i = 0; i < this->sCount; i++) {
					if (this->sStuArray[i]->sSex == sex && this->sStuArray[i]->sAge == age && this->sStuArray[i]->sAdress == adress) {
						this->sStuArray[i]->showInfo();
						count++;
					}
				}

				cout << "符合条件的人数为" << count << endl;
			}
		}
		else if (choose == 8) { // 模糊查找
			cout << "请输入您要查询的姓名中含的某字 如陈" << endl;
			char name;
			cin >> name;

			int count = 0; // 符合条件的人数

			for (int i = 0; i < this->sCount; i++) {
				for (int j = 0; j < this->sStuArray[i]->sName.length(); j++) {
					if (this->sStuArray[i]->sName[j] == name) {
						this->sStuArray[i]->showInfo();
						count++;
					}
				}
			}

			cout << "符合条件的人数为" << count << endl;
		}
		else {
			cout << "查询失败  您输入的数字有误" << endl;
		}
	} 

	system("pause");
	system("cls");
}

// 1 按学号查找
void studentManager::findNum(string num) {
	int res = this->isStuExit(num); // 判断该学号学生是否存在

	if (res != -1) { // 学生存在
		this->sStuArray[res]->showInfo();
	}
	else {
		cout << "查找失败 该学号学生不存在" << endl;
	}
}

// 2 按姓名查找
void studentManager::findName(string name) {
	int flag = false; // 是否查找成功的标志
	int count = 0; // 符合条件的学生人数

	for (int i = 0; i < this->sCount; i++) {
		if (this->sStuArray[i]->sName == name) {
			this->sStuArray[i]->showInfo();
			flag = true;
			count++;
		}
	}
	if (flag == true) {
		cout << "该姓名的学生人数为" << count << endl;
	}
	else if (flag == false) {
		cout << "查找失败 该姓名学生不存在" << endl;
	}
}

// 3 按性别查找
void studentManager::findSex(string sex) {
	int count = 0; // 符合条件的学生人数
	if (sex == "男" || sex == "女") {
		for (int i = 0; i < this->sCount; i++) {
			if (this->sStuArray[i]->sSex == sex) {
				this->sStuArray[i]->showInfo();
				count++;
			}
		}

		cout << "该性别的学生人数为" << count << endl;
	}
	else {
		cout << "查找失败 性别只能输入男或女" << endl;
	}
}

// 4 按年龄查找
void studentManager::findAge(int age) {
	int flag = false; // 是否查找成功的标志
	int count = 0; // 符合条件的学生人数
	for (int i = 0; i < this->sCount; i++) {
		if (this->sStuArray[i]->sAge == age) {
			this->sStuArray[i]->showInfo();
			flag = true;
			count++;
		}
	}
	if (flag == true) {
		cout << "该年龄的学生人数为" << count << endl;
	}
	else if (flag == false) {
		cout << "查找失败 该年龄学生不存在" << endl;
	}
}

// 5 按住址查找
void studentManager::findAdress(string adress) {
	int flag = false; // 是否查找成功的标志
	int count = 0; // 符合条件的学生人数
	for (int i = 0; i < this->sCount; i++) {
		if (this->sStuArray[i]->sAdress == adress) {
			this->sStuArray[i]->showInfo();
			flag = true;
			count++;
		}
	}
	if (flag == true) {
		cout << "该住址的学生人数为" << count << endl;
	}
	else if (flag == false) {
		cout << "查找失败 该住址学生不存在" << endl;
	}
}

// 6 按专业查找
void studentManager::findMajor(string major) {
	int flag = false; // 是否查找成功的标志
	int count = 0; // 符合条件的学生人数
	for (int i = 0; i < this->sCount; i++) {
		if (this->sStuArray[i]->sMajor == major) {
			this->sStuArray[i]->showInfo();
			flag = true;
			count++;
		}
	}
	if (flag == true) {
		cout << "该专业的学生人数为" << count << endl;
	}
	else if (flag == false) {
		cout << "查找失败 该专业学生不存在" << endl;
	}
}

// 按学号大小排序
void studentManager::sortStu() {
	for (int i = 0; i < this->sCount; i++) {
		// 按前4位数(年份)排序
		string splitNum4i; // 要切割出来的学号的前4个数字
		splitNum4i = getNchar(this->sStuArray[i]->sNum, 4);  // 保存学号的前4个数字(年份)

		int min = i; // 目前最小值的下标
		for (int j = i + 1; j < this->sCount; j++) {
			string splitNum4j; // 要切割出来的学号的前n个数字
			splitNum4j = getNchar(this->sStuArray[j]->sNum, 4);  // 保存学号的前4个数字(年份)

			if (splitNum4i > splitNum4j) {
				min = j;
			}
		}

		if (min != i) {
			Student* temp = this->sStuArray[i];
			this->sStuArray[i] = this->sStuArray[min];
			this->sStuArray[min] = temp;
		}

		// 按学号排序
		int min2 = i; // 目前最小值的下标
		for (int j = i + 1; j < this->sCount; j++) {
			if (this->sStuArray[i]->sNum > this->sStuArray[j]->sNum) {
					min2 = j;
			}
		}

		if (min2 != i) {
			Student* temp = this->sStuArray[i];
			this->sStuArray[i] = this->sStuArray[min2];
			this->sStuArray[min2] = temp;
		}
	}

	cout << "排序成功 最新的数据为" << endl;
	this->saveStu();
	this->showStu();

	system("pause");
	system("cls");
}

// 析构函数
studentManager::~studentManager() {
	// 手动释放堆区数据
	if (this->sStuArray != NULL) {
		delete[] this->sStuArray;
		this->sStuArray = NULL;
	}
}

4.  student.h

#pragma once
#include <iostream>
using namespace std;
#include <string>

class Student {
public:
	virtual void showInfo() = 0; // 显示学生信息
	
	string sNum; // 学号
	string sName; // 姓名
	string sSex; // 性别
	int sAge; // 年龄
	string sAdress; // 住址
	string sMajor; // 专业
};

5. studentManager.h

#pragma once
#include <iostream>
using namespace std;

#include "student.h"
#include "info.h"

#include <string>
#include <fstream>
#include <ctype.h>
#include <vector>

class studentManager {
public:
	studentManager(); // 构造函数

	void showMenu(); // 展示菜单

	void backsystem(); // 退出系统

	void addStu(); // 添加学生

	int sCount; // 原本学生人数

	Student** sStuArray; // 记录学生的数组

	string getNchar(string str, int n); // 返回输入的前n个字符

	void saveStu(); // 保存学生

	bool isFileEmpty; // 标志文件是否为空

	int getCount(); // 获取学生人数

	void initStu(); // 初始化学生数组

	void showStu(); // 展示学生信息

	int isStuExit(string num); // 根据学号判断该学生学生是否存在

	void deletStu(); // 删除学生

	void editStu(); // 修改学生

	void findStu(); // 查找学生

	void findNum(string num);       // 1 按学号查找
	void findName(string name);     // 2 按姓名查找
	void findSex(string sex);       // 3 按性别查找
	void findAge(int age);          // 4 按年龄查找
	void findAdress(string adress); // 5 按住址查找
	void findMajor(string major);   // 6 按专业查找

	void sortStu(); // 排序

	~studentManager(); // 析构函数
};

6. info.h

#pragma once
#include <iostream>
using namespace std;
#include "student.h"

class Info: public Student {
public:
	// 构造函数
	Info(string num, string name, string sex, int age, string adress, string major);

	// 展示学生信息
	virtual void showInfo();
};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值