教务管理系统C++全部代码

设计说明请点此链接

//main.cpp
#include"Base.h"
#include"tool.h"
#include"dialog.h"
#include"index.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
    Base student;                      //处理学生基本信息的对象
    First major;                         //处理主修专业的对象
    Fbase minor;                       //处理辅修专业的对象
    int n = -1, m = 0;
    bool logoned = false;                   //用户登录状态变量
    Index index;                                  //建立索引
    Dialog dialog;


    while (!logoned)                                  //如未登录则开始登录
    {

        switch (dialog.logon())//选择登录身份
        {
        case 0://以下是招生处的功能
            logoned = true;            //已登录
            while (logoned)                //如已登录则开始会话,在后面的程序运行中,如果用户选择登出,则跳出循环
            {
                switch (dialog.AdmissionOpt())//以下是招生处的功能
                {
                case 0:                                 //展示学生列表
                    index.show();
                    break;
                case 1:                                         //检索并查看信息
                    n = index.select();                        //让用户通过输入学号或姓名来检索要展示的数据
                    if (n == -1)                              //返回值为“-1”代表未检索到相关信息
                    {
                        cout << "此信息不存在!" << endl;
                        break;
                    }
                    student.read(to_string(n));           //读取磁盘信息到对象
                    student.Info();                              //展示读取到的信息
                    break;
                case 2:                                           //修改学生信息
                    n = index.select();
                    if (n == -1)
                    {
                        cout << "信息不存在,请录入" << endl;
                        n = index.search(0);                //如果学号不存在,则选择一个空位置。
                        cout << "请输入该生的学号:" << endl;
                        m = inputnumber();               //学号的临时变量
                        while (index.search(m) >= 0)     //如果新输入的学号已经存在,需要重新输入
                        {
                            cout << "请重新输入该生的学号:" << endl;
                            m = inputnumber();
                        }
                    }
                    else
                    {
                        cout << "信息已存在,是否修改?\n不修改请输入0,修改请输入1" << endl;
                        if (inputnumber(1) == 0) break;
                        m = index.getnum(n);                //获取序号对应的学号
                    }
                    student.InfoInput(m);                     //输入对象信息
                    student.save(to_string(n));                //保存相应信息
                    index.create();                                  //重新建立索引
                    break;

                case 3:           //删除信息
                    n = index.select();;
                    if (n == -1)
                    {
                        cout << "此人信息不存在!" << endl;
                        break;
                    }
                    else
                    {
                        clear(to_string(n)); //调用函数清空相应信息
                        index.create();//重建索引
                        cout << "删除成功" << endl;
                    }
                    break;
                case 4:
                    logoned = false;                                   //用户登出,跳出循环
                    break;
                case 5:           //更改密码
                    dialog.KeyReset(0);
                    break;
                case 6:
                    return 0;                                         //程序结束
                default:
                    break;
                }
            }

            break;//招生处功能模块结束


        case 1://以下是主修教务员的功能
            logoned = true;
            while (logoned)
            {
                switch (dialog.M_teacherOpt())
                {
                case 0:
                    index.show();
                    break;
                case 1://功能:查看学生信息,选择学生序号,显示基本信息和主修科目分数
                    n = index.select();;
                    student.read(to_string(n));
                    major.read(to_string(n));
                    student.Info();
                    major.display();
                    break;
                case 2://修改成绩信息并保存
                    n = index.select();;
                    if (n == -1)
                    {
                        cout << "此人不存在!" << endl;  //如果基本信息尚未录入,则不能修改成绩
                        break;
                    }
                    major.MscoreInput();
                    major.save(to_string(n));
                    break;
                case 3://筛选出成绩优秀的学生
                    for (int i = 0; i < MAX_INDEX; i++)
                    {
                        major.read(to_string(i));
                        if (major.outstand())
                        {
                            index.show(i);
                            major.display();
                        }
                    }
                    cout << "以上是成绩优秀的学生。\n" << endl;
                    break;
                case 4://筛选出需要补考的学生
                    for (int i = 0; i < MAX_INDEX; i++)
                    {
                        major.read(to_string(i));
                        if (major.resit())
                        {
                            index.show(i);
                            major.display();
                        }
                    }
                    cout << "以上是需要补考的学生。\n" << endl;
                    break;
                case 5://重新选择身份
                    logoned = false;
                    break;
                case 6://更改密码
                    dialog.KeyReset(1);
                    break;
                case 7://退出程序
                    return 0;
                default:
                    break;
                }
            }

            break;//主修教务员模块结束

        case 2://以下是辅修教务员的功能
            logoned = true;
            while (logoned)
            {

                switch (dialog.F_teacherOpt())
                {
                case 0:
                    index.show();
                    break;
                case 1://查看学生信息
                    n = index.select();;
                    student.read(to_string(n));
                    student.Info();
                    minor.read(to_string(n));
                    minor.display();
                    break;
                case 2://设置辅修科目
                    n = index.select();;
                    if (n == -1)
                    {
                        cout << "此人不存在!" << endl;//如果基本信息尚未录入,则不能修改成绩
                        break;
                    }
                    minor.FdepSet();
                    minor.save(to_string(n));
                    break;
                case 3://录入辅修成绩
                    n = index.select();;
                    if (n == -1)
                    {
                        cout << "此人不存在!" << endl;//如果基本信息尚未录入,则不能修改成绩
                        break;
                    }
                    minor.FscoreInput();
                    minor.save(to_string(n));
                    break;
                case 4://查看需要补考人员
                    for (int i = 0; i < 4; i++)
                    {
                        minor.read(to_string(i));
                        if (minor.resit())
                        {
                            index.show(i);
                            minor.display();
                        }
                    }
                    cout << "以上是需要补考的学生。\n" << endl;
                    break;

                case 5://重新选择身份
                    logoned = false;
                    break;
                case 6://更改密码
                    dialog.KeyReset(2);
                    break;
                case 7://退出程序
                    return 0;
                default:
                    break;
                }
            }
            break;//辅修教务员功能模块结束


        case 3://用户选择退出系统
            return 0;

        case -1://密码校验失败,重新选择身份
            logoned = false;
            break;
        default:
            break;
        }
    }
}

分页

//base.h
#pragma once
#include<iostream>
#include"tool.h"
#include <fstream>

using namespace std;

constexpr auto MDEPT_NUM = 5; //主修科目有5个
constexpr auto FDEP_NUM = 4;//辅修科目有4个

class Base
{
public:
	void InfoInput(int n = 0);//输入基本信息
	void Info();//显示基本信息
	void save(string);         //读取信息
	void read(string);          //保存信息
private:
	long num = 0;  //学号
	char name[20] = "NULL";//姓名
	int sex;//性别,0表示男,1表示女
	int age;//年龄
	string addr;//地址
	string mdept;//主修专业

};



class First : public Base
{
public:
	void MscoreInput(); //录入成绩
	void display();       //显示成绩
	int getscore(int);      //获取单科成绩
	void save(string);  //保存文件
	void read(string);//读取文件
	bool outstand();   //是否三门课达到85分,并且其他两门分数不低于70
	bool resit();     //有一门不及格则返回值为真
private:
	float Mscore[60] = { 0 };//主修分数
};

class Fbase :public Base
{

public:
	void FdepSet();
	void FscoreInput();
	bool resit();          //有一门不及格则返回值为真
	int getscore(int);
	void display();
	void save(string);
	void read(string a);

	string fdep = "NULL";//辅修专业

private:
	float Fscore[35] = { 0 };//辅修分数

};

分页

//base.cpp
#include "Base.h"
#include<string>

void First::MscoreInput()
{
	for (int i = 0; i < MDEPT_NUM; i++)
	{
		std::cout << "请输入第" << i + 1 << "门主修课的成绩:" << std::endl;
		Mscore[i] = inputfloat(100);
	}
	std::cout << "该学生全部主修成绩录入完毕" << std::endl;
}

void First::display()
{
	cout << "主修专业成绩:" << endl;
	for (int i = 0; i < MDEPT_NUM; i++)
	{
		cout << getscore(i) << '\t';
	}
	cout << endl;
}

int First::getscore(int i)
{
	return Mscore[i];
}

void First::save(string a)
{
	ofstream outfile;
	outfile.open("data\\major" + a + ".txt", ios::out);
	for (int i = 0; i < MDEPT_NUM; i++)
	{
		outfile << getscore(i) << endl;
	}
	outfile.close();
}

void First::read(string a)
{
	ifstream infile;
	infile.open("data\\major" + a + ".txt", ios::out);
	if (infile.good())
	{
		for (int i = 0; i < MDEPT_NUM; i++)
		{
			infile >> Mscore[i];
		}
	}
	else
	{
		for (int i = 0; i < MDEPT_NUM; i++)
		{
			Mscore[i] = 0;
		}
	}

	infile.close();
}


bool First::outstand()
{
	int t;
	int m = 0;
	int n = 0;
	for (int i = 0; i < MDEPT_NUM; i++)
	{
		t = getscore(i);
		if (t >= 85) m++;
		if (t < 70) n++;
	}
	if (m >= 3 && n <= 0)		return true;
	else return false;
}

bool First::resit()
{
	bool record = false;//检查是否已经被记录
	bool fail = false;//检查是否不及格
	for (int i = 0; i < MDEPT_NUM; i++)
	{
		if (getscore(i) < 60) fail = true;
		if (getscore(i) > 0) record = true;
	}
	return fail && record;
}

void Fbase::FdepSet()
{
	cout << "请设置该学生辅修专业" << endl;
	cin >> fdep;
}

void Fbase::FscoreInput()
{
	if (fdep == "NULL")
	{
		cout << "请先指定辅修专业" << endl;
	}
	else
	{
		for (int i = 0; i < FDEP_NUM; i++)
		{
			std::cout << "请输入第" << i + 1 << "门辅修课的成绩:" << std::endl;
			Fscore[i] = inputfloat(100);
		}
		std::cout << "该学生全部辅修成绩录入完毕" << std::endl;
	}
}

bool Fbase::resit()
{
	bool record = false;//检查是否已经被记录
	bool fail = false;//检查是否不及格
	for (int i = 0; i < FDEP_NUM; i++)
	{
		if (getscore(i) < 60) fail = true;
		if (getscore(i) > 0) record = true;
	}
	return fail && record;
}

int Fbase::getscore(int i)
{
	return Fscore[i];
}

void Fbase::display()
{
	cout << "辅修专业:" << fdep << endl;
	cout << "辅修专业成绩:" << endl;
	for (int i = 0; i < FDEP_NUM; i++)
	{
		cout << getscore(i) << '\t';
	}
	cout << endl;
}


void Fbase::save(string a)
{
	ofstream outfile;
	outfile.open("data\\minor" + a + ".txt", ios::out);
	outfile << fdep << endl;
	for (int i = 0; i < MDEPT_NUM; i++)
	{
		outfile << getscore(i) << endl;
	}
	outfile.close();
}

void Fbase::read(string a)
{
	ifstream infile;
	infile.open("data\\minor" + a + ".txt", ios::out);
	if (infile.good())
	{
		infile >> fdep;
		for (int i = 0; i < MDEPT_NUM; i++)
		{
			infile >> Fscore[i];
		}
	}
	else
	{
		fdep[0] = '\0';
		for (int i = 0; i < MDEPT_NUM; i++)
		{
			Fscore[i] = 0;
		}
	}

	infile.close();
}

void Base::InfoInput(int n)
{
	if (n == 0)
	{
		cout << "请输入该生的学号:" << endl;
		num = inputnumber();
	}
	else
	{
		num = n;
	}
	cout << "请输入该生的姓名:" << endl;
	cin >> name;
	cout << "请输入该生的性别(0表示男,1表示女):" << endl;
	sex = inputnumber(1);
	cout << "请输入该生的年龄:" << endl;
	age = inputnumber(200);
	cout << "请输入该生的地址:" << endl;
	cin >> addr;
	cout << "请输入该生的主修专业:" << endl;
	cin >> mdept;
	cout << "学生" << name << "的基本信息录入完成\n";
}

void Base::Info()
{
	if (num == 0)
	{
		cout << "信息尚未录入!" << endl;
	}
	else
	{
		cout << "学号:" << num << "\n姓名:" << name << "\n性别:";
		if (sex)  	cout << "女";
		else cout << "男";
		cout << "\n年龄:" << age << "\n地址:" << addr << "\n主修专业:" << mdept << endl;
	}
}

void Base::save(string a)
{
	ofstream outfile;
	outfile.open("data\\student" + a + ".txt", ios::out | ios::trunc);
	outfile << num << endl << name << endl << sex << endl << age << endl << addr << endl << mdept << endl;
	outfile.close();
}

void Base::read(string a)
{
	ifstream infile;
	infile.open("data\\student" + a + ".txt", ios::in);
	if (infile.good())
	{
		infile >> num >> name >> sex >> age >> addr >> mdept;
	}
	infile.close();
}

分页

//dialog.h
#pragma once
#include"tool.h"
#include"index.h"

class Dialog
{
public:

	bool KeyCheck(int n);                                //检查密码,n为不同用户的代号

	void KeyReset(int n);                                     //更改密码,n为不同用户的代号

	int logon();                                              //登录界面

	int AdmissionOpt();                                 //教务处工作人员的对话框

	int M_teacherOpt();                                //主修专业教务员的对话框

	int F_teacherOpt();                                 //辅修专业教务员的对话框

};

分页

//dialog.cpp
#include"Dialog.h"

bool Dialog::KeyCheck(int n)//n为不同用户的代号
{
	int password[3]{};
	int key;
	ifstream infile;
	infile.open("data\\password.txt", ios::in);
	if (infile.good())
	{
		infile >> password[0] >> password[1] >> password[2];
	}
	infile.close();
	cout << "请输入密码:";
	key = inputnumber();
	if (password[n] == key) return true;
	else  return false;
}

void Dialog::KeyReset(int n)//更改密码,n为不同用户的代号
{
	int password[3]{};
	ifstream infile;
	infile.open("data\\password.txt", ios::in);
	if (infile.good())
	{
		infile >> password[0] >> password[1] >> password[2];
	}
	infile.close();
	cout << "请输入新密码:" << endl;
	password[n] = inputnumber();

	ofstream outfile;
	outfile.open("data\\password.txt", ios::out);
	if (outfile.good())
	{
		outfile << password[0] << endl << password[1] << endl << password[2];
	}

	outfile.close();
	cout << "更改密码成功" << endl;
}


int Dialog::logon()//登录系统
{
	int a;
	cout << "欢迎登录教务管理!" << endl;
	cout << "请选择您的身份\n0:招生办工作人员\n1:主修系教务员\n2:辅修系教务员\n3:退出系统" << endl;
	a = inputnumber(3);
	if (a == 3)	return 3;//3对应退出系统选项
	else if (KeyCheck(a))
	{
		cout << "登录成功!" << endl;
		return a;
	}
	else
	{
		cout << "密码错误请重新登录" << endl;
		return -1;
	}
}


int Dialog::AdmissionOpt()
{
	int a;
	cout << "请输入要进行的操作\n0:查看学生列表\n1:查看学生信息\n2:修改学生信息\n3:删除学生信息\n4:重新选择身份\n5:更改密码\n6:退出系统" << endl;
	a = inputnumber(6);
	return a;
}

int Dialog::M_teacherOpt()
{
	int a;
	cout << "请输入要进行的操作\n0:查看学生列表\n1:查看学生信息\n2:录入主修成绩\n3:查看成绩优秀的同学\n4:查看需要补考的同学\n5:重新选择身份\n6:更改密码\n7:退出系统" << endl;
	a = inputnumber(7);
	return a;
}


int Dialog::F_teacherOpt()
{
	int a;
	cout << "请输入要进行的操作\n0:查看学生列表\n1:查看学生信息\n2:设置辅修专业\n3:录入辅修成绩\n4:查看需要补考的同学\n5:重新选择身份\n6:更改密码\n7:退出系统" << endl;
	a = inputnumber(7);
	return a;
}

分页

//index.h
#pragma once
#include<string>
#include"tool.h"
constexpr auto MAX_INDEX = 100;

class Index
{
public:
	Index();              //建立索引
	void create();
	int search(int);             //检索
	int search(string);           //检索
	int select();
	void show();                   //显示索引
	void show(int n);           //显示单个学生的学号和姓名
	int getnum(int );          //获取学号
private:
	int num[MAX_INDEX]{};
	string name[MAX_INDEX]{};
};

分页

//index.cpp
#include "index.h"


Index::Index()
{
	create();
}

void Index::create()
{
	ifstream file;
	ofstream outfile;

	outfile.open("data\\index.txt");

	for (int i = 0; i < MAX_INDEX; i++)
	{
		file.open("data\\student" + to_string(i) + ".txt", ios::in);
		if (file.is_open())
		{
			file >> num[i] >> name[i];
		}
		else
		{
			num[i] = 0;
			name[i] = "NULL";
		}
		file.close();
		outfile << "[id]\t" << i << "\n[num]\t" << num[i] << "\n[name]\t" << name[i] << endl;
	}
	outfile.close();
}

int Index::search(int n)
{
	for (int i = 0; i < MAX_INDEX; i++)
	{
		if (num[i] == n) return i;
	}
	return -1;
}

int Index::search(string n)
{
	for (int i = 0; i < MAX_INDEX; i++)
	{
		if (name[i] == n) return i;
	}
	return -1;
}

int Index::select()
{
	int n, i;
	string str;
	cout << "请输入要查看学生学号或姓名:";
	if (!(cin >> n))
	{
		cin.clear();
		cin >> str;
		i = search(str);
	}
	else
	{
		i = search(n);
	}
	return i;
}


void Index::show()
{
	int n = 0;
	for (int i = 0; i < MAX_INDEX; i++)
	{
		if (num[i] == 0)  continue;
		n++;
		cout << " 学号:" << num[i] << "\t 姓名:" << name[i] << endl;
	}
	cout << " 共有" << n << "名学生" << endl;
}

void Index::show(int n)
{
	cout << "学号:\t" << num[n] << "   姓名: \t" << name[n] << endl;
}

int Index::getnum(int n)
{
	return num[n];
}

分页

//tool.h
#pragma once
#include<iostream>
#include<fstream>
using namespace std;


bool interval(int a, int min, int max);       //判断变量是否在区间内

int inputnumber(int a= 2147483647);     //输入一个数字,并确保不发生错误,a为数值上限

float inputfloat(int a);                //参数为数字上限

void clear(string);                                 //清除一个学生的数据

分页

//tool.cpp
#include"tool.h"

bool interval(int a, int min, int max)
{
	if (a<min || a>max)
	{
		return false;
	}
	else
	{
		return true;
	}
}

int inputnumber(int a )//参数为数字上限
{
	int n;
	while (!(cin >> n))
	{
		cin.clear();
		while (cin.get() != '\n')
		{
			continue;
		}//跳过错误输入
		cout << "请输入一个数字: \n";
	}
	while (!interval(n, 0, a))
	{
		cout << "输入有误,请在0到"<<a<<"区间内选择: ";
		cin >> n;
	}
	return n;
}

float inputfloat(int a)//参数为数字上限
{
	float n;
	while (!(cin >> n))
	{
		cin.clear();
		while (cin.get() != '\n')
		{
			continue;
		}//跳过错误输入
		cout << "请输入一个数字: \n";
	}
	while (!interval(n, 0, a))
	{
		cout << "输入有误,请在0到" << a << "区间内选择: ";
		cin >> n;
	}
	return n;
}


void clear(string n) 
{
	ofstream file;
	file.open("data\\student" + n + ".txt", ios::out | ios::trunc);
	file << "0\n"<<"NULL\0";
	file.close();
	file.open("data\\major" + n + ".txt", ios::out | ios::trunc);
	file.close();
	file.open("data\\minor" + n + ".txt", ios::out | ios::trunc);
	file.close();
}
  • 9
    点赞
  • 93
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

高堂明镜悲白发

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

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

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

打赏作者

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

抵扣说明:

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

余额充值