基于C++写的学生管理系统(参考他人写的)

前言

C++初学者学完封装、继承和多态后参考他人写的学生管理系统,不过此处没有用到多态的知识。

开发环境:Microsoft Visual C++2010学习版。

如有不对的地方欢迎大家指正。

功能实现

0、退出管理程序
1、增加学生信息
2、显示学生信息
3、删除毕业学生
4、修改学生信息
5、查找学生信息
6、按照要求排序
7、保存到文件中
8、清空所有文档

在这里插入图片描述

代码内容

#include <iostream>
using namespace std;
#include <string>
#include <fstream>
#define FILENAME "stuFile.txt"

class Student
{
public:
	Student(){}
	Student(int id,string name,int math,int chinese,int english)
	{
		this -> m_Id = id;
		this -> m_Name = name;
		this -> m_Math = math;
		this -> m_Chinese = chinese;
		this -> m_English = english;
	}

	void showInfo()//显示个人信息
	{
		cout << "学号: " << this -> m_Id << "\t姓名: " << this -> m_Name << 
			"\t数学: "  << this -> m_Math << "\t语文" << this -> m_Chinese << "\t英语"
			<< this ->m_English << "\t总成绩: "<< this ->getnum() << endl;
	}

	int getnum()//计算总成绩
	{
		return this -> m_Math + this -> m_Chinese + this ->m_English;
	}

	int m_Id;//学号
	string m_Name;//姓名
	int m_Math;//数学
	int m_Chinese;//语文
	int m_English;//英语
};

class StudentManager
{
public:
	StudentManager();// 构造函数
	void Show_Menu();//展示菜单
	void ExitSystem();//退出系统
	int m_StuNum;//记录学生人数
	Student *m_StuArray;//学生数组指针
	void Add_Stu();//添加学生
	void save();//保存文件
	bool m_FileIsEmpty;//判断文件是否为空 标志
	int get_StuNum();//统计文件中人数
	void init_Stu();//初始化学生
	void Show_Stu();//显示学生
	void Del_Stu();//删除学生
	int IsExist(int id);//判断学生是否存在,如果存在,返回学生所在数组中的位置,不存在返回-1
	void Mod_Stu();//修改学生
	void Find_Stu();//查找学生
	void Sort_Stu();//按照要求排序
	void Clean_File();//清空文件
	~StudentManager();// 析构函数
};



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;
	}

	//2、文件存在,数据为空
	char ch;
	ifs >> ch;
	if( ifs.eof() )
	{
		//文件为空
		//cout << "文件为空" << endl;
		//初始化记录人数
		this -> m_StuNum = 0;
		//初始化数组指针
		this -> m_StuArray =NULL;
		//初始化文件是否为空
		this -> m_FileIsEmpty = true;
		ifs.close();
		return;
	}

	//3、文件存在,并且记录数据
	int num = this -> get_StuNum();
	this -> m_StuNum = num;

	//开辟空间
	this -> m_StuArray = new Student[this -> m_StuNum];
	//将文件中的数据存到数组中
	this -> init_Stu();
	this -> m_FileIsEmpty = false;
}

void StudentManager::Show_Menu()
{
	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 << "*************  7.保存到文件中  *************" << endl;
	cout << "*************  8.清空所有文档  *************" << endl;
	cout << "********************************************" << endl;
	cout << endl;
}

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

//添加学生
void StudentManager::Add_Stu()
{
	cout << "请输入添加学生数量: " << endl;

	int addNum = 0; //保存用户的输入数量
	cin >> addNum;

	if( addNum > 0 )
	{
		//添加
		//计算添加新空间大小
		int newSize = this -> m_StuNum + addNum; //新空间人数 = 原来纪录人数 + 新增人数

		//开辟新空间
		Student* newSpace = new Student[newSize];

		//将原来空间下数据,拷贝到新空间下
		if( this -> m_StuArray != NULL )
		{
			for(int i = 0; i < this->m_StuNum; i++)
			{
				newSpace[i] = this -> m_StuArray[i];
			}
		}

		//批量添加新数据
		for(int	i = 0; i < addNum; i++)
		{
			int id; //学号
			string name; //姓名
			int math;
			int chinese;
			int english;

			cout << "请输入第" << i+1 << "个新学生学号、姓名、数学、语文、英语" << endl;
			cin >> id >> name >> math >> chinese >> english;

			Student stu(id,name,math,chinese,english) ;
			//保存到数组中
			newSpace[this -> m_StuNum + i] = stu;
		}

		//释放原有空间
		delete [] this -> m_StuArray;

		//更改新空间的指向
		this -> m_StuArray = newSpace;

		//更新新的学生人数
		this -> m_StuNum = newSize;

		//更新学生不为空标志
		this -> m_FileIsEmpty = false;

		//提示添加成功  
		cout << "成功添加" << addNum << "名新学生!" << endl;

	}
	else
	{
		cout << "输入数据有误"  << endl;
	}

	//按任意键后 清屏回到上级目录
	system("pause");
	system("cls");
}

//保存文件
void StudentManager::save()
{
	ofstream ofs;
	ofs.open(FILENAME,ios::out);//写文件

	//将每个人的数据写入到文件中
	for(int i=0; i < this->m_StuNum; i++)
	{
		ofs << this -> m_StuArray[i] . m_Id << " "
			<< this -> m_StuArray[i] .m_Name << " "
			<< this -> m_StuArray[i] . m_Math<< " "
			<< this -> m_StuArray[i] . m_Chinese<< " "
			<< this -> m_StuArray[i] . m_English << endl;
	}
	ofs.close();//关闭文件
	cout << "保存成功!" << endl;
	//按任意键后 清屏回到上级目录
	system("pause");
	system("cls");
}


//统计文件中人数
int StudentManager::get_StuNum()
{
	ifstream ifs;
	ifs.open(FILENAME,ios::in);//打开文件 读

	int id;
	string name;
	int math;
	int chinese;
	int english;

	int num=0;
	while(ifs >> id && ifs >> name && ifs >> math && ifs >> chinese && ifs >> english
		)
	{
		//统计人数变量
		num++;
	}
	return num;
}

//初始化学生
void StudentManager::init_Stu()
{
	ifstream ifs;
	ifs.open(FILENAME,ios::in);//打开文件 读

	int id;
	string name;
	int math;
	int chinese;
	int english;

	int index = 0;
	while(ifs >> id && ifs >> name && ifs >> math && ifs >> chinese && ifs >> english
		)
	{
		Student stu(id,name,math,chinese,english);
		this -> m_StuArray[index] = stu;
		index++;
	}
	ifs.close();
}

//显示学生
void StudentManager::Show_Stu()
{
	//判断文件是否为空
	if(this -> m_FileIsEmpty)
	{
		cout << "文件不存在或记录为空" << endl;
	}
	else
	{
		for(int i=0; i<this->m_StuNum; i++)
		{
			this -> m_StuArray[i] .showInfo();
		}
	}
	//按任意键清屏
	system("pause");
	system("cls");
}


//删除学生
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--; //更新数组中记录人员个数

			cout << "删除成功" << endl;
		}
		else
		{
			cout << "删除失败,未找到该学生" << endl;
		}
	}
	//按任意键 清屏
	system("pause");
	system("cls");
}

//判断学生是否存在,如果存在,返回学生所在数组中的位置,不存在返回-1
int StudentManager::IsExist(int id)
{
	int index = -1;

	for(int i=0; i<this ->m_StuNum; i++)
	{
		if(this ->m_StuArray[i] . m_Id == id)
		{
			index = i;

			break;
		}
	}
	return index;
}

//修改学生
void StudentManager::Mod_Stu()
{
	if(this->m_FileIsEmpty)
	{
		cout << "文件不存在或记录为空" << endl;
	}
	else
	{
		cout << "请输入修改学生的学号" << endl;
		int id;
		cin >> id;

		int ret = this -> IsExist(id);

		if( ret != -1 )
		{
			//查找到该学号学生

			int newId = 0;
			string newName = "";
			int newMath=0;
			int newChinese=0;
			int newEnglish=0;

			cout << "查到" << id << "号学生,请输入新学生学号、姓名、数学、语文、英语:" << endl;
			cin >> newId >> newName >> newMath >> newChinese >> newEnglish;

			Student stu(newId,newName,newMath,newChinese,newEnglish);


			//更新数据 到数组中
			this -> m_StuArray[ret] = stu;

			cout << "修改成功" << endl;

		}
		else
		{
			cout << "修改失败,查无此人" << endl;
		}
	}
	//按任意键 清屏
	system("pause");
	system("cls");
}


//查找学生
void StudentManager::Find_Stu()
{
	if(this ->m_FileIsEmpty)
	{
		cout << "文件不存在或记录为空!" << endl;
	}
	else
	{
		cout << "请输入查找方式:" << endl;
		cout << "1、按学生学号查找" << endl;
		cout << "2、按学生姓名查找" << endl;

		int select = 0;
		cin >> select;

		if( select == 1 )
		{
			//按照学号查
			int id;
			cout << "请输入查找的学生学号: " << endl;
			cin >> id;

			int ret = this -> IsExist(id);

			if(  ret != -1 )
			{
				//找到学生
				cout << "查找成功!该学生信息如下:" << endl;
				this -> m_StuArray[ret] . showInfo();
			}
			else
			{
				cout << "查找失败,查无此人" << endl;
			}
		}
		else if( select == 2 )
		{
			//按照姓名查
			string name;
			cout << "请输入查找的学生姓名: " << endl;
			cin >> name;

			//加入判断是否查到标志
			bool flag = false;//默认未找到学生

			for(int i=0; i<this->m_StuNum; i++)
			{
				if( this -> m_StuArray[i].m_Name == name)
				{
					cout << "查找成功,学生学号为: " 
						<< this -> m_StuArray[i].m_Id
						<< "号学生信息如下:" <<endl;

					flag = true;

					this -> m_StuArray[i] . showInfo();
				}
			}
			if( flag ==false )
			{
				cout << "查找失败,查无此人" << endl;
			}

		}
		else
		{
			cout << "输入选项有误!" << endl;
		}
	}
	//按任意键 清屏
	system("pause");
	system("cls");
}


//按照学号排序
void StudentManager::Sort_Stu()
{

	if (this->m_FileIsEmpty)
	{
		cout << "文件不存在或记录为空!" << endl;
		//按任意键 清屏
		system("pause");
		system("cls");
	}
	else
	{
		cout << "--------------------------------------------" << endl;

		cout << "请选择排序种类: " << endl;
		cout << "1、按学号进行排序" << endl;
		cout << "2、按总成绩进行排序" << endl;

		int en=0;
		cin >> en;

		if(en == 1)
		{
			cout << "--------------------------------------------" << endl;

			cout << "请选择排序方式:" << endl;
			cout << "1、按学号进行升序" << endl;
			cout << "2、按学号进行降序" << endl;

			int select = 0;
			cin >> select;

			for (int i = 0; i < m_StuNum; i++)
			{
				int minOrMax = i;
				for (int j = i + 1; j < m_StuNum; j++)
				{
					if (select == 1) //升序
					{
						if (m_StuArray[minOrMax].m_Id > m_StuArray[j].m_Id)
						{
							minOrMax = j;
						}
					}
					else  //降序
					{
						if (m_StuArray[minOrMax].m_Id < m_StuArray[j].m_Id)
						{
							minOrMax = j;
						}
					}
				}

				if (i != minOrMax)
				{
					Student  temp = m_StuArray[i];
					m_StuArray[i] = m_StuArray[minOrMax];
					m_StuArray[minOrMax] = temp;
				}
			}

			cout << "--------------------------------------------" << endl;
			cout << "排序成功,排序后结果为:" << endl;
			this->Show_Stu();
		}
		else
		{
			cout << "--------------------------------------------" << endl;
			cout << "请选择排序方式:" << endl;
			cout << "1、按总成绩进行升序" << endl;
			cout << "2、按总成绩进行降序" << endl;

			int select1 = 0;
			cin >> select1;

			for (int i = 0; i < m_StuNum; i++)
			{
				int minOrMax = i;
				for (int j = i + 1; j < m_StuNum; j++)
				{
					if (select1 == 1) //升序
					{
						if (m_StuArray[minOrMax].getnum() > m_StuArray[j].getnum() )
						{
							minOrMax = j;
						}
					}
					else  //降序
					{
						if (m_StuArray[minOrMax].getnum() < m_StuArray[j].getnum() )
						{
							minOrMax = j;
						}
					}
				}

				if (i != minOrMax)
				{
					Student  temp = m_StuArray[i];
					m_StuArray[i] = m_StuArray[minOrMax];
					m_StuArray[minOrMax] = temp;
				}
			}
			cout << "--------------------------------------------" << endl;
			cout << "排序成功,排序后结果为:" << endl;
			this->Show_Stu();
		}

	}

}

//清空文件
void StudentManager::Clean_File()
{
	cout << "确认清空?" << endl;
	cout << "1、确认" << endl;
	cout << "2、返回" << endl;

	int select = 0;
	cin >> select;

	if (select == 1)
	{
		//打开模式 ios::trunc 如果存在删除文件并重新创建
		ofstream ofs(FILENAME, ios::trunc);
		ofs.close();

		if (this->m_StuArray != NULL)
		{
			//删除堆区数组指针
			this->m_StuNum = 0;
			delete[] this->m_StuArray;
			this->m_StuArray = NULL;
			this->m_FileIsEmpty = true;
		}
		cout << "清空成功!" << endl;
	}
	system("pause");
	system("cls");
}

StudentManager::~StudentManager()
{
	if( this -> m_StuArray != NULL)
	{
		delete[] this -> m_StuArray;
		this -> m_StuArray = NULL;
	}
}

int main()
{	
	//system("color 4");
	//实例化管理者对象
	StudentManager wm;
	int choice = 0;
	while( true )
	{
		//调用展示菜单成员函数
		wm.Show_Menu();

		cout << "请输入您的选择:" << endl;
		cin >> choice;

		switch ( choice )
		{
		case 0:    //退出系统
			wm.ExitSystem();
			break;
		case 1:    //增加学生
			wm.Add_Stu();
			break;
		case 2:    //显示学生
			wm.Show_Stu();
			break;
		case 3:    //删除学生
			wm.Del_Stu();
			break;
		case 4:    //修改学生
			wm.Mod_Stu();
			break;
		case 5:    //查找学生
			wm.Find_Stu();
			break;
		case 6:    //排序学生
			wm.Sort_Stu();
			break;
		case 7:   //保存数据到文件中
			wm.save();
			break;
		case 8:   //清空文档
			wm.Clean_File();
			break;
		default:
			system( "cls" ) ;//清屏
			break;
		}
	}

	system("pause");
	return 0;
}
  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值