C++课程设计-高校学籍管理系统
简述:主要运用C++
的类和i/o文件操作完成了这个小型的高校学学籍信息管理系统。本程序实现
老师或者学生不同身份登录实现不同的功能,老师登录可输入并存储学生学籍信息 输出学
生的学籍信息 查询学生的学籍信息 修改学生的学籍信息 删除学生的学籍信息 增添学生
的学籍信息
基本功能
教师登录:
1.输入学生的学籍信息:通过输入学生的学号、姓名、系别、班级、各科成绩,
然后就可以把数据进行增、删、查、改。
2、输出学生学籍的所有信息并用i/o流文件保存,通过类的对象调用输出函数用i/o文件
指针读取文件就可以把所有的信息打印在屏幕上。
3、把所有的信息保存成文件便于在原有的信息上就进行数据的变动,可以不必再费太多的时间。
学生登录;
1.用类的对象调用学生类的查询及显示函数用i/o文件指针操作可进行学籍信息的查询及打印
2.学生的权限有限只可查询及显示学生学籍信息不可执行对信息的改动操作
源码:
-------------------------------------------------------------------------------------------------------
basic.h
#ifndef __BASIC_H
#define __BASIC_H
//基本信息类
class Basic
{
public:
virtual void enter();
virtual void show();
char num[11];
char name[30];
char department[20];
char Class[30];
};
#endif
-------------------------------------------------------------------------------------------------------
basic.cpp
#include "basic.h"
#include
#include
using namespace std;
void Basic::enter() //基本信息输入
{
cout << "请输入学生信息" << endl;
cout << "学号:";
cin >> num ;
cout << "姓名:";
cin >> name ;
cout << "系别:";
cin >> department;
cout << "班级:";
cin >> Class;
}
void Basic::show() //基本信息显示
{
cout << "学号" << "\t" << "姓名" << "\t"
<< "系别" << "\t" << "班级" << endl;
cout << num << "\t" << name << "\t"
<< department << "\t" << Class << endl;
}
-------------------------------------------------------------------------------------------------------
begin.h
#ifndef __BEGIN_H
#define __BEGIN_H
//开始类
void Begin();
int List();
int Choose();
#endif
-------------------------------------------------------------------------------------------------------
begin.cpp
#include
#include
using namespace std;
void Begin()
{
cout << "\t\t *****************************" << endl;
cout << "\t\t *Welcome to 高校学籍管理系统*" << endl;
cout << "\t\t *****************************" << endl;
cout << "\t\t—————————————————————————————————————————"
<< endl;
cout << "\t\t|\t1.[教师]\t2.[学生]\t|" << endl;
cout << "\t\t| 其他任意字符退出高校学籍管理系统
|" << endl;
cout << "\t\t—————————————————————————————————————————"
<< endl;
}
int List()
{
cout << "\t\t****************教师在线*****************"
<< endl;
cout << "\t\t—————————————————————————————————————————"
<< endl;
cout << "\t\t|\t1.[添加]\t2.[显示]\t|" << endl;
cout << "\t\t|\t3.[查找]\t4.[修改]\t|" << endl;
cout << "\t\t|\t5.[删除]\t0.[返回]\t|" << endl;
cout << "\t\t—————————————————————————————————————————"
<< endl;
cout << "\n\t请选择操作[1~7][ ]\b\b";
char m;
cin >> m;
return m;
}
int Choose()
{
cout << "\t\t****************学生在线*****************"
<< endl;
cout << "\t\t—————————————————————————————————————————"
<< endl;
cout << "\t\t|\t1.[显示]\t2.[查找]\t|" << endl;
cout << "\t\t|\t 0.[返回登录界面]\t\t|"
<< endl;
cout << "\t\t—————————————————————————————————————————"
<< endl;
cout << "\n\t请选择操作[0~2][ ]\b\b";
char k;
cin >> k;
return k;
}
-------------------------------------------------------------------------------------------------------
data.h
#ifndef __DATA_H
#define __DATA_H
#include "basic.h"
//数据类
class Data:public Basic
{
public:
virtual void enter();
virtual void show();
double Total_grade();
double
Average_scores();
private:
double c;
double math;
double english;
double sport;
};
#endif
-------------------------------------------------------------------------------------------------------
data.cpp
#include "data.h"
#include
using namespace std;
void Data::enter() //成绩输入
{
Basic::enter();
cout << "**************录入成绩**************" <<
endl;
cout << "C++ 语言:";
cin >> c;
cout << "高等数学:";
cin >> math;
cout << "大学英语:";
cin >> english;
cout << "体育健康:";
cin >> sport;
}
void Data::show() //成绩显示
{
Basic::show();
cout << "************成绩*************" << endl;
cout << "C++ 语言:" << c << endl;
cout << "高等数学:" << math << endl;
cout << "大学英语:" << english << endl;
cout << "体育健康:" << sport <
cout << "总 成 绩:" << Total_grade() <
cout << "平均成绩:" << Average_scores()
<
cout << "—————————————————————————————" << endl;
}
double Data::Total_grade() //计算总成绩
{
return (c + math + english + sport);
}
double Data::Average_scores()//计算平均成绩
{
return (Total_grade()/4.0);
}
-------------------------------------------------------------------------------------------------------
student.h
#ifndef __STUDENT_H
#define __STUDENT_H
#include "data.h"
//学生类
class Student
{
public:
void Look();
void Print();
private:
Data people_one;
};
#endif
-------------------------------------------------------------------------------------------------------
student.cpp
#include "student.h"
#include
#include
using namespace std;
void Student::Look() //学生显示信息
{
ifstream infile("file.txt",ios::binary);
if(infile.fail())
{
cout << "打开文件失败!" <
exit(1);
}
cout << "\n\t\t\t********显示学生信息********" <
infile.clear();
infile.seekg(0,ios::beg);
while(infile.read(reinterpret_cast(&people_one),sizeof(Data))
!= 0)
{
people_one.show();
}
infile.close();
}
void Student::Print() //学生查询信息
{
int n;
char ch;
char str_num[20],str_name[20];
cout << "\n\t\t\t********查询学生信息********" <
ifstream infile("file.txt",ios::binary);
if(infile.fail())
{
cout << "打开文件失败!" <
exit(1);
}
do{
cout << "请选择:";
cout << "1.按学号查找 2.按姓名查找" <<
endl;
cin >> n;
infile.clear();
infile.seekg(0,ios::beg); //文件指针定位到开头
if(n == 1) //按编号查询Grade的信息
{
cout << "输入学号: ";
cin >> str_num;
while(infile.read((char *)(&people_one),sizeof(Data))
!=0)
{
if(strcmp(str_num,people_one.num) == 0)
{
people_one.show();
}
}
}
else if(n == 2) //按姓名查询Grade的信息
{
cout << "输入姓名: ";
cin >> str_name;
while(infile.read(reinterpret_cast(&people_one),sizeof(Data))
!= 0)
{
if(strcmp(str_name,people_one.name) == 0)
{
people_one.show();
}
}
}
cout << "\n是否继续此操作[Y/y]: ";
cin >> ch;
}while(ch =='Y' || ch == 'y');
infile.close();
}
-------------------------------------------------------------------------------------------------------
teacher.h
#ifndef __TEACHER_H
#define __TEACHER_H
#include "data.h"
//教师类
class Teacher
{
public:
void Add();
void Find();
void Modify();
void Delete();
void See();
private:
Data people;
};
#endif
-------------------------------------------------------------------------------------------------------
teacher.cpp
#include "teacher.h"
#include
#include
using namespace std;
void Teacher::Add() //教师录入信息
{
char ch;
ofstream openfile("file.txt",ios::binary | ios::app);
//文件写入操作
if( openfile.fail() )
{
cout << "打开文件失败!" <
exit(1);
}
cout << "***********增加学生信息*************" <
do{
people.enter();
openfile.write(reinterpret_cast(&people),sizeof(Data));
//强制类型转换成char*写入
cout << "\n是否继续此操作[Y/y]: ";
cin >> ch;
}while(ch == 'Y' || ch == 'y');
openfile.close();
}
void Teacher::See() //教师显示信息
{
ifstream infile("file.txt",ios::binary);//文件读取操作
if(infile.fail())
{
cout << "打开文件失败!" <
exit(1);
}
cout << "\n\t\t\t********显示学生信息********" <
infile.clear();
infile.seekg(0,ios::beg);
while(infile.read(reinterpret_cast(&people),sizeof(Data)) !=
0)
{
people.show();
}
infile.close();
}
void Teacher::Find() //教师查询信息
{
int n;
char ch;
char str_num[20],str_name[20];
cout << "\n\t\t\t********查询学生信息********" <
ifstream infile("file.txt",ios::binary);
if(infile.fail())
{
cout << "打开文件失败!" <
exit(1);
}
do{
cout << "请选择:";
cout << "1.按学号查找 2.按姓名查找" <<
endl;
cin >> n;
infile.clear();
infile.seekg(0,ios::beg); //文件指针定位到开头
if(n == 1) //按编号查询Grade的信息
{
cout << "输入学号: ";
cin >> str_num;
while(infile.read((char *)(&people),sizeof(Data)) !=0)
{
if(strcmp(str_num,people.num) == 0) //数据比较
{
people.show();
}
}
}
else if(n == 2) //按姓名查询Grade的信息
{
cout << "输入姓名: ";
cin >> str_name;
while(infile.read(reinterpret_cast(&people),sizeof(Data)) !=
0)
{
if(strcmp(str_name,people.name) == 0)
{
people.show();
}
}
}
cout << "\n是否继续此操作[Y/y]: ";
cin >> ch;
}while(ch =='Y' || ch == 'y');
infile.close();
}
void Teacher::Modify()
//教师修改信息
{
char ch;
char str_num[15];
bool pass;
do
{
cout << "\n\t\t\t********修改学生信息********" <
ofstream openfile("temp.txt",ios::binary);
ifstream infile("file.txt",ios::binary);
if(infile.fail())
{
cout << "打开文件失败" << endl;
exit(1);
}
if(openfile.fail())
{
cout << "打开文件失败" << endl;
exit(1);
}
cout << "输入学号:";
cin >> str_num;
infile.clear();
infile.seekg(0,ios::beg); //文件定位到最前面
while(infile.read(reinterpret_cast(&people),sizeof(Data))!=0)
{
if(strcmp(str_num,people.num) == 0)
{
people.show();
cout << "...............修改..............."
<
cout << endl;
people.enter();
pass = true;
}
openfile.write(reinterpret_cast(&people),sizeof(Data));
}
infile.close();
openfile.close();
if(pass == true)
{
cout << "修改成功!" << endl;
remove("file.txt");
rename("temp.txt","file.txt");
pass = false;
}
else
{
cout << "没找到你输入的信息!" << endl;
}
cout << "\n是否继续此操作[Y/y]:";
cin >> ch;
}while(ch == 'y' || ch == 'Y');
}
void Teacher::Delete() //教师删除信息
{
char ch;
char str_num[15];
bool pass;
do
{
cout << "\n\t\t\t********删除学生信息********" <
ofstream openfile("temp.txt",ios::binary);
ifstream infile("file.txt",ios::binary);
if(infile.fail())
{
cout << "打开文件失败" << endl;
exit(1);
}
if(openfile.fail())
{
cout << "打开文件失败" << endl;
exit(1);
}
cout << "输入学号:";
cin >> str_num;
infile.clear();
infile.seekg(0,ios::beg); //文件定位到最前面
while(infile.read(reinterpret_cast(&people),sizeof(Data))!=0)
{
if(strcmp(str_num,people.num) == 0)
{
people.show();
cout << "正在删除。。。" <
cout << endl;
pass = true;
}
else
{
openfile.write(reinterpret_cast(&people),sizeof(Data));
}
}
infile.close();
openfile.close();
if(pass == true)
{
cout << "删除成功!" << endl;
remove("file.txt");
rename("temp.txt","file.txt");
pass = false;
}
else
{
cout << "没找到你输入的信息!" << endl;
}
cout << "\n是否跳过此操作[Y/y]:";
cin >> ch;
}while(ch == 'y' || ch == 'Y');
}
-------------------------------------------------------------------------------------------------------
main.cpp
#include "teacher.h"
#include "student.h"
#include "begin.h"
#include
using namespace std;
int main()
{
Loop:
Begin();
char n;
cout << "\n\t登录类型[1~2][ ]\b\b";
cin >> n;
if (n == '1')
{
Teacher tp;
while(1)
{
switch(List())
{
case '1' :tp.Add();break;
case '2' :tp.See();break;
case '3' :tp.Find();break;
case '4' :tp.Modify();break;
case '5' :tp.Delete();break;
case '0' : goto Loop;break;
default:
cout << "\t\t—————————————————————————————————————————"
<< endl;
cout <
cout << "\t\t—————————————————————————————————————————"
<< endl;
}
}
}
else if (n == '2')
{
Student sp;
while(1)
{
switch(Choose())
{
case '1' :sp.Look();break;
case '2' :sp.Print();break;
case '0' : goto Loop;break;
default:
cout << "\t\t—————————————————————————————————————————"
<< endl;
cout <
cout << "\t\t—————————————————————————————————————————"
<< endl;
}
}
}
else
{
cout << "\t\t\t**********\n";
cout << "\t\t\t*Bey Bey!*\n";
cout << "\t\t\t**********\n";
exit(0);
}
return 0;
}
-------------------------------------------------------------------------------------------------------
运行截图: