C++学生管理系统

(草稿)

学生信息默认存储在同文件夹下的 test01.txt 文件中

#include <iostream>
#include <string>
#include <fstream>
using namespace std;

// constexpr auto FILENAME = "test01.txt";
const string FILENAME = "test01.txt";

//学生类
struct Student
{
   
	string id;    //学号
	string name;  //姓名
	float chi;    //语文成绩
	float math;   //数学成绩
	float eng;    //英语成绩

	Student(string id, string name, float chi, float math, float eng) {
   
		this->id = id; this->name = name; this->chi = chi; this->math = math; this->eng = eng;
	}
};

//管理系统类management system
struct MS
{
   
	int m_StuNum;  //记录文件中的学生个数

	Student ** m_StuArr;  //学生数组的指针

	bool m_FileIsEmpty;  //标志文件是否为空

	MS() {
   
		ifstream ifs;
		ifs.open(FILENAME, ios::in);

		//文件不存在情况
		if (!ifs.is_open())
		{
   
			cout << "文件不存在" << endl; //测试输出
			this->m_StuNum = 0;  //初始化人数
			this->m_FileIsEmpty = true; //初始化文件为空标志
			this->m_StuArr = NULL; //初始化数组
			ifs.close(); //关闭文件
			return;
		}

		//文件存在,并且没有记录
		char ch;
		ifs >> ch;
		if (ifs.eof())
		{
   
			cout << "文件为空!" << endl;
			this->m_StuNum = 0;
			this->m_FileIsEmpty = true;
			this->m_StuArr = NULL;
			ifs.close();
			return;
		}

		int num = this->get_StuNum();
		//cout << "\n已存有信息的学生个数为:" << num << "\n\n";  //测试代码
		this->m_StuNum = num;  //更新成员属性 
		this->m_FileIsEmpty = false; 

		//根据学生数创建数组
		this->m_StuArr = new Student *[this->m_StuNum];
		//初始化学生
		init_Stu();

		//测试代码
		/*for (int i = 0; i < m_StuNum; i++)
		{
			cout << "学号:" << this->m_StuArr[i]->id
				<< "  学生姓名:" << this->m_StuArr[i]->name
				<< "  语文成绩:" << this->m_StuArr[i]->chi
				<< "  数学成绩:" << this->m_StuArr[i]->math
				<< "  英语成绩:" << this->m_StuArr[i]->eng << endl;
		}*/
	}

	~MS() {
   
		if (this->m_StuArr != NULL)
		{
   
			delete[] this->m_StuArr;
		}
	}

	//展示菜单功能
	void show() {
   
		cout << "已存有信息的学生个数为:" << this->m_StuNum << "\n\n";
		for (int i = 0; i < this-
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Quantum_Wu

一起加油呀ヾ(◍°∇°◍)ノ゙

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

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

打赏作者

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

抵扣说明:

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

余额充值