使用C++結合文件操作和鏈表實現(xiàn)學生成績管理系統(tǒng)
來源:程序員人生 發(fā)布時間:2014-09-08 22:44:38 閱讀次數(shù):3007次
對于學生成績管理系統(tǒng),我是不會陌生,幾乎學習C語言的人,做項目的時候都會想到學生成績管理系統(tǒng),我也不例外,在學了一段時間C語言后,也用C語言做了一個學生管理系統(tǒng),后來聯(lián)系做了幾個,算過來,這個系統(tǒng)對前面的系統(tǒng)有所改進,增加了文件操作可以不用手動輸入學生信息,可以直接從文件中讀取學生信息,從而簡化了操作
使用C語言實現(xiàn)學生成績管理系統(tǒng) http://blog.csdn.net/u010105970/article/details/17752193
使用鏈表實現(xiàn)學生成績管理系統(tǒng)
http://blog.csdn.net/u010105970/article/details/25058379
使用C++實現(xiàn)學生成績管理系統(tǒng)
http://blog.csdn.net/u010105970/article/details/27958237
程序模塊

首先定義兩個類Student類和Node類
Student類用于表示學生信息
class Student//學生類
{
public:
Student();//構造函數(shù)
void SetLen(int l);//設置學生的人數(shù)
void SetName(string N);//設置學生的姓名
void SetAge(int A);//設置學生的年齡
void SetNo(int N);//設置學生的編號
void SetCpp(int C);//設置C++成績
void SetMath(int M);//設置高數(shù)成績
void SetEnglish(int E);//設置英語成績
void CountTotal();//計算總分
void CountAve();//計算平均分
void SetS(int S);//設置序號(用于排名)
int GetLen();//得到學生的人數(shù)
string GetName();//得到學生的姓名
int GetAge();//得到學生的年齡
int GetNo();//得到學生的編號
int GetCpp();//得到學生的C++成績
int GetMath();//得到學生的高數(shù)成績
int GetEnglish();//得到學生的英語成績
int GetTotal();//得到學生的總分
float GetAve();//得到學生的平均分
int GetS();//得到序號(用于排名)
private:
int len; //學生的人數(shù)
string Name; //姓名
int Age; //年齡
int No; //學號
int Cpp; //C++成績
int Math; //高數(shù)成績
int English; //英語成績
int Total; //總分
float Ave; //平均分
int Sort; //排名
};
定義Student類中的成員函數(shù)
//構造函數(shù)
Student::Student()
{
Sort = 0;
}
void Student::SetLen(int l)//設置學生的人數(shù)
{
len = l;
}
void Student::SetName(string N)//設置學生的姓名
{
Name = N;
}
void Student::SetAge(int A)//設置學生的年齡
{
Age = A;
}
void Student::SetNo(int N)//設置學生的編號
{
No = N;
}
void Student::SetCpp(int C)//設置C++成績
{
Cpp = C;
}
void Student::SetMath(int M)//設置高數(shù)成績
{
Math = M;
}
void Student::SetEnglish(int E)//設置英語成績
{
English = E;
}
void Student::CountTotal()//計算總分
{
Total = Cpp + Math + English;
}
void Student::CountAve()//計算學生的平均分
{
Ave = Total / 3;
}
void Student::SetS(int s)//設置序號(用于排名)
{
Sort = s;
}
int Student::GetLen()//得到學生的人數(shù)
{
return len;
}
string Student::GetName()//得到學生的姓名
{
return Name;
}
int Student::GetAge()//得到學生的年齡
{
return Age;
}
int Student::GetNo()//得到學生的編號
{
return No;
}
int Student::GetCpp()//得到學生的C++成績
{
return Cpp;
}
int Student::GetMath()//得到學生的高數(shù)成績
{
return Math;
}
int Student::GetEnglish()//得到學生的英語成績
{
return English;
}
int Student::GetTotal()//得到學生的總分
{
return Total;
}
float Student::GetAve()//得到學生的平均分
{
return Ave;
}
int Student::GetS()//得到序號(用于排名)
{
return Sort;
}
定義Node類用于處理學生信息
class Node//結點類
{
public:
void InputStudent();//輸入學生信息
void OutputStudent();//輸出學生信息
Node* AddStudent();//增加學生信息
bool DeleteStudent();//刪除學生信息
void ChangeStudent();//修改學生信息
void SearchStudent();//查找學生信息
void SortCpp();//將C++成績按照從大到小排序
void SortMath();//將高數(shù)成績按照從大到小排序
void SortEnglish();//將英語成績按照從大到小排序
void SortTotal();//將總分按照從大到小排序
void SetScort();//設置排名
void ChangeNo(Node *p);//修改學生的學號
private:
Student st;//數(shù)據(jù)域
Node *pNext;//指針域
Node *pHead;//頭結點
};
typedef Node NODE;
typedef Node* PNODE;
/*
NODE 相當于 Node
PNODE 相當于 Node*
*/
定義Node類的成員函數(shù)
//輸入學生信息
void Node::InputStudent()
{
//創(chuàng)建一個頭結點
pHead = new NODE[sizeof(NODE)];
if(NULL == pHead)
{
cout<<"動態(tài)內存分配失敗,程序終止!"<<endl;
exit(0);
}
PNODE pTail = pHead;//創(chuàng)建一個指向頭結點的指針
pTail->pNext = NULL;//初始化指針的指針域為NULL
//將文件中的數(shù)據(jù)輸入到程序中
ifstream infile("score.dat", ios::in);
if(!infile)
{
cout<<"文件打開失敗,程序終止!"<<endl;
exit(0);
}
int l;//人數(shù)
string name;//姓名
int age;//年齡
int no;//學號
int cpp;//C++成績
int math;//數(shù)學成績
int english;//英語成績
cout<<"請輸入學生的人數(shù):";
cin>>l;
for(int i=1; i<=l; i++)
{
//創(chuàng)建一個保存數(shù)據(jù)的新結點
PNODE pNew = new NODE[sizeof(NODE)];
if(NULL == pNew)
{
cout<<"動態(tài)內存分配失敗,程序終止!"<<endl;
exit(0);
}
//讀取文件中的數(shù)據(jù)
infile>>name>>age>>no>>cpp>>math>>english;
//初始化結點
pNew->st.SetLen(l);//學生的人數(shù)
pNew->st.SetName(name);//學生的姓名
pNew->st.SetAge(age);//學生的年齡
pNew->st.SetNo(no);//學生的編號
pNew->st.SetCpp(cpp);//學生的C++成績
pNew->st.SetMath(math);//學生的數(shù)學成績
pNew->st.SetEnglish(english);//學生的英語成績
pNew->st.CountTotal();//計算學生的總分
pNew->st.CountAve();//計算學生的平均分
pTail->pNext = pNew;//將pNew掛在老結點的后面
pTail = pNew;//將指針pTail移到pNew上
pTail->pNext = NULL;//清空指針域
}
//關閉文件
infile.close();
cout<<"已經(jīng)成功的向程序中輸入了"<<l<<"個學生的信息"<<endl;
}
//遍歷鏈表
void Node::OutputStudent()
{
PNODE p = pHead->pNext;
cout<<"姓名 "<<"年齡 "<<"學號 "<<"C++ "<<"數(shù)學 "<<"英語 "<<"總分 "<<"平均分 "<<"排名"<<endl;
//遍歷學生信息
while(NULL != p)
{
cout<<p->st.GetName()<<" "<<p->st.GetAge()<<" "<<p->st.GetNo()<<" "<<p->st.GetCpp()
<<" "<<p->st.GetMath()<<" "<<p->st.GetEnglish()<<" "<<p->st.GetTotal()<<" "<<p->st.GetAve()<<" "<<p->st.GetS()<<endl;
p = p->pNext;
}
}
PNODE Node::AddStudent(void)//增加學生信息
{
PNODE p = pHead->pNext;
Student st;//定義一個學生類
int i = 1000;
int pos;
string name;//姓名
int age;//年齡
int no;//學號
int cpp;//C++成績
int math;//數(shù)學成績
int english;//英語成績
cout<<"請輸入一個學生的學號:";
cin>>pos;
cout<<"你將在學號為"<<pos<<"的學生后增加一個學生"<<endl;
st.SetNo(pos+1);//設置需要添加的學生的學號
cout<<"增加的學生的學號為"<<pos+1<<endl;
cout<<"請輸入學號為"<<st.GetNo()<<"的學生的姓名:";
cin>>name;
cout<<"請輸入學號為"<<st.GetNo()<<"的學生的年齡:";
cin>>age;
cout<<"請輸入學號為"<<st.GetNo()<<"的學生的C++成績:";
cin>>cpp;
cout<<"請輸入學號為"<<st.GetNo()<<"的學生的數(shù)學成績:";
cin>>math;
cout<<"請輸入學號為"<<st.GetNo()<<"的學生的英語成績:";
cin>>english;
while(NULL != p && i<pos-1)
{
p = p->pNext;
i++;
}
if(NULL == p || i>pos)
{
cout<<"程序錯誤!"<<endl;
exit(1);
}
PNODE pt = new NODE[sizeof(NODE)];
if(NULL == pt)
{
printf("動態(tài)內存分配失敗,程序終止!
");
exit(-1);
}
//初始化結點
pt->st.SetName(name);//學生的姓名
pt->st.SetAge(age);//學生的年齡
pt->st.SetNo(pos+1);//學生的學號
pt->st.SetCpp(cpp);//學生的C++成績
pt->st.SetMath(math);//學生的數(shù)學成績
pt->st.SetEnglish(english);//學生的英語成績
pt->st.CountTotal();//計算學生的總分
pt->st.CountAve();//計算學生的平均分
PNODE q = p->pNext;
p->pNext = pt;
pt->pNext = q;
return p->pNext;
}
bool Node::DeleteStudent()//刪除學生信息
{
int i = 1000;
int pos;
PNODE p = pHead;
cout<<"請輸入要刪除的學生的學號:";
cin>>pos;
cout<<"
刪除學號為"<<pos<<"后的學生信息:"<<endl;
while(NULL != p && i<pos-1)
{
p = p->pNext;
i++;
}
if(NULL == p || i>pos)
{
return true;
}
PNODE q = p->pNext;
p->pNext = p->pNext->pNext;
delete []q;
return true;
}
void Node::ChangeStudent()//修改學生信息
{
PNODE p = pHead->pNext;
string name;//姓名
int flag = 0;//標識符,初始化表示沒找到
cout<<"請輸入你需要修改的學生的姓名:";
cin>>name;
//遍歷學生信息
while(NULL != p)
{
if(name == p->st.GetName())
{
flag = 1;
cout<<"
修改前的學生信息:"<<endl;
cout<<"姓名 "<<"年齡 "<<"學號 "<<"C++ "<<"數(shù)學 "<<"英語 "<<"總分 "<<"平均分 "<<"排名"<<endl;
cout<<p->st.GetName()<<" "<<p->st.GetAge()<<" "<<p->st.GetNo()<<" "<<p->st.GetCpp()
<<" "<<p->st.GetMath()<<" "<<p->st.GetEnglish()<<" "<<p->st.GetTotal()<<" "<<p->st.GetAve()<<endl;
break;
}
p = p->pNext;
}
if(0 == flag)
{
cout<<"沒找到你需要修改的學生信息!
"<<endl;
return;
}
cout<<"
你將修改學號為"<<p->st.GetNo()<<"的學生信息"<<endl;
int age;//年齡
int no;//學號
int cpp;//C++成績
int math;//數(shù)學成績
int english;//英語成績
no = p->st.GetNo();//得到學生的學號
cout<<"請輸入學號為"<<p->st.GetNo()<<"的學生的姓名:";
cin>>name;
cout<<"請輸入學號為"<<p->st.GetNo()<<"的學生的年齡:";
cin>>age;
cout<<"請輸入學號為"<<p->st.GetNo()<<"的學生的C++成績:";
cin>>cpp;
cout<<"請輸入學號為"<<p->st.GetNo()<<"的學生的數(shù)學成績:";
cin>>math;
cout<<"請輸入學號為"<<p->st.GetNo()<<"的學生的英語成績:";
cin>>english;
//初始化結點
p->st.SetName(name);//學生的姓名
p->st.SetAge(age);//學生的年齡
p->st.SetNo(no);//學生的編號
p->st.SetCpp(cpp);//學生的C++成績
p->st.SetMath(math);//學生的數(shù)學成績
p->st.SetEnglish(english);//學生的英語成績
p->st.CountTotal();//計算學生的總分
p->st.CountAve();//計算學生的平均分
cout<<"
修改后的學生信息:"<<endl;
cout<<"姓名 "<<"年齡 "<<"學號 "<<"C++ "<<"數(shù)學 "<<"英語 "<<"總分 "<<"平均分 "<<"排名"<<endl;
cout<<p->st.GetName()<<" "<<p->st.GetAge()<<" "<<p->st.GetNo()<<" "<<p->st.GetCpp()
<<" "<<p->st.GetMath()<<" "<<p->st.GetEnglish()<<" "<<p->st.GetTotal()<<" "<<p->st.GetAve()<<endl;
}
void Node::SearchStudent()//查找學生信息
{
PNODE p = pHead->pNext;
string name;//姓名
int flag = 0;//標識符,初始化表示沒找到
cout<<"請輸入你需要查找的學生的姓名:";
cin>>name;
cout<<"姓名 "<<"年齡 "<<"學號 "<<"C++ "<<"數(shù)學 "<<"英語 "<<"總分 "<<"平均分 "<<"排名"<<endl;
//遍歷學生信息
while(NULL != p)
{
if(name == p->st.GetName())
{
flag = 1;
cout<<p->st.GetName()<<" "<<p->st.GetAge()<<" "<<p->st.GetNo()<<" "<<p->st.GetCpp()
<<" "<<p->st.GetMath()<<" "<<p->st.GetEnglish()<<" "<<p->st.GetTotal()<<" "<<p->st.GetAve()<<endl;
break;//退出循環(huán)
}
p = p->pNext;
}
if(0 == flag)
{
cout<<"沒找到你需要的學生信息!"<<endl;
}
}
void Node::SortCpp()//將C++成績按照從大到小排序
{
PNODE p, q;//定義兩個指針
NODE temp;//定義一個臨時結點
for(p = pHead->pNext; NULL != p; p = p->pNext)
{
for(q = p->pNext; NULL != q; q = q->pNext)
{
if(p->st.GetCpp() < q->st.GetCpp())//當前一個學生的C++成績小于后一個學生的C++成績時
{
temp.st = p->st;//交換學生的位置
p->st = q->st;
q->st = temp.st;
}
}
}
}
void Node::SortMath()//將高數(shù)成績按照從大到小排序
{
PNODE p, q;//定義兩個指針
NODE temp;//定義一個臨時結點
for(p = pHead->pNext; NULL != p; p = p->pNext)
{
for(q = p->pNext; NULL != q; q = q->pNext)
{
if(p->st.GetMath() < q->st.GetMath())//當前一個學生的高數(shù)成績小于后一個學生的高數(shù)成績時
{
temp.st = p->st;//交換學生的位置(交換結點中的數(shù)據(jù)域)
p->st = q->st;
q->st = temp.st;
}
}
}
}
void Node::SortEnglish()//將英語成績按照從大到小排序
{
PNODE p, q;//定義兩個指針
NODE temp;//定義一個臨時結點
for(p = pHead->pNext; NULL != p; p = p->pNext)
{
for(q = p->pNext; NULL != q; q = q->pNext)
{
if(p->st.GetEnglish() < q->st.GetEnglish())//當前一個學生的英語成績小于后一個學生的英語成績時
{
temp.st = p->st;//交換學生的位置(交換結點中的數(shù)據(jù)域)
p->st = q->st;
q->st = temp.st;
}
}
}
}
void Node::SortTotal()//將總分按照從大到小排序
{
PNODE p, q;//定義兩個指針
NODE temp;//定義一個臨時結點
for(p = pHead->pNext; NULL != p; p = p->pNext)
{
for(q = p->pNext; NULL != q; q = q->pNext)
{
if(p->st.GetTotal() < q->st.GetTotal())//當前一個學生的總分小于后一個學生的總分時
{
temp.st = p->st;//交換學生的位置(交換結點中的數(shù)據(jù)域)
p->st = q->st;
q->st = temp.st;
}
}
}
}
void Node::SetScort()//設置排名
{
PNODE p;//定義一個指向結點的指針
int i;//保存排名
//給學生的排名賦值
for(p=pHead->pNext, i=1; NULL !=p; p=p->pNext, i++)
{
p->st.SetS(i);
}
}
//修改學生的學號
void Node::ChangeNo(PNODE p)
{
int i = p->st.GetNo();//提供修改的數(shù)據(jù)
//遍歷學生信息
while(NULL != p)
{
p->st.SetNo(i);
i++;
p = p->pNext;
}
}
程序的全部代碼
#include <iostream>
#include <cstdlib>
#include <string>
#include <fstream>
using namespace std;
class Student//學生類
{
public:
Student();//構造函數(shù)
void SetLen(int l);//設置學生的人數(shù)
void SetName(string N);//設置學生的姓名
void SetAge(int A);//設置學生的年齡
void SetNo(int N);//設置學生的編號
void SetCpp(int C);//設置C++成績
void SetMath(int M);//設置高數(shù)成績
void SetEnglish(int E);//設置英語成績
void CountTotal();//計算總分
void CountAve();//計算平均分
void SetS(int S);//設置序號(用于排名)
int GetLen();//得到學生的人數(shù)
string GetName();//得到學生的姓名
int GetAge();//得到學生的年齡
int GetNo();//得到學生的編號
int GetCpp();//得到學生的C++成績
int GetMath();//得到學生的高數(shù)成績
int GetEnglish();//得到學生的英語成績
int GetTotal();//得到學生的總分
float GetAve();//得到學生的平均分
int GetS();//得到序號(用于排名)
private:
int len; //學生的人數(shù)
string Name; //姓名
int Age; //年齡
int No; //學號
int Cpp; //C++成績
int Math; //高數(shù)成績
int English; //英語成績
int Total; //總分
float Ave; //平均分
int Sort; //排名
};
//構造函數(shù)
Student::Student()
{
Sort = 0;
}
void Student::SetLen(int l)//設置學生的人數(shù)
{
len = l;
}
void Student::SetName(string N)//設置學生的姓名
{
Name = N;
}
void Student::SetAge(int A)//設置學生的年齡
{
Age = A;
}
void Student::SetNo(int N)//設置學生的編號
{
No = N;
}
void Student::SetCpp(int C)//設置C++成績
{
Cpp = C;
}
void Student::SetMath(int M)//設置高數(shù)成績
{
Math = M;
}
void Student::SetEnglish(int E)//設置英語成績
{
English = E;
}
void Student::CountTotal()//計算總分
{
Total = Cpp + Math + English;
}
void Student::CountAve()//計算學生的平均分
{
Ave = Total / 3;
}
void Student::SetS(int s)//設置序號(用于排名)
{
Sort = s;
}
int Student::GetLen()//得到學生的人數(shù)
{
return len;
}
string Student::GetName()//得到學生的姓名
{
return Name;
}
int Student::GetAge()//得到學生的年齡
{
return Age;
}
int Student::GetNo()//得到學生的編號
{
return No;
}
int Student::GetCpp()//得到學生的C++成績
{
return Cpp;
}
int Student::GetMath()//得到學生的高數(shù)成績
{
return Math;
}
int Student::GetEnglish()//得到學生的英語成績
{
return English;
}
int Student::GetTotal()//得到學生的總分
{
return Total;
}
float Student::GetAve()//得到學生的平均分
{
return Ave;
}
int Student::GetS()//得到序號(用于排名)
{
return Sort;
}
class Node//結點類
{
public:
void InputStudent();//輸入學生信息
void OutputStudent();//輸出學生信息
Node* AddStudent();//增加學生信息
bool DeleteStudent();//刪除學生信息
void ChangeStudent();//修改學生信息
void SearchStudent();//查找學生信息
void SortCpp();//將C++成績按照從大到小排序
void SortMath();//將高數(shù)成績按照從大到小排序
void SortEnglish();//將英語成績按照從大到小排序
void SortTotal();//將總分按照從大到小排序
void SetScort();//設置排名
void ChangeNo(Node *p);//修改學生的學號
private:
Student st;//數(shù)據(jù)域
Node *pNext;//指針域
Node *pHead;//頭結點
};
typedef Node NODE;
typedef Node* PNODE;
/*
NODE 相當于 Node
PNODE 相當于 Node*
*/
//輸入學生信息
void Node::InputStudent()
{
//創(chuàng)建一個頭結點
pHead = new NODE[sizeof(NODE)];
if(NULL == pHead)
{
cout<<"動態(tài)內存分配失敗,程序終止!"<<endl;
exit(0);
}
PNODE pTail = pHead;//創(chuàng)建一個指向頭結點的指針
pTail->pNext = NULL;//初始化指針的指針域為NULL
//將文件中的數(shù)據(jù)輸入到程序中
ifstream infile("score.dat", ios::in);
if(!infile)
{
cout<<"文件打開失敗,程序終止!"<<endl;
exit(0);
}
int l;//人數(shù)
string name;//姓名
int age;//年齡
int no;//學號
int cpp;//C++成績
int math;//數(shù)學成績
int english;//英語成績
cout<<"請輸入學生的人數(shù):";
cin>>l;
for(int i=1; i<=l; i++)
{
//創(chuàng)建一個保存數(shù)據(jù)的新結點
PNODE pNew = new NODE[sizeof(NODE)];
if(NULL == pNew)
{
cout<<"動態(tài)內存分配失敗,程序終止!"<<endl;
exit(0);
}
//讀取文件中的數(shù)據(jù)
infile>>name>>age>>no>>cpp>>math>>english;
//初始化結點
pNew->st.SetLen(l);//學生的人數(shù)
pNew->st.SetName(name);//學生的姓名
pNew->st.SetAge(age);//學生的年齡
pNew->st.SetNo(no);//學生的編號
pNew->st.SetCpp(cpp);//學生的C++成績
pNew->st.SetMath(math);//學生的數(shù)學成績
pNew->st.SetEnglish(english);//學生的英語成績
pNew->st.CountTotal();//計算學生的總分
pNew->st.CountAve();//計算學生的平均分
pTail->pNext = pNew;//將pNew掛在老結點的后面
pTail = pNew;//將指針pTail移到pNew上
pTail->pNext = NULL;//清空指針域
}
//關閉文件
infile.close();
cout<<"已經(jīng)成功的向程序中輸入了"<<l<<"個學生的信息"<<endl;
}
//遍歷鏈表
void Node::OutputStudent()
{
PNODE p = pHead->pNext;
cout<<"姓名 "<<"年齡 "<<"學號 "<<"C++ "<<"數(shù)學 "<<"英語 "<<"總分 "<<"平均分 "<<"排名"<<endl;
//遍歷學生信息
while(NULL != p)
{
cout<<p->st.GetName()<<" "<<p->st.GetAge()<<" "<<p->st.GetNo()<<" "<<p->st.GetCpp()
<<" "<<p->st.GetMath()<<" "<<p->st.GetEnglish()<<" "<<p->st.GetTotal()<<" "<<p->st.GetAve()<<" "<<p->st.GetS()<<endl;
p = p->pNext;
}
}
PNODE Node::AddStudent(void)//增加學生信息
{
PNODE p = pHead->pNext;
Student st;//定義一個學生類
int i = 1000;
int pos;
string name;//姓名
int age;//年齡
int no;//學號
int cpp;//C++成績
int math;//數(shù)學成績
int english;//英語成績
cout<<"請輸入一個學生的學號:";
cin>>pos;
cout<<"你將在學號為"<<pos<<"的學生后增加一個學生"<<endl;
st.SetNo(pos+1);//設置需要添加的學生的學號
cout<<"增加的學生的學號為"<<pos+1<<endl;
cout<<"請輸入學號為"<<st.GetNo()<<"的學生的姓名:";
cin>>name;
cout<<"請輸入學號為"<<st.GetNo()<<"的學生的年齡:";
cin>>age;
cout<<"請輸入學號為"<<st.GetNo()<<"的學生的C++成績:";
cin>>cpp;
cout<<"請輸入學號為"<<st.GetNo()<<"的學生的數(shù)學成績:";
cin>>math;
cout<<"請輸入學號為"<<st.GetNo()<<"的學生的英語成績:";
cin>>english;
while(NULL != p && i<pos-1)
{
p = p->pNext;
i++;
}
if(NULL == p || i>pos)
{
cout<<"程序錯誤!"<<endl;
exit(1);
}
PNODE pt = new NODE[sizeof(NODE)];
if(NULL == pt)
{
printf("動態(tài)內存分配失敗,程序終止!
");
exit(-1);
}
//初始化結點
pt->st.SetName(name);//學生的姓名
pt->st.SetAge(age);//學生的年齡
pt->st.SetNo(pos+1);//學生的學號
pt->st.SetCpp(cpp);//學生的C++成績
pt->st.SetMath(math);//學生的數(shù)學成績
pt->st.SetEnglish(english);//學生的英語成績
pt->st.CountTotal();//計算學生的總分
pt->st.CountAve();//計算學生的平均分
PNODE q = p->pNext;
p->pNext = pt;
pt->pNext = q;
return p->pNext;
}
bool Node::DeleteStudent()//刪除學生信息
{
int i = 1000;
int pos;
PNODE p = pHead;
cout<<"請輸入要刪除的學生的學號:";
cin>>pos;
cout<<"
刪除學號為"<<pos<<"后的學生信息:"<<endl;
while(NULL != p && i<pos-1)
{
p = p->pNext;
i++;
}
if(NULL == p || i>pos)
{
return true;
}
PNODE q = p->pNext;
p->pNext = p->pNext->pNext;
delete []q;
return true;
}
void Node::ChangeStudent()//修改學生信息
{
PNODE p = pHead->pNext;
string name;//姓名
int flag = 0;//標識符,初始化表示沒找到
cout<<"請輸入你需要修改的學生的姓名:";
cin>>name;
//遍歷學生信息
while(NULL != p)
{
if(name == p->st.GetName())
{
flag = 1;
cout<<"
修改前的學生信息:"<<endl;
cout<<"姓名 "<<"年齡 "<<"學號 "<<"C++ "<<"數(shù)學 "<<"英語 "<<"總分 "<<"平均分 "<<"排名"<<endl;
cout<<p->st.GetName()<<" "<<p->st.GetAge()<<" "<<p->st.GetNo()<<" "<<p->st.GetCpp()
<<" "<<p->st.GetMath()<<" "<<p->st.GetEnglish()<<" "<<p->st.GetTotal()<<" "<<p->st.GetAve()<<endl;
break;
}
p = p->pNext;
}
if(0 == flag)
{
cout<<"沒找到你需要修改的學生信息!
"<<endl;
return;
}
cout<<"
你將修改學號為"<<p->st.GetNo()<<"的學生信息"<<endl;
int age;//年齡
int no;//學號
int cpp;//C++成績
int math;//數(shù)學成績
int english;//英語成績
no = p->st.GetNo();//得到學生的學號
cout<<"請輸入學號為"<<p->st.GetNo()<<"的學生的姓名:";
cin>>name;
cout<<"請輸入學號為"<<p->st.GetNo()<<"的學生的年齡:";
cin>>age;
cout<<"請輸入學號為"<<p->st.GetNo()<<"的學生的C++成績:";
cin>>cpp;
cout<<"請輸入學號為"<<p->st.GetNo()<<"的學生的數(shù)學成績:";
cin>>math;
cout<<"請輸入學號為"<<p->st.GetNo()<<"的學生的英語成績:";
cin>>english;
//初始化結點
p->st.SetName(name);//學生的姓名
p->st.SetAge(age);//學生的年齡
p->st.SetNo(no);//學生的編號
p->st.SetCpp(cpp);//學生的C++成績
p->st.SetMath(math);//學生的數(shù)學成績
p->st.SetEnglish(english);//學生的英語成績
p->st.CountTotal();//計算學生的總分
p->st.CountAve();//計算學生的平均分
cout<<"
修改后的學生信息:"<<endl;
cout<<"姓名 "<<"年齡 "<<"學號 "<<"C++ "<<"數(shù)學 "<<"英語 "<<"總分 "<<"平均分 "<<"排名"<<endl;
cout<<p->st.GetName()<<" "<<p->st.GetAge()<<" "<<p->st.GetNo()<<" "<<p->st.GetCpp()
<<" "<<p->st.GetMath()<<" "<<p->st.GetEnglish()<<" "<<p->st.GetTotal()<<" "<<p->st.GetAve()<<endl;
}
void Node::SearchStudent()//查找學生信息
{
PNODE p = pHead->pNext;
string name;//姓名
int flag = 0;//標識符,初始化表示沒找到
cout<<"請輸入你需要查找的學生的姓名:";
cin>>name;
cout<<"姓名 "<<"年齡 "<<"學號 "<<"C++ "<<"數(shù)學 "<<"英語 "<<"總分 "<<"平均分 "<<"排名"<<endl;
//遍歷學生信息
while(NULL != p)
{
if(name == p->st.GetName())
{
flag = 1;
cout<<p->st.GetName()<<" "<<p->st.GetAge()<<" "<<p->st.GetNo()<<" "<<p->st.GetCpp()
<<" "<<p->st.GetMath()<<" "<<p->st.GetEnglish()<<" "<<p->st.GetTotal()<<" "<<p->st.GetAve()<<endl;
break;//退出循環(huán)
}
p = p->pNext;
}
if(0 == flag)
{
cout<<"沒找到你需要的學生信息!"<<endl;
}
}
void Node::SortCpp()//將C++成績按照從大到小排序
{
PNODE p, q;//定義兩個指針
NODE temp;//定義一個臨時結點
for(p = pHead->pNext; NULL != p; p = p->pNext)
{
for(q = p->pNext; NULL != q; q = q->pNext)
{
if(p->st.GetCpp() < q->st.GetCpp())//當前一個學生的C++成績小于后一個學生的C++成績時
{
temp.st = p->st;//交換學生的位置
p->st = q->st;
q->st = temp.st;
}
}
}
}
void Node::SortMath()//將高數(shù)成績按照從大到小排序
{
PNODE p, q;//定義兩個指針
NODE temp;//定義一個臨時結點
for(p = pHead->pNext; NULL != p; p = p->pNext)
{
for(q = p->pNext; NULL != q; q = q->pNext)
{
if(p->st.GetMath() < q->st.GetMath())//當前一個學生的高數(shù)成績小于后一個學生的高數(shù)成績時
{
temp.st = p->st;//交換學生的位置(交換結點中的數(shù)據(jù)域)
p->st = q->st;
q->st = temp.st;
}
}
}
}
void Node::SortEnglish()//將英語成績按照從大到小排序
{
PNODE p, q;//定義兩個指針
NODE temp;//定義一個臨時結點
for(p = pHead->pNext; NULL != p; p = p->pNext)
{
for(q = p->pNext; NULL != q; q = q->pNext)
{
if(p->st.GetEnglish() < q->st.GetEnglish())//當前一個學生的英語成績小于后一個學生的英語成績時
{
temp.st = p->st;//交換學生的位置(交換結點中的數(shù)據(jù)域)
p->st = q->st;
q->st = temp.st;
}
}
}
}
void Node::SortTotal()//將總分按照從大到小排序
{
PNODE p, q;//定義兩個指針
NODE temp;//定義一個臨時結點
for(p = pHead->pNext; NULL != p; p = p->pNext)
{
for(q = p->pNext; NULL != q; q = q->pNext)
{
if(p->st.GetTotal() < q->st.GetTotal())//當前一個學生的總分小于后一個學生的總分時
{
temp.st = p->st;//交換學生的位置(交換結點中的數(shù)據(jù)域)
p->st = q->st;
q->st = temp.st;
}
}
}
}
void Node::SetScort()//設置排名
{
PNODE p;//定義一個指向結點的指針
int i;//保存排名
//給學生的排名賦值
for(p=pHead->pNext, i=1; NULL !=p; p=p->pNext, i++)
{
p->st.SetS(i);
}
}
//修改學生的學號
void Node::ChangeNo(PNODE p)
{
int i = p->st.GetNo();//提供修改的數(shù)據(jù)
//遍歷學生信息
while(NULL != p)
{
p->st.SetNo(i);
i++;
p = p->pNext;
}
}
void main()
{
cout<<"================================================================================
"<<endl;
cout<<"================================================================================
"<<endl;
cout<<"*************************歡迎使用學生成績管理系統(tǒng)*******************************
"<<endl;
cout<<"-----------------------------------------------------------------制作人:梅沙小子
"<<endl;
cout<<"********************************************************************************
"<<endl;
cout<<"================================================================================
"<<endl;
cout<<"請按任意將進入學生管理系統(tǒng):";
getchar();
system("cls");
cout<<"===============================================================================
";
cout<<"------------------------ 請選擇要操作的命令:----------------------------------
";
cout<<"-------------------------- 1 輸入學生信息--------------------------------------
";
cout<<"-------------------------- 2 輸出學生信息--------------------------------------
";
cout<<"-------------------------- 3 增加學生信息--------------------------------------
";
cout<<"-------------------------- 4 刪除學生信息--------------------------------------
";
cout<<"-------------------------- 5 修改學生信息--------------------------------------
";
cout<<"-------------------------- 6 查找學生信息--------------------------------------
";
cout<<"-------------------------- 7 將學生的C++成績按從大到小排----------------------
";
cout<<"-------------------------- 8 將學生的高數(shù)成績按從大到小排----------------------
";
cout<<"-------------------------- 9 將學生的英語成績按從大到小排----------------------
";
cout<<"--------------------------10 將學生的總成績按從大到小排------------------------
";
cout<<"===============================================================================
";
int Item;//操作命令
NODE pHead;//定義一個結點
while(1)
{
cout<<"
請選擇操作命令:";
cin>>Item;
system("cls");//清屏
switch(Item)
{
case 1://輸入學生信息
{
//創(chuàng)建鏈表
pHead.InputStudent();
}
break;
case 2://輸出學生信息
{
//遍歷鏈表
pHead.OutputStudent();
}
break;
case 3://增加學生信息
{
PNODE q;
q = pHead.AddStudent();
pHead.ChangeNo(q);//修改學號8
}
break;
case 4://刪除學生信息
{
pHead.DeleteStudent();
pHead.OutputStudent();
}
break;
case 5://修改學生信息
{
pHead.ChangeStudent();
}
break;
case 6://查找學生信息
{
pHead.SearchStudent();
}
break;
case 7://對學生的C++成績排序
{
pHead.SortCpp();//C++排序
cout<<"C++成績排序后的結果:"<<endl;
pHead.SetScort();//設置排名
pHead.OutputStudent();//打印排序后的結果
}
break;
case 8://對學生的高數(shù)成績排序
{
pHead.SortMath();//高數(shù)排序
cout<<"高數(shù)成績排序后的結果:"<<endl;
pHead.SetScort();//設置排名
pHead.OutputStudent();//打印排序后的結果
}
生活不易,碼農(nóng)辛苦
如果您覺得本網(wǎng)站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
------分隔線----------------------------
------分隔線----------------------------