C++:设计一个学生学籍管理系统,设计相关信息,并执行一些计算和文件操作

news2025/2/22 16:49:51

题目:

设计一个学生学籍管理系统:

  1.   学生信息包括:姓名、学号、性别和英语、数学、程序设计、体育成绩。
  1.   数据录入支持键盘输入和文件导入;同时支持导入+输入,如自动列出“姓名、学号、性别”,而成绩部分由键盘输入。录入结果存入数据文件student.dat。
  1.   支持按学生姓名和学号查询,支持指定班级的两位序号范围查询,查询结果回显到屏幕上。
  1. l对所有学生,按照班级计算各科平均成绩。
  1. l支持按单科成绩排序和总分排序,排序结果写入文件并回显,文件名自拟。

需求分析:

由电脑程序执行的管理系统广泛用于个人信息,物品信息的储存上,而学生信息管理系统对于每个学校来说都是现代化管理不可或缺的一部分。

学生信息管理系统其开发主要包括后台数据库的建立和维护以及前端应用程序的开发两个方面,对于前者要求建立起数据库一致性和完整性、安全性好的数据库。而对于后者则要求应用程序功能完备,易使用的特点。

学生信息管理系统要实现的目标是为学校提供学生管理解决方案,具体目标如下:

  1. 提高学生信息管理效率,节约管理成本,增强学生管理的安全性。
  2. 满足学校学生管理的人员、老师和学生的不同层次和不同方面的需要。
  3. 为学校将来的信息化建设提供必要的支持。总之,通过该系统的建设来提高学校的学生信息管理效率,使得学校的发展能够适应当前的教育信息化建设的中体发展趋势。

究其要点,学生信息管理系统正在朝着更快速,更便捷,更安全的发展,并且将成为现在及未来很长一段时间学校对学生信息管理的重要工具。

设计思想和方案设计:

  1. 建立简洁明了的菜单,每个菜单都用函数包装好,便于代码的后序拼接和修改。
  2. 建立符合要求的学生类,学生重要的信息(姓名,学号等)应该作为私有成员被添加,并且提供函数便于外界访问或修改相关内容。
  3. 分数应该使用double类型方便求平均分,学号姓名等用string更方便。
  4. 一些常用的功能比如类的复制,带参构造,输出,访问,修改函数应该提前声明好方便后续运用。
  5. 重复多次的步骤尽量给其写一个通用函数,减少代码量和阅读压力。
  6. 所有的函数应该按顺序封装到一个头文件里,防止主函数和函数顺序混账不清的情况。
  7. 为了在读取文件到程序中时获得更大的储存空间,应该在主函数外声明大容量的数组。
  8. 进行文件读写时,应该确定以哪种方式,比如:添加应该以追加(ios::app),排序回显应该用清除再写(ios::trunc)。
  9. 在进行信息添加时应该注意雷同的情况,即:一个学号只能够对应一个人,而可以有多个同名的人。因此对于相同学号,应该给予更新,对于原先不存在的应该给予添加。
  10. 每实现一个功能应该立刻对其进行测试,否则全写完测试会导致错误难以判断甚至出现连环错误的情况,并且要检查当前实现的功能和之前实现的功能是否存在冲突,发现问题立刻修改,而不是搁置。
  11. 增加一些注释方便阅读,也方便长时间后再看进行回忆。

核心代码:


/*总控制函数*/

void long_main() {
	ifstream ifs;
	while (true) {
		switch (Mainmenu())
		{
		case 1: {
			switch (findstumenu()) {
			case 1: {
				bool p = false;		/*是否查到*/
				string num;
				cout << "请输入学生学号:";
				cin >> num;
				ifs.open("student.dat", ios::in);
				string buf;			//文件读取
				while (getline(ifs, buf))
				{
					stu* stu1 = new stu();
					stringtostudent(buf, stu1);
					if (stu1->getnum() == num) {
						stu1->studisplay();
						p = true;
					}
				}
				if (!p)cout << "无此学生信息" << endl;
				ifs.close();
				break;
			}
			case 2: {
				string name;
				int len = 0;
				cout << "请输入学生姓名:";
				cin >> name;
				ifs.open("student.dat", ios::in);
				string buf;			//文件读取
				while (getline(ifs, buf))
				{
					stu* stu1 = new stu();
					stringtostudent(buf, stu1);
					if (stu1->getname() == name) {
						st[len].stucpy(*stu1);
						len++;
					}
				}
				if (!len)cout << "无此学生信息" << endl;
				else {
					for (int i = 0; i < len; i++) {		/*输出排序*/
						for (int j = 0; j < len - 1; j++) {
							if (st[j].getnum() > st[j + 1].getnum()) {
								stu s;
								s.stucpy(st[j]);
								st[j].stucpy(st[j + 1]);
								st[j + 1].stucpy(s);
							}
						}
					}
					cout << "查询到" << len << "条信息:" << endl;
					for (int i = 0; i < len; i++) {
						cout << i + 1 << ": " << st[i].getnum() << " " << st[i].getname() << endl;		/*不确定具体信息时只展示姓名和学号*/
					}
					cout << "选择查询学生序号:";
					int k;		/*选择具体信息编号*/
					do {
						cin >> k;
						if (k > len || k < 1)cout << "无效的指令,请重新输入" << endl;
						else break;
					} while (true);
					st[k - 1].studisplay();
				}
				ifs.close();
				break;
			}
			case 3: {
				string clas;
				int len = 0;
				cout << "请输入班号:";
				cin >> clas;
				ifs.open("student.dat", ios::in);
				string buf;			//文件读取
				while (getline(ifs, buf))
				{
					stu* stu1 = new stu();
					stringtostudent(buf, stu1);
					if (strsplit(stu1->getnum(), 0, 7) == clas) {
						st[len].stucpy(*stu1);
						len++;
					}
				}
				if (!len)cout << "无此班级" << endl;
				else {
					cout << "查询到" << len << "条信息:" << endl;
					for (int i = 0; i < len; i++) {
						cout << i + 1 << ": " << st[i].getnum() << " " << st[i].getname() << endl;		/*不确定具体信息时只展示姓名和学号*/
					}
					cout << "选择查询学生序号:";
					int k;
					do {
						cin >> k;
						if (k > len || k < 1)cout << "无效的指令,请重新输入" << endl;
						else break;
					} while (true);
					st[k - 1].studisplay();
				}
				ifs.close();
				break;
			}
			case 4:break;
			}break;
		case 2: {
			switch (addstumenu())
			{
			case 1: {

				cout << "输入学号,姓名,性别,英语成绩,数学成绩,程序设计成绩,体育成绩" << endl;
				stu stu1;
				string nu, na, se;
				cin >> nu >> na >> se >> stu1.English >> stu1.Math >> stu1.Program_design >> stu1.Sports;		/*手动输入(空格为间隔)*/
				bool p = false;		/*相同学号是否已存在*/
				ifs.open("student.dat", ios::in);
				string buf;			//文件读取
				while (getline(ifs, buf))
				{
					stu* stu2 = new stu();
					stringtostudent(buf, stu2);
					if (stu2->getnum() == nu) {
						bool p = true;
					}
				}
				ifs.close();
				if (!p) {		/*不存在添加*/
					int len = 0;
					ifs.open("student.dat", ios::in);
					string buf;			//文件读取
					while (getline(ifs, buf))
					{
						stu* stu2 = new stu();
						stringtostudent(buf, stu2);
						st[len].stucpy(*stu2);
						len++;
					}
					ofstream ofs;
					ofs.open("student.dat", ios::out);
					for (int i = 0; i < len; i++) {
						ofs << st[i].getnum() << " " << st[i].getname() << " " << st[i].English << " " << st[i].Math << " " << st[i].Program_design << " " << st[i].Sports << " " << "1" << endl;
					}
					ofs << nu << " " << na << " " << se << " " << stu1.English << " " << stu1.Math << " " << stu1.Program_design << " " << stu1.Sports << " " << "1" << endl;
					ofs.close();
					num++;
				}
				else {		/*存在则更新*/
					int len = 0;
					ifs.open("student.dat", ios::in);
					string buf;			//文件读取
					while (getline(ifs, buf))
					{
						stu* stu2 = new stu();
						stringtostudent(buf, stu2);
						st[len].stucpy(*stu2);
						len++;
					}
					st[len].stucpy(stu1);
					st[len].changename(na);
					st[len].changenum(nu);
					st[len].changesex(se);
					len++;
					ifs.close();
					ofstream ofs;
					ofs.open("student.dat", ios::out);
					for (int i = 0; i < len; i++) {
						ofs << st[i].getnum() << " " << st[i].getname() << " " << st[i].English << " " << st[i].Math << " " << st[i].Program_design << " " << st[i].Sports << " " << "1" << endl;
					}
					ofs.close();
				}
				break;
			}
			case 2: {
				cout << "输入文件路径" << endl;
				string ss;
				cin >> ss;
				int len = 0;
				int len1 = 0;
				stu st1[maxsize];
				ifs.open("student.dat", ios::in);
				string buf;			//文件读取
				while (getline(ifs, buf))
				{
					stu* stu2 = new stu();
					stringtostudent(buf, stu2);
					st[len].stucpy(*stu2);
					len++;
				}
				ifs.close();
				ifs.open(ss, ios::in);
				if (!ifs.is_open()) {		/*打不开证明没有这个文件,或者权限不够*/
					cout << "文件路径无效!" << endl;
					break;
				}
				while (getline(ifs, buf))
				{
					stu* stu2 = new stu();
					stringtostudent(buf, stu2);
					st1[len1].stucpy(*stu2);
					len1++;
				}
				ifs.close();
				ofstream ofs;
				ofs.open("student.dat", ios::app);
				for (int i = 0; i < len1; i++) {
					bool t = true;		/*是否已存在*/
					for (int j = 0; j < len; j++) {
						if (st1[i].getnum() == st[j].getnum())t = false;
					}
					if (t)ofs << st1[i].getnum() << " " << st1[i].getname() << " " << st1[i].getsex() << " " << st1[i].English << " " << st1[i].Math << " " << st1[i].Program_design << " " << st1[i].Sports << " " << "1" << endl;		/*未存在添加,已存在不添加*/
				}
				ofs.close();
				check();
				break;
			}
			case 3:break;
			}
			break;
		}break;
		case 3: {
			int len = 0;
			ifs.open("student.dat", ios::in);
			string buf;			//文件读取
			while (getline(ifs, buf))
			{
				bool b = true;	/*未找到班级*/
				stu* stu1 = new stu();
				stringtostudent(buf, stu1);
				for (int i = 0; i < len; i++) {
					if (strsplit(st[i].getnum(), 0, 7) == strsplit(stu1->getnum(), 0, 7)) {		/*班级名和学生学号前八位相等*/
						b = false;		
						st[i].English += stu1->English;		/*此段代码中st数组作为班级数组*/
						st[i].Math += stu1->Math;
						st[i].Program_design += stu1->Program_design;
						st[i].Sports += stu1->Sports;
						st[i].score++;		/*记录人数*/
					}
				}
				if (b) {		/*没有班级则新建班级*/
					st[len].changenum(strsplit(stu1->getnum(), 0, 7));
					len++;
					st[len].English += stu1->English;
					st[len].Math += stu1->Math;
					st[len].Program_design += stu1->Program_design;
					st[len].Sports += stu1->Sports;
					st[len].score++;		/*记录人数*/
				}
			}
			for (int i = 0; i < len; i++) {		/*输出排序*/
				for (int j = 0; j < len - 1; j++) {
					if (st[j].getnum() > st[j + 1].getnum()) {
						stu s;
						s.stucpy(st[j]);
						st[j].stucpy(st[j + 1]);
						st[j + 1].stucpy(s);
					}
				}
			}
			for (int i = 0; i < len; i++) {
				cout << "班号:" << st[i].getnum() << "  " << "英语平均分:" << st[i].English / st[i].score << "  数学平均分:" << st[i].Math / st[i].score << "  程序设计平均分" << st[i].Program_design / st[i].score << "  体育平均分" << st[i].Sports / st[i].score << endl;
			}
		}break;
		case 4: {
			ifstream ifs;
			int len = 0;
			ifs.open("student.dat", ios::in);
			string buf;			//文件读取
			while (getline(ifs, buf))
			{
				stu* stu2 = new stu();
				stringtostudent(buf, stu2);
				st[len].stucpy(*stu2);
				len++;
			}
			ifs.close();
			switch (scoresortmenu()) {
			case 1: {
				for (int i = 0; i < len; i++) {
					for (int j = 0; j < len - 1; j++) {		/*成绩排序*/
						if (st[j].total < st[j + 1].total) {
							stu stu1;
							stu1.stucpy(st[j]);
							st[j].stucpy(st[j + 1]);
							st[j+1].stucpy(stu1);
						}
					}
				}
				ofstream ofs;
				ofs.open("Total_stu.dat", ios::trunc);		/*重写文件*/
				for (int i = 0; i < len; i++) {
					ofs << st[i].getnum() << " " << st[i].getname() << " ";
					if (st[i].getsex() == "m")ofs << "男";
					else ofs << "女";
					ofs<<" "<<st[i].total << endl;
					cout << st[i].getnum() << " " << st[i].getname() << " ";
					if (st[i].getsex() == "m")cout << "男";
					else cout << "女";
					cout << " " << st[i].total<< endl;
				}
				ofs.close();
				break;
			}
			case 2: {
				for (int i = 0; i < len; i++) {
					for (int j = 0; j < len - 1; j++) {
						if (st[j].English < st[j + 1].English) {
							stu stu1;
							stu1.stucpy(st[j]);
							st[j].stucpy(st[j + 1]);
							st[j + 1].stucpy(stu1);
						}
					}
				}
				ofstream ofs;
				ofs.open("English_stu.dat", ios::trunc);
				for (int i = 0; i < len; i++) {
					ofs << st[i].getnum() << " " << st[i].getname() << " ";
					if (st[i].getsex() == "m")ofs << "男";
					else ofs << "女";
					ofs << " " << st[i].English  << endl;
					ofs.close();
					cout << st[i].getnum() << " " << st[i].getname() << " ";
					if (st[i].getsex() == "m")cout << "男";
					else cout << "女";
					cout << " " << st[i].English << endl;
				}
				break;
			}
			case 3: {
				for (int i = 0; i < len; i++) {
					for (int j = 0; j < len - 1; j++) {
						if (st[j].Math < st[j + 1].Math) {
							stu stu1;
							stu1.stucpy(st[j]);
							st[j].stucpy(st[j + 1]);
							st[j + 1].stucpy(stu1);
						}
					}
				}
				ofstream ofs;
				ofs.open("Math_stu.dat", ios::trunc);
				for (int i = 0; i < len; i++) {
					ofs << st[i].getnum() << " " << st[i].getname() << " ";
					if (st[i].getsex() == "m")ofs << "男";
					else ofs << "女";
					ofs << " " << st[i].Math<< endl;
					cout << st[i].getnum() << " " << st[i].getname() << " ";
					if (st[i].getsex() == "m")cout << "男";
					else cout << "女";
					cout << " " << st[i].Math<< endl;
				}
				ofs.close();
				break;
			}
			case 4: {
				for (int i = 0; i < len; i++) {
					for (int j = 0; j < len - 1; j++) {
						if (st[j].Program_design < st[j + 1].Program_design) {
							stu stu1;
							stu1.stucpy(st[j]);
							st[j].stucpy(st[j + 1]);
							st[j + 1].stucpy(stu1);
						}
					}
				}
				ofstream ofs;
				ofs.open("Program_design_stu.dat", ios::trunc);
				for (int i = 0; i < len; i++) {
					ofs << st[i].getnum() << " " << st[i].getname() << " ";
					if (st[i].getsex() == "m")ofs << "男";
					else ofs << "女";
					ofs << " " << st[i].Program_design  << endl;
					cout << st[i].getnum() << " " << st[i].getname() << " ";
					if (st[i].getsex() == "m")cout << "男";
					else cout << "女";
					cout << " " << st[i].Program_design << endl;
				}
				ofs.close();
				break;
			}
			case 5: {
				for (int i = 0; i < len; i++) {
					for (int j = 0; j < len - 1; j++) {
						if (st[j].Sports < st[j + 1].Sports) {
							stu stu1;
							stu1.stucpy(st[j]);
							st[j].stucpy(st[j + 1]);
							st[j + 1].stucpy(stu1);
						}
					}
				}
				ofstream ofs;
				ofs.open("Sports_stu.dat", ios::trunc);
				for (int i = 0; i < len; i++) {
					ofs << st[i].getnum() << " " << st[i].getname() << " ";
					if (st[i].getsex() == "m")ofs << "男";
					else ofs << "女";
					ofs << " "<< st[i].Sports << endl;
					cout << st[i].getnum() << " " << st[i].getname() << " ";
					if (st[i].getsex() == "m")cout << "男";
					else cout<< "女";
					cout << " "<< st[i].Sports << endl;
				}
				ofs.close();
				break;
			}
			case 6:break;
			}
		}break;
		default:return;
		}

		}
	}
}

测试用例:

本代码涉及大量文件操作:

运行要素:

  1. 在源代码所在文件夹中应该创建一个student.dat文件
  2. 在进行以文件形式添加人员信息时应该指明添加信息所在的路径

运行截图:

查询操作:

 

 

添加操作(stuadd.dat为同文件夹内的文件): 

平均成绩(按学号中的班级计算):

排名回显:

总结:

  1. 在此次实验中,我相对于以前对“面对对象”开发和“如何使用好封装类”有了更好的认识。并且对于C++相关文件的操作有了更加透彻的理解,方便日后进一步的使用。
  2. 对结构化,标准化的程序设计有了更深的理解,较以前更能够将自己的一段代码标准化和兼容化,不仅方便对代码的解读,也在代码的写作上更加省时省力。
  3. 程序的不足之处在于功能很少,有更多完善空间,面对一些问题还是没有足够的解决方法,比如:转专业,分流等导致学号和班级不符,一些操作没有纠错处理等。
  4. 在程序设计过程中,发现了自己在文件操作上有很多不足,经常错误的使用读取或者写入方法导致实验数据丢失,功亏一篑的情况。
  5. 在此次实验后,我更加意识到我在C++方面的很多不足,尤其是知识掌握程度,面对未来的学习,我应该不断查缺补漏,并学习更多新知识提升自己的程序设计能力和与程序设计相关的逻辑思维能力。

全部代码:

stu.cpp:

#include<iostream>
#include<algorithm>
#include<string>
#include<fstream>
#define spaces 7
#define maxsize 1000
using namespace std;
static int num = 0;

class stu {
private:		/*学生信息属于私密信息*/
	string name;	/*姓名*/
	string num;		/*学号*/
	string sex;		/*性别*/
public:
	double English;		/*各科成绩*/
	double Math;
	double Program_design;
	double Sports;
	double total;		/*总成绩*/
	int score;		/*判断此条信息是否完善*/
	stu() {		/*默认构造*/
		name = " "; num = " "; sex = " "; English = 0; Math = 0; Program_design = 0; Sports = 0; score = 0;
		settotal();
	}
	stu(string na, string nu, string se, double en, double ma, double c, double s) {		/*全参数构造*/
		name = na; num = nu;  English = en; Math = ma; Program_design = c; Sports = s; score = 1;
		if (se == "男" || se == "m")sex = 'm';
		else sex = 'w';
		settotal();
	}
	stu(string na, string nu, string se) {		/*不含成绩的构造*/
		name = na, num = nu;
		English = 0; Math = 0; Program_design = 0; Sports = 0; score = 0;
		if (se == "男" || se == "m")sex = 'm';
		else sex = 'w';
		settotal();
	}
	void stucpy(stu st) {		/*值复制函数*/
		name = st.getname();
		num = st.getnum();
		sex = st.getsex();
		English = st.English;
		Math = st.Math;
		Program_design = st.Program_design;
		Sports = st.Sports;
		score = st.score;
		settotal();
	}
	void studisplay() {		/*输出函数*/
		cout << num << " " << name << " " << English << " " << Math << " " << Program_design << " " << Sports << endl;
	}
	void settotal() {		/*计和函数*/
		total = English + Math + Program_design + Sports;
	}
	string getname() {		/*私有成员访问*/
		return name;
	}
	string getnum() {
		return num;
	}
	string getsex() {
		return sex;
	}
	void changename(string na) {		/*私有成员修改*/
		name = na;
	}
	void changenum(string nu) {
		num = nu;
	}
	void changesex(string se) {
		sex = se;
	}
};
stu st[maxsize];
#include"stu_deal.h"		/*函数文件*/
int main() {
	check();		/*查阅人数*/
	long_main();
	return 0;
}


stu_deal.h:

#pragma once
void check() {		/*查阅函数,统计文件中学生个数*/
	num = 0;
	ifstream ifs;
	ifs.open("student.dat", ios::in);
	if (!ifs.is_open())
	{
		cout << "Erro Read" << endl;
	}
	string buf;			//文件读取
	while (getline(ifs, buf))
	{
		num++;
	}
	ifs.close();
}

void findspace(string s, int* a) {	/*找字符串中空格方便进行字符串分割*/
	int len = 0;
	for (int i = 0; i < s.length(); i++) {
		if (s[i] == ' ') {
			a[len] = i;
			len++;
		}
	}
}

string strsplit(string s, int begin, int end) {		/*取begin到end的所有字符*/
	string ss;
	for (int i = begin; i <= end; i++) {
		ss.push_back(s[i]);
	}
	return ss;
}

double stringtodouble(string s) {		/*从文件中提取分数为string类型,将其转化为double类型(有精度损失)*/
	double ss = 0, sss = 0;
	int loc = s.length();
	for (int i = 0; i < s.length(); i++) {
		if (s[i] == '.')loc = i;
	}
	for (int i = 0; i < loc; i++) {		/*整数位*/
		ss *= 10;
		ss += (int)s[i] - 48;
	}
	for (int i = s.length() - 1; i >= loc; i--) {		/*小数位*/
		sss /= 10;
		sss += (int)s[i] - 48;
	}
	return sss + ss;
}


void stringtostudent(string s, stu* student) {		/*从文件提取的信息为string类型,将其转化为student(class)类型*/
	string na, nu, se, en, ma, c, sp;
	int* a;
	a = new int[spaces + 1];
	findspace(s, a);
	nu = strsplit(s, 0, a[0] - 1);		/*从长字符串中提取各项信息*/
	na = strsplit(s, a[0] + 1, a[1] - 1);
	se = strsplit(s, a[1] + 1, a[2] - 1);
	en = strsplit(s, a[2] + 1, a[3] - 1);
	ma = strsplit(s, a[3] + 1, a[4] - 1);
	c = strsplit(s, a[4] + 1, a[5] - 1);
	sp = strsplit(s, a[5] + 1, a[6] - 1);
	student->changename(na);
	student->changenum(nu);
	student->changesex(se);
	student->English = stringtodouble(en);
	student->Math = stringtodouble(ma);
	student->Program_design = stringtodouble(c);
	student->Sports = stringtodouble(sp);
	student->score = 1;
}



int Mainmenu() {		/*主菜单*/
	cout << endl << "档案中共有学生" << num << "人" << endl;
	cout << "请选择操作:" << endl;
	cout << "1:查询学生" << endl;
	cout << "2:添加学生" << endl;
	cout << "3:平均成绩" << endl;
	cout << "4:分数排名" << endl;
	cout << "5:退出" << endl;
	int chose;
	do {
		cin >> chose;
		if (chose < 1 || chose>5)cout << "无效指令,重新输入!" << endl;		/*指令异常处理*/
		else break;
	} while (true);
	return chose;
}

int findstumenu() {		/*查询信息菜单*/
	cout << endl << "选择查询方式:" << endl;
	cout << "1.学号查询" << endl;
	cout << "2.姓名查询" << endl;
	cout << "3.班级序号查询" << endl;
	cout << "4.后退" << endl;
	int chose;
	do {
		cin >> chose;
		if (chose < 1 || chose>4)cout << "无效指令,重新输入!" << endl;
		else break;
	} while (true);
	return chose;
}

int addstumenu() {		/*添加信息菜单*/
	cout << endl << "选择添加方式:" << endl;
	cout << "1.手动输入" << endl;
	cout << "2.文件导入" << endl;
	cout << "3.后退" << endl;
	int chose;
	do {
		cin >> chose;
		if (chose < 1 || chose>3)cout << "无效指令,重新输入!" << endl;
		else break;
	} while (true);
	return chose;
}

int scoresortmenu() {		/*分数排名菜单*/
	cout << endl<<"选择排序所参照的成绩" << endl;
	cout << "1.总分" << endl;
	cout << "2.英语" << endl;
	cout << "3.数学" << endl;
	cout << "4.程序设计" << endl;
	cout << "5.体育" << endl;
	cout << "6.后退" << endl;
	int chose;
	do {
		cin >> chose;
		if (chose < 1 || chose>5)cout << "无效指令,重新输入!" << endl;
		else break;
	} while (true);
	return chose;
}

void long_main() {
	ifstream ifs;
	while (true) {
		switch (Mainmenu())
		{
		case 1: {
			switch (findstumenu()) {
			case 1: {
				bool p = false;		/*是否查到*/
				string num;
				cout << "请输入学生学号:";
				cin >> num;
				ifs.open("student.dat", ios::in);
				string buf;			//文件读取
				while (getline(ifs, buf))
				{
					stu* stu1 = new stu();
					stringtostudent(buf, stu1);
					if (stu1->getnum() == num) {
						stu1->studisplay();
						p = true;
					}
				}
				if (!p)cout << "无此学生信息" << endl;
				ifs.close();
				break;
			}
			case 2: {
				string name;
				int len = 0;
				cout << "请输入学生姓名:";
				cin >> name;
				ifs.open("student.dat", ios::in);
				string buf;			//文件读取
				while (getline(ifs, buf))
				{
					stu* stu1 = new stu();
					stringtostudent(buf, stu1);
					if (stu1->getname() == name) {
						st[len].stucpy(*stu1);
						len++;
					}
				}
				if (!len)cout << "无此学生信息" << endl;
				else {
					for (int i = 0; i < len; i++) {		/*输出排序*/
						for (int j = 0; j < len - 1; j++) {
							if (st[j].getnum() > st[j + 1].getnum()) {
								stu s;
								s.stucpy(st[j]);
								st[j].stucpy(st[j + 1]);
								st[j + 1].stucpy(s);
							}
						}
					}
					cout << "查询到" << len << "条信息:" << endl;
					for (int i = 0; i < len; i++) {
						cout << i + 1 << ": " << st[i].getnum() << " " << st[i].getname() << endl;		/*不确定具体信息时只展示姓名和学号*/
					}
					cout << "选择查询学生序号:";
					int k;		/*选择具体信息编号*/
					do {
						cin >> k;
						if (k > len || k < 1)cout << "无效的指令,请重新输入" << endl;
						else break;
					} while (true);
					st[k - 1].studisplay();
				}
				ifs.close();
				break;
			}
			case 3: {
				string clas;
				int len = 0;
				cout << "请输入班号:";
				cin >> clas;
				ifs.open("student.dat", ios::in);
				string buf;			//文件读取
				while (getline(ifs, buf))
				{
					stu* stu1 = new stu();
					stringtostudent(buf, stu1);
					if (strsplit(stu1->getnum(), 0, 7) == clas) {
						st[len].stucpy(*stu1);
						len++;
					}
				}
				if (!len)cout << "无此班级" << endl;
				else {
					cout << "查询到" << len << "条信息:" << endl;
					for (int i = 0; i < len; i++) {
						cout << i + 1 << ": " << st[i].getnum() << " " << st[i].getname() << endl;		/*不确定具体信息时只展示姓名和学号*/
					}
					cout << "选择查询学生序号:";
					int k;
					do {
						cin >> k;
						if (k > len || k < 1)cout << "无效的指令,请重新输入" << endl;
						else break;
					} while (true);
					st[k - 1].studisplay();
				}
				ifs.close();
				break;
			}
			case 4:break;
			}break;
		case 2: {
			switch (addstumenu())
			{
			case 1: {

				cout << "输入学号,姓名,性别,英语成绩,数学成绩,程序设计成绩,体育成绩" << endl;
				stu stu1;
				string nu, na, se;
				cin >> nu >> na >> se >> stu1.English >> stu1.Math >> stu1.Program_design >> stu1.Sports;		/*手动输入(空格为间隔)*/
				bool p = false;		/*相同学号是否已存在*/
				ifs.open("student.dat", ios::in);
				string buf;			//文件读取
				while (getline(ifs, buf))
				{
					stu* stu2 = new stu();
					stringtostudent(buf, stu2);
					if (stu2->getnum() == nu) {
						bool p = true;
					}
				}
				ifs.close();
				if (!p) {		/*不存在添加*/
					int len = 0;
					ifs.open("student.dat", ios::in);
					string buf;			//文件读取
					while (getline(ifs, buf))
					{
						stu* stu2 = new stu();
						stringtostudent(buf, stu2);
						st[len].stucpy(*stu2);
						len++;
					}
					ofstream ofs;
					ofs.open("student.dat", ios::out);
					for (int i = 0; i < len; i++) {
						ofs << st[i].getnum() << " " << st[i].getname() << " " << st[i].English << " " << st[i].Math << " " << st[i].Program_design << " " << st[i].Sports << " " << "1" << endl;
					}
					ofs << nu << " " << na << " " << se << " " << stu1.English << " " << stu1.Math << " " << stu1.Program_design << " " << stu1.Sports << " " << "1" << endl;
					ofs.close();
					num++;
				}
				else {		/*存在则更新*/
					int len = 0;
					ifs.open("student.dat", ios::in);
					string buf;			//文件读取
					while (getline(ifs, buf))
					{
						stu* stu2 = new stu();
						stringtostudent(buf, stu2);
						st[len].stucpy(*stu2);
						len++;
					}
					st[len].stucpy(stu1);
					st[len].changename(na);
					st[len].changenum(nu);
					st[len].changesex(se);
					len++;
					ifs.close();
					ofstream ofs;
					ofs.open("student.dat", ios::out);
					for (int i = 0; i < len; i++) {
						ofs << st[i].getnum() << " " << st[i].getname() << " " << st[i].English << " " << st[i].Math << " " << st[i].Program_design << " " << st[i].Sports << " " << "1" << endl;
					}
					ofs.close();
				}
				break;
			}
			case 2: {
				cout << "输入文件路径" << endl;
				string ss;
				cin >> ss;
				int len = 0;
				int len1 = 0;
				stu st1[maxsize];
				ifs.open("student.dat", ios::in);
				string buf;			//文件读取
				while (getline(ifs, buf))
				{
					stu* stu2 = new stu();
					stringtostudent(buf, stu2);
					st[len].stucpy(*stu2);
					len++;
				}
				ifs.close();
				ifs.open(ss, ios::in);
				if (!ifs.is_open()) {		/*打不开证明没有这个文件,或者权限不够*/
					cout << "文件路径无效!" << endl;
					break;
				}
				while (getline(ifs, buf))
				{
					stu* stu2 = new stu();
					stringtostudent(buf, stu2);
					st1[len1].stucpy(*stu2);
					len1++;
				}
				ifs.close();
				ofstream ofs;
				ofs.open("student.dat", ios::app);
				for (int i = 0; i < len1; i++) {
					bool t = true;		/*是否已存在*/
					for (int j = 0; j < len; j++) {
						if (st1[i].getnum() == st[j].getnum())t = false;
					}
					if (t)ofs << st1[i].getnum() << " " << st1[i].getname() << " " << st1[i].getsex() << " " << st1[i].English << " " << st1[i].Math << " " << st1[i].Program_design << " " << st1[i].Sports << " " << "1" << endl;		/*未存在添加,已存在不添加*/
				}
				ofs.close();
				check();
				break;
			}
			case 3:break;
			}
			break;
		}break;
		case 3: {
			int len = 0;
			ifs.open("student.dat", ios::in);
			string buf;			//文件读取
			while (getline(ifs, buf))
			{
				bool b = true;	/*未找到班级*/
				stu* stu1 = new stu();
				stringtostudent(buf, stu1);
				for (int i = 0; i < len; i++) {
					if (strsplit(st[i].getnum(), 0, 7) == strsplit(stu1->getnum(), 0, 7)) {		/*班级名和学生学号前八位相等*/
						b = false;		
						st[i].English += stu1->English;		/*此段代码中st数组作为班级数组*/
						st[i].Math += stu1->Math;
						st[i].Program_design += stu1->Program_design;
						st[i].Sports += stu1->Sports;
						st[i].score++;		/*记录人数*/
					}
				}
				if (b) {		/*没有班级则新建班级*/
					st[len].changenum(strsplit(stu1->getnum(), 0, 7));
					len++;
					st[len].English += stu1->English;
					st[len].Math += stu1->Math;
					st[len].Program_design += stu1->Program_design;
					st[len].Sports += stu1->Sports;
					st[len].score++;		/*记录人数*/
				}
			}
			for (int i = 0; i < len; i++) {		/*输出排序*/
				for (int j = 0; j < len - 1; j++) {
					if (st[j].getnum() > st[j + 1].getnum()) {
						stu s;
						s.stucpy(st[j]);
						st[j].stucpy(st[j + 1]);
						st[j + 1].stucpy(s);
					}
				}
			}
			for (int i = 0; i < len; i++) {
				cout << "班号:" << st[i].getnum() << "  " << "英语平均分:" << st[i].English / st[i].score << "  数学平均分:" << st[i].Math / st[i].score << "  程序设计平均分" << st[i].Program_design / st[i].score << "  体育平均分" << st[i].Sports / st[i].score << endl;
			}
		}break;
		case 4: {
			ifstream ifs;
			int len = 0;
			ifs.open("student.dat", ios::in);
			string buf;			//文件读取
			while (getline(ifs, buf))
			{
				stu* stu2 = new stu();
				stringtostudent(buf, stu2);
				st[len].stucpy(*stu2);
				len++;
			}
			ifs.close();
			switch (scoresortmenu()) {
			case 1: {
				for (int i = 0; i < len; i++) {
					for (int j = 0; j < len - 1; j++) {		/*成绩排序*/
						if (st[j].total < st[j + 1].total) {
							stu stu1;
							stu1.stucpy(st[j]);
							st[j].stucpy(st[j + 1]);
							st[j+1].stucpy(stu1);
						}
					}
				}
				ofstream ofs;
				ofs.open("Total_stu.dat", ios::trunc);		/*重写文件*/
				for (int i = 0; i < len; i++) {
					ofs << st[i].getnum() << " " << st[i].getname() << " ";
					if (st[i].getsex() == "m")ofs << "男";
					else ofs << "女";
					ofs<<" "<<st[i].total << endl;
					cout << st[i].getnum() << " " << st[i].getname() << " ";
					if (st[i].getsex() == "m")cout << "男";
					else cout << "女";
					cout << " " << st[i].total<< endl;
				}
				ofs.close();
				break;
			}
			case 2: {
				for (int i = 0; i < len; i++) {
					for (int j = 0; j < len - 1; j++) {
						if (st[j].English < st[j + 1].English) {
							stu stu1;
							stu1.stucpy(st[j]);
							st[j].stucpy(st[j + 1]);
							st[j + 1].stucpy(stu1);
						}
					}
				}
				ofstream ofs;
				ofs.open("English_stu.dat", ios::trunc);
				for (int i = 0; i < len; i++) {
					ofs << st[i].getnum() << " " << st[i].getname() << " ";
					if (st[i].getsex() == "m")ofs << "男";
					else ofs << "女";
					ofs << " " << st[i].English  << endl;
					ofs.close();
					cout << st[i].getnum() << " " << st[i].getname() << " ";
					if (st[i].getsex() == "m")cout << "男";
					else cout << "女";
					cout << " " << st[i].English << endl;
				}
				break;
			}
			case 3: {
				for (int i = 0; i < len; i++) {
					for (int j = 0; j < len - 1; j++) {
						if (st[j].Math < st[j + 1].Math) {
							stu stu1;
							stu1.stucpy(st[j]);
							st[j].stucpy(st[j + 1]);
							st[j + 1].stucpy(stu1);
						}
					}
				}
				ofstream ofs;
				ofs.open("Math_stu.dat", ios::trunc);
				for (int i = 0; i < len; i++) {
					ofs << st[i].getnum() << " " << st[i].getname() << " ";
					if (st[i].getsex() == "m")ofs << "男";
					else ofs << "女";
					ofs << " " << st[i].Math<< endl;
					cout << st[i].getnum() << " " << st[i].getname() << " ";
					if (st[i].getsex() == "m")cout << "男";
					else cout << "女";
					cout << " " << st[i].Math<< endl;
				}
				ofs.close();
				break;
			}
			case 4: {
				for (int i = 0; i < len; i++) {
					for (int j = 0; j < len - 1; j++) {
						if (st[j].Program_design < st[j + 1].Program_design) {
							stu stu1;
							stu1.stucpy(st[j]);
							st[j].stucpy(st[j + 1]);
							st[j + 1].stucpy(stu1);
						}
					}
				}
				ofstream ofs;
				ofs.open("Program_design_stu.dat", ios::trunc);
				for (int i = 0; i < len; i++) {
					ofs << st[i].getnum() << " " << st[i].getname() << " ";
					if (st[i].getsex() == "m")ofs << "男";
					else ofs << "女";
					ofs << " " << st[i].Program_design  << endl;
					cout << st[i].getnum() << " " << st[i].getname() << " ";
					if (st[i].getsex() == "m")cout << "男";
					else cout << "女";
					cout << " " << st[i].Program_design << endl;
				}
				ofs.close();
				break;
			}
			case 5: {
				for (int i = 0; i < len; i++) {
					for (int j = 0; j < len - 1; j++) {
						if (st[j].Sports < st[j + 1].Sports) {
							stu stu1;
							stu1.stucpy(st[j]);
							st[j].stucpy(st[j + 1]);
							st[j + 1].stucpy(stu1);
						}
					}
				}
				ofstream ofs;
				ofs.open("Sports_stu.dat", ios::trunc);
				for (int i = 0; i < len; i++) {
					ofs << st[i].getnum() << " " << st[i].getname() << " ";
					if (st[i].getsex() == "m")ofs << "男";
					else ofs << "女";
					ofs << " "<< st[i].Sports << endl;
					cout << st[i].getnum() << " " << st[i].getname() << " ";
					if (st[i].getsex() == "m")cout << "男";
					else cout<< "女";
					cout << " "<< st[i].Sports << endl;
				}
				ofs.close();
				break;
			}
			case 6:break;
			}
		}break;
		default:return;
		}

		}
	}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/115371.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

SFP、SFP+、SFP28、QSFP+和QSFP28之间的区别以及不同场景的使用选型

SFP、SFP+、SFP28、QSFP+和QSFP28之间的区别以及不同场景的使用选型。 SFP、SFP+、SFP28、QSFP+和QSFP28这些光模块类型对专业人员来说并不陌生,这些热拔插模块都可用于连接网络交换机和其他网络设备(如服务器或收发器)进行数据传输。但你了解这些模块的具体区别吗?QSFP28…

Python爬虫爬取某电影排行榜图片实例

今天继续给大家介绍Python 爬虫相关知识&#xff0c;本文主要内容是Python爬虫爬取某电影排行榜图片实例。 一、要求分析 在上文Python爬虫爬取某电影排行实例中&#xff0c;我们已经能够使用Python程序爬取某电影排行榜中的电影名称。今天&#xff0c;我们来尝试以下下载电影…

入职第一天,HR拿了一个橙子进门说:你的学历不是统招本科,不符合公司要求,给你个橘子,走吧!...

今天来讲一件又好笑又好气的事&#xff0c;这是一位网友的亲身经历&#xff1a;入职第一天&#xff0c;入职材料填到一半&#xff0c;HR拿了一个橙子进门&#xff0c;放在桌子上开口说&#xff1a;抱歉&#xff0c;由于之前工作失误&#xff0c;没确认你的第一学历不是统招本科…

RK3568平台开发系列讲解(调试篇)Linux相关日志分析

🚀返回专栏总目录 文章目录 一、dmesg二、动态打印案例沉淀、分享、成长,让自己和他人都能有所收获!😄 📢本文我们要介绍Linux内核的日志分析。 一、dmesg printk是在内核中运行的向控制台输出显示的函数,Linux内核首先在内核空间分配一个静态缓冲区,作为显示用的空…

​杭州蓝然创业板IPO终止:应收账款、存货账面高,楼永通为实控人​

近日&#xff0c;杭州蓝然技术股份有限公司&#xff08;下称“杭州蓝然”&#xff09;向深圳证券交易所提交了撤回在创业板上市申请文件的申请。同时&#xff0c;其保荐机构也撤回保荐。12月23日&#xff0c;深圳证券交易所做出决定&#xff0c;终止对杭州蓝然在创业板IPO的审核…

学习笔记:Java 并发编程②

若文章内容或图片失效&#xff0c;请留言反馈。 部分素材来自网络&#xff0c;若不小心影响到您的利益&#xff0c;请联系博主删除。 视频链接&#xff1a;https://www.bilibili.com/video/av81461839配套资料&#xff1a;https://pan.baidu.com/s/1lSDty6-hzCWTXFYuqThRPw&am…

Matlab 实现磁测数据日变改正

1 算法 算法来自于GEMLink 5.2的帮助文档&#xff0c;这个文档基本解决了算法问题。 GemLink日变改正模块界面 1.1 概述 日变改正模块旨在执行磁力日变数据计算&#xff0c;而不用在仪器上进行日变&#xff08;仪器是未经过校正的原始数据&#xff09;。这个模块要求已经保…

[ CTF ] WriteUp-2022年春秋杯网络安全联赛-冬季赛

没啥时间打比赛就大概看了一下做了几题 文章目录[Misc] reindeer game[Misc] 调查问卷[Web] ezphp[Misc] reindeer game ​ 这题最简单的方法就是玩游戏了&#xff0c;然后直接出flag flag{82a2acb6-9803-4936-92db-f1431d90c6d1} [Misc] 调查问卷 flag{the_harder_u_struggl…

故事的开始:RaidAI

目录引言RapidAI/RapidOCRRapidAI/YOLO2COCORapidOcrAndroidOnnxRapidAI/RapidOcrNcnnRapidAI/PaddleOCRModelConverterRapidAI/RapidTTSRapidAI/RapidASRRapidAI/RapidPix2Pix引言 RapidAI是一个将AI模型应用到工程中的开源组织&#xff0c;致力于搭建AI模型从学术界到工程界…

RabbitMQ:消息分发模型

Work queues&#xff0c;也被称为 Task queues&#xff0c;任务模型&#xff0c;当消息处理比较耗时时的时候&#xff0c;可能产生消息的速度会远远大于消息的消费速度&#xff0c;消息会堆积越来越多&#xff0c;无法及时处理&#xff0c;此时就可以使用work模型&#xff1a;让…

圣诞的荒诞小故事并记录互联网协议-五层模型

今天敲代码敲着敲着灵光乍现&#xff0c;突然一个荒诞的故事&#x1f4a1;映入脑海。 1.未来和过去&#xff1a; 人高度发达&#xff08;以下称之为渡&#xff09; 渡可以打开时空穿越过去&#xff08;以下称之为旧迹&#xff09;&#xff0c;并且可以进随心所欲的来去自如&a…

【微服务】Nacos的寻址机制

目录 一、 Nacos的寻址机制 1、前提 2、设计 3、内部实现 3.1、单机寻址 3.2、文件寻址 3.3、地址服务器寻址 4、未来可扩展点 4.1、集群节点自动扩缩容 &#x1f496; Spring家族及微服务系列文章 一、 Nacos的寻址机制 1、前提 Nacos 支持单机部署以及集群部署&am…

xxl-job基本原理以及底层流程讲解

任务执行策略 任务阻塞策略 整体架构部署 这里主要讲解下每个模块的作用 调度模块&#xff1a;负责管理调度信息&#xff0c;按照调度配置发出调度请求&#xff0c;自身不承担任何业务代码。调度系统于任务解耦&#xff0c;提高了系统可用性和稳定性&#xff0c;同时调度系统性…

MFC工程的文件说明

工程创建 使用VS创建一个MFC的工程&#xff0c;这里不做说明 文件说明 使用VS创建好的MFC工程有如下文件&#xff1a; MFC全称Microsoft Foundation Classes&#xff0c;也就是微软基础类库;是VC的核心&#xff0c;是C与Windows API的结合&#xff0c;很彻底的用C封装了Win…

这个医生说的防疫措施,我挺认可的

上面那个语音是一个朋友发给我的&#xff0c;语音时间比较长&#xff0c;但是里面讲的很多内容我觉得挺不错的&#xff0c;现在疫情反复&#xff0c;我们会听到很多关于疫情的信息&#xff0c;有的人说奥密克戎感染性很强&#xff0c;之前专家说的无症状是骗人的&#xff0c;根…

元宇宙产业委联席秘书长叶毓睿:去中心化和去中介化的定义、区别,以及和元宇宙的关系

原创 Peter Ye 转自&#xff1a;乐生活与爱IT Plus 近日有个有关元宇宙的线上分享&#xff0c;有位名叫谢晓雪的听众提了一个我之前没思考过的问题&#xff1a;去中心化和去中介化的区别&#xff1f; 当时我回答了一部分&#xff0c;但主要是讲的之间的联系&#xff0c;区…

【年终总结】求职面试一定要扬长避短

时光荏苒&#xff0c;这周日就是元旦了&#xff0c;我也把年终总结提上了日程。 前言 今年的年终总结我打算多写几篇&#xff0c;每篇瞄准一个方向&#xff0c;写一些对大家有帮助、有启发的内容。 初步的想法会整理三篇&#xff1a; 第一篇分享求职面试的经验第二篇分享接私…

Mybaits(环境搭建和基本使用)

目录   一、什么是 Mybaits   二、配置环境     2.1 导入 MyBatis Framework     2.2 连接 MyBatis   三、增删改查功能     3.1 创建实体类     3.2 select     3.3 delete 和 update     3.4 insert   四、SQL 注入     4.1 什么是 SQL…

2022年终总结、展望2023

2022年终总结、展望2023前言一、2022年工作成绩二、2022年工作不足即如何改进三、可以传承的工作方法或者经验四、2023年工作目标 &#xff08;目标细化、可落地&#xff09;<font colorred> 1、薪资待遇2、云端高效的实时智能视频处理平台架构图3、 云端高效的实时智能视…

Redis从部署群集到ASK路由

目录 数据库简介 一、数据库分类 二、Redis重要特性 三、redis应用场景 安装redis redis基本命令 redis持久化 redis主从复制 redis集群 群集实施 配置节点发现 Redis Cluster 通讯流程 Redis Cluster手动分配槽位 Redis Cluster ASK路由介绍 模拟故障转移 自动搭建部署Redis C…