文章首发于我的个人博客
此项目为一个小型学生管理系统,仅供初学者学习交流使用,使用者可以在此项目中用已学的C++基础知识进行实战演练,加深对所学知识的了解。
可以从github直接克隆项目(项目地址)
----2020/12/9 更新----
很多同学在问怎么添加学生成绩排序功能。其实很简单,学生信息我定义了类UnderStudent来保存,在进入程序后,会从文件中读取数据将数据保存在类型为UnderStudent的List容器,只需要在学生信息时先对该List容器使用sort()进行排序即可(先要在UnderStudent中重载<运算符)。这个功能我已经实现了,复制最新的代码即可。管理员功能界面中 “7)功能” 就是。
项目总览:
1. 项目要点
1、在每次进入最初登陆界面时,由于要再次加载文件内容,因此需先将list underst 和 list ad 中的内容使用clear()函数清空后再读入。
2、在读取文件时,由于使用!infile.eof()函数会导致最后一行读取两次。因此,在读文件循环内加入infile.get(),目的是在读完一行后立即换行。
2. 功能介绍
从上图可以看出,此项目主要有三个功能模块:开通管理员账户、管理员功能界面(管理员身份登录)、本科生功能界面(本科生省份登录)。第一次运行本项目时,首先要开通管理员账户才能进行其他的操作。
2.1 管理员功能界面
从上图可以看到管理员共有五项功能。
- 查看所有学生信息
- 按姓名查看学生信息
- 按学号查看学生信息
- 录入学生信息
- 按学号删除学生信息
2.2 学生功能界面
从上图可以看到学生共有两项功能。
- 查看个人信息
- 修改密码
需要注意的是,在登录学生账户之前首先要登进管理员系统创建学生信息之后才可以使用该账户登录学生界面。
3. code(以下代码均在VS2017上编译运行通过)
/*Administer.h 此文件为Administer类的头文件*/
#pragma once
#include<iostream>
#include<string>
using namespace std;
class Administer
{
private:
string name;
string ID;
string password;
public:
Administer() {
}
Administer(string na, string id, string passw);
string get_id(){
return ID;}
string get_name(){
return name;}
string get_password(){
return password; }
void display();
};
/*Administer.cpp 此文件为Administer类的实现*/
#include"Administer.h"
Administer::Administer(string na, string id, string passw) :name(na), ID(id), password(passw)
{}
void Administer::display()
{
cout << endl << "******************" << endl;
cout << endl << "* 姓名:" << name;
cout << endl << "* 账号:" << ID;
cout << endl << "******************" << endl;
}
/*UnderStudent.h 此文件为UnderStuent类的额头文件*/
#pragma once
#include<iostream>
#include<string>
using namespace std;
class Understudent
{
private:
string name;
string ID;
string password;
float grade;
string sex;
public:
Understudent() {
}
Understudent(string na, string id, string passw, float gra, string s);
string get_name(){
return name;}
string get_id(){
return ID;}
string get_password(){
return password;}
float get_grade() {
return grade; }
string get_sex() {
return sex; }
void display();
bool operator == (const Understudent &u)const //注意此处参数必须为const类型,才能与STL中list的内置函数匹配
{
return ID == u.ID;
}
// 2020/12/9 modify
// 补充功能:7) 按照成绩排序输出所有学生数据
// 重载学生类的 < 运算符
bool operator < (const Understudent &u)const
{
return this->grade < u.grade;
}
void set_password(string passw)
{
password = passw;
}
};
/*UnderStudent.cpp 此文件为UnderStudent类的实现*/
#include"UnderStudent.h"
Understudent::Understudent(string na, string id, string passw, float gra, string s):name(na),ID(id),password(passw),grade(gra), sex(s)
{
}
void Understudent::display()
{
cout << "******************"<< endl;
cout << "* 姓名:" << name << endl;
cout << "* 学号:" << ID <<endl ;
cout << "* 性别:" << sex <<endl ;
cout << "* 绩点:" << grade << endl;
cout << "******************"<< endl;
}
/*System.h 此文件为System的头文件*/
/*System.h 此文件为System的头文件*/
#pragma once
#include<list>
#include<fstream>
#include"UnderStudent.h"
#include"Administer.h"
class System
{
private:
list<Understudent> underst;
list<Administer> ad;
static int underst_count;
static int ad_count;
public:
virtual void load_interface(); //登陆界面
void exit_system(); //退出系统
void understudent_functionshow(); //学生用户功能界面
void administer_functionshow(); //管理员功能界面
void set_ad_account(); //设置管理员账户
void enter_ad_account(); //管理员身份登陆
void enter_underst_account(); //本科生身份登陆
void save_undst(); //保存本科生数据
void save_ad(); //保存管理员数据
void load_undst(); //读取本科生数据
void load_ad(); //读取管理员数据
/*管理员功能*/
void input_underst_info(); //录入本科生信息
void look_all_underst(); //查看所有本科生信息
//modify 2020/12/9
void look_all_underst_by_grade(); //按照成绩排序查看所有学生信息
void look_underst_by_name(string name); //根据姓名查看本科生信息
void look_underst_by_id(string id); //根据ID查看本科生信息
void delete_underst_by_id(string id); //根据ID删除本科生信息
/*本科生功能*/
void change_password(string id); //修改密码
};
/*System.cpp 此文件为System类的实现*/
#include"System.h"
int System::underst_count = 0;
int