牛刀小试 - C++ 学生信息管理系统

news2024/12/24 8:57:28

参考文档:

C++实现:学生管理系统(详细解析)

关于switch中的default里面的break是否可以省略这件事

需求要求:

(1)增加记录
(2)查找记录
(3)删除记录
(4)成绩修改
(5)统计分析:单科统计、课程标准差和合格率;
(6)排序功能:总分排序(从高到低),如果相同,则按学号排
序;
(7)文件操作:可以打开文件,显示班级的所有学生信息;可以将增加或修改后的成绩重新写入文件;可以将排序好的信息写入新的文件;

需求思路:

抽象处理:

  • 学科类:class Subject
  • 学生类:class Student
  • 学生列表类:class StudentList

完整代码:

StudentAdmin.h

#ifndef STUDENTADMIN_H_
#define STUDENTADMIN_H_

#include <vector>
#include <string>

using namespace std;

// 学科类
class Subject
{
public:
	string proName;		//学科名
	double proScore;	//分数

	void showCpInfo();	//显示分数
	static void showHeader(); // 显示表头
};

//学生类
class Student
{
public:
	string stuNum;		//学号
	string stuName;		//姓名
	string gender;		//性别
	int gradeNum;		//年级
	string department;	//专业
	int classNum;		//班级
	vector<Subject> cps;//学生的各个科目
	int cpNum;			//科目门数
	double score;		//平均分

	// 构造函数
	Student(){};
	Student(string stuNum, string stuName, string gender, int gradeNum, string department, int classNum, vector<Subject> cps, int cpNum, double score)
	{
		this->stuNum = stuNum;
		this->stuName = stuName;
		this->gender = gender;
		this->gradeNum = gradeNum;
		this->department = department;
		this->classNum = classNum;
		this->cps = cps;
		this->cpNum = cpNum;
		this->score = score;
	};
	void showStuInfo();	//输出属性值
	static void showHeader();	//输出表头
};

//学生列表类
class StudentList
{
public:
	StudentList(){}
	StudentList(vector<Student> stuList)
	{
		this->stuList = stuList;
	}

	void init();		//初始化
	void menu();		//主菜单
	void readFile();	//读取数据
	void writeFile();	//写入数据
	void insertList();	//添加学生信息
	void deleteList();	//删除学生信息
	void updateList();	//修改学生信息
	void selectList();	//查询学生信息
	void displayStu();	//遍历学生信息
	void displayCp();	//查询获奖信息
	void diaplayList();	//显示信息列表
	void statisticList();//统计学生数据
	void clearList();	//清空系统数据

	// 二级函数
	void doInsert();
	void doDeleteByNum();	//按学号删除
	void doDeleteByName();	//按姓名删除
	void doUpdateInfo();	//更新学生信息
	void doUpdateScore();	//更新学生成绩
	void doSelectByNum();	//按学号查询
	void doSelectByName();	//按姓名查询
	void doSortByNum();		//按学号排序
	void doSortByScore();	//按平均分排序
	void doClear();			//清理数据

	// 三级函数
	void doUpDataScoreByOne(Student &s); //基于原有成绩信息修改
	void doUpDataScoreByAll(Student &s); //清空所有科目及成绩信息
	
	//工具类函数
	static bool cmpNum(const Student& s1, const Student& s2);	//将学生列表按学号升序排列 
	static bool cmpScroe(const Student& s1, const Student& s2); //将学生列表按平均分降序排列,平均分相同的再按学号升序排列
private:
	vector<Student> stuList;
}
;
#endif

StudentAdmin.cpp

#define _CRT_SECURE_NO_WARNINGS
#define _CRT_NONSTDC_NO_DEPRECATE

#include "StudentAdmin.h"

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>	// 算法库,提供大量迭代器成员函数, sort()
#include <iomanip>		// 格式输入输出
#include <conio.h>		// 使用getch()函数
#include <windows.h>	//使用Sleep()函数

using namespace std;

void Subject::showCpInfo()
{
	cout << left << setw(45) << proName;  // 设置字段宽度为45位
	cout << left << setw(6) << proScore << endl;
}

void Subject::showHeader()
{
	cout << left << setw(45) << "科目";
	cout << left << setw(6) << "分数" << endl;
}

void Student::showStuInfo()
{
	cout << left << setw(12) << stuNum;
	cout << left << setw(10) << stuName;
	cout << left << setw(6) << gender;
	cout << left << setw(8) << gradeNum;
	cout << left << setw(23) << department;
	cout << left << setw(6) << classNum;
	cout << left << setw(6) << score << endl;
}

void Student::showHeader()
{
	cout << left << setw(12) << "学号";
	cout << left << setw(10) << "姓名";
	cout << left << setw(6) << "性别";
	cout << left << setw(8) << "年级";
	cout << left << setw(23) << "专业";
	cout << left << setw(6) << "班级";
	cout << left << setw(6) << "平均分" << endl;
}

void StudentList::init()
{
	readFile();
}

void StudentList::menu()
{

	system("cls");
	cout << "\t\t\t**********欢迎来到学生成绩管理系统**********" << endl;
	cout << "\t\t\t你可以进行以下操作:" << endl;
	cout << "\t\t\t|------------------------------------------|" << endl;
	cout << "\t\t\t|             1   添加学生信息             |" << endl;
	cout << "\t\t\t|------------------------------------------|" << endl;
	cout << "\t\t\t|             2   删除学生信息             |" << endl;
	cout << "\t\t\t|------------------------------------------|" << endl;
	cout << "\t\t\t|             3   修改学生信息             |" << endl;
	cout << "\t\t\t|------------------------------------------|" << endl;
	cout << "\t\t\t|             4   查询学生信息             |" << endl;
	cout << "\t\t\t|------------------------------------------|" << endl;
	cout << "\t\t\t|             5   显示信息列表             |" << endl;
	cout << "\t\t\t|------------------------------------------|" << endl;
	cout << "\t\t\t|             6   统计学生数据             |" << endl;
	cout << "\t\t\t|------------------------------------------|" << endl;
	cout << "\t\t\t|             7   清空系统数据             |" << endl;
	cout << "\t\t\t|------------------------------------------|" << endl;
	cout << "\t\t\t|             0   退出                     |" << endl;
	cout << "\t\t\t|------------------------------------------|" << endl;

	bool sign = true;
	while (sign)
	{
		switch (getch())
		{
		case '1':
			insertList();
			sign = false;
			break;
		case '2':
			deleteList();
			sign = false;
			break;
		case '3':
			updateList();
			sign = false;
			break;
		case '4':
			selectList();
			sign = false;
			break;
		case '5':
			diaplayList();
			sign = false;
			break;
		case '6':
			statisticList();
			sign = false;
			break;
		case '7':
			clearList();
			sign = false;
			break;
		case '0':
			exit(0);
			break;
		default:
			cout << "\n\t\t\t输入不合法,请重新选择【0-7】:";
		}
	}
}

void StudentList::readFile()
{
	ifstream ifs("stuList.txt", ios::in);
	if (ifs)
	{
		int n = 0;	// 学生数量
		ifs >> n;
		for (int i = 0; i < n; i++)
		{
			Student s;
			ifs >> s.stuNum >> s.stuName >> s.gender >> s.gradeNum >> s.department >> s.classNum >> s.cpNum >> s.score;
			for (int j = 0; j < s.cpNum; j++)
			{
				Subject cp;
				ifs >> cp.proName >> cp.proScore;
				s.cps.push_back(cp);
			}
			stuList.push_back(s);
		}
		ifs.close();
	}

}

void StudentList::writeFile()
{
	ofstream ofs("stuList.txt", ios::out);

	if (!ofs)
	{
		cout << "\t\t存档失败!程序即将自动退出 . . ." << endl;
		Sleep(3000);//睡眠3000毫秒,即停留3秒钟 
		exit(0);
	}
	ofs << stuList.size() << endl;	//写入学生数量
	for (int i = 0; i < stuList.size(); i++)
	{
		ofs << stuList[i].stuNum << " " << stuList[i].stuName << " " << stuList[i].gender
			<< " " << stuList[i].gradeNum << " " << stuList[i].department << " " << stuList[i].classNum
			<< " " << stuList[i].cpNum << " " << stuList[i].score << endl;
		for (int j = 0; j < stuList[i].cps.size(); j++)
		{
			ofs << stuList[i].cps[j].proName << " " << stuList[i].cps[j].proScore << endl;
		}
	}
	ofs.close();
}

void StudentList::insertList()
{
	
	system("cls");
	cout << "\t\t**********************欢迎来到添加学生信息功能*************************" << endl;
	cout << "\t\t学生基本信息一览表:" << endl;
	displayStu();
	cout << endl;
	cout << "\t\t------------------" << endl;
	cout << "\t\t1 添加学生信息" << endl;
	cout << "\t\t2 返回主菜单" << endl;
	cout << "\t\t------------------" << endl;
	cout << "\t\t请选择【1-2】:";
	bool sign = true;
	while (sign)
	{
		switch (getch())
		{
		case '1':
			doInsert();
			sign = false;
			break;
		case '2':
			menu();
			sign = false;
			break;
		default:
			cout << "\n\t\t输入不合法,请重新选择【1-2】:";
		}
	}
}

void StudentList::deleteList()
{
	system("cls");
	cout << "\t\t***********************欢迎来到删除学生信息功能***********************" << endl;
	string sel = "0";
	cout << "\t\t-----------------" << endl;
	cout << "\t\t1 按学号删除" << endl;
	cout << "\t\t2 按姓名删除" << endl;
	cout << "\t\t3 返回主菜单" << endl;
	cout << "\t\t-----------------" << endl;
	cout << "\t\t请进行选择【1-3】:";

	bool sign = true;
	while (sign)
	{
		switch (getch())
		{
		case '1':
			doDeleteByNum();
			sign = false;
			break;
		case '2':
			doDeleteByName();
			sign = false;
			break;
		case '3':
			menu();
			sign = false;
			break;
		default:
			cout << "\n\t\t输入不合法,请重新选择【1-3】:";
		}
	}
}

void StudentList::updateList()
{
	system("cls");
	cout << "\t\t***********************欢迎来到修改学生信息功能***********************" << endl;
	string sel = "0";
	cout << "\t\t-----------------" << endl;
	cout << "\t\t1 修改学生基本信息" << endl;
	cout << "\t\t2 修改学生成绩信息" << endl;
	cout << "\t\t3 返回主菜单" << endl;
	cout << "\t\t-----------------" << endl;
	cout << "\t\t请进行选择【1-3】:";
	bool sign = true;
	while (sign)
	{
		switch (getch())
		{
		case '1':
			doUpdateInfo();
			sign = false;
			break;
		case '2':
			doUpdateScore();
			sign = false;
			break;
		case '3':
			menu();
			sign = false;
			break;
		default:
			cout << "\n\t\t输入不合法,请重新选择【1-3】:";
		}

	}
}

void StudentList::selectList()
{
		system("cls");
		cout << "\t\t***********************欢迎来到查询学生信息功能************************" << endl;
		cout << "\t\t-----------------" << endl;
		cout << "\t\t1 按学号查询" << endl;
		cout << "\t\t2 按姓名查询" << endl;
		cout << "\t\t3 返回主菜单" << endl;
		cout << "\t\t-----------------" << endl;
		cout << "\t\t请进行选择【1-3】:";
		bool sign = true;
		while (sign)
		{
			switch (getch())
			{
			case '1':
				doSelectByNum();
				sign = false;
				break;
			case '2':
				doSelectByName();
				sign = false;
				break;
			case '3':
				menu();
				sign = false;
				break;
			default:
				cout << "\n\t\t输入不合法,请重新选择【1-3】:";
			}
		}
	}

void StudentList::displayStu()
{
	cout << "\t\t-----------------------------------------------------------------------" << endl;
	cout << "\t\t";
	Student::showHeader();
	cout << "\t\t-----------------------------------------------------------------------" << endl;
	for (int i = 0; i < stuList.size(); i++)
	{
		cout << "\t\t";
		stuList[i].showStuInfo();
	}
	cout << "\t\t-----------------------------------------------------------------------" << endl;
}

void StudentList::displayCp()
{
	cout << "\t\t-----------------------------------------------------------------------" << endl;
	cout << "\t\t";
	cout << left << setw(12) << "学号";
	cout << left << setw(10) << "姓名";
	Subject::showHeader();
	cout << "\t\t-----------------------------------------------------------------------" << endl;
	for (int i = 0; i < stuList.size(); i++)
	{
		for (int j = 0; j < stuList[i].cps.size(); j++)
		{
			cout << "\t\t";
			cout << left << setw(12) << stuList[i].stuNum;
			cout << left << setw(10) << stuList[i].stuName;
			stuList[i].cps[j].showCpInfo();
		}
	}
	cout << "\t\t-----------------------------------------------------------------------" << endl;
}

void StudentList::diaplayList()
{
	system("cls");
	cout << "\t\t***********************欢迎来到显示信息列表功能************************" << endl;
	cout << "\t\t表1:学生基本信息一览表" << endl;
	this->displayStu();
	cout << "\n\t\t表2:学生成绩信息一览表" << endl;
	this->displayCp();
	cout << "\n\t\t按任意键返回菜单" << endl;
	while (getch())
	{
		menu();
	}
}

void StudentList::statisticList()
{
	system("cls");
	cout << "\t\t***********************欢迎来到统计学生数据功能************************" << endl;
	string sel = "0";
	cout << "\t\t-----------------" << endl;
	cout << "\t\t1 按学号排序" << endl;
	cout << "\t\t2 按平均分排序" << endl;
	cout << "\t\t3 返回主菜单" << endl;
	cout << "\t\t-----------------" << endl;
	cout << "\t\t请进行选择【1-3】:";
	bool sign = true;
	while (sign)
	{
		switch (getch())
		{
		case '1':
			doSortByNum();
			sign = false;
			break;
		case '2':
			doSortByScore();
			sign = false;
			break;
		case '3':
			menu();
			sign = false;
			break;
		default:
			cout << "\n\t\t输入不合法,请重新选择【1-3】:";
		}
	}
}

void StudentList::clearList()
{
	system("cls");
	cout << "\t\t**************欢迎来到清空系统数据功能***************" << endl;
	cout << "\t\t------------------" << endl;
	cout << "\t\t1 确认清空系统数据" << endl;
	cout << "\t\t2 返回主菜单" << endl;
	cout << "\t\t------------------" << endl;
	cout << "\t\t请慎重选择【1-2】:";
	bool sign = true;
	while (sign)
	{
		switch (getch())
		{
		case '1':
			doClear();
			sign = false;
			break;
		case '2':
			menu();
			sign = false;
			break;
		default:
			cout << "\n\t\t输入不合法,请重新选择【1-3】:";
		}
	}
}

void StudentList::doInsert()
{
	string flag = "1";
	while (flag == "1")
	{
		cout << "\n\t\t输入学生信息:" << endl;
		cout << "\t\t学号:";
		Student s;
		bool check = false;
		do
		{
			check = false;
			cin >> s.stuNum;
			for (int i = 0; i < stuList.size(); ++i)
			{
				if (s.stuNum == stuList[i].stuNum)
				{
					cout << "\t\t该学号已存在,请重新输入:";
					check = true;
					break;
				}

			}

		} while (check);

		cout << "\t\t姓名:";
		cin >> s.stuName;
		cout << "\t\t性别:";
		cin >> s.gender;
		cout << "\t\t年级:";
		cin >> s.gradeNum;
		cout << "\t\t专业:";
		cin >> s.department;
		cout << "\t\t班级:";
		cin >> s.classNum;

		s.score = 0;
		string option = "1";
		cout << "\t\t是否添加成绩信息?(1 是 0 否)" << endl;
		cout << "\t\t请进行选择【0-1】:";
		cin >> option;
		while (option != "0" && option != "1")
		{
			cout << "\n\t\t输入不合法,请重新选择【0-1】:";
			cin >> option;
		}
		if (option == "1")
		{
			int cnt = 0;
			while (option == "1")
			{

				cnt++;
				Subject cp;
				cout << "\t\t第" << cnt << "科科目名称:";
				cin >> cp.proName;
				cout << "\t\t第" << cnt << "科科目成绩:";
				cin >> cp.proScore;
				s.cps.push_back(cp);
				cout << "\t\t该科成绩信息添加成功!是否继续添加?(1 是 0 否)" << endl;
				cout << "\t\t请进行选择【0-1】:";
				cin >> option;
				while (option != "0" && option != "1")
				{
					cout << "\n\t\t输入不合法,请重新选择【0-1】:";
					cin >> option;
				}
			}
		}

		for (int i = 0; i < s.cps.size(); i++)
		{
			s.score += s.cps[i].proScore;
		}
		if (s.cps.size() > 0) 
			s.score /= s.cps.size(); //计算平均分
		s.cpNum = s.cps.size();
		stuList.push_back(s);
		writeFile();

		cout << "\n\t\t该名学生信息添加成功!是否继续添加?(1 是 0 否)" << endl;
		cout << "\t\t请进行选择【0-1】:";
		cin >> flag;
		while (flag != "0" && flag != "1")
		{
			cout << "\n\t\t输入不合法,请重新选择【0-1】:";
			cin >> flag;
		}
	}
	insertList();	 //返回上一级
}

void StudentList::doDeleteByNum()
{
	string keyNum;
	bool flag = false;  //成功找到该学生
	cout << "\n\t\t请输入待删除学生的学号:";
	cin >> keyNum;
	for (vector<Student>::iterator it = stuList.begin(); it != stuList.end(); ++it)
	{
		if (it->stuNum == keyNum)
		{
			flag = true;
			cout << "\t\t待删除学生的信息如下:" << endl;
			cout << "\t\t-----------------------------------------------------------------------" << endl;
			cout << "\t\t";
			Student::showHeader();
			cout << "\t\t-----------------------------------------------------------------------" << endl;
			cout << "\t\t";
			it->showStuInfo();
			cout << "\t\t-----------------------------------------------------------------------" << endl;
			cout << "\t\t确认删除?(1 是 0 否)" << endl;
			cout << "\t\t请进行选择【0-1】:";
			string ch = "0";
			cin >> ch;
			while (ch != "0" && ch != "1")
			{
				cout << "\n\t\t输入不合法,请重新选择【0-1】:";
				cin >> ch;
			}
			if (ch == "0") break;
			else
			{
				stuList.erase(it);
				writeFile();
				cout << "\t\t删除成功!" << endl;
				break;
			}
		}
	}
	if (!flag) 
		cout << "\t\t查无此人,无法删除!\n" << endl;

	cout << "\n\t\t按任意键返回上一级" << endl;
	while (getch())
	{
		deleteList(); // 返回上一级
	}
}

void StudentList::doDeleteByName()
{
	string keyName;
	bool flag = false;	//成功找到该学生
	cout << "\t\t请输入待删除学生的姓名:";
	cin >> keyName;
	for (vector<Student>::iterator it = stuList.begin(); it != stuList.end(); ++it)
	{
		if (it->stuName == keyName)
		{
			flag = true;
			cout << "\t\t待删除学生的信息如下:" << endl;
			cout << "\t\t-----------------------------------------------------------------------" << endl;
			cout << "\t\t\t";
			Student::showHeader();
			cout << "\t\t-----------------------------------------------------------------------" << endl;
			cout << "\t\t";
			it->showStuInfo();
			cout << "\t\t-----------------------------------------------------------------------" << endl;
			cout << "\t\t确认删除?(1 是 0 否)" << endl;
			cout << "\t\t请进行选择【0-1】:";
			string ch = "0";
			cin >> ch;
			while (ch != "0" && ch != "1")
			{
				cout << "\n\t\t输入不合法,请重新选择【0-1】:";
				cin >> ch;
			}
			if (ch == "0") break;
			else
			{
				stuList.erase(it);
				writeFile();
				cout << "\t\t删除成功!" << endl;
				break;
			}
		}
	}
	if (!flag) cout << "\t\t查无此人,无法删除!\n" << endl;
	cout << "\n\t\t按任意键返回上一级" << endl;
	while (getch())
	{
		deleteList(); // 返回上一级
	}
}

void StudentList::doUpdateInfo()
{
	system("cls");
	string keyNum;
	bool flag = false;	//成功找到该学生
	cout << "\n\t\t请输入待修改学生的学号:";
	cin >> keyNum;
	for (int i = 0; i < stuList.size(); i++)
	{
		if (stuList[i].stuNum == keyNum)
		{
			flag = true;
			cout << "\t\t待修改学生基本信息如下:" << endl;
			cout << "\t\t-----------------------------------------------------------------------" << endl;
			cout << "\t\t";
			Student::showHeader();
			cout << "\t\t-----------------------------------------------------------------------" << endl;
			cout << "\t\t";
			stuList[i].showStuInfo();
			cout << "\t\t-----------------------------------------------------------------------" << endl;

			Student s = stuList[i];
			cout << "\t\t请输入修改后的学号:";
			bool check = false;
			do
			{
				check = false;
				cin >> s.stuNum;
				for (int j = 0; j < stuList.size(); j++)
				{
					if (s.stuNum == stuList[j].stuNum && i != j)
					{
						cout << "\t\t该学号已被录入,请重新输入学号:";
						check = true;
						break;
					}
				}
			} while (check);
			cout << "\t\t请输入修改后的姓名:";
			cin >> s.stuName;
			cout << "\t\t请输入修改后的性别:";
			cin >> s.gender;
			cout << "\t\t请输入修改后的年级:";
			cin >> s.gradeNum;
			cout << "\t\t请输入修改后的专业:";
			cin >> s.department;
			cout << "\t\t请输入修改后的班级:";
			cin >> s.classNum;
			cout << "\n\t\t是否确认修改?(1 是 0 否)" << endl;
			cout << "\t\t请进行选择【0-1】:";
			string ch = "0";
			cin >> ch;
			while (ch != "0" && ch != "1")
			{
				cout << "\n\t\t输入不合法,请重新选择【0-1】:";
				cin >> ch;
			}

			if (ch == "0") break;
			else
			{
				stuList[i] = s;
				cout << "\t\t修改成功!" << endl;
				writeFile();
				break;
			}

		}
	}
	if (!flag) cout << "\t\t查无此人,无法修改!\n" << endl;
	cout << "\n\t\t按任意键返回上一级" << endl;
	while (getch())
	{
		updateList(); // 返回上一级
	}
}

void StudentList::doUpdateScore()
{
	system("cls");
	string keyNum;
	bool flag = false;	//成功找到该学生
	cout << "\n\t\t请输入待修改学生的学号:";
	cin >> keyNum;
	for (int i = 0; i < stuList.size(); i++)
	{
		if (stuList[i].stuNum == keyNum)
		{
			flag = true;
			cout << "\t\t待修改学生成绩信息如下:" << endl;
			cout << "\t\t----------------------------------------------------------" << endl;
			cout << "\t\t";
			cout << left << setw(8) << "编号";
			Subject::showHeader();
			cout << "\t\t----------------------------------------------------------" << endl;
			for (int j = 0; j < stuList[i].cps.size(); j++)
			{
				cout << "\t\t";
				cout << left << setw(8) << j + 1;
				stuList[i].cps[j].showCpInfo();
			}
			cout << "\t\t----------------------------------------------------------" << endl;
			Student s = stuList[i];

			cout << "\n\t\t请选择修改方式:" << endl;
			cout << "\t\t-------------------------------" << endl;
			cout << "\t\t1 基于该学生原有成绩信息进行修改" << endl;
			cout << "\t\t2 清空该学生所有科目及成绩信息" << endl;
			cout << "\t\t-------------------------------" << endl;
			cout << "\t\t请进行选择【1-2】:";
			bool sign = true;
			while (sign)
			{
				switch (getch())
				{
				case '1':
					doUpDataScoreByOne(s);
					sign = false;
					break;
				case '2':
					doUpDataScoreByAll(s);
					sign = false;
					break;
				default:
					cout << "\n\t\t\t输入不合法,请重新选择【1-2】:";
				}
			}
			// 计算平均分
			for (int i = 0; i < s.cps.size(); i++)
			{
				s.score += s.cps[i].proScore; 
			}
			if (s.cps.size() > 0)
				s.score /= s.cps.size();
			s.cpNum = s.cps.size();
			cout << "\n\t\t是否确认修改?(1 是 0 否)" << endl;
			cout << "\t\t请进行选择【0-1】:";
			string ch = "0";
			cin >> ch;
			while (ch != "0" && ch != "1")
			{
				cout << "\n\t\t输入不合法,请重新选择【0-1】:";
				cin >> ch;
			}
			if (ch == "0") break;
			else
			{
				stuList[i] = s;
				cout << "\t\t修改成功!" << endl;
				writeFile();
				break;
			}
		}
	}
	if (!flag) cout << "\t\t查无此人,无法修改!\n" << endl;
	cout << "\n\t\t按任意键返回上一级" << endl;
	while (getch())
	{
		updateList(); // 返回上一级
	}
}

void StudentList::doUpDataScoreByOne(Student &s)
{
	Subject cp;
	s.cps.clear();
	s.score = 0;
	s.cpNum = 0;
	cout << "\n\t\t输入修改后的学科成绩信息:" << endl;
	int cnt = 0;
	bool sign = true;
	while (sign)
	{
		cnt++;
		cout << "\n\t\t第" << cnt << "科科目名称:";
		cin >> cp.proName;
		cout << "\t\t第" << cnt << "科科目成绩:";
		cin >> cp.proScore;
		s.cps.push_back(cp);
		cout << "\t\t添加成功!是否继续添加?(1 是 0 否)" << endl;
		cout << "\t\t请进行选择【0-1】:";
		string ch = "0";
		cin >> ch;
		while (ch != "0" && ch != "1")
		{
			cout << "\n\t\t输入不合法,请重新选择【0-1】:";
			cin >> ch;
		}
		if (ch == "0")
		{
			sign = false;
		}
	}
}

void StudentList::doUpDataScoreByAll(Student &s)
{
	s.cps.clear();
	s.score = 0;
	s.cpNum = 0;
}

void StudentList::doSelectByNum()
{
	system("cls");
	string keyNum;
	bool flag = false;
	cout << "\n\t\t请输入待查询学生的学号:";
	cin >> keyNum;
	cout << "\t\t查询结果如下:" << endl;
	cout << "\t\t基本信息:" << endl;
	cout << "\t\t-----------------------------------------------------------------------" << endl;
	cout << "\t\t";
	Student::showHeader();
	cout << "\t\t-----------------------------------------------------------------------" << endl;
	for (int i = 0; i < stuList.size(); i++)
	{
		if (stuList[i].stuNum == keyNum)
		{
			flag = true;
			cout << "\t\t";
			stuList[i].showStuInfo();
			cout << "\t\t-----------------------------------------------------------------------" << endl;
			cout << endl;
			cout << "\t\t" << stuList[i].stuName << "成绩信息:" << endl;
			cout << "\t\t----------------------------------------------------------" << endl;
			cout << "\t\t";
			cout << left << setw(8) << "编号";
			Subject::showHeader();
			cout << "\t\t----------------------------------------------------------" << endl;
			for (int j = 0; j < stuList[i].cps.size(); j++)
			{
				cout << "\t\t";
				cout << left << setw(8) << j + 1;
				stuList[i].cps[j].showCpInfo();
			}
			cout << "\t\t----------------------------------------------------------" << endl;
			break;
		}
	}

	if (!flag)
		cout << "\t\t查无此人!\n" << endl;
	cout << "\n\t\t按任意键返回上一级" << endl;
	while (getch())
	{
		selectList(); // 返回上一级
	}
}

void StudentList::doSelectByName()
{
	system("cls");
	string keyName;
	bool flag = false;
	cout << "\n\t\t请输入待查询联系人的姓名:";
	cin >> keyName;
	cout << "\t\t查询结果如下:" << endl;
	cout << "\t\t-----------------------------------------------------------------------" << endl;
	cout << "\t\t";
	Student::showHeader();
	cout << "\t\t-----------------------------------------------------------------------" << endl;
	for (int i = 0; i < stuList.size(); i++)
	{
		if (stuList[i].stuName == keyName)
		{
			flag = true;
			cout << "\t\t";
			stuList[i].showStuInfo();
			cout << "\t\t-----------------------------------------------------------------------" << endl;
			cout << endl;
			cout << "\t\t" << stuList[i].stuName << "成绩信息:" << endl;
			cout << "\t\t----------------------------------------------------------" << endl;
			cout << "\t\t";
			cout << left << setw(8) << "编号";
			Subject::showHeader();
			cout << "\t\t----------------------------------------------------------" << endl;
			for (int j = 0; j < stuList[i].cps.size(); j++)
			{
				cout << "\t\t";
				cout << left << setw(8) << j + 1;
				stuList[i].cps[j].showCpInfo();
			}
			cout << "\t\t----------------------------------------------------------" << endl;
			break;
		}
	}
	if (!flag)
		cout << "\t\t查无此人!\n" << endl;
	cout << "\n\t\t按任意键返回上一级" << endl;
	while (getch())
	{
		selectList(); // 返回上一级
	}
}

void StudentList::doSortByNum()
{
	system("cls");
	sort(stuList.begin(), stuList.end(), cmpNum);
	cout << "\n\t\t按学号升序排列如下:" << endl;
	displayStu();
	int numMale = 0, numFemale = 0;
	for (int i = 0; i < stuList.size(); i++)
	{
		if (stuList[i].gender == "男") numMale++; 
		else if (stuList[i].gender == "女") numFemale++;
	}
	cout << "\t\t一共 " << stuList.size() << "人,其中男生 " << numMale << " 人,女生 " << numFemale << " 人。" << endl;

	cout << "\n\t\t按任意键返回上一级" << endl;
	while (getch())
	{
		statisticList(); // 返回上一级
	}
}

void StudentList::doSortByScore()
{
	system("cls");
	sort(stuList.begin(), stuList.end(), cmpScroe);
	cout << "\n\t\t按平均分降序排列如下:" << endl;
	displayStu();
	vector<int> scoreRange(10);
	double totalScore = 0;
	for (int i = 0; i < stuList.size(); i++)
	{
		totalScore += stuList[i].score;
		if (0 <= stuList[i].score && stuList[i].score < 60) scoreRange[0]++;
		else if (60 <= stuList[i].score && stuList[i].score < 70) scoreRange[1]++;
		else if (70 <= stuList[i].score && stuList[i].score < 80) scoreRange[2]++;
		else if (80 <= stuList[i].score && stuList[i].score < 90) scoreRange[3]++;
		else scoreRange[4]++;
	}
	cout << "\n\t\t60分以上 ------------ " << scoreRange[0] << " 人" << endl;
	cout << "\t\t60分 ~ 70分 --------- " << scoreRange[1] << " 人" << endl;
	cout << "\t\t70分 ~ 80分 --------- " << scoreRange[2] << " 人" << endl;
	cout << "\t\t80分 ~ 90分 --------- " << scoreRange[3] << " 人" << endl;
	cout << "\t\t90分以上 ------------ " << scoreRange[4] << " 人" << endl;
	cout << "\n\t\t所有学生平均分为:" << totalScore / stuList.size() << endl;
	cout << "\t\t" << stuList.back().gradeNum << "级" << stuList.back().department << stuList.back().classNum << "班---";
	cout << stuList.back().stuName << "平均分最低,为:" << stuList.back().score << "分" << endl;
	cout << "\t\t" << stuList.front().gradeNum << "级" << stuList.front().department << stuList.front().classNum << "班---";
	cout << stuList.front().stuName << "平均分最高,为:" << stuList.front().score << "分" << endl;

	cout << "\n\t\t按任意键返回上一级" << endl;
	while (getch())
	{
		statisticList(); // 返回上一级
	}
}

void StudentList::doClear()
{
	stuList.clear();
	cout << "\n\t\t清空成功!" << endl;
	writeFile();
	cout << "\n\t\t按任意键返回菜单" << endl;
	while (getch())
	{
		menu();
	}
}

bool StudentList::cmpNum(const Student& s1, const Student& s2)
{
	return s1.stuNum < s2.stuNum;
}

bool StudentList::cmpScroe(const Student& s1, const Student& s2)
{
	if (s1.stuNum != s2.stuNum) return s1.score > s2.score;
	else return s1.stuNum < s2.stuNum;
}

main.cpp

/*********************************************************************
程序名:
说明:
*********************************************************************/
#include <iostream>
#include <conio.h>		// 使用getch()函数
#include "StudentAdmin.h"

using namespace std;

int main() {
	StudentList stuList;
	stuList.init(); //读入文件数据初始化
	stuList.menu(); //打开主菜单

	system("PAUSE");
	return 0;
}

运行示例:

(1)主界面
在这里插入图片描述

(2)增加记录

在这里插入图片描述
在这里插入图片描述

(3)查找记录

在这里插入图片描述
在这里插入图片描述

(4)删除记录
在这里插入图片描述

(5)成绩修改

在这里插入图片描述
在这里插入图片描述

(5)统计分析:单科统计、课程标准差和合格率;

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

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

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

相关文章

linux ext3/ext4文件系统(part2 jbd2)

概述 jbd2&#xff08;journal block device 2&#xff09;是为块存储设计的 wal 机制&#xff0c;它为要写设备的buffer绑定了一个journal_head&#xff0c;这个journal_head与一个transaction绑定&#xff0c;随着事务状态的转移&#xff08;运行&#xff0c;生成日志&#…

智能工厂建设模式及解决方案供应商---万界星空科技

今天就聊聊企业智能工厂的打造&#xff0c;企业想实现数字化转型建立智能工厂&#xff0c;就需要先建设数字化车间&#xff0c;可以说数字化车间是建设智能工厂的重要一环&#xff0c;智能工厂的基础是数字化车间。 一、智能工厂特征&#xff1a; 1&#xff0c;智能系统集成…

【前端】前端三要素之BOM

写在前面&#xff1a;本文仅包含BOM内容&#xff0c;JavaScript传送门在这里&#xff0c;DOM传送门在这里。 本文内容是假期中刷的黑马Pink老师视频&#xff08;十分感谢Pink老师&#xff09;&#xff0c;原文保存在个人的GitLab中&#xff0c;如果需要写的网页内容信息等可以评…

如何更换过期的SSL证书?

SSL证书是保护网站安全的重要组成部分&#xff0c;它能在客户端和服务器之间建立数据传输加密通道&#xff0c;防止数据在传输过程中被泄露、劫持和窃听。但SSL证书也有有效期限&#xff0c;当SSL证书到期时&#xff0c;您需要及时更换它&#xff0c;以确保网站的安全性和可信度…

Spring相关注解

文章目录 Spring注解Bean1、Bean 概述2、Bean 的声明1&#xff09;搭配 Configuration2&#xff09;搭配 Component3&#xff09;搭配 ApplicationContext 3、Bean 的注入1&#xff09;NO&#xff08;主要关注这个&#xff09;【1】同一配置类【2】不同配置类 2&#xff09;BY_…

信奥一本通:1085:球弹跳高度的计算

这个题的点在于注意他求得是一共经过的米数&#xff0c;也就是下降起跳都算在里面&#xff0c;例如第一次下降20再起跳15就是一次循环的高度 #include <iostream> # include <iomanip> using namespace std; double h,c1,s; int main(){cin >> h;s h;while…

1902_野火FreeRTOS教程内核在STM32中用到的2个中断PENDSV和SYSTICK

1902_野火FreeRTOS教程内核在STM32中用到的2个中断PENDSV和SYSTICK 全部学习汇总&#xff1a; g_FreeRTOS: FreeRTOS学习笔记 (gitee.com) 上面是涉及到的源代码&#xff0c;而这次需要分析的就是78、79行的两个中断。首先&#xff0c;需要确认NVIC_SYSPRI2寄存器的作用。 进一…

MySQL篇—事务和隔离级别介绍

☘️博主介绍☘️&#xff1a; ✨又是一天没白过&#xff0c;我是奈斯&#xff0c;DBA一名✨ ✌✌️擅长Oracle、MySQL、SQLserver、Linux&#xff0c;也在积极的扩展IT方向的其他知识面✌✌️ ❣️❣️❣️大佬们都喜欢静静的看文章&#xff0c;并且也会默默的点赞收藏加关注❣…

程序员们的“不关机”现象:一种技术文化还是不良习惯?

文章目录 程序员们的“不关机”现象&#xff1a;一种技术文化还是不良习惯&#xff1f;引言程序员为何不爱关机&#xff1f;开发环境的冷启动成本持续集成与持续部署远程办公与全球协作运行中的服务与调试环境随时待命的紧急响应 长期开机是否会对硬件产生损害&#xff1f;最后…

Word第一课

文章目录 1. 文件格式1.1 如何显示文件扩展名1.2 Word文档格式的演变1.3 常见的Word文档格式 3. 文档属性理解文档属性查看文档属性 4. 显示比例方式一&#xff1a; 手动调整方式二&#xff1a; 自动调整 5. 视图、窗口视图 1. 文件格式 1.1 如何显示文件扩展名 文档格式指的…

2-IOC容器的初始化流程

IOC容器的初始化流程 AbstractApplicationContext.refresh() 准备BeanFactory&#xff08;DefaultListableBeanFactory&#xff09; 设置ClassLoader 设置Environment 扫描要放入容器的Bean&#xff0c;得到对应的BeanDefinition 注册BeanPostProcessor 处理国际化 处理事件…

vue+element (el-progress)标签 隐藏百分比(%) ,反向显示 ,自定义颜色, demo 复制粘贴拿去用

1 效果: 2 页面代码: <el-row :gutter"10" ><el-col :span"12"><el-card ><div class"fourqu"><div><span slot"title">{{推送任务TOP5}}</span></div></div><div class&…

Unity NavMesh 清除不可行走区域

通常场景中物体设置为static或Navigation Static后&#xff0c;打开Navigation使用默认设置烘焙NavMesh&#xff0c;模型顶部和底部会出现蓝色网格&#xff0c;但其中有部分属于不可能到达区域&#xff0c;如下图 本文介绍两种可去掉NavMesh中不需要网格的方法&#xff1a; 方…

K210基础实验——点亮LED灯

一、目的是点亮K210开发板左下角的LED0和LED1&#xff0c;LED0是红灯&#xff0c;LED1是绿灯&#xff0c;两颗LED灯都是低电平点亮&#xff0c;高电平熄灭。 二、这是原理图上的硬件连接&#xff0c;LED0连接的是IO0&#xff0c;LED1连接的是IO17。 三、在src目录下新建文件夹 …

适用于高云FPGA的JTAG

目标板卡&#xff1a;小梅哥芯海无涯GOWIN高云ACG525(GW5A-LV25UG324) 1.软件要求&#xff1a;必须用商业版&#xff0c;因为教育版(V1.9.9Beta-4 Education)不支持此封装的GW5A。商业版需要上网申请License&#xff0c;此处提供D4D853392AD8.lic文件&#xff08;此方法为临时…

开启智能互动新纪元——ChatGPT提示词工程的引领力

目录 提示词工程的引领力 高效利用ChatGPT提示词方法 提示词工程的引领力 近年来&#xff0c;随着人工智能技术的迅猛发展&#xff0c;ChatGPT提示词工程正逐渐崭露头角&#xff0c;为智能互动注入了新的活力。这一技术的引入&#xff0c;使得人机交流更加流畅、贴近用户需求&…

MATLAB卷积神经网络——基于ResNet-50进行图像分类

一、ResNet50工具箱安装 &#xff08;1&#xff09;下载工具箱 https://ww2.mathworks.cn/matlabcentral/fileexchange/64626-deep-learning-toolbox-model-for-resnet-50-network &#xff08;2&#xff09;在matlab打开下载的resnet50.mlpkginstall文件 &#xff08;3&…

达梦数据库--DM8两节点DSC集群安装部署(达梦数据库DSC集群搭建)

1 前期规划 系统规划 本地磁盘规划 共享存储规划 DMDSC 集群为了实现多实例同时访问和修改数据&#xff0c;需要数据文件、控制文件和日志文件都放到共享存储上。DM 支持两种共享存储&#xff0c;裸设备和 DMASM&#xff0c;裸设备是未经过格式化的特殊字符设备&#xff0c;…

Unity3d Shader篇(七)— 纹理采样

文章目录 前言一、什么是纹理采样&#xff1f;1. 纹理采样的工作原理2. 纹理采样的优缺点优点缺点 二、使用步骤1. Shader 属性定义2. SubShader 设置3. 渲染 Pass4. 定义结构体和顶点着色器函数5. 片元着色器函数 三、效果四、总结使用场景 前言 纹理采样是一种常用的图形学技…

Ubuntu 23.10:内网安装rapidocr_paddle(GPU)及其前置准备

Ubuntu 23.10&#xff1a;内网安装rapidocr_paddle&#xff08;GPU&#xff09;及其前置准备 – WhiteNights Site 标签&#xff1a;Linux, ocr, ubuntu, 系统相关 安装NVIDIA驱动、安装CUDA&&CUDNN、安装… rapidocr_paddle主要是用于ocr识别的引擎。本文主要针对于…