当前位置:文档之家› C++学生管理系统课程设计 源代码

C++学生管理系统课程设计 源代码

//===================================================================== ============================================//student.h 定义了抽象类Person,派生了student(及为信息类)、定义了节点类Node、定义了链表类List//===================================================================== ============================================#ifndef STUDATA_H#define STUDATA_H#include <fstream.h>//================================================================int GetID();class List;//-------------------------------------class Person{ //抽象类protected:int ID; //学号char Name[15]; //姓名public:virtual bool operator<(Person&)=0; //<运算符重载virtual bool operator>=(Person&)=0; //>=运算符重载};//---------------------------------------------------------------------------------------class Student:public Person{ //学生类int Score[4]; //成绩void SwapData(Student*); //交换数据public:Student(int =0,char* =NULL,int* =NULL); //构造函数bool operator<(Person&); //<运算符重载bool operator>=(Person&); //>=运算符重载friend ofstream&operator<<(ofstream&,Student*); //<<运算符重载输出到文件friend ifstream&operator>>(ifstream&,Student*); //>>运算符重载从文件输入friend ostream_withassign& operator<<(ostream_withassign&,Student*); //<<运算符重载输出到显示器friend istream_withassign& operator>>(istream_withassign&,Student*); //>>运算符重载从键盘输入friend class List;};//---------------------------------------------------------------------------------------------------------------- class Node{ //结点类Student *Stup; //指向学生对象指针Node *Prev,*Next; //指向前后结点指针public:Node(); //构造函数Node(Student*); //构造函数Node(Node&); //拷贝构造函数~Node(); //析构函数friend class List;};//----------------------------------------------------------------------------------------------------------------- class List{Node *Head,*Tail; //链表头尾指针void InitList(); //初始化函数public:List(); //构造函数~List(); //析构函数void ClearList(); //清空链表Node *FindID(int&); //根据学号寻找学生所在结点指针型函数void Insert(Node*); //插入一个学生Node *CreateNode(Student*); //建立一个学生结点指针型函数void Add(); //增加一个学生void Update(); //修改学生成绩void Delete(); //删除一个学生void Query(); //查找一个学生void SortID(); //按学号排序void SortScore(); //按学生总成绩排序void SaveBin(); //以二进制形式保存数据void PrintList(); //显示链表void PrintTitle(); //显示标题void PrintOne(Student*); //显示一个学生数据};#endif//===================================================================== =====//student.cpp的实现//===================================================================== =====#include <iostream.h>#include <iomanip.h>#include <string.h>#include "student.h"//===================================================================== =====Student::Student(int id,char *name,int *score){ //构造函数ID=id;if(name!=NULL)strcpy(Name,name);if(score!=NULL)for(int i=0;i<=3;i++)Score[i]=score[i];}bool Student::operator<(Person& per){Student &stu=(Student&)per;return (Score[3]<stu.Score[3]);}bool Student::operator>=(Person& per){return (!operator<(per));//return (Score[3]>=stu.Score[3])}void Student::SwapData(Student* stup){ //学生数据交换this指针指向调用成员函数的对象Student t=*this;*this=*stup;*stup=t;}ifstream& operator>>(ifstream& in,Student* stup){ //>>运算符重载从文件输入in.read((char*)stup,sizeof(Student)); //从相应的流中提取sizeof(Student)个字节,并把它们放入stup所指的缓冲区中return in;}ofstream& operator<<(ofstream& out,Student* stup){ //<<运算符重载输出至文件out.write((char*)stup,sizeof(Student)); //从stup所指的缓冲区把sizeof(Student)个字节插入到相应的流上return out;}istream_withassign& operator>>(istream_withassign& in,Student *stup){ //>>运算符重载从标准文件输入int i;cout<<"Input Name,Score1-Score3"<<endl;cout<<"Name=";in>>stup->Name;stup->Score[3]=0;for(i=0;i<3;i++){cout<<"Score"<<i<<"=";in>>stup->Score[i];stup->Score[3]+=stup->Score[i];}return in; //输入流对象in返回的到调用处}ostream_withassign& operator<<(ostream_withassign& out,Student *stup){ //<<运算符重载输出至标准文件out<<stup->ID<<setw(8)<<stup->Name<<setw(8)<<stup->Score[0]<<setw(10)<<stup->Score[1]<<setw(10)<<stup->Score[2]<<setw(10)<<stup->Score[3]<<endl;out<<endl;return out; //输处流对象out返回的调用处/*int i;out<<stup->ID<<"\t"<<stup->Name<<"\t" ;for (i=0;i<4;i++){out<<stup->Score[i]<<"\t";}out<<endl;return out; */}//================================================================//node.cpp的实现//================================================================#include<iostream.h>#include"student.h"//---------------------------------------------------------------Node::Node(){Stup=NULL;Prev=Next=NULL;}Node::Node(Student *stup){//默认的构造函数,完成建立一个空节点Stup=stup;Prev=Next=NULL;}Node::Node(Node &node){ //复制构造函数的实现OKStup=node.Stup;Prev=node.Prev;Next=node.Next;}Node::~Node(){ //释放空间delete Stup;}//===================================================================== ============================//list.cpp的实现//===================================================================== ===========================#include <iostream>#include <string>#include "student.h"//===================================================================== ============================List::List(){ //构造函数Head=Tail=new Node(); //建立链表头结点Head->Next=NULL;Head->Prev=NULL;InitList(); //从文件读取数据在下面调用此函数}List::~List(){ //析构函数ClearList(); //在下面调用此函数delete Head; //释放空间}void List::InitList() //从文件读取数据{ifstream inStudent;ofstream outStudent;inStudent.open("gx.dat",ios::binary); //打开学生文件if(!inStudent) //文件不存在,建立该文件{inStudent.close();outStudent.open("gx.dat",ios::binary);outStudent.close(); //关闭文件}else //文件存在,读取学生数据{Student *stup=new Student();Node *nodep;inStudent>>stup;while(!inStudent.eof()){ //未达到文件尾端则执行循环,否则退出循环nodep=CreateNode(stup); //调用GreateNode()函数建立一个学生结点指针型函数Insert(nodep);stup=new Student();inStudent>>stup;}delete stup;}inStudent.close();}void List::Add(){ //增加一个学生Student *stup;Node *nodep;int id=GetID();if((nodep=FindID(id))!=NULL)throw(1); //输入学号相同,抛出异常stup=new Student();stup->ID=id;cin>>stup; //>>运算符重载nodep=CreateNode(stup);Insert(nodep);PrintTitle();PrintOne(stup);}void List::Update(){ //修改学生成绩int id=GetID();Node *nodep;if (FindID(id)!=NULL)nodep=FindID(id);else{cout<<"该学生不存在!";return ;}nodep->Stup->Score[3]=0;for(int i=0;i<3;i++){cout<<"score"<<i<<"=";cin>>nodep->Stup->Score[i];nodep->Stup->Score[3]+=nodep->Stup->Score[i];}PrintTitle();PrintOne(nodep->Stup);}void List::Delete(){ //删除一个学生int id=GetID();Node *nodep;if ((nodep=FindID(id))!=NULL){nodep->Prev->Next=nodep->Next;if(nodep==Tail)Tail=nodep->Prev;elsenodep->Next->Prev=nodep->Prev;delete nodep;}PrintList();}void List::Query(){ //查询学生数据int id=GetID();Node *nodep;if (FindID(id)!=NULL){nodep=FindID(id);PrintTitle();PrintOne(nodep->Stup);}else{cout<<"该学生不存在!";return ;}}void List::ClearList(){ //清空链表Node*nodep;while(Head->Next!=NULL){nodep=Head->Next;Head->Next=Head->Next->Next;delete nodep;}Tail=Head;}Node*List::FindID(int& id){ //查找学号Node *nodep=Head->Next;while(nodep!=NULL&&nodep->Stup->ID!=id)nodep=nodep->Next;return nodep;}void List::Insert(Node *nodep){ //在链表尾插入一个学生*****我有问题*********Tail->Next=nodep;nodep->Prev=Tail;Tail=nodep;}void List::SortID(){ //按学号排序*******我有问题*********Node *nodef,*nodel=Tail;if(Head!=Tail){while(Head->Next!=nodel){nodef=Head->Next;while(nodef!=nodel){if(nodef->Stup->ID>nodef->Next->Stup->ID)nodef->Stup->SwapData(nodef->Next->Stup);nodef=nodef->Next;}nodel=nodel->Prev;}PrintList();}}void List::SortScore(){ //按总成绩排序//学生设计Node*nodef,*nodel=Tail;if(Head!=Tail){while(Head->Next!=nodel){nodef=Head->Next;while(nodef!=nodel){if(nodef->Stup->Score[3]<nodef->Next->Stup->Score[3])nodef->Stup->SwapData(nodef->Next->Stup);nodef=nodef->Next;}nodel=nodel->Prev;}PrintList();}}void List::SaveBin(){ //链表数据保存在二进制文件ofstream outStudent;outStudent.open("gx.dat",ios::binary);Node *nodep=Head->Next;while(nodep!=NULL){outStudent<<nodep->Stup;nodep=nodep->Next;}outStudent.close();}Node*List::CreateNode(Student*stup){ //建立一个学生对象Node *nodep=new Node(stup);return nodep;}void List::PrintList(){ //显示链表中数据Node *nodep=Head->Next ;if(Head!=Tail){//学生设计PrintTitle();while(nodep!=NULL){PrintOne(nodep->Stup);nodep=nodep->Next;}cout<<endl;}elsecout<<"No student"<<endl;}void List::PrintTitle(){cout<<"ID Name Score1 Score2 Score3 Score"<<endl;}void List::PrintOne(Student*stup){cout<<stup; //<<运算符重载}//===================================================================== =============//main.cpp主函数的实现//===================================================================== ============#include <iostream.h>#include "student.h"//===================================================================== =============int EnterChoice(); //输入选项int GetID(); //输入学号enum Choices{ADD=1,UPDATE,DELETE,QUERY,DISPLAY,SORTID,SORTSCORE,END};void main(){List ls;int choice;while((choice=EnterChoice())!=END){switch(choice){case ADD:try{ls.Add();}catch(int){cout<<"Wrong!This ID is equal to other ID"<<endl;}break;case UPDA TE: //修改学生成绩ls.Update();break;case DELETE: //删除一个学生ls.Delete();break;case QUERY: //查询一个学生数据ls.Query();break;case DISPLAY: //显示全部学生数据ls.PrintList();break;case SORTID: //根据学号排序ls.SortID();break;case SORTSCORE: //根据总成绩排序ls.SortScore();default:cerr<<"Incorrect choice\n";}}ls.SaveBin(); //保存学生数据}int EnterChoice(){cout<<"\nEnter your Choice\n"<<"1:Add student 2:Update student\n"<<"3:Delete student 4:Query student\n"<<"5:Display students 6:Sort student by ID\n"<<"7:Sort student by score 8:End\n";int menuChoice;cin>>menuChoice;return menuChoice;}int GetID() //输入学号{int id;do{cout<<"Input student's ID(>0)"<<endl;cin>>id;}while (id<=0);return id;}。

相关主题