c++学生管理系统源码
//management system.cpp
#include<iostream>
#include<string>
#include"Student.h"
using namespace std;
Interface UI;
StuInfor Stu;
Visitor visitor;
Teacher teacher(&Stu);
Student student;
void Teacher_work();
void Student_work();
void Data_initliza();
void vistor_work();
int main() {
Data_initliza();
int control;
while (1) {
UI.main_Interface();
cin >> control;
switch (control) {
case 1:
Teacher_work();
break;
case 2:
Student_work();
break;
case 3:
vistor_work();
break;
default:
Stu.Savedata();
exit(0);
}
system("cls");
}
return 0;
}
void Teacher_work() {
int control;
while (1) {
system("cls");
UI.Teacher_Interface();
cin >> control;
switch (control) {
case 1:
teacher.Check_rank(Stu);
break;
case 2:
teacher.SearchInfor(Stu);
break;
case 3:
teacher.Insert();
break;
case 4:
teacher.Delete();
break;
case 5:
teacher.Modify();
break;
case 6:
return;
}
}
}
void Student_work() {
int control;
while (1) {
system("cls");
UI.Student_Interface();
cin >> control;
switch (control) {
case 1:
student.Check_rank(Stu);
break;
case 2:
student.SearchInfor(Stu);
break;
case 3:
return;
}
}
}
void vistor_work() {
int control;
while (1) {
system("cls");
UI.Vistor_Interface();
cin >> control;
switch (control) {
case 1:
visitor.Check_rank(Stu);
break;
case 2:
visitor.SearchInfor(Stu);
break;
case 3:
return;
}
}
}
void Data_initliza() {
Stu.Input();
}
//Student.cpp
#include "Student.h"
#include<iostream>
#include<list>
#include<iomanip>
using namespace std;
BtreeStu& BtreeStu::operator=(BtreeStu& T) {
this->inf = T.inf;
return *this;
}
void StuInfor::Input() {
FILE* fp = NULL;
if (!(fp = fopen(Infile.c_str(), "rb"))) {
cout << "file open false" << endl;
exit(0);
}
fseek(fp, 0, SEEK_END);
int N = ftell(fp) / (sizeof(Infor) + sizeof('\n'));
this->stu_papulation = N;
fseek(fp, 0, SEEK_SET);
for (int i = 0; i < N; ++i) {
Infor* temp = new Infor;
fread(temp, sizeof(Infor), 1, fp);
fgetc(fp);
this->Tempsave.push_back(temp);
this->stu = insert(this->stu, temp); //以学号的顺序从小到大
}
fclose(fp);
}
void StuInfor::Savedata() {
FILE* fp = NULL;
if (!(fp = fopen(Infile.c_str(), "wb"))) {
cout << "file open false" << endl;
exit(0);
}
for (auto x : this->Tempsave) {
fwrite(x, sizeof(Infor), 1, fp);
fputc('\n', fp);
}
fclose(fp);
}
BtreeStu* StuInfor::insert(BtreeStu* Bt,Infor* temp) {
if (!Bt) {
BtreeStu* Tmp = new BtreeStu(temp);
Bt = Tmp;
}
else
if (Bt->inf->_stunum > temp->_stunum)
Bt->left = insert(Bt->left, temp);
else
Bt->right = insert(Bt->right, temp);
return Bt;
}
BtreeStu* StuInfor::insert(BtreeStu* Bt, Infor* temp, int kind) {
if (!Bt) {
BtreeStu* Tmp = new BtreeStu(temp);
Bt = Tmp;
}
else
if (Bt->inf->_score[kind] < temp->_score[kind])
Bt->left = insert(Bt->left, temp, kind);
else if (Bt->inf->_score[kind] > temp->_score[kind])
Bt->right = insert(Bt->right, temp, kind);
else
if(Bt->inf->_stunum>temp->_stunum)
Bt->left = insert(Bt->left, temp, kind);
else
Bt->right = insert(Bt->right, temp, kind);
return Bt;
}
BtreeStu* StuInfor::sort(BtreeStu* Bt, list<Infor*>& L,int Sort_status) {
for (auto P : L)
Bt = insert(Bt, P, Sort_status);
return Bt;
}
BtreeStu* StuInfor::Delete(BtreeStu* Bt, int stunum, BtreeStu** Del) {
if (!Bt)
throw"Not Find";
else {
if (Bt->inf->_stunum > stunum)
Bt->left = Delete(Bt->left, stunum, Del);
else if (Bt->inf->_stunum < stunum)
Bt->right = Delete(Bt->right, stunum, Del);
else
if (Bt->right && Bt->left) {
BtreeStu* temp = FindMin(Bt->right);
*Bt = *temp;
Bt->right = Delete(Bt->right, temp->inf->_stunum, Del);
}
else {
*Del = Bt;
if (!Bt->left)
Bt = Bt->right;
else
Bt = Bt->left;
}
}
return Bt;
}
void StuInfor::clear_Tree(BtreeStu* root) {
if (root) {
clear_Tree(root->left);
clear_Tree(root->right);
delete root;
}
}
void StuInfor::check() {
int control = 0;
if (this[control].stu == NULL) {
Interface::Stderr_Interface();
system("pause");
return;
}
int Sort_status = 1;
BtreeStu* temp = this->stu;
while (1) {
system("cls");
Interface::Check_Interface();
cout << "\n\n";
output(temp);
cin >> control;
if (control == 6)
break;
if (control != Sort_status) {
if (Sort_status != 1)
clear_Tree(temp);
Sort_status = control;
if (Sort_status == 1)
temp = this->stu;
else
temp = this->sort(NULL, this->Tempsave, Sort_status - 2);
}
}
}
void StuInfor::output(BtreeStu* Bt) const{
Interface::Score_Interface();
Inorder_out(Bt);
}
void StuInfor::output(Infor* Bt) const {
cout << "|" << setw(11) << Bt->_stunum
<< "|" << setw(11) << Bt->_stuname
<< "|" << setw(10) << Bt->_sex
<< "|" << setw(10) << Bt->_age;
for (int i = 0; i < Scorenum; ++i)
cout << "|" << setw(9) << Bt->_score[i];
cout << "|\n";
}
void StuInfor::Inorder_out(BtreeStu* Bt) const {
if (Bt) {
Inorder_out(Bt->left);
output(Bt->inf);
Inorder_out(Bt->right);
}
}
void StuInfor::searchfor() const {
system("cls");
int control;
while (1) {
Interface::InStunum_Interface();
int stunum;
cin >> stunum;
BtreeStu* temp = FindInfor(this->stu, stunum);
if (!temp)
cout << "------->Not Found!\n";
else {
Interface::Score_Interface();
output(temp->inf);
}
/****************************************/
{
cout
<< "\n\n\n"
<<"+---------------------------+\n"
<< "------>1. 继续查询 <--------\n"
<< "------>2.返回上一级<--------\n";
cin >> control;
}
/*****************************************/
if (control == 2)
break;
}
}
void StuInfor::clear() {
for (auto P = Tempsave.begin(); P != Tempsave.end(); ++P)
delete* P;
}
BtreeStu* StuInfor::FindMin(BtreeStu* Bt) {
if (Bt)
while (Bt->left)
Bt = Bt->left;
return Bt;
}
BtreeStu* StuInfor::FindInfor(BtreeStu* Bt, int stunum) const {
while (Bt) {
if (Bt->inf->_stunum > stunum)
Bt = Bt->left;
else if (Bt->inf->_stunum < stunum)
Bt = Bt->right;
else
break;
}
return Bt;
}
/*****************************************************************/
void Teacher::Insert() {
int N, control;
while (1) {
system("cls");
Interface::Insert_Interface();
cin >> N;
/*************************************/
for (int i = 0; i < N; ++i) {
Infor* temp = new Infor;
cout << "第" << i+1 << "位学生:\n";
cout << "------->学号:\t"; cin >> temp->_stunum;
cout << "------->姓名:\t"; cin >> temp->_stuname;
cout << "------->性别:\t"; cin >> temp->_sex;
cout << "------->年龄:\t"; cin >> temp->_age;
for (int i = 0; i < Scorenum; ++i) {
cout << "------>" << Scorename[i] << ": ";
cin >> temp->_score[i];
}
this->stuInf->stu_papulation++;
this->stuInf->Tempsave.push_back(temp);
this->stuInf->stu = this->stuInf->insert(this->stuInf->stu, temp);
cout << "\n\n";
}
/***************后续界面******************/
{
cout
<<"------->添加成功!\n"
<< "\n\n\n"
<< "+---------------------------+\n"
<< "------>1. 继续添加 <--------\n"
<< "------>2.返回上一级<--------\n";
cin >> control;
}
/****************************************/
if (control == 2)
break;
}
}
void Teacher::Delete() {
system("cls");
int control;
/**********************************************/
while (1) {
Interface::InStunum_Interface();
int stunum;
cin >> stunum;
try {
BtreeStu* temp = NULL;
this->stuInf->stu = this->stuInf->Delete(this->stuInf->stu, stunum, &temp);
this->stuInf->Tempsave.remove(temp->inf);
this->stuInf->stu_papulation--;
Infor* T = temp->inf;
delete temp;
cout << "----->删除成功!\n";
}
catch (const char* str) {
cout << "------>" << str << endl;
}
/***************后续界面******************/
{
cout
<< "\n\n\n"
<< "+---------------------------+\n"
<< "------>1. 继续删除 <--------\n"
<< "------>2.返回上一级<--------\n";
cin >> control;
}
/********************************************/
if (control == 2)
break;
}
}
void Teacher::Modify() {
system("cls");
int control = 0,stunum;
while (1) {
Interface::InStunum_Interface();
cin >> stunum;
BtreeStu* temp = this->stuInf->FindInfor(this->stuInf->stu, stunum);
if (temp == NULL) {
cout << "Not Found!\n";
}
else {
Interface::Score_Interface();
this->stuInf->output(temp->inf);
Interface::modify_Interface();
for (int i = 0; i < Scorenum; ++i)
cin >> temp->inf->_score[i];
Interface::Score_Interface();
this->stuInf->output(temp->inf);
cout << "---->修改成功!\n";
}
/***************后续界面******************/
{
cout
<< "\n\n\n"
<< "+---------------------------+\n"
<< "------>1. 继续修改 <--------\n"
<< "------>2.返回上一级<--------\n";
cin >> control;
}
/********************************************/
if (control == 2)
break;
}
}
/*********************用户界面*********************************/
void Interface::main_Interface() {
cout
<< "\t\t\t***************************************************\n"
<< "\t\t\t* *\n"
<< "\t\t\t+ 学生信息管理系统 +\n"
<< "\t\t\t* *\n"
<< "\t\t\t***************************************************\n"
<< "\t\t\t* +----------------------------+ *\n"
<< "\t\t\t* | 1.教师模式 | *\n"
<< "\t\t\t* ---------------------------- *\n"
<< "\t\t\t* | 2.学生模式 | *\n"
<< "\t\t\t* ---------------------------- *\n"
<< "\t\t\t* | 3.游客模式 | *\n"
<< "\t\t\t* ---------------------------- *\n"
<< "\t\t\t* | 4.退出程序 | *\n"
<< "\t\t\t* +----------------------------+ *\n"
<< "\t\t\t* *\n"
<< "\t\t\t***************************************************\n";
}
void Interface::Teacher_Interface() {
cout
<< "\t\t\t****************************************************************\n"
<< "\t\t\t* 管理员模式 *\n"
<< "\t\t\t****************************************************************\n"
<< "\t\t\t+----------------------------+---------------------------------+\n"
<< "\t\t\t| 1.查看排名 | 2.查询学生 |\n"
<< "\t\t\t+----------------------------+---------------------------------+\n"
<< "\t\t\t| 3.添加学生信息 | 4.删除学生信息 |\n"
<< "\t\t\t+----------------------------+---------------------------------+\n"
<< "\t\t\t| 5.修改成绩 | 6.返回上一级 |\n"
<< "\t\t\t+----------------------------+---------------------------------+\n";
}
void Interface::Student_Interface() {
cout
<< "\t\t\t****************************************************************\n"
<< "\t\t\t* 学生模式 *\n"
<< "\t\t\t****************************************************************\n"
<< "\t\t\t+----------------------------+---------------------------------+\n"
<< "\t\t\t| 1.查看排名 | 2.查询学生 |\n"
<< "\t\t\t+----------------------------+---------------------------------+\n"
<< "\t\t\t| ********* | ********* |\n"
<< "\t\t\t+----------------------------+---------------------------------+\n"
<< "\t\t\t| 3.返回上一级 | 0_Dczzzzzz |\n"
<< "\t\t\t+----------------------------+---------------------------------+\n";
}
void Interface::Check_Interface() {
cout
<< "+------------------------------------+\n"
<< "| 1.按学号顺序(默认) |\n"
<< "| 2.按高数成绩 |\n"
<< "| 3.按OS成绩 |\n"
<< "| 4.按DB成绩 |\n"
<< "| 5.按机组成绩 |\n"
<< "| 6.返回上一级 |\n"
<< "+------------------------------------+\n";
}
void Interface::Insert_Interface() {
cout
<< "*********请输入添加学生信息***********\n"
<< "+------------>插入人数<-------------+\n"
<< "-------------->:";
}
void Interface::Stderr_Interface() {
cout
<< "+------------------------+\n"
<< "| 暂无数据 |\n"
<< "+------------------------+\n";
}
void Interface::Score_Interface() {
cout
<< "+-----------+-----------+----------+----------+---------+--------+---------+---------+\n"
<< "| 学 号 | 姓 名 | 性别 | 年龄 | 高数 | OS | DB | 机组 |\n"
<< " -------------------------------------------------------------------------------------\n";
}
void Interface::Vistor_Interface() {
cout
<< "\t\t\t****************************************************************\n"
<< "\t\t\t* 游客模式 *\n"
<< "\t\t\t****************************************************************\n"
<< "\t\t\t+----------------------------+---------------------------------+\n"
<< "\t\t\t| 1.查看排名 | 2.查询学生 |\n"
<< "\t\t\t+----------------------------+---------------------------------+\n"
<< "\t\t\t| ********* | ********* |\n"
<< "\t\t\t+----------------------------+---------------------------------+\n"
<< "\t\t\t| 3.返回上一级 | 0_Dczzzzzz |\n"
<< "\t\t\t+----------------------------+---------------------------------+\n";
}
void Interface::InStunum_Interface() {
cout
<< "***************************\n"
<< "-------->输入学号<--------+\n"
<< "----->";
}
void Interface::modify_Interface() {
cout
<< "+--->依次输入下列成绩:\n"
<< "+ \n"
<< "高 数|O S|D B|机 组|\n";
}
#include<iostream>
#include<string>
#include<list>
#ifndef STUDENT_H_
#define STUDENT_H_
const int Scorenum = 4; //学生课程数
const std::string Scorename[Scorenum]{ "高等数学","操作系统","数据库原理","计算机组成" };
const std::string Infile("studentInfor.txt"); //学生信息保存文件
const std::string Acount("AcountInfor.txt"); //账户信息保存文件
struct Infor {
int _stunum;
int _age;
char _sex;
std::string _stuname;
int _score[Scorenum];
};
struct BtreeStu {
Infor* inf;
BtreeStu* left;
BtreeStu* right;
BtreeStu(Infor* temp) :inf(temp),left(NULL), right(NULL){}
BtreeStu& operator=(BtreeStu& T);
};
//以二叉树模型储存学生信息
class StuInfor {
private:
BtreeStu* stu; //学生信息树的根节点
std::list<Infor*> Tempsave; //用一个指针数组保存每个学生信息的指针,排序过程中便于操作
unsigned int stu_papulation; //学生的总数
/******************************************************************/
BtreeStu* FindMin(BtreeStu*);
void Inorder_out(BtreeStu*) const;
BtreeStu* FindInfor(BtreeStu*, int) const;
void output(BtreeStu*) const;
void output(Infor*) const;
BtreeStu* insert(BtreeStu*,Infor* temp); //以学号的顺序插入
BtreeStu* insert(BtreeStu*,Infor* temp, int kind); //以各课的成绩的顺序插入
BtreeStu* Delete(BtreeStu*,int stunum,BtreeStu**); //删除学生信息
BtreeStu* sort(BtreeStu* temp, std::list<Infor*>&,int Sort_status);
/*********************************************************************/
public:
friend class Teacher;
StuInfor() :stu(NULL),stu_papulation(0){}
~StuInfor() {
this->clear_Tree(this->stu);
//this->clear();
}
void Input(); //从文件中读取数据
void Savedata(); //将数据保存到文件中
void searchfor() const; //查找信息
void check(); //查看信息
void clear(); //清除数据
void clear_Tree(BtreeStu*);
};
class Visitor {
public:
void Check_rank(StuInfor& Stu) {
Stu.check();
}
void SearchInfor(StuInfor& Stu) {
Stu.searchfor();
}
};
class Teacher:public Visitor {
private:
StuInfor* stuInf;
//std::string account_num;
//std::string password;
public:
Teacher(StuInfor* temp) :stuInf(temp) {}
void Insert();
void Delete();
void Modify();
};
class Student: public Visitor {
private:
//std::string account_num;
//std::string password;
public:
/***学生特有的事情***/
/*****暂时没有想到*****/
};
class Interface {
public:
static void main_Interface();
static void Teacher_Interface();
static void Student_Interface();
static void Check_Interface();
static void Insert_Interface();
static void Stderr_Interface();
static void Score_Interface();
static void Vistor_Interface();
static void InStunum_Interface();
static void modify_Interface();
};
#endif
这是visual stdio 上面进行编写的。在初次执行的时候是没有学生信息数据的,需要自己添加在使用文件操作一系列的函数时可能报错,需要将 _CRT_SECURE_NO_WARNINGS 这条预处理指令写入具体操作如下
将预处理指令复制进去即可