1. 程式人生 > 程式設計 >C++實現學生資訊管理系統

C++實現學生資訊管理系統

C++的課設作業,分享出來,希望給初學者一些幫助。

該系統主要用到C++的繼承,虛擬函式。所有學生的資訊儲存在一個異質連結串列中,並且連結串列也進行了封裝

課設題目

設計一個學生管理系統,要求如下:

1、學生來自英語系,數學系、計算機系:

a) 每個學生的公共資訊部分包括:學號、姓名、年齡、系別,高數成績、英語成績、體育成績;
b) 英語系學生有綜合英語、口語等課程成績;
c) 數學系學生有實變函式、泛函分析、微分幾何等課程成績;
d) 計算機系學生有計算機組成原理、計算機體系結構、組合語言等課程成績。

每個學生可以查詢相應資訊(學號、姓名、年齡、系別及成績資訊);可以查詢各門課程的平均成績。

用類及類的派生實現上述功能,要求使用類的繼承、虛擬函式、虛基類的C++語言特性;要求每個類採用不同的方式實現建構函式;

2、設計一個學生管理類,其中包含可變學生數目,要求實現:

1)初始化,所有學生的資訊組織以連結串列方式實現;
2)實現指定學生的資訊查詢(如利用學號、姓名等方式);
3)按照公共課平均成績排序;
4)按照學生所有課程的平均成績排序;
5)增加學生;
6)刪除指定學生。

3、注意建構函式和解構函式的實現方式。每個類的申明需放在單獨的標頭檔案中(即一個類的申明放在一個字尾名為h的檔案中),每個類的實現放在一個單獨的cpp檔案中。測試程式(即包含main函式的程式)單獨放在一個cpp檔案中。

整體分析

根據題目要求該學生資訊管理系統可分為五個功能模組:

1、 資訊讀取及輸出功能:從檔案中讀取學生資訊
將學生資訊列印到螢幕上

2、 資訊維護功能:增加學生資訊
刪除學生資訊

3、 資訊查詢功能:根據學號查詢學生資訊
根據姓名查詢學生資訊

4、 成績統計功能:計算各學科的平均分

5、 排序功能:按照公共課平均成績進行排序
按照全部課程的平均成績進行排序
為實現以上功能,使用了類的繼承,虛擬函式的C++語言特性

共使用五個類:

1、 Student類:包含學生共同資訊的類,共派生出三個類
2、 Csstudent類:計算機系學生類,由Student類派生而來
3、 Enstudent類:英語系學生類,由Student類派生而來

4、Mstudent類:數學系學生類,由Student類派生而來
4、 List類:連結串列類,包含對學生資訊連結串列的各種操作

共使用到13個檔案:<

1、 menu.cpp:內含多個建立選單函式,用於實現不同模組的功能顯示
2、 main.cpp:呼叫所有的函式,並進行適當的組合實現完整的學生成績管理系統
3、 student.h:包含用於儲存學生公共資訊及相關操作函式的Student類
4、 student.cpp:包含Student類的成員函式的實現
5、 csstudent.h:包含用於儲存計算機系學生資訊及相關操作的Csstudent類
6、 csstudent.cpp:包含Csstudent類的成員函式的實現
7、 enstudent.h:包含用於儲存英語系學生資訊及相關操作的Enstudent類
8、 enstudent.cpp:包含Enstudent類的成員函式的實現
9、 mstudent.h:包含用於儲存數學系學生資訊及相關操作的Mstudent類
10、 mstudent.cpp:包含Mstudent類的成員函式的實現
11、 list.h:包含用於操作學生資訊的的List類
12、 list.cpp:包含List類的成員函式的實現
13、 student.txt:包含學生的全部資訊

多個類的繼承關係圖如下:

C++實現學生資訊管理系統

原始碼

main.cpp

#include<iostream>
using namespace std;
//主介面
void menu()
{
 cout<<"\n\n\t\t----------------學生成績管理系統------------------"<<endl<<endl;
 cout<<"\t\t1、資訊維護功能"<<" "<<"\t2、資訊查詢功能"<<endl<<endl;
 cout<<"\t\t3、成績統計功能"<<" "<<"\t4、排序功能"<<endl<<endl;
 cout<<"\t\t5、退出"<<endl<<endl;
 return;
}

 
//資訊維護功能介面
void menu1()
{
 cout<<"\n\n\t\t資訊維護功能"<<endl<<endl;
 cout<<"\t\t1、增加學生資訊"<<endl<<endl;
 cout<<"\t\t2、刪除學生資訊"<<endl<<endl;
 cout<<"\t\t3、返回"<<endl<<endl;
 return;
}

//資訊查詢功能介面
void menu2()
{
 cout<<"\n\n\t\t資訊查詢功能"<<endl<<endl;
 cout<<"\t\t1、按姓名查詢"<<endl<<endl;
 cout<<"\t\t2、按學號查詢"<<endl<<endl;
 cout<<"\t\t3、返回"<<endl<<endl;
 return;
}

//成績統計功能介面
void menu3()
{
 cout<<"\n\n\t\t成績統計功能"<<endl<<endl;
 cout<<"\t\t1、各科目的平均成績"<<endl<<endl;
 cout<<"\t\t2、返回"<<endl<<endl;
 return;
}


//排序功能介面
void menu4()
{
 cout<<"\n\n\t\t排序功能"<<endl<<endl;
 cout<<"\t\t1、根據公共課平均成績排序"<<endl<<endl;
 cout<<"\t\t2、根據全部科目平均成績排序"<<endl<<endl;
 cout<<"\t\t3、返回"<<endl<<endl;
 return;
}

main.cpp

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include"student.h"
#include"csstudent.h"
#include"list.h"
using namespace std;

void menu();
void menu1();
void menu2();
void menu3();
void menu4();

int main(void)
{

 int choice,choice1,choice2,choice3,choice4;
 List pHead;
 string subj;
 pHead.input_info();

 while(1)
 {
 menu();
 cout<<"請選擇你要進行的操作:";
 cin>>choice;
 switch(choice)
 {
 //資訊維護功能
 case 1:
 while(1)
 {
  pHead.input_info();
  system("cls");
  menu1();
  cout<<"請選擇你要進行的操作:";
  cin>>choice1;
  switch(choice1)
  {
  //增加學生資訊
  case 1:
  pHead.output_info();//遍歷連結串列,並列印學生的資訊
  pHead.add_info();//增加學生資訊
  system("cls");
  cout<<"\n\n\n\t\t學生成績資訊表(更新)"<<endl;
  pHead.output_info();//遍歷連結串列,並列印學生的資訊
  pHead.save_list();//將改動儲存到檔案中
  cout<<"\n改動已儲存到檔案中"<<endl<<endl;
  system("pause");
  break;
  //刪除學生資訊
  case 2:
  pHead.output_info();//遍歷連結串列,並列印學生的資訊
  pHead.delete_info();//刪除學生資訊
  system("cls");
  cout<<"\n\n\n\t\t學生成績資訊表(更新)"<<endl;
  pHead.output_info();//遍歷連結串列,並列印學生的資訊
  pHead.save_list();//將改動儲存到檔案中
  cout<<"\n改動已儲存到檔案中"<<endl<<endl;
  system("pause");
  break;
  default:
  system("cls");
  break;
  }
  if(choice1!=1&&choice1!=2)
  break;
 }
 break;

 //資訊查詢功能
 case 2:
 while(1)
 {
  system("cls");
  menu2();
  cout<<"請選擇你要進行的操作:";
  cin>>choice2;
  switch(choice2)
  {
  //按姓名進行查詢
  case 1:
  pHead.search_by_name();
  system("pause");
  break;
  //按學號進行查詢
  case 2:
  pHead.search_by_ID();
  system("pause");
  //system("cls");
  break;
  default:
  system("cls");
  break;
  }
  if(choice2!=1&&choice2!=2)
  break;
  //break;
 }
 break;

 //成績統計功能
 case 3:
 while(1)
 {
  system("cls");
  menu3();
  cout<<"請選擇你要進行的操作:";
  cin>>choice3;
  switch(choice3)
  {
  //計算各科目的平均成績
  case 1:
  pHead.count_sub_avg();//計算各學科的平均成績
  system("pause");
  break;
  default:
  system("cls");
  break;
  }
  if(choice3!=1)
  break;
 }
 break;

 //排序功能
 case 4:
 while(1)
 {
  system("cls");
  menu4();
  cout<<"請選擇你要進行的操作:";
  cin>>choice4;
  switch(choice4)
  {
  //根據公共課平均成績排序
  case 1:
  pHead.sort_by_public();
  pHead.output_info();
  cout<<endl;
  system("pause");
  break;
  case 2:
  //根據全部科目平均成績排序
  pHead.sort_by_all();
  pHead.output_info();
  cout<<endl;
  system("pause");
  break;
  default:
  system("cls");
  break;
  }
  if(choice4!=1&&choice4!=2)
  break;
 }
 break;
 default:
 return 0;
 }
 }
 pHead.delete_list();
 return 0;
}

(三)student.h

#ifndef _STUDENT_H
#define _STUDENT_H
#include<iostream>
#include<cstring>
#include<sstream>
#include<fstream>
using namespace std;
class Student
{
private:
 //共同的學生資訊
 int num;//學生的編號
 string stuID;//學生的學號
 string name;//學生的姓名
 int age;//學生的年齡
 string department;//學生所屬學院
 //共有的科目成績
 double math;//高數成績
 double english;//英語成績
 double PE;//體育成績
 double sum_part;//總分
 double average_part;//公共成績的平均分
public:
 Student(){}//無引數的建構函式
 Student(string _name,string _stuID,int _age,string _department,double _math,double _english,double _pe);//含引數的建構函式
 Student(const Student& stu);//拷貝建構函式
 virtual ~Student(){}//解構函式
public:
 //設定學生資訊相關函式
 void set_num(int n);//設定學生的編號
 void set_stuID(string ID);//設定學生的學號
 void set_name(string na);//設定學生的姓名
 void set_age(int a);//設定學生的年齡
 void set_department(string dept);//設定學生所屬的學院
 void set_scores_part(double m,double en,double pe);//設定學生的各科成績

 virtual void count_sum();//計算公共科目的總分
 virtual void count_average();//計算公共科目的平均分

 //獲取學生資訊相關函式
 int get_num();//獲取學生的編號
 string get_stuID();//獲得學生的學號
 string get_name();//獲得學生的姓名
 int get_age();//獲得學生的年齡
 string get_department();//獲得學生的所屬學院
 virtual void get_scores(double *m,double *en,double *pe);//獲得學生的公共科目的各項成績
 virtual double get_sum();//獲得公共成績的總分
 virtual double get_average();//獲得公共成績的平均分

 virtual void display_info();//將學生資訊列印到螢幕上
 virtual void save_node(ofstream& fout);//將學生資訊儲存到檔案中

};
#endif // _STUDENT_H

student.cpp

#include<iostream>
#include<cstring>
#include<sstream>
#include"student.h"
#include<fstream>
using namespace std;
//含引數的建構函式
Student::Student(string _name,double _pe)
{
 stuID=_stuID;
 name=_name;
 age=_age;
 department=_department;
 math=_math;
 english=_english;
 PE=_pe;
}
//拷貝建構函式
Student::Student(const Student& stu)
{
 stuID=stu.stuID;
 name=stu.name;
 age=stu.age;
 department=stu.department;
 math=stu.math;
 english=stu.english;
 PE=stu.PE;
}
//設定學生的編號
void Student::set_num(int n)
{
 num=n;
}
//設定學生的學號
void Student::set_stuID(string ID)
{
 stuID=ID;
}
//設定學生的姓名
void Student::set_name(string na)
{
 name=na;
}
//設定學生的年齡
void Student::set_age(int a)
{
 age=a;
}
//設定學生所屬的學院
void Student::set_department(string dept)
{
 department=dept;
}
//設定學生的各科成績
void Student::set_scores_part(double m,double pe=0)
{
 math=m;
 english=en;
 PE=pe;
}
//計算公共科目的總分
void Student::count_sum()
{
 sum_part=math+english+PE;
}
//計算公共科目的平均分
void Student::count_average()
{
 average_part=(math+english+PE)/3;
}

//獲取學生的編號
int Student::get_num()
{
 return num;
}
//獲得學生的學號
string Student::get_stuID()
{
 return stuID;
}
//獲得學生的姓名
string Student::get_name()
{
 return name;
}
//獲得學生的年齡
int Student::get_age()
{
 return age;
}
//獲得學生的所屬學院
string Student::get_department()
{
 return department;
}
//獲得學生的各項成績
void Student::get_scores(double *m,double *pe)
{
 *m=math;
 *en=english;
 *pe=PE;
}
//獲取學生的編號
double Student::get_sum()
{
 return sum_part;
}
//獲得公共成績的平均分
double Student::get_average()
{
 return average_part;
}
//將學生資訊列印到螢幕上
void Student::display_info()
{
 cout<<num<<'\t'<<name<<'\t'<<stuID<<'\t'<<age<<'\t'<<department<<"\t\t"<<math<<'\t'<<english<<'\t'<<PE<<'\t'<<average_part<<'\t';
}
//將學生資訊儲存到檔案中
void Student::save_node(ofstream& fout)
{
 fout<<name<<' '<<stuID<<' '<<age<<' '<<department<<' '<<math<<' '<<english<<' '<<PE<<' ';
}

csstudent.h

#ifndef _CSSTUDENT_H
#define _CSSTUDENT_H
#include<iostream>
#include<cstring>
#include<sstream>
#include<fstream>
#include"student.h"
using namespace std;
class Csstudent:public Student
{
public:
 Csstudent(){}//無引數的建構函式
 //帶初始化列表的建構函式
 Csstudent(string _name,double _pe,double c_c,double c_a,double ass):
 Student(_name,_stuID,_age,_department,_math,_english,_pe)
 {
 c_composition=c_c;
 c_architecture=c_a;
 assembly_lan=ass;
 }

 virtual ~Csstudent(){}//解構函式
public:
 //設定學生資訊相關函式
 virtual void count_sum();//計算總分
 virtual void count_average();//計算平均分

 //獲取學生資訊相關函式
 virtual double get_sum();//獲得總分
 virtual double get_average();//獲得平均分
 virtual void get_scores(double* c_c,double*c_a,double *ass);//獲取學生的專業成績
public:
 virtual void display_info();//將學生資訊列印到螢幕上
 virtual void save_node(ofstream& fout);//將學生資訊儲存到檔案中
private:
 double sum;//全部科目的總分
 double average;//全部科目的平均分
 double c_composition;//計算機組成原理成績
 double c_architecture;//計算機體系結構成績
 double assembly_lan;//組合語言成績
};
#endif

csstudent.cpp

#include<iostream>
#include<cstring>
#include<sstream>
#include<fstream>
#include"student.h"
#include"csstudent.h"

//計算所有科目的總分
void Csstudent::count_sum()
{
 double part=Student::get_sum();
 sum=part+c_composition+c_architecture+assembly_lan;
}
//計算全部科目的平均分
void Csstudent::count_average()
{
 average=sum/6;
}

//獲得學生全部科目的總分
double Csstudent::get_sum()
{
 return sum;
}
//獲得學生全部科目的平均分
double Csstudent::get_average()
{
 return average;
}
//獲取學生的專業成績
void Csstudent::get_scores(double* c_c,double *ass)
{
 *c_c=c_composition;
 *c_a=c_architecture;
 *ass=assembly_lan;
}
//將學生資訊列印到螢幕上
void Csstudent::display_info()
{
 Student::display_info();
 cout<<c_composition<<'\t'<<c_architecture<<'\t'<<assembly_lan<<'\t'<<average<<endl;
}
//將學生資訊儲存到檔案中
void Csstudent::save_node(ofstream& fout)
{
 Student::save_node(fout);
 fout<<c_composition<<' '<<c_architecture<<' '<<assembly_lan<<endl;
}

enstudent.h

#ifndef _ENSTUDENT_H
#define _ENSTUDENT_H
#include<iostream>
#include<cstring>
#include<sstream>
#include<fstream>
#include"student.h"
using namespace std;
class Enstudent:public Student
{
public:
 Enstudent(){}//不含引數的建構函式
 //帶初始化列表的建構函式
 Enstudent(string _name,double c_e,double s_e):
 Student(_name,_pe)
 {
 comprehensive_en=c_e;
 spoken_en=s_e;
 }

 virtual ~Enstudent(){}//解構函式
public:
 //設定學生資訊相關函式
 virtual void count_sum();//計算總分
 virtual void count_average();//計算平均分

 //獲取學生資訊相關函式
 virtual double get_sum();//獲得總分
 virtual double get_average();//獲得平均分
 virtual void get_scores(double* c_e,double* s_e,double* no);//獲得學生的專業成績
public:
 virtual void display_info();//將學生資訊列印到螢幕上
 virtual void save_node(ofstream& fout);//將學生資訊儲存到檔案中
private:
 double sum;//全部科目的總分
 double average;//全部科目的平均分
 double comprehensive_en;//綜合英語成績
 double spoken_en;//口語成績
};
#endif
(八)enstudent.cpp
#include<iostream>
#include<cstring>
#include<sstream>
#include<fstream>
#include"student.h"
#include"enstudent.h"

//計算所有科目的總分
void Enstudent::count_sum()
{
 double part=Student::get_sum();
 sum=part+comprehensive_en+spoken_en;
}
//計算全部科目的平均分
void Enstudent::count_average()
{
 average=sum/5;
}

//獲得學生全部科目的總分
double Enstudent::get_sum()
{
 return sum;
}
//獲得學生全部科目的平均分
double Enstudent::get_average()
{
 return average;
}
//獲得學生的專業成績
void Enstudent::get_scores(double* c_e,double* no)
{
 *c_e=comprehensive_en;
 *s_e=spoken_en;
}
//將學生資訊列印到螢幕上
void Enstudent::display_info()
{
 Student::display_info();
 cout<<comprehensive_en<<'\t'<<spoken_en<<"\t\t"<<average<<endl;
}
//將學生資訊儲存到檔案中
void Enstudent::save_node(ofstream& fout)
{
 Student::save_node(fout);
 fout<<comprehensive_en<<' '<<spoken_en<<endl;
}

mstudent.h

#ifndef _MSTUDENT_H
#define _MSTUDENT_H
#include<iostream>
#include<cstring>
#include<sstream>
#include<fstream>
#include"student.h"
using namespace std;
class Mstudent:public Student
{
public:
 Mstudent(){}//不含引數的建構函式
 //帶初始化列表的建構函式
 Mstudent(string _name,double r_f,double f_a,double d_g):
 Student(_name,_pe)
 {
 real_variable_f=r_f;
 f_analysis=f_a;
 diff_geometry=d_g;
 }

 virtual ~Mstudent(){}//解構函式
public:
 //設定學生資訊相關函式
 virtual void count_sum();//計算總分
 virtual void count_average();//計算平均分

 //獲取學生資訊相關函式
 virtual double get_sum();//獲得總分
 virtual double get_average();//獲得平均分
 virtual void get_scores(double* r_f,double* f_a,double* d_g);//獲得學生的專業成績
public:
 virtual void display_info();//將學生資訊列印到螢幕上
 virtual void save_node(ofstream& fout);//將學生資訊儲存到檔案中
private:
 double sum;//全部科目的總分
 double average;//全部科目的平均分
 double real_variable_f;//實變函式成績
 double f_analysis;//泛函分析成績
 double diff_geometry;//微分幾何成績
};
#endif

mstudent.cpp

#include<iostream>
#include<cstring>
#include<sstream>
#include<fstream>
#include"student.h"
#include"mstudent.h"
//設定所有科目的總分

//計算所有科目的總分
void Mstudent::count_sum()
{
 double part=Student::get_sum();
 sum=part+real_variable_f+f_analysis+diff_geometry;
}
//計算全部科目的平均分
void Mstudent::count_average()
{
 average=sum/6;
}

//獲得學生全部科目的總分
double Mstudent::get_sum()
{
 return sum;
}
//獲得學生全部科目的平均分
double Mstudent::get_average()
{
 return average;
}
//獲得學生的專業成績
void Mstudent::get_scores(double* r_f,double* d_g)
{
 *r_f=real_variable_f;
 *f_a=f_analysis;
 *d_g=diff_geometry;
}
//將學生資訊列印到螢幕上
void Mstudent::display_info()
{
 Student::display_info();
 cout<<real_variable_f<<'\t'<<f_analysis<<'\t'<<diff_geometry<<'\t'<<average<<endl;
}
 
//將學生資訊儲存到檔案中
void Mstudent::save_node(ofstream& fout)
{
 Student::save_node(fout);
 fout<<real_variable_f<<' '<<f_analysis<<' '<<diff_geometry<<endl;
}

list.h

#ifndef _LIST_H
#define _LIST_H
#include<iostream>
#include"student.h"
#include"csstudent.h"
#include"enstudent.h"
#include"mstudent.h"
using namespace std;
class List
{
public:
 List(){}//無引數建構函式
public:
 void input_info();//從檔案匯入學生資訊,儲存到連結串列中
 void output_info();//將連結串列中的資料列印到螢幕上
 void count_sub_avg();//計算各個科目的平均分
public:
 void search_by_ID();//根據學號查詢學生資訊
 void search_by_name();//根據姓名查詢學生資訊
public:
 void sort_by_public();//根據專業課成績平均分進行排序
 void sort_by_all();//根據學生所有科目的平均分進行排序
public:
 void add_info();//增加學生資訊
 void delete_info();//刪除指定學生的資訊
 //void modify_info();//修改學生資訊
public:
 void save_list();//將連結串列儲存到檔案中
 void delete_list();//銷燬連結串列

private:
 List *pHead;//連結串列的頭指標
 Student *data;//指向學生資訊的指標
 List *pNext;//指向下一個結點的指標
};
 
#endif // _LIST_H

(十二)list.cpp

#include<iostream>
#include<fstream>
#include<stdlib.h>
#include<stdio.h>
#include<sstream>
#include"list.h"
#include"student.h"
#include"csstudent.h"
#include"enstudent.h"
#include"mstudent.h"
using namespace std;

//從檔案匯入學生資訊,儲存到連結串列中
void List::input_info()
{
 int num=0;//學生的編號
 string stuID;//學生的學號
 string name;//學生的姓名
 int age;//學生的年齡
 string department;//學生所屬學院

 double math;//高數成績
 double english;//英語成績
 double pe;//體育成績

 double c_c;//計算機組成原理成績
 double c_a;//計算機體系結構成績
 double ass;//組合語言成績

 double c_e;//綜合英語成績
 double s_e;//口語成績

 double r_f;//實變函式成績
 double f_a;//泛函分析成績
 double d_g;//微分幾何成績

 List *current=NULL;//指向當前結點的指標
 List *previous=NULL;//指向前一個結點的指標
 ifstream fin;
 fin.open("student.txt",ios::in);
 if(!fin)
 {
 cout<<"Fail to open the file!"<<endl;
 exit(0);
 }
 //建立連結串列
 while(1)
 {
 //判斷是否讀完檔案
 if(!(fin>>name>>stuID>>age>>department>>math>>english>>pe))
 break;
 else
 {
 ++num;
 //根據不同專業的學生 new 不同的物件,根據不同院系設定各科分數
 if(department=="計算機")
 {
 current=new List;//建立新結點
 fin>>c_c>>c_a>>ass;
 current->data=new Csstudent(name,stuID,age,department,math,english,pe,c_c,c_a,ass);
 }
 else if(department=="英語系")
 {
 current=new List;//建立新結點
 fin>>c_e>>s_e;
 current->data=new Enstudent(name,c_e,s_e);
 }
 else if(department=="數學系")
 {
 current=new List;//建立新結點
 fin>>r_f>>f_a>>d_g;
 current->data=new Mstudent(name,r_f,f_a,d_g);
 }
 else
 {
 cout<<"Don't have this department"<<endl;
 exit(0);
 }
 }

 if(previous==NULL)
 pHead=current;//儲存頭結點
 if(previous!=NULL)
 previous->pNext=current;//儲存前一個結點的地址

 current->data->set_num(num);
 current->data->Student::count_sum();//計算公共科目的總分
 current->data->Student::count_average();//計算公共科目的平均分
 current->data->count_sum();//計算全部科目的總分
 current->data->count_average();//計算全部科目的平均分

 current->pNext=NULL;
 previous=current;
 }
 fin.close();//關閉檔案
}
//將連結串列中的資料列印到螢幕上
void List::output_info()
{
 List *p=pHead;//儲存頭節點
 int num=0;//設定編號
 cout<<"編號\t"<<"姓名\t"<<"學號\t"<<"年齡\t"<<"系別\t\t"<<"數學\t"<<"英語\t"<<"體育\t"<<"平均分\t"<<"專業課1\t"<<"專業課2\t"<<"專業課3\t"<<"平均分\t"<<endl;
 while(p!=NULL)
 {
 ++num;
 p->data->set_num(num);
 p->data->display_info();
 p=p->pNext;
 }
}
//計算各個科目的平均分
void List::count_sub_avg()
{
 string stuID;//學生的學號
 string department;//學生所屬學院

 double math,pe;//高數成績,英語成績,體育成績
 double math_sum=0,english_sum=0,pe_sum=0;//高數、英語、體育總分
 double math_avg,english_avg,pe_avg;//高數、英語、體育平均分

 double c_c,ass;//計算機組成原理成績,計算機體系結構成績,組合語言成績
 double c_c_sum=0,c_a_sum=0,ass_sum=0;//計算機組成原理成績,計算機體系結構成績,組合語言總分
 double c_c_avg,c_a_avg,ass_avg;//計算機組成原理成績,計算機體系結構成績,組合語言平均分

 double c_e,s_e;//綜合英語成績,口語成績
 double c_e_sum=0,s_e_sum=0;//綜合英語成績,口語總分
 double c_e_avg,s_e_avg;//綜合英語成績,口語平均分

 double r_f,d_g;//實變函式成績,泛函分析成績,微分幾何成績
 double r_f_sum=0,f_a_sum=0,d_g_sum=0;//實變函式成績,微分幾何成績總分
 double r_f_avg,f_a_avg,d_g_avg;//實變函式成績,微分幾何成績平均分

 int n=0;//記錄所有學生的數量
 int cn=0;//記錄計算機系的學生數量
 int en=0;//記錄英語系的學生數量
 int mn=0;//記錄數學系的學生數量

 List* p=pHead;
 while(p!=NULL)
 {
 ++n;
 department=p->data->get_department();
 p->data->Student::get_scores(&math,&english,&pe);
 math_sum+=math;
 english_sum+=english;
 pe_sum+=pe;
 if(department=="計算機")
 {
 p->data->get_scores(&c_c,&c_a,&ass);
 c_c_sum+=c_c;
 c_a_sum+=c_a;
 ass_sum+=ass;
 ++cn;
 }
 else if(department=="英語系")
 {
 p->data->get_scores(&c_e,&s_e,NULL);
 c_e_sum+=c_e;
 s_e_sum+=s_e;
 ++en;
 }
 else if(department=="數學系")
 {
 p->data->get_scores(&r_f,&f_a,&d_g);
 r_f_sum+=r_f;
 f_a_sum+=f_a;
 d_g_sum+=d_g;
 ++mn;
 }
 else
 {
 cout<<"Don't have this department!"<<endl;
 exit(0);
 }
 p=p->pNext;
 }
 math_avg=math_sum/n;//數學平均成績
 english_avg=english_sum/n;
 pe_avg=pe_sum/n;

 c_c_avg=c_c_sum/cn;
 c_a_avg=c_a_sum/cn;
 ass_avg=ass_sum/cn;

 c_e_avg=c_e_sum/en;
 s_e_avg=s_e_sum/en;

 r_f_avg=r_f_sum/mn;
 f_a_avg=f_a_sum/mn;
 d_g_avg=d_g_sum/mn;
 cout<<"公共課平均成績:"<<endl;
 cout<<"數學\t"<<"英語\t"<<"體育"<<endl;
 cout<<math_avg<<'\t'<<english_avg<<'\t'<<pe_avg<<endl<<endl;

 cout<<"計算機系專業課平均成績:"<<endl;
 cout<<"計算機組成原理:"<<c_c_avg<<endl;
 cout<<"計算機體系結構:"<<c_a_avg<<endl;
 cout<<"組合語言:"<<ass_avg<<endl<<endl;

 cout<<"數學系專業課平均成績:"<<endl;
 cout<<"實變函式:"<<r_f_avg<<endl;
 cout<<"泛函分析:"<<f_a_avg<<endl;
 cout<<"微分幾何:"<<d_g_avg<<endl<<endl;

 cout<<"英語系專業課平均成績:"<<endl;
 cout<<"綜合英語:"<<c_e_avg<<endl;
 cout<<"英語口語:"<<s_e_avg<<endl;
}
//根據學號查詢學生資訊
void List::search_by_ID()
{
 List *p=pHead;//儲存頭節點
 int flag=0;//用來標記是否找到對應的學生
 string ID;//儲存學生的學號
 cout<<"請輸入你要查詢的學生的學號:";
 cin>>ID;
 cout<<"編號\t"<<"姓名\t"<<"學號\t"<<"年齡\t"<<"系別\t\t"<<"數學\t"<<"英語\t"<<"體育\t"<<"平均分\t"<<"專業課1\t"<<"專業課2\t"<<"專業課3\t"<<"平均分\t"<<endl;
 while(p!=NULL)
 {
 if(ID==p->data->get_stuID())
 {
 ++flag;
 p->data->display_info();
 break;
 }
 p=p->pNext;
 }
 if(flag==0)
 cout<<"Can't find this student!"<<endl;
}
//根據姓名查詢學生資訊
void List::search_by_name()
{
 List *p=pHead;//儲存頭節點
 int flag=0;//用來標記是否找到對應的學生
 string name;//儲存學生的學號
 cout<<"請輸入你要查詢的學生的姓名:";
 cin>>name;
 cout<<"編號\t"<<"姓名\t"<<"學號\t"<<"年齡\t"<<"系別\t\t"<<"數學\t"<<"英語\t"<<"體育\t"<<"平均分\t"<<"專業課1\t"<<"專業課2\t"<<"專業課3\t"<<"平均分\t"<<endl;
 while(p!=NULL)
 {
 if(name==p->data->get_name())
 {
 ++flag;
 p->data->display_info();
 break;
 }
 p=p->pNext;
 }
 if(flag==0)
 cout<<"Can't find this student!"<<endl;
}

//根據公共課成績平均分進行降序排序
void List::sort_by_public()
{
 List *p=NULL;
 List *q=NULL;
 double avg1,avg2;
 for(p=pHead;p->pNext!=NULL;p=p->pNext)
 for(q=p->pNext;q!=NULL;q=q->pNext)
 {
 avg1=p->data->Student::get_average();//呼叫基類的函式得到學生公共課的平均成績
 avg2=q->data->Student::get_average();
 if(avg1<avg2)
 {
 List temp;//臨時結點物件
 temp.data=p->data;
 p->data=q->data;
 q->data=temp.data;
 }
 }
}
//根據學生所有科目的平均分進行排序
void List::sort_by_all()
{
 List *p=NULL;
 List *q=NULL;
 double avg1,avg2;
 for(p=pHead;p->pNext!=NULL;p=p->pNext)
 for(q=p->pNext;q!=NULL;q=q->pNext)
 {
 avg1=p->data->get_average();//呼叫派生類的函式,得到學生所有科目的平均分
 avg2=q->data->get_average();
 if(avg1<avg2)
 {
 List temp;//臨時結點物件
 temp.data=p->data;
 p->data=q->data;
 q->data=temp.data;
 }
 }
}
//增加學生資訊
void List::add_info()
{
 int num=0;//學生的編號
 string stuID;//學生的學號
 string name;//學生的姓名
 int age;//學生的年齡
 string department;//學生所屬學院

 double math;//高數成績
 double english;//英語成績
 double pe;//體育成績

 double c_c;//計算機組成原理成績
 double c_a;//計算機體系結構成績
 double ass;//組合語言成績

 double c_e;//綜合英語成績
 double s_e;//口語成績

 double r_f;//實變函式成績
 double f_a;//泛函分析成績
 double d_g;//微分幾何成績

 int location=0;//位置編號
 int flag=0;//標記是否有對應的編號

 List *p=pHead;//指向當前結點的指標
 List *pf=NULL;//指向前一個結點的指標
 List *new_node;//指向新增結點的指標
 cout<<"請輸入你想增加的資訊的位置(位置編號,大於0):";
 cin>>location;

 while(p!=NULL)//遍歷連結串列
 {
 num=p->data->get_num();
 if(num==location)
 {
 ++flag;

 cout<<"請輸入新增學生所屬系別(計算機/英語系/數學系):";
 cin>>department;
 cout<<"請輸入學生的資訊"<<endl;
 if(department=="計算機")
 {
 new_node=new List;//建立一個新的結點
 cout<<"姓名\t"<<"學號\t"<<"年齡\t"<<"數學\t"<<"英語\t"<<"體育\t"<<"計算機組成原理\t"<<"計算機體系結構\t"<<"組合語言\t"<<endl;
 cin>>name>>stuID>>age>>math>>english>>pe>>c_c>>c_a>>ass;
 new_node->data=new Csstudent(name,ass);
 }
 else if(department=="英語系")
 {
 new_node=new List;//建立一個新的結點
 cout<<"姓名\t"<<"學號\t"<<"年齡\t"<<"數學\t"<<"英語\t"<<"體育\t"<<"綜合英語\t"<<"口語\t"<<endl;
 cin>>name>>stuID>>age>>math>>english>>pe>>c_e>>s_e;
 new_node->data=new Enstudent(name,s_e);
 }
 else if(department=="數學系")
 {
 new_node=new List;//建立一個新的結點
 cout<<"姓名\t"<<"學號\t"<<"年齡\t"<<"數學\t"<<"英語\t"<<"體育\t"<<"實變函式\t"<<"泛函分析\t"<<"微分幾何\t"<<endl;
 cin>>name>>stuID>>age>>math>>english>>pe>>r_f>>f_a>>d_g;
 new_node->data=new Mstudent(name,d_g);
 }
 else
 {
 cout<<"Don't have this department!"<<endl;
 exit(0);
 }
 new_node->data->Student::count_sum();//計算公共科目的總分
 new_node->data->Student::count_average();//計算公共科目的平均分
 new_node->data->count_sum();//計算全部科目的總分
 new_node->data->count_average();//計算全部科目的平均分

 if(pf==NULL)
 {
 new_node->pNext=p;
 pHead=new_node;
 break;
 }
 else
 {
 new_node->pNext=p;
 pf->pNext=new_node;
 break;
 }
 }
 pf=p;
 p=p->pNext;
 }
 if(flag==0)//沒有找到輸入的位置編號,則預設將學生資訊加在最後
 {
 cout<<"請輸入新增學生所屬系別(計算機/英語系/數學系):";
 cin>>department;
 cout<<"請輸入學生的資訊"<<endl;
 if(department=="計算機")
 {
 new_node=new List;//建立一個新的結點
 cout<<"姓名\t"<<"學號\t"<<"年齡\t"<<"數學\t"<<"英語\t"<<"體育\t"<<"計算機組成原理\t"<<"計算機體系結構\t"<<"組合語言\t"<<endl;
 cin>>name>>stuID>>age>>math>>english>>pe>>c_c>>c_a>>ass;
 new_node->data=new Csstudent(name,d_g);
 }
 else
 {
 cout<<"Don't have this department!"<<endl;
 exit(0);
 }
 new_node->data->Student::count_sum();//計算公共科目的總分
 new_node->data->Student::count_average();//計算公共科目的平均分
 new_node->data->count_sum();//計算全部科目的總分
 new_node->data->count_average();//計算全部科目的平均分

 pf->pNext=new_node;
 new_node->pNext=NULL;
 }
}
//刪除指定學生的資訊
void List::delete_info()
{
 string name;//姓名
 string stuID;//學號

 List *p=pHead;//指向當前結點的指標
 List *pf=NULL;//指向前一個結點的指標

 char content[20];
 cout<<"請輸入資訊待刪除學生的姓名或學號:";
 cin>>content;

 //根據輸入來進行查詢並刪除
 if(content[0]>='0'&&content[0]<='9')
 {
 int flag=0;//標記是否找到對應學生;
 string ID=content;
 char answer;//記錄回答的內容

 while(p!=NULL)
 {
 stuID=p->data->get_stuID();
 if(stuID==ID)
 {
 flag++;
 cout<<"編號\t"<<"姓名\t"<<"學號\t"<<"年齡\t"<<"系別\t\t"<<"數學\t"<<"英語\t"<<"體育\t"<<"平均分\t"<<"專業課1\t"<<"專業課2\t"<<"專業課3\t"<<"平均分\t"<<endl;
 p->data->display_info();

 cout<<"你確定要刪除這組資訊嗎?(Y/N)";
 cin>>answer;
 if(tolower(answer)=='y')
 {
  if(pf==NULL)
  {
  delete p->data;//刪除學生資訊所佔記憶體
  List *temp=p;//暫時儲存指向該結點的指標
  pHead=p->pNext;//跳過當前結點,實現資訊的刪除
  delete temp;//將其所佔記憶體刪除
  }
  else
  {
  delete p->data;//刪除學生資訊所佔記憶體
  List *temp=p;//暫時儲存指向該結點的指標
  pf->pNext=p->pNext;//跳過當前結點,實現資訊的刪除
  delete temp;//將其所佔記憶體刪除
  }
  cout<<"\t該組資訊已刪除!"<<endl;
  break;
 }
 else
 {
  break;
 }
 }
 pf=p;//儲存當前指標
 p=p->pNext;//設定指標指向下一個結點
 }
 if(flag==0)
 cout<<"沒有找到該學生!"<<endl;
 }
 else
 {
 int flag=0;//標記是否找到對應學生;
 string na=content;//將字串陣列轉化成string型別
 char answer;//記錄回答的內容

 while(p!=NULL)
 {
 //++n;
 name=p->data->get_name();
 if(name==na)
 {
 flag++;
 cout<<"編號\t"<<"姓名\t"<<"學號\t"<<"年齡\t"<<"系別\t\t"<<"數學\t"<<"英語\t"<<"體育\t"<<"平均分\t"<<"專業課1\t"<<"專業課2\t"<<"專業課3\t"<<"平均分\t"<<endl;
 p->data->display_info();

 cout<<"你確定要刪除這組資訊嗎?(Y/N)";
 cin>>answer;
 if(tolower(answer)=='y')
 {
  if(pf==NULL)
  {
  delete p->data;//刪除學生資訊所佔記憶體
  List *temp=p;//暫時儲存指向該結點的指標
  pHead=p->pNext;//跳過當前結點,實現資訊的刪除
  delete temp;//將其所佔記憶體刪除
  }
  else
  {
  delete p->data;//刪除學生資訊所佔記憶體
  List *temp=p;//暫時儲存指向該結點的指標
  pf->pNext=p->pNext;//跳過當前結點,實現資訊的刪除
  delete temp;//將其所佔記憶體刪除
  }

  cout<<"\t該組資訊已刪除!"<<endl;
  break;
 }
 else
 {
  break;
 }
 }
 pf=p;//儲存當前指標
 p=p->pNext;//設定指標指向下一個結點
 }
 if(flag==0)
 cout<<"沒有找到該學生!"<<endl;
 }
}
//將連結串列儲存到檔案中
void List::save_list()
{
 List *p=pHead;//用來遍歷連結串列的指標
 ofstream fout;
 fout.open("student.txt",ios::out);
 while(p!=NULL)
 {
 p->data->save_node(fout);
 p=p->pNext;
 }
 fout.close();//關閉檔案
}
//銷燬連結串列
void List::delete_list()
{
 List *p=pHead;
 List *pt;
 while(p!=NULL)
 {
 pt=p;
 p=p->pNext;
 delete pt->data;//刪除學生資訊所佔記憶體
 delete pt;//刪除連結串列結點所佔記憶體
 }
 pt=NULL;
 pHead=NULL;
 p=NULL;
}

student.txt

小王 02 18 英語系 78 79 80 68 89
小趙 01 19 計算機 90 90 90 89 89 89
小張 03 19 數學系 78 89 69 89 90 60
小劉 04 20 英語系 89 79 79 80 89
小李 05 19 計算機 69 60 67 89 90 90
小田 06 21 數學系 89 90 90 78 89 90

如果程式無法執行,或有什麼問題,歡迎在評論區留言.

推薦幾篇文章:

C++實現簡單的圖書管理系統

C++實現簡單的職工資訊管理系統

C++基礎學生管理系統

關於管理系統的更多內容請點選《管理系統專題》進行學習

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。