C++期末课程设计:设计一个学生学籍管理系统

学生信息包括:姓名、学号、性别和英语、数学、程序设计、体育成绩。

  从键盘输入数据,建立数据文件student.dat。

  实现对学生或学号查询,显示信息。

  对所有学生,按照班级计算平均成绩。

  分别按照英语、数学、程序设计和体育成绩排序生成结果文件。

话不多说,上测试结果图

 

 

 代码如下:

#include <iostream>
#include <fstream>
#include <Windows.h>
using namespace std;
#define max 30
class student		//定义学生数据类
{
private:			//数据保护
	string name;
	string id;
	string gender;
	float English = 0;
	float Math = 0;
	float Program = 0;
	float PE = 0;
public:						//写入数据
	void setstuinfo(string name, string id, string gender, float English, float Math, float Program, float PE) {
		this->name = name;
		this->id = id;
		this->gender = gender;
		this->English = English;
		this->Math = Math;
		this->Program = Program;
		this->PE = PE;
	}
	void setName(string name) {
		this->name = name;
	}
	void setId(string id) {
		this->id = id;
	}
	void setGender(string gender) {
		this->gender = gender;
	}
	void setEnglish(float English) {
		this->English = English;
	}
	void setMath(float Math) {
		this->Math = Math;
	}
	void setProgram(float Program) {
		this->Program = Program;
	}
	void setPE(float PE) {
		this->PE = PE;
	}
	string showname() {
		return this->name;
	}
	string showid() {
		return this->id;
	}
	string showGender() {
		return this->gender;
	}
	float showEnglish() {
		return this->English;
	}
	float showMath() {
		return this->Math;
	}
	float showProgram() {
		return this->Program;
	}
	float showPE() {
		return this->PE;
	}
};
class addressbooks {		//定义接收数据量:30个
public:
	class student studentarray[max];
	int showsize() {
		return this->size;
	}
	void sizeadd() {
		this->size++;
	}
	void inisize() {
		this->size = 0;
	}
private:
	int size = 0;	//数据保护
};

void addstudent(addressbooks* abs){		//录入学生成绩信息
	if (abs->showsize() == max)
	{
		cout << "存储容量已满。" << endl;
	}
	else
	{
		string name;
		cout << "请输入学生姓名:" << endl;
		cin >> name;
		abs->studentarray[abs->showsize()].setName(name);
		string id;
		cout << "请输入学生学号:" << endl;
		cin >> id;
		abs->studentarray[abs->showsize()].setId(id);
		string Gender;
		cout << "请输入学生的性别:" << endl;
		cin >> Gender;
		abs->studentarray[abs->showsize()].setGender(Gender);
		float English;
		cout << "请输入学生的英语成绩:" << endl;
		cin >> English;
		abs->studentarray[abs->showsize()].setEnglish(English);
		float Math;
		cout << "请输入学生的数学成绩:" << endl;
		cin >> Math;
		abs->studentarray[abs->showsize()].setMath(Math);
		float Program;
		cout << "请输入学生的程序设计成绩" << endl;
		cin >> Program;
		abs->studentarray[abs->showsize()].setProgram(Program);
		float PE;
		cout << "请输入学生的体育成绩" << endl;
		cin >> PE;
		abs->studentarray[abs->showsize()].setPE(PE);		
		cout << endl;
		ofstream fout;			//创建对象管理输出流
		fout.open("student.dat", ios_base::out | ios_base::app);
		fout << abs->studentarray[abs->showsize()].showname() << " " << abs->studentarray[abs->showsize()].showGender() << " " << abs->studentarray[abs->showsize()].showid() << " " << abs->studentarray[abs->showsize()].showEnglish() << " " << abs->studentarray[abs->showsize()].showMath() << " " << abs->studentarray[abs->showsize()].showProgram() << " " << abs->studentarray[abs->showsize()].showPE()<<" ";
		cout << "单次录入完毕,一秒后清屏。" << endl;		//至此,单个学生信息输入完毕
		fout.close();
		Sleep(1000);
		system("cls");
	}
}

void clear() {        //用打开文件的默认方式清除数据
	ofstream cls1;
	ofstream cls2;
	ofstream cls3;
	ofstream cls4;
	ofstream cls5;
	ofstream cls6;
	cls1.open("student.dat");
	cls2.open("average.dat");
	cls3.open("englishsort.dat");
	cls4.open("mathsort.dat");
	cls5.open("programsort.dat");
	cls6.open("pesort.dat");
	cout << "数据已清除。。。" << endl;
	cout << "一秒后清屏。。。" << endl;
	cout << endl;
	Sleep(1000);
	system("cls");
}

int ifexit(string tmps) {
	if (tmps == "end") {
		return 1;
	}
	return 0;
}

void getdata(addressbooks* abs2) {
	int exit = 0;
	string tmps;
	float tmpf;
	abs2->inisize();
	int i = 0;
	ifstream fin("student.dat", ios_base::in);
	while (i<max) {		//判断是否接收到数据末尾
		fin >> tmps;
		exit = ifexit(tmps);
		if (exit == 1) {
			break;			//避免收集空数据
		}
		abs2->studentarray[i].setName(tmps);
		fin >> tmps;
		abs2->studentarray[i].setGender(tmps);
		fin >> tmps;
		abs2->studentarray[i].setId(tmps);
		fin >> tmpf;
		abs2->studentarray[i].setEnglish(tmpf);
		fin >> tmpf;
		abs2->studentarray[i].setMath(tmpf);
		fin >> tmpf;
		abs2->studentarray[i].setProgram(tmpf);
		fin >> tmpf;
		abs2->studentarray[i].setPE(tmpf);
		abs2->sizeadd();
		i++;
	}
}

void namesearch(addressbooks* abs2){
	getdata(abs2);
	string name;
	int i = 0;
	cout << "请输入要查找学生的姓名:" << endl;
	cin >> name;
	for (i = 0; i < abs2->showsize(); i++)			//对学生姓名进行遍历查找
	{
		if (name == abs2->studentarray[i].showname())
		{
			cout << "学生: " << abs2->studentarray[i].showname() << " 性别:" << abs2->studentarray[i].showGender() << " 学号:" << abs2->studentarray[i].showid() << " 的英语成绩为:" << abs2->studentarray[i].showEnglish() << " 数学成绩为:" << abs2->studentarray[i].showMath() << " 程序设计成绩为:" << abs2->studentarray[i].showProgram() << " 体育成绩为:" << abs2->studentarray[i].showPE() << endl;
			break;
		}
	}
	if (i >= abs2->showsize())				//查无此人
	{
		cout << "查无此人" << endl;
	}
}

void idsearch(addressbooks* abs2) {
	getdata(abs2);
	string id;
	int i = 0;
	cout << "请输入要查找学生的学号:" << endl;
	cin >> id;
	for (i = 0; i < abs2->showsize(); i++)			//对学生姓名进行遍历查找
	{
		if (id == abs2->studentarray[i].showid())
		{
			cout << "学生: " << abs2->studentarray[i].showname() << " 性别:" << abs2->studentarray[i].showGender() << " 学号:" << abs2->studentarray[i].showid() << " 的英语成绩为:" << abs2->studentarray[i].showEnglish() << " 数学成绩为:" << abs2->studentarray[i].showMath() << " 程序设计成绩为:" << abs2->studentarray[i].showProgram() << " 体育成绩为:" << abs2->studentarray[i].showPE() << endl;
			break;
		}
	}
	if (i >= abs2->showsize())				//查无此人
	{
		cout << "查无此人" << endl;
	}

}

void average(addressbooks* abs2) {
	getdata(abs2);
	float averagesc[max];
	for (int i = 0; i < abs2->showsize(); i++) {
		averagesc[i] = (abs2->studentarray[i].showEnglish()+abs2->studentarray[i].showMath()+abs2->studentarray[i].showProgram()+abs2->studentarray[i].showPE()) / 4;
	}
	ofstream fout;
	fout.open("average.dat", ios_base::out | ios_base::app);
	int t = 0;
	for (t = 0; t < abs2->showsize(); t++) {
		fout << "学生:" << abs2->studentarray[t].showname() << " 学号:" << abs2->studentarray[t].showid() << " 性别:" << abs2->studentarray[t].showGender() << " 四门课程平均成绩:" << averagesc[t] << endl;
	}
	cout << "结果已输出至文件:average.dat" << endl;

}

void stusortE(addressbooks* abs2) {			//采用冒泡排序算法,记录下数据条的变化,输出时改变顺序即可
	getdata(abs2);
	int sortn[max];
	float score[max];
	int tmpn[max+1];
	int j = 0;
	for (j = 0; j < abs2->showsize(); j++) {
		tmpn[j] = j;
	}
	tmpn[j] = '\0';
	for (int i = 0; i < abs2->showsize(); i++) {
		score[i] = abs2->studentarray[i].showEnglish();
	}
	for (int i = 0; i < abs2->showsize(); i++) {
		for (int j = 0; j < abs2->showsize() - i - 1; j++) {
			if (score[j] < score[j+1]) {
				int temp;
				int tempn;
				temp = score[j + 1];
				tempn = tmpn[j];
				tmpn[j] = tmpn[j+1];
				score[j + 1] = score[j];
				tmpn[j + 1] = tempn;
				score[j] = temp;

			}
		}
	}
	ofstream fout;
	fout.open("englishsort.dat", ios_base::out | ios_base::app);
	cout << "按照英语成绩排序后的结果已输出至englishsort.dat" << endl;
	fout << "按照英语成绩排序后的结果" << endl;
	for (int i = 0;i<j; i++) {
		fout << "学生: " << abs2->studentarray[tmpn[i]].showname() << " 性别:" << abs2->studentarray[tmpn[i]].showGender() << " 学号:" << abs2->studentarray[tmpn[i]].showid() << " 的英语成绩为:" << abs2->studentarray[tmpn[i]].showEnglish() << " 数学成绩为:" << abs2->studentarray[tmpn[i]].showMath() << " 程序设计成绩为:" << abs2->studentarray[tmpn[i]].showProgram() << " 体育成绩为:" << abs2->studentarray[tmpn[i]].showPE() << endl;

	}
}

void stusortM(addressbooks* abs2) {			//采用冒泡排序算法,记录下数据条的变化,输出时改变顺序即可
	getdata(abs2);
	int sortn[max];
	float score[max];
	int tmpn[max + 1];
	int j = 0;
	for (j = 0; j < abs2->showsize(); j++) {
		tmpn[j] = j;
	}
	tmpn[j] = '\0';
	for (int i = 0; i < abs2->showsize(); i++) {
		score[i] = abs2->studentarray[i].showMath();
	}
	for (int i = 0; i < abs2->showsize(); i++) {
		for (int j = 0; j < abs2->showsize() - i - 1; j++) {
			if (score[j] < score[j + 1]) {
				int temp;
				int tempn;
				temp = score[j + 1];
				tempn = tmpn[j];
				tmpn[j] = tmpn[j+1];
				score[j + 1] = score[j];
				tmpn[j + 1] = tempn;
				score[j] = temp;

			}
		}
	}
	ofstream fout;
	fout.open("mathsort.dat", ios_base::out | ios_base::app);
	cout << "按照数学成绩排序后的结果已输出至mathsort.dat" << endl;
	fout << "按照数学成绩排序后的结果" << endl;
	for (int i = 0; i < j; i++) {
		cout << tmpn[i] << " ";
	}
	for (int i = 0; i<j; i++) {
		fout << "学生: " << abs2->studentarray[tmpn[i]].showname() << " 性别:" << abs2->studentarray[tmpn[i]].showGender() << " 学号:" << abs2->studentarray[tmpn[i]].showid() << " 的英语成绩为:" << abs2->studentarray[tmpn[i]].showEnglish() << " 数学成绩为:" << abs2->studentarray[tmpn[i]].showMath() << " 程序设计成绩为:" << abs2->studentarray[tmpn[i]].showProgram() << " 体育成绩为:" << abs2->studentarray[tmpn[i]].showPE() << endl;

	}
}

void stusortPr(addressbooks* abs2) {			//采用冒泡排序算法,记录下数据条的变化,输出时改变顺序即可
	getdata(abs2);
	int sortn[max];
	float score[max];
	int tmpn[max + 1];
	int j = 0;
	for (j = 0; j < abs2->showsize(); j++) {
		tmpn[j] = j;
	}
	tmpn[j] = '\0';
	for (int i = 0; i < abs2->showsize(); i++) {
		score[i] = abs2->studentarray[i].showProgram();
	}
	for (int i = 0; i < abs2->showsize(); i++) {
		for (int j = 0; j < abs2->showsize() - i - 1; j++) {
			if (score[j] < score[j + 1]) {
				int temp;
				int tempn;
				temp = score[j + 1];
				tempn = tmpn[j];
				tmpn[j] = tmpn[j+1];
				score[j + 1] = score[j];
				tmpn[j + 1] = tempn;
				score[j] = temp;

			}
		}
	}
	ofstream fout;
	fout.open("programsort.dat", ios_base::out | ios_base::app);
	cout << "按照程序设计成绩排序后的结果已输出至programsort.dat" << endl;
	fout << "按照程序设计成绩排序后的结果" << endl;
	for (int i = 0; i<j; i++) {
		fout << "学生: " << abs2->studentarray[tmpn[i]].showname() << " 性别:" << abs2->studentarray[tmpn[i]].showGender() << " 学号:" << abs2->studentarray[tmpn[i]].showid() << " 的英语成绩为:" << abs2->studentarray[tmpn[i]].showEnglish() << " 数学成绩为:" << abs2->studentarray[tmpn[i]].showMath() << " 程序设计成绩为:" << abs2->studentarray[tmpn[i]].showProgram() << " 体育成绩为:" << abs2->studentarray[tmpn[i]].showPE() << endl;

	}
}

void stusortPE(addressbooks* abs2) {			//采用冒泡排序算法,记录下数据条的变化,输出时改变顺序即可
	getdata(abs2);
	int sortn[max];
	float score[max];
	int tmpn[max + 1];
	int j = 0;
	for (j = 0; j < abs2->showsize(); j++) {
		tmpn[j] = j;
	}
	tmpn[j] = '\0';
	for (int i = 0; i < abs2->showsize(); i++) {
		score[i] = abs2->studentarray[i].showPE();
	}
	for (int i = 0; i < abs2->showsize(); i++) {
		for (int j = 0; j < abs2->showsize() - i - 1; j++) {
			if (score[j] < score[j + 1]) {
				int temp;
				int tempn;
				temp = score[j + 1];
				tempn = tmpn[j];
				tmpn[j] = tmpn[j+1];
				score[j + 1] = score[j];
				tmpn[j + 1] = tempn;
				score[j] = temp;

			}
		}
	}
	ofstream fout;
	fout.open("pesort.dat", ios_base::out | ios_base::app);
	cout << "按照体育成绩排序后的结果已输出至pesort.dat" << endl;
	fout << "按照体育成绩排序后的结果" << endl;
	for (int i = 0; i<j; i++) {
		fout << "学生: " << abs2->studentarray[tmpn[i]].showname() << " 性别:" << abs2->studentarray[tmpn[i]].showGender() << " 学号:" << abs2->studentarray[tmpn[i]].showid() << " 的英语成绩为:" << abs2->studentarray[tmpn[i]].showEnglish() << " 数学成绩为:" << abs2->studentarray[tmpn[i]].showMath() << " 程序设计成绩为:" << abs2->studentarray[tmpn[i]].showProgram() << " 体育成绩为:" << abs2->studentarray[tmpn[i]].showPE() << endl;

	}
}

void exit() {
	ifstream fin;
	fin.open("student.dat", ios_base::in);
	string temps;
	fin >> temps;
	if (temps != "\0") {
		ofstream fout;			//创建对象管理输出流
		fout.open("student.dat", ios_base::out | ios_base::app);
		fout << " end";			//记录结束信号
	}
}

void showmenu1()		//显示一级菜单
{
	cout << endl;
	cout << " ===============学生学籍管理系统===============" << endl;
	cout << " ----------------------------------------------" << endl;
	cout << "|                  0、退出系统                 |" << endl;
	cout << "|                  1、写入数据                 |" << endl;
	cout << "|                  2、查找学生                 |" << endl;
	cout << "|                  3、计算平均                 |" << endl;
	cout << "|                  4、进行排序                 |" << endl;
	cout << "|                  5、清除数据                 |" << endl;
	cout << " ----------------------------------------------" << endl;
	cout << " ===============学生学籍管理系统===============" << endl;
	cout << "注意:本系统目前支持查找前30个学生的数据。如要扩增,修改max的定义值即可,但可能会超出堆栈保留大小" << endl;
	cout << "提示:如在执行写入操作之后需进行除写入以及清除以外的操作,请先退出系统重新进入" << endl;
}

void showmenu2(addressbooks* abs2)		//显示查找学生数据的二级菜单
{
	
	int select;
	cout << endl;
	cout << " ===============学生学籍管理系统===============" << endl;
	cout << " ----------------------------------------------" << endl;
	cout << "|                  0、返回上页                 |" << endl;
	cout << "|                  1、姓名查找                 |" << endl;
	cout << "|                  2、学号查找                 |" << endl;
	cout << " ----------------------------------------------" << endl;
	cout << " ===================查找模块===================" << endl;
	cout << " ===============学生学籍管理系统===============" << endl;
	cout << endl;
	cout << "请选择模式:" << endl;
	cin >> select;
	switch (select)					//模式选择
	{
	case 0:system("cls"); break;
	case 1:namesearch(abs2); break;
	case 2:idsearch(abs2); break;
	case -1:; break;
	default:break;
	}

}

void showmenu3(addressbooks* abs2)		//显示查找学生数据的二级菜单
{
	abs2->inisize();
	int sortn[max];
	int select;
	cout << endl;
	cout << " ===============学生学籍管理系统===============" << endl;
	cout << " ----------------------------------------------" << endl;
	cout << "|                  0、返回上页                 |" << endl;
	cout << "|                  1、英语排序                 |" << endl;
	cout << "|                  2、数学排序                 |" << endl;
	cout << "|                3、程序设计排序               |" << endl;
	cout << "|                  4、体育排序                 |" << endl;
	cout << " ----------------------------------------------" << endl;
	cout << " ===================查找模块===================" << endl;
	cout << " ===============学生学籍管理系统===============" << endl;
	cout << endl;
	cout << "请选择模式:" << endl;
	cin >> select;
	switch (select)					//模式选择
	{
	case 0:system("cls"); break;
	case 1:stusortE(abs2); break;
	case 2:stusortM(abs2); break;
	case 3:stusortPr(abs2); break;
	case 4:stusortPE(abs2); break;
	default:break;
	}

}

int main() {
	addressbooks abs1;	//用于写入
	addressbooks abs2;	//用于获取
	abs1.inisize();		//初始化本次数据总量
	abs2.inisize();
	int select;
	while (true)					//显示菜单界面
	{
		showmenu1();
		cout << "请选择模式:" << endl;
		cin >> select;
		switch (select)					//模式选择
		{
		case 0:exit(); cout << "感谢使用。" << endl; return 0; break;	//退出系统
		case 1:addstudent(&abs1); break;		//添加数据
		case 2:showmenu2(&abs2); break;		//进入查询二级菜单
		case 3:average(&abs2); break;			//计算平均分
		case 4:showmenu3(&abs2); break;						//进入排序二级菜单
		case 5:clear(); break;				//清除文件中的所有数据
		default:system("cls"); cout << "输入不合规范,请重新输入。" << endl; break;
		}
	}
}

  • 2
    点赞
  • 51
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
### 回答1: C语言程序设计期末考试题库是一个包含各种关于C语言程序设计的问题和答案的资源库。它通常由老师和学生们共同建立,目的是帮助学生更好地准备期末考试。 该题库包含了许多重要的知识点,如数据类型、循环语句、数组、函数、指针等,考察的难度也会逐步增加。通过练习这些题目,学生能够更深入地理解C语言程序设计的概念和技巧,提高编程能力,增强解决问题的能力。 此外,C语言程序设计期末考试题库还包括了不同难度级别的练习题和模拟试题,以及答案解析和详细讲解。学生可以利用这些资源进行反复练习和讨论,从而为考试做好充分的准备。 总之,C语言程序设计期末考试题库是一个非常有用的学习资源,对于提高学生的编程水平和应对考试都有很大的帮助。 ### 回答2: C语言程序设计期末考试的题库通常包括以下内容: 1.基础知识题:包括C语言的数据类型、运算符、控制语句、函数、指针等基础知识的理解、运用及编写。 2.程序设计题:如编写函数实现一定的功能,编写程序解决某个问题,或根据要求设计并编写完整的C语言程序等。 3.调试题:一般给出一段有错误的代码,要求找出其中的逻辑错误并进行修正。 4.综合应用题:要求考生根据所学的C语言知识,自主思考并解决实际生活、工作或学习中的问题,常见的包括计算机图形处理、字符处理、文件处理、网络编程等。 由于C语言是计算机专业的必修课程,其期末考试通常难度较高,考察范围也相对较广。因此,考生需要在平时的课堂学习中认真学习和掌握基础知识,多做程序设计和调试练习,提高程序设计和解决问题的能力。同时,加强对综合应用题的训练,能够更好地应用所学知识,解决实际问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值