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

C++實現學生成績管理系統

終於結束了期末考試,有時間將這學期C++作業整理一下,都是一些基礎的內容,寫出來希望給一些初學者一些參考。主要使用到C++的面對物件程式設計思想,對學生資訊類及連結串列類進行封裝

文章可能有點長,可以只參考需要的部分

題目要求

用C++及類和物件來進行抽象、封裝與實現,用作課內的第四次作業。注意學生的成績需要用連結串列來實現,並且連結串列也需要實現封裝。
成績管理系統
資訊描述:
現有學生成績資訊,內容如下
姓名 學號 語文 數學 英語
張明明 01 67 78 82
李成友 02 78 91 88
張輝燦 03 68 82 56
王露 04 56 45 77
陳東明 05 67 38 47
… … … … …

請用C++編寫一系統,實現學生資訊管理,包含以下功能:

(1)資訊維護

要求:學生資訊資料要以檔案的形式儲存,能實現學生資訊資料的維護。此模組包括子模組有:增加學生資訊、刪除學生資訊、修改學生資訊 。所有的學生資料需要存放於檔案中;能夠從檔案中讀入資料,並在程式中以連結串列的形式予以實現。

(2)資訊查詢

要求:查詢時可實現按姓名查詢、按學號查詢

(3)成績統計

要求:

輸入任意的一個課程名(如數學)給出該門課程的成績的分段統計(以10分為一個成績段,如90-100,80-89,70-79,60-69,小於60),給出在此分數段的學生數目。
根據指定的課程名求該門課所有學生的平均成績
給出每個學生的平均成績

排序:能對使用者指定的任意課程名,按成績升序或降序排列學生資料並顯示排序結果
至少採用文字選單介面

整體分析

此係統為學生成績管理系統,根據題目要求,它需要包含:資訊維護、資訊查詢、成績統計、排序、文字介面選單五個模組。
為實現這些功能,該程式包含兩個類:
1、Student類:包含一個學生的所有資訊,以及學生資訊的設定和獲取函式
2、List類:包含對學生資訊連結串列的所有操作

實現該系統共使用到7個檔案:

1、 menu.cpp:內含多個建立選單函式,用於實現不同模組的功能顯示
2、 student.h:包含用於儲存學生資訊的Student類
3、 student.cpp:包含Student類的成員函式的實現

4、 list.h:包含用於操作學生資訊的的List類
5、 list.cpp:包含List類的成員函式的實現
6、 main.cpp:呼叫所有的函式,並進行適當的組合實現完整的學生成績管理系統
7、 student.txt:包含學生的全部資訊

程式原始碼

menu.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;
  cout<<"\t\t4、返回"<<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;
  cout<<"\t\t3、返回"<<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

//主函式,採用switch結構的多層巢狀
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include"student.h"
#include"list.h"

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

using namespace std;
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;
          //修改學生資訊
          case 3:
            pHead.output_info();//遍歷連結串列,並列印學生的資訊
            pHead.modify_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&&choice1!=3)
            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:
            cout<<"輸入課程名稱:";
            cin>>subj;
            if(subj=="數學")
              pHead.separate_by_math();
            else if(subj=="英語")
              pHead.separate_by_english();
            else if(subj=="C++")
              pHead.separate_by_cpp();
            else
              cout<<"沒有該科目!"<<endl;
            system("pause");
            break;
          //計算各科目的平均成績
          case 2:
            pHead.count_subject_avg();//計算各學科的平均成績
            system("pause");
            break;
          default:
            system("cls");
            break;
          }
          if(choice3!=1&&choice3!=2)
            break;
        }
        break;

      //排序功能
      case 4:
        while(1)
        {
          system("cls");
          menu4();
          cout<<"請選擇你要進行的操作:";
          cin>>choice4;
          switch(choice4)
          {
          //升序排列
          case 1:
            cout<<"請輸入科目名稱:";
            cin>>subj;
            cout<<endl;
            if(subj=="數學")
            {
              cout<<"\t\t數學成績降序排列表"<<endl<<endl;
              pHead.sortMath();
            }
            else if(subj=="英語")
            {
              cout<<"\t\t英語成績降序排列表"<<endl<<endl;
              pHead.sortEnglish();
            }
            else if(subj=="C++")
            {
              cout<<"\t\tC++成績降序排列表"<<endl<<endl;
              pHead.sortCpp();
            }
            else
              cout<<"沒有該科目!"<<endl;
            pHead.output_info();
            cout<<endl;
            system("pause");
            break;
          case 2:
            cout<<"請輸入科目名稱:";
            cin>>subj;
            cout<<endl;
            if(subj=="數學")
            {
              cout<<"\t\t數學成績升序排列表"<<endl<<endl;
              pHead.sortMath_s();
            }
            else if(subj=="英語")
            {
              cout<<"\t\t英語成績升序排列表"<<endl<<endl;
              pHead.sortEnglish_s();
            }
            else if(subj=="C++")
            {
              cout<<"\t\tC++成績升序排列表"<<endl<<endl;
              pHead.sortCpp_s();
            }
            else
              cout<<"沒有該科目!"<<endl;
            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<string>
#include<sstream>
using namespace std;
class Student
{
public:
  Student();
  //~Student();
public:
  void set_stuID(string ID);//設定學號
  void set_name(string na);//設定姓名
  void set_scores(double en,double m,double cp);//設定學生的各科成績
  void set_num(int n);//設定學生編號

  void count_avg();//計算平均成績
  void count_total();//計算總成績

  void get_scores(double *en,double *m,double *cp,double *sum,double *avg);//得到學生的各科成績(大英、高數、C++、總分、平均分)
  string get_stuID();//得到學生的學號
  string get_name();//得到學生的姓名
  int get_num();//得到學生的編號

private:
  string stuID;//學號
  string name;//姓名
  double english;//英語成績
  double math;//數學成績
  double cpp;//C++成績
  double total;//總分
  double average;//平均成績
  int num;//學生編號
};
#endif

student.cpp

#include"student.h"
#include<iostream>
#include<string>
#include<sstream>
Student::Student()
{
  stuID="00";
}
//設定學號
void Student::set_stuID(string ID)
{
  stuID=ID;
}
//設定姓名
void Student::set_name(string na)
{
  name=na;
}
//設定學生的各科成績
void Student::set_scores(double en,double cp)
{
  english=en;
  math=m;
  cpp=cp;
}
//設定學生編號
void Student::set_num(int n)
{
  num=n;
}

//計算平均成績
void Student::count_avg()
{
  average=(english+math+cpp)/3;
}
//計算總成績
void Student::count_total()
{
  total=english+math+cpp;
}

//得到學生的各類成績(大英、高數、C++、總分、平均分)
void Student::get_scores(double *en,double *avg)
{
  *en=english;
  *m=math;
  *cp=cpp;
  *sum=total;
  *avg=average;
}
//得到學生的學號
string Student::get_stuID()
{
  return stuID;
}
//得到學生的姓名
string Student::get_name()
{
  return name;
}
//得到學生的編號
int Student::get_num()
{
  return num;
}

list.h

#ifndef __LIST_H__
#define __LIST_H__
#include<iostream>
#include"student.h"
class List
{
public:
  List(){};//建構函式
  ~List();//解構函式

public:
  //學生資訊讀取相關函式
  void input_info();//建立連結串列,並從檔案讀取學生的資訊儲存到連結串列中
  void output_info();//遍歷連結串列,並列印學生的資訊

  //學生資訊查詢相關函式
  void search_by_name();//通過姓名查詢學生
  void search_by_ID();//通過學號查詢學生

  //排序功能函式
  void sortEnglish();//根據英語成績對學生進行降序排名
  void sortMath();//根據數學成績對學生進行降序排名
  void sortCpp();//根據C++成績對學生進行降序排名

  void sortEnglish_s();//根據英語成績對學生進行升序排名
  void sortMath_s();//根據數學成績對學生進行升序排名
  void sortCpp_s();//根據C++成績對學生進行升序排名
  void sortTotal();//根據總成績對學生進行降序排序

  //成績統計功能相關函式
  void separate_by_english();//根據英語成績進行分段統計
  void separate_by_math();//根據數學成績進行分段統計
  void separate_by_cpp();//根據C++成績進行分段統計
  void count_subject_avg();//計算各學科的平均成績

  //學生資訊維護功能函式
  void modify_info();//修改學生資訊
  void delete_info();//刪除學生資訊
  void add_info();//增加學生資訊

public:
  void save_list();//將列表儲存至檔案中
  void delete_list();//銷燬連結串列,釋放記憶體空間

private:
  Student stu;//學生資料體
  List *pHead;//頭指標
  List *pNext;//指向下一組學生資訊的指標
};
#endif

list.cpp

#include<iostream>
#include<fstream>
#include<stdio.h>
#include<cstdlib>
#include<cstring>
#include<iomanip>
#include"list.h"
using namespace std;

List::~List()
{
  pHead=NULL;
  pNext=NULL;
}

//從檔案匯入學生資訊,並儲存在連結串列中
void List::input_info()
{
  List *current=NULL;
  List *previous=NULL;
  pHead=NULL;

  string name;//姓名
  string stuID;//學號
  double english;//英語成績
  double math;//數學成績
  double cpp;//C++成績
  int n=0;//用於統計學生的數量,同時設定編號

  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>>english>>math>>cpp))//從檔案中讀取資料
      break;

    ++n;//編號遞增
    //cout<<name<<' '<<stuID<<' '<<english<<' '<<math<<' '<<cpp<<endl;

    current=new List;//建立結點
    if(pHead==NULL)
      pHead=current;//儲存頭指標
    if(previous!=NULL)
      previous->pNext=current;//將前一個結點的pNext指向當前結點

    //將檔案中的一組資料儲存在當前結點
    current->stu.set_name(name);//儲存學生的姓名
    current->stu.set_stuID(stuID);//儲存學生的學號
    current->stu.set_scores(english,math,cpp);//儲存英語、數學、C++成績
    current->stu.count_avg();//計算平均成績,並儲存
    current->stu.count_total();//計算總分
    current->stu.set_num(n);

    current->pNext=NULL;
    previous=current;

  }

  //關閉檔案
  fin.close();
  //cout<<"已成功從檔案匯入"<<n<<"個學生的資訊"<<endl;
}
//遍歷連結串列,列印學生資訊
void List::output_info()
{
  string name;//姓名
  string stuID;//學號
  int num;
  double english;//英語成績
  double math;//數學成績
  double cpp;//C++成績
  double total;//總成績
  double average;//平均成績
  int n=0;//設定編號

  cout<<"\n編號\t"<<"姓名\t"<<"學號\t"<<"英語\t"<<"數學\t"<<"C++\t"<<"總分\t"<<"平均分\t"<<endl;

  List *p=pHead;

  while(p!=NULL)
  {
    ++n;
    p->stu.set_num(n);//重新設定編號
    name=p->stu.get_name();
    stuID=p->stu.get_stuID();
    num=p->stu.get_num();
    p->stu.get_scores(&english,&math,&cpp,&total,&average);
    cout<<fixed<<setprecision(2)<<num<<'\t'<<name<<'\t'<<stuID<<'\t'<<english<<'\t'<<math<<'\t'<<cpp<<'\t'<<total<<'\t'<<average<<endl;
    p=p->pNext;//設定指標指向下一個結點
  }
  cout<<endl;
}

//根據姓名來查詢學生資訊
void List::search_by_name()
{
  string name;//姓名
  string stuID;//學號
  double english;//英語成績
  double math;//數學成績
  double cpp;//C++成績
  double total;//總成績
  double average;//平均成績
  int flag=0;//用來標記是否找到對應的學生

  List *p=pHead;

  string na;
  cout<<"請輸入你要查詢的學生的姓名:";
  cin>>na;
  while(p!=NULL)
  {
    name=p->stu.get_name();
    if(name==na)
    {
      flag++;
      stuID=p->stu.get_stuID();
      p->stu.get_scores(&english,&average);
      cout<<"\n該學生的資訊如下:"<<endl<<endl;
      cout<<"姓名\t"<<"學號\t"<<"英語\t"<<"數學\t"<<"C++\t"<<"總分\t"<<"平均分\t"<<endl;
      cout<<fixed<<setprecision(2)<<name<<'\t'<<stuID<<'\t'<<english<<'\t'<<math<<'\t'<<cpp<<'\t'<<total<<'\t'<<average<<endl;
      break;
    }
    p=p->pNext;//設定指標指向下一個結點
  }
  if(flag==0)
    cout<<"沒有找到該學生!"<<endl;
}
//根據學號來查詢學生
void List::search_by_ID()
{
  string name;//姓名
  string stuID;//學號
  double english;//英語成績
  double math;//數學成績
  double cpp;//C++成績
  double total;//總成績
  double average;//平均成績
  int flag=0;//用來標記是否找到對應的學生

  List *p=pHead;

  string ID;
  cout<<"請輸入你要查詢的學生的學號:";
  cin>>ID;
  while(p!=NULL)
  {
    stuID=p->stu.get_stuID();
    if(stuID==ID)
    {
      flag++;
      name=p->stu.get_name();
      cout<<"\n該學生的資訊如下:"<<endl<<endl;
      p->stu.get_scores(&english,&average);
      cout<<"姓名\t"<<"學號\t"<<"英語\t"<<"數學\t"<<"C++\t"<<"總分\t"<<"平均分\t"<<endl;
      cout<<fixed<<setprecision(2)<<name<<'\t'<<stuID<<'\t'<<english<<'\t'<<math<<'\t'<<cpp<<'\t'<<total<<'\t'<<average<<endl;
      break;
    }
    p=p->pNext;//設定指標指向下一個結點
  }
  if(flag==0)
    cout<<"沒有找到該學生!"<<endl;
}

//根據C++成績對學生進行降序排名
void List::sortCpp()
{
  double english;//英語成績
  double math;//數學成績
  double total;//總成績
  double average;//平均成績
  //C++成績
  double cpp1;
  double cpp2;

  List *p;
  List *q;
  List temp;//臨時的物件
  for(p=pHead;p->pNext!=NULL;p=p->pNext)
    for(q=p->pNext;q!=NULL;q=q->pNext)
  {
    p->stu.get_scores(&english,&cpp1,&average);
    q->stu.get_scores(&english,&cpp2,&average);
    if(cpp1<cpp2)
    {
      temp.stu=q->stu;
      q->stu=p->stu;
      p->stu=temp.stu;
    }
  }
}
//根據數學成績對學生進行降序排名
void List::sortMath()
{
  double english;//英語成績
  double cpp;//C++成績
  double total;//總成績
  double average;//平均成績
  //數學成績
  double math1;
  double math2;

  List *p;
  List *q;
  List temp;//臨時的物件
  for(p=pHead;p->pNext!=NULL;p=p->pNext)
    for(q=p->pNext;q!=NULL;q=q->pNext)
  {
    p->stu.get_scores(&english,&math1,&math2,&average);
    if(math1<math2)
    {
      temp.stu=q->stu;
      q->stu=p->stu;
      p->stu=temp.stu;
    }
  }
}
//根據英語成績對學生進行降序排名
void List::sortEnglish()
{
  double cpp;//C++成績
  double math;//數學成績
  double total;//總成績
  double average;//平均成績
  //英語成績
  double english1;
  double english2;

  List *p;
  List *q;
  List temp;//臨時的物件
  for(p=pHead;p->pNext!=NULL;p=p->pNext)
    for(q=p->pNext;q!=NULL;q=q->pNext)
  {
    p->stu.get_scores(&english1,&average);
    q->stu.get_scores(&english2,&average);
    if(english1<english2)
    {
      temp.stu=q->stu;
      q->stu=p->stu;
      p->stu=temp.stu;
    }
  }
}
//根據英語成績對學生進行升序排名
void List::sortEnglish_s()
{
  double cpp;//C++成績
  double math;//數學成績
  double total;//總成績
  double average;//平均成績
  //英語成績
  double english1;
  double english2;

  List *p;
  List *q;
  List temp;//臨時的物件
  for(p=pHead;p->pNext!=NULL;p=p->pNext)
    for(q=p->pNext;q!=NULL;q=q->pNext)
  {
    p->stu.get_scores(&english1,&average);
    if(english1>english2)
    {
      temp.stu=q->stu;
      q->stu=p->stu;
      p->stu=temp.stu;
    }
  }
}
//根據數學成績對學生進行升序排名
void List::sortMath_s()
{
  double english;//英語成績
  double cpp;//C++成績
  double total;//總成績
  double average;//平均成績
  //數學成績
  double math1;
  double math2;

  List *p;
  List *q;
  List temp;//臨時的物件
  for(p=pHead;p->pNext!=NULL;p=p->pNext)
    for(q=p->pNext;q!=NULL;q=q->pNext)
  {
    p->stu.get_scores(&english,&average);
    if(math1>math2)
    {
      temp.stu=q->stu;
      q->stu=p->stu;
      p->stu=temp.stu;
    }
  }
}
//根據C++成績對學生進行升序排名
void List::sortCpp_s()
{
  double english;//英語成績
  double math;//數學成績
  double total;//總成績
  double average;//平均成績
  //C++成績
  double cpp1;
  double cpp2;

  List *p;
  List *q;
  List temp;//臨時的物件
  for(p=pHead;p->pNext!=NULL;p=p->pNext)
    for(q=p->pNext;q!=NULL;q=q->pNext)
  {
    p->stu.get_scores(&english,&average);
    if(cpp1>cpp2)
    {
      temp.stu=q->stu;
      q->stu=p->stu;
      p->stu=temp.stu;
    }
  }
}
//根據總成績對學生進行降序排名
void List::sortTotal()
{
  double english;//英語成績
  double cpp;//C++成績
  double math;//數學成績
  double average;//平均成績
  //總成績
  double sum1;
  double sum2;

  List *p;
  List *q;
  List temp;//臨時的物件
  for(p=pHead;p->pNext!=NULL;p=p->pNext)
    for(q=p->pNext;q!=NULL;q=q->pNext)
  {
    p->stu.get_scores(&english,&sum1,&sum2,&average);
    if(sum1<sum2)
    {
      temp.stu=q->stu;
      q->stu=p->stu;
      p->stu=temp.stu;
    }
  }
}

//根據英語成績進行分段統計
void List::separate_by_english()
{
  //sortEnglish();//呼叫根據英語成績排序函式

  string name;//姓名
  double english;//英語成績
  double math;//數學成績
  double cpp;//C++成績
  double total;//總成績
  double average;//平均成績
  int t1=0,t2=0,t3=0,t4=0,t5=0;//用於統計各分數段的人數

  List *p=pHead;

  while(p!=NULL)
  {
    p->stu.get_scores(&english,&average);
    if(english>=90&&english<=100)
      ++t1;
    else if(english>=80&&english<=89)
      ++t2;
    else if(english>=70&&english<=79)
      ++t3;
    else if(english>=60&&english<=69)
      ++t4;
    else
      ++t5;
    p=p->pNext;
  }
  cout<<"\n\t\t\t英語成績分段統計表"<<endl<<endl;
  cout<<"\t\t分數段\t"<<"100-90\t"<<"89-80\t"<<"79-70\t"<<"69-60\t"<<"59-0\t"<<endl<<endl;
  cout<<"\t\t人數\t"<<t1<<'\t'<<t2<<'\t'<<t3<<'\t'<<t4<<'\t'<<t5<<endl<<endl;
}
//根據數學成績進行分段統計
void List::separate_by_math()
{
  //sortEnglish();//呼叫根據英語成績排序函式

  string name;//姓名
  double english;//英語成績
  double math;//數學成績
  double cpp;//C++成績
  double total;//總成績
  double average;//平均成績
  int t1=0,&average);
    if(math>=90&&math<=100)
      ++t1;
    else if(math>=80&&math<=89)
      ++t2;
    else if(math>=70&&math<=79)
      ++t3;
    else if(math>=60&&math<=69)
      ++t4;
    else
      ++t5;
    p=p->pNext;
  }
  cout<<"\n\t\t\t數學成績分段統計表"<<endl<<endl;
  cout<<"\t\t分數段\t"<<"100-90\t"<<"89-80\t"<<"79-70\t"<<"69-60\t"<<"59-0\t"<<endl<<endl;
  cout<<"\t\t人數\t"<<t1<<'\t'<<t2<<'\t'<<t3<<'\t'<<t4<<'\t'<<t5<<endl<<endl;
}
//根據C++成績進行分段統計
void List::separate_by_cpp()
{
  //sortEnglish();//呼叫根據英語成績排序函式

  string name;//姓名
  double english;//英語成績
  double math;//數學成績
  double cpp;//C++成績
  double total;//總成績
  double average;//平均成績
  int t1=0,&average);
    if(cpp>=90&&cpp<=100)
      ++t1;
    else if(cpp>=80&&cpp<=89)
      ++t2;
    else if(cpp>=70&&cpp<=79)
      ++t3;
    else if(cpp>=60&&cpp<=69)
      ++t4;
    else
      ++t5;
    p=p->pNext;
  }
  cout<<"\n\t\t\tC++成績分段統計表"<<endl<<endl;
  cout<<"\t\t分數段\t"<<"100-90\t"<<"89-80\t"<<"79-70\t"<<"69-60\t"<<"59-0\t"<<endl<<endl;
  cout<<"\t\t人數\t"<<t1<<'\t'<<t2<<'\t'<<t3<<'\t'<<t4<<'\t'<<t5<<endl<<endl;
}
//計算各學科的平均成績
void List::count_subject_avg()
{

  double english;//英語成績
  double math;//數學成績
  double cpp;//C++成績
  double total;//總成績
  double average;//平均成績

  double english_avg;//英語平均成績
  double math_avg;//數學平均成績
  double cpp_avg;//C++平均成績
  int n=0;//用於記錄學生人數
  double Esum=0,Msum=0,Csum=0;//用於計算各科目總分

  List *p=pHead;
  while(p!=NULL)
  {
    p->stu.get_scores(&english,&average);
    Esum+=english;
    Msum+=math;
    Csum+=cpp;
    ++n;
    p=p->pNext;
  }
  english_avg=Esum/n;
  math_avg=Msum/n;
  cpp_avg=Csum/n;
  cout<<"各科目的平均成績如下"<<endl;
  cout<<"英語\t"<<"數學\t"<<"C++\t"<<endl;
  cout<<english_avg<<'\t'<<math_avg<<'\t'<<cpp_avg<<endl;
}

//修改學生的資訊
void List::modify_info()
{
  string name;//姓名
  string stuID;//學號
  double english;//英語成績
  double math;//數學成績
  double cpp;//C++成績
  double total;//總成績
  double average;//平均成績

  List *p=pHead;

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

  //根據輸入來進行查詢並修改
  if(content[0]>='0'&&content[0]<='9')
  {
    int flag=0;//標記是否找到對應學生;
    string ID=content;
    while(p!=NULL)
    {
      stuID=p->stu.get_stuID();
      if(stuID==ID)
      {
        flag++;
        name=p->stu.get_name();
        p->stu.get_scores(&english,&average);
        cout<<"\n姓名\t"<<"學號\t"<<"英語\t"<<"數學\t"<<"C++\t"<<"總分\t"<<"平均分\t"<<endl;
        cout<<name<<'\t'<<stuID<<'\t'<<english<<'\t'<<math<<'\t'<<cpp<<'\t'<<total<<'\t'<<average<<endl<<endl;

        cout<<"請輸入修改後的資訊"<<endl;
        cout<<"姓名\t"<<"學號\t"<<"英語\t"<<"數學\t"<<"C++\t"<<endl;
        cin>>name>>stuID>>english>>math>>cpp;
        p->stu.set_name(name);//重新設定姓名
        p->stu.set_stuID(stuID);//重新設定學號
        p->stu.set_scores(english,cpp);//重新設定各科成績

        p->stu.count_total();//重新計算總成績
        p->stu.count_avg();//重新計算平均成績

        break;
      }
      p=p->pNext;//設定指標指向下一個結點
    }
    if(flag==0)
      cout<<"沒有找到該學生!"<<endl;
  }
  else
  {
    int flag=0;//標記是否找到對應學生;
    string na=content;//將字串陣列轉化成string型別
    while(p!=NULL)
    {
      name=p->stu.get_name();
      if(name==na)
      {
        flag++;
        stuID=p->stu.get_stuID();
        p->stu.get_scores(&english,&average);
        cout<<"姓名\t"<<"學號\t"<<"英語\t"<<"數學\t"<<"C++\t"<<"總分\t"<<"平均分\t"<<endl;
        cout<<name<<'\t'<<stuID<<'\t'<<english<<'\t'<<math<<'\t'<<cpp<<'\t'<<total<<'\t'<<average<<endl<<endl;

        cout<<"請輸入修改後的資訊"<<endl;
        cout<<"姓名\t"<<"學號\t"<<"英語\t"<<"數學\t"<<"C++\t"<<endl;
        cin>>name>>stuID>>english>>math>>cpp;

        p->stu.set_name(name);//重新設定姓名
        p->stu.set_stuID(stuID);//重新設定學號
        p->stu.set_scores(english,cpp);//重新設定各科成績

        p->stu.count_total();//重新計算總成績
        p->stu.count_avg();//重新計算平均成績

        break;
      }
      p=p->pNext;//設定指標指向下一個結點
    }
    if(flag==0)
      cout<<"沒有找到該學生!"<<endl;
  }
}
//刪除學生資訊
void List::delete_info()
{
  string name;//姓名
  string stuID;//學號
  double english;//英語成績
  double math;//數學成績
  double cpp;//C++成績
  double total;//總成績
  double average;//平均成績
  //int n=0;//記錄已遍歷的學生的人數

  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)
    {
      //++n;
      stuID=p->stu.get_stuID();
      if(stuID==ID)
      {
        flag++;
        name=p->stu.get_name();
        p->stu.get_scores(&english,&average);
        cout<<"姓名\t"<<"學號\t"<<"英語\t"<<"數學\t"<<"C++\t"<<"總分\t"<<"平均分\t"<<endl;
        cout<<name<<'\t'<<stuID<<'\t'<<english<<'\t'<<math<<'\t'<<cpp<<'\t'<<total<<'\t'<<average<<endl<<endl;

        cout<<"你確定要刪除這組資訊嗎?(Y/N)";
        cin>>answer;
        if(tolower(answer)=='y')
        {
          if(pf==NULL)
          {
            List *temp=p;//暫時儲存指向該結點的指標
            pHead=p->pNext;//跳過當前結點,實現資訊的刪除
            delete temp;//將其所佔記憶體刪除
          }
          else
          {
            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->stu.get_name();
      if(name==na)
      {
        flag++;
        stuID=p->stu.get_stuID();
        p->stu.get_scores(&english,&average);
        cout<<"姓名\t"<<"學號\t"<<"英語\t"<<"數學\t"<<"C++\t"<<"總分\t"<<"平均分\t"<<endl;
        cout<<name<<'\t'<<stuID<<'\t'<<english<<'\t'<<math<<'\t'<<cpp<<'\t'<<total<<'\t'<<average<<endl<<endl;

        cout<<"你確定要刪除這組資訊嗎?(Y/N)";
        cin>>answer;
        if(tolower(answer)=='y')
        {
          if(pf==NULL)
          {
            List *temp=p;//暫時儲存指向該結點的指標
            pHead=p->pNext;//跳過當前結點,實現資訊的刪除
            delete temp;//將其所佔記憶體刪除
          }
          else
          {
            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::add_info()
{
  string name;//姓名
  string stuID;//學號
  int num;//編號
  double english;//英語成績
  double math;//數學成績
  double cpp;//C++成績
  int location=0;//位置編號
  int flag=0;//標記是否有對應的編號

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

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

      cout<<"請輸入新增學生的資訊"<<endl;
      cout<<"姓名\t"<<"學號\t"<<"英語\t"<<"數學\t"<<"C++\t"<<endl;
      cin>>name>>stuID>>english>>math>>cpp;

      List *new_node=new List;//建立一個新的結點
      new_node->stu.set_name(name);
      new_node->stu.set_stuID(stuID);
      new_node->stu.set_scores(english,cpp);
      new_node->stu.count_total();//計算總成績
      new_node->stu.count_avg();//計算平均成績

      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<<"請輸入新增學生的資訊"<<endl;
    cout<<"姓名\t"<<"學號\t"<<"英語\t"<<"數學\t"<<"C++\t"<<endl;
    cin>>name>>stuID>>english>>math>>cpp;

    List *new_node=new List;//建立一個新的結點
    new_node->stu.set_name(name);
    new_node->stu.set_stuID(stuID);
    new_node->stu.set_scores(english,cpp);
     new_node->stu.count_total();//計算總成績
    new_node->stu.count_avg();//計算平均成績

    pf->pNext=new_node;
    new_node->pNext=NULL;
  }
}

//儲存連結串列至檔案中
void List::save_list()
{
  string name;//姓名
  string stuID;//學號
  double english;//英語成績
  double math;//數學成績
  double cpp;//C++成績
  double total;//總成績
  double average;//平均成績

  List *p=pHead;

  ofstream fout;
  fout.open("student.txt",ios::out);
  while(p!=NULL)
  {
    name=p->stu.get_name();
    stuID=p->stu.get_stuID();
    p->stu.get_scores(&english,&average);
    fout<<name<<' '<<stuID<<' '<<english<<' '<<math<<' '<<cpp<<endl;

    p=p->pNext;
  }
  fout.close();
}
//銷燬連結串列,釋放記憶體空間
void List::delete_list()
{
  List *p=pHead;
  List *pt;
  while(p!=NULL)
  {
    pt=p;
    p=p->pNext;
    delete pt;
  }
  pt=NULL;
  pHead=NULL;
  p=NULL;
}

student.txt

小趙 01 90 90 90
小王 02 84 84 89
小方 03 86 85 90
小李 04 90 76 70
小劉 05 89 78 90
小曹 06 60 78 69
小張 07 80 70 90
小田 08 90 90 90
小童 09 89 78 67

如果程式執行不了,或有什麼問題的話,歡迎在評論區留言

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