c++面向对象的学生管理系统

这是我大一上学期做的一个项目,快做完的时候老师说要用c做,就放弃了,但这个程序基本已经做完了,要添加新的功能很容易,比如搜索学生等,需要做学生管理系统的可以借鉴一下。

需要c语言完整版的看我下一篇博客。

我的制作思路是先创建一个学生的类,里面没有成员函数,只有数据成员,包括姓名,学号,各种成绩。整个程序的稳定性还是很高的。

#pragma once
#include<stdio.h>
#include<iostream>
#include<string>
using namespace std;
class Student {
public:
	string name;//姓名
	int id;//学号
	int mathScore;//高数成绩
	int politicsScore;//思政成绩
	int englishScore;//英语成绩
	int Score;//总成绩


};

 然后我有创建了一个功能类,里面包含了各种功能的声明,(可以接着往里边添加其他功能)

#pragma once
using namespace std;
#include"Student.h"
#include"studentManager.h"
#include<fstream>
#define FILENAME "empFile.txt"
class studentManager {
public:
	studentManager();
	//显示菜单
	void Show_Menu();
	//退出界面
	void exitSystem();
	//添加学生
	void Add_Stu();
	//保存文件
	void save();
	//初始化学生
	void init_Stu();
	//显示学生
	void Show_Stu();
	//判断学生是否存在
	int IsExist(int id);
	//删除学生
	void Del_Stu();
	//得到学生个数
	int get_StuNum();
	//记录文件是否为空
	bool m_FileIsEmpty;
	//记录学生人数
	int m_StuNum;
	//创建该对象数组;储存每个对象的指针;
	Student**m_StuArray;
	//用于释放内存
	~studentManager();
};

下面是一个源cpp是用来定义上边那个功能类中的函数的

#include"studentManager.h"
#include"Student.h"
using namespace std;

//构造函数用于初始化
studentManager::studentManager() {
	//1、文件不存在
	ifstream ifs;
	ifs.open(FILENAME, ios::in);
	if (!ifs.is_open()) {
		this->m_StuNum = 0;
		this->m_StuArray = NULL;
		this->m_FileIsEmpty = true;
		ifs.close();
		return;
	}
	//当文件存在且有内容
	int num = this->get_StuNum();
	this->m_StuNum = num;
	//开辟空间
	this->m_StuArray = new Student*[this->m_StuNum];
	//将文件的数据,存放到数组中;
	this->init_Stu();
}


//退出函数
void studentManager::exitSystem() {
	cout << "欢迎下次使用" << endl;
	system("pause");
	exit(0);
}


//显示菜单函数
void studentManager::Show_Menu() {
	cout << "****************" << endl;
	cout << "0、退出系统" << endl;
	cout << "1、增加学生信息" << endl;
	cout << "2、显示学生信息" << endl;
	cout << "3、删除学生信息" << endl;
   }


//添加学生函数
void studentManager::Add_Stu() {
	cout << "请输入添加学生数量" << endl;
	int addNum = 0;
	cin >> addNum;
	if (addNum > 0) {
		this->m_StuNum = 0;
		int newSize = this->m_StuNum + addNum;//新空间大小
		//开辟新空间
		Student**newSpace = new Student*[newSize];
		if (this->m_StuNum != NULL) {
			for (int i = 0; i < this->m_StuNum; i++) {
				newSpace[i] = this->m_StuArray[i];//将原有数组中的元素赋给新空间
			}
		}
		for (int j = 0; j < addNum; j++) {
			string name;//姓名
			int id;//学号
			int mathScore = 0;//高数成绩
			int politicsScore = 0;//思政成绩
			int englishScore = 0;//英语成绩
			int Score;//总成绩
			cout << "请输入第" << j + 1 << "个新学生的姓名:" << endl;
			cin >> name;
			cout << "请输入第" << j + 1 << "个新学生的id:" << endl;
			cin >> id;
			cout << "请输入第" << j + 1 << "个新学生的高数成绩" << endl;
			cin >> mathScore;
			cout << "请输入第" << j + 1 << "个新学生的思政成绩" << endl;
			cin >> politicsScore;
			cout << "请输入第" << j + 1 << "个新学生的英语成绩" << endl;
			cin >> englishScore;
			Score = mathScore + politicsScore + englishScore;
			Student *student = new Student;
			student->id = id;
			student->name = name;
			student->englishScore = englishScore;
			student->mathScore = mathScore;
			student->politicsScore = politicsScore;
			student->Score = Score;
			//将学生指针保存到数组中	
			newSpace[this->m_StuNum + j] = student;//将添加的元素赋给新空间
		}
		//释放原有的空间
		delete[]this->m_StuArray;
		this->m_StuArray = NULL;
		//更改新空间的指向
		this->m_StuArray = newSpace;
		this->m_StuNum = newSize;
		this->m_FileIsEmpty = false;
		this->save();
		cout << "成功添加" << addNum << "名新职工" << endl;
	}
	else
	{
		cout << "输入数据有误" << endl;
	}
	//按任意键后清平
	system("pause");
	system("cls");

}


//得到学生个数函数
int studentManager::get_StuNum() {
	ifstream ifs;
	ifs.open(FILENAME, ios::in);
	int id; string name; 
	int englishScore;
	int mathScore;
	int politicsScore;
	int Score;
	int num = 0;
	while (ifs >> id && ifs >> name && ifs >>mathScore&&ifs>> politicsScore&&ifs>> englishScore&&ifs>>Score) {
		num++;
	}
	ifs.close();
	return num;
}
	
//将个人数据保存到文件里的保存函数
void studentManager::save() {
	ofstream ofs;
	ofs.open(FILENAME, ios::out);//用输出的方式写文件
		//将每个人的数据写入到文件中
	for (int i = 0; i < this->m_StuNum; i++) {
		ofs << this->m_StuArray[i]->id << " "
			<< this->m_StuArray[i]->name << " "
			<< this->m_StuArray[i]->mathScore << " "
			<< this->m_StuArray[i]->politicsScore << " "
			<< this->m_StuArray[i]->englishScore << " "
			<< this->m_StuArray[i]->Score << endl;
	}
	ofs.close();
}

//保存到数组中的函数
void studentManager::init_Stu() {
	ifstream ifs;
	ifs.open(FILENAME, ios::in);
	int id;
	string name;
	int mathScore;
	int politicsScore;
	int englishScore;
	int Score;
	int index = 0;
	while (ifs >> id && ifs >> name && ifs >> mathScore && ifs >> politicsScore && ifs >> englishScore&&ifs>>Score) {
		Student*student = new Student;
		student->id = id;
		student->name = name;
		student->mathScore = mathScore;
		student->politicsScore = politicsScore;
		student->englishScore = englishScore;
		student->Score = Score;
		this->m_StuArray[index] = student;
		index++;
	}
	ifs.close();
}

//显示学生信息函数
void studentManager::Show_Stu() {
	if (this->m_FileIsEmpty)
	{
		cout << "文件不存在或记录为空" << endl;
		system("pause");
	}
	else {
		for (int i = 0; i < m_StuNum; i++) {

				cout << "id为:" << this->m_StuArray[i]->id << " "
					<< "姓名:" << this->m_StuArray[i]->name << " "
					<< "高数成绩:" << this->m_StuArray[i]->mathScore << " "
					<< "思政成绩" << this->m_StuArray[i]->politicsScore << " "
					<< "英语成绩:" << this->m_StuArray[i]->englishScore << " "
					<< "总成绩:" << this->m_StuArray[i]->Score << endl;
			
		}
		system("pause");
	}
	
	system("cls");
	
	}


//判断学生是否存在函数
int studentManager::IsExist(int id) {
	int index = -1;
	for (int i = 0; i < this->m_StuNum; i++) {
		if (this->m_StuArray[i]->id == id)
		{
			//找到职工
			index = i;
			break;
		}
	}
	return index;
}

//删除学生函数
void studentManager::Del_Stu() {
	if (this->m_FileIsEmpty) {
		cout << "文件不存在或记录为空" << endl;

	}
	else {
		cout << "请输入想要删除的学生编号" << endl;
		int id = 0;
		cin >> id;
		int index = this->IsExist(id);
		if (index != -1)//职工存在
		{
			for (int i = index; i < this->m_StuNum - 1; i++) {
				this->m_StuArray[i] = this->m_StuArray[i + 1];

			}
			this->m_StuNum--;
			//同步更新到文件中
			this->save();
			cout << "删除成功!" << endl;
		}
		else {
			cout << "删除失败,未找到该学生" << endl;
		}
	}
	system("pause");
	system("cls");
}

//析构函数清除内存
studentManager::~studentManager() {
	if (this->m_StuArray != NULL) {
		delete[]this->m_StuArray;
		this->m_StuArray = NULL;
	}
}

 最后是main主程序,用来调用类中的函数

#include"studentManager.h"
#include"Student.h"
int main() {

	//实例化管理者对象
	studentManager sm;
	int choice = 0;
	while (true) {
		sm.Show_Menu();
		cout << "请输入您的选择" << endl;
		cin >> choice;
		switch (choice) {
		case 0://退出系统
			sm.exitSystem();
			break;
		case 1://添加学生
			sm.Add_Stu();
			break;
		case 2://显示学生信息
			sm.Show_Stu();
			break;
		case 3://删除学生
			sm.Del_Stu();
			break;
		default:
			system("cls");
			break;
		}

	}

	system("pause");
	return 0;
}

此程序用到了标准输入流和输出流方面的知识,会在当前文件生成一个txt文件保存数据。

学生成绩管理系统 该系统可用于管理某高校的本科生、研究生2类人员信息,人员信息包括:编号、姓名、性别、出生日期、专业、班级、类别(区分2类人员标记)等,具体功能包括: ① 专业管理:包括专业基本信息的添加、修改、删除、查询功能。学生必须归属于某个专业。 班级管理:包括班级基本信息的添加、修改、删除、查询功能。学生必须归属于某个班级。 ② 添加功能:分本科生和研究生两类人员,实现下列添加功能。 A.本科生:能够添加学生信息,如果高数成绩、英语成绩、C语言成绩都输入,则系统自动计算总成绩。 B.研究生:能够添加学生信息,如果课程综合成绩、论文成绩都输入,则系统自动计算总成绩。 修改功能:分本科生和研究生两类人员,实现下列修改功能。 A.本科生:根据学号来修改任意学生的除学号外的信息。如果高数成绩、英语成绩、c语言成绩都存在,则系统自动计算总成绩。 B.研究生:根据学号来修改任意学生的除学号外的信息。如果课程综合成绩、论文成绩都存在,则系统自动计算总成绩 ③ 删除功能:分本科生和研究生两类人员,能够根据学号删除一个学生。 ④ 排名功能:分本科生和研究生两类人员,实现下列排名功能。 说明:排名包括班级排名和年级排名,排名规则按体育竞赛规则处理,若出现两个并列第1名,下个名次为第3名,依此类推。 A:班级排名:分本科生和研究生两类学生,计算每个学生总成绩在班级中的名次。 B:年级排名:分本科生和研究生两类学生,计算每个学生总成绩在本专业、本年级中的名次。 ⑤ 查询功能:分本科生和研究生两类人员,实现下列查询功能。 1) 能够按班级显示本班全部学生信息。 2) 能够根据学号或者姓名查询学生信息。 3) 能够在某个班级中查询某门课成绩不及格学生信息。 ⑥ 排序功能:分本科生和研究生两类人员,实现下列排序功能。 1) 所有学生信息按学号从低到高排序并显示。 2) 某个班学生信息按总成绩从高到低排序并显示。 ⑦ 统计功能:分本科生和研究生两类人员,实现下列统计与显示功能。 1) 统计某班级某课程的平均成绩、最高成绩、最低成绩。如果学生该门课没有成绩,统计平均成绩时忽略该生。 2) 统计某班级某课程超过课程平均成绩的学生名单及人数。 3) 统计某班级某课程不及格学生名单及人数。 统计某班级某课程不同等级的学生人数。等级标准:优—大于等于90;良—大于等于80且小于90;中:大于等于70且小于80;及格:大于等于60且小于70;不及格:小于60。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

倔强菠萝

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值