1.編譯器
Dev c++;
2.系統功能需求分析
2.1學生成績管理系統需求分析
包含學生的基本資訊:姓名,性别,生日。
學生的成績資訊:學号,五科成績,五科課程的名字(不同專業學生有不同的課程),總分,專業,績點等。
學生成績管理系統具有以下這些功能:
(1)學生成績的錄入:錄入新的學生成績等資訊;
(2)學生成績的添加:添加新的學生成績等資訊;
(3)學生成績的删除:删除不需要的學生成績等資訊。
(4)學生成績的查找:查找你需要的學生成績等資訊。
(5)學生成績的修改:修改錯誤的學生成績等資訊。
(6)學生成績的排序:按學生的績點排序,排序後結果寫入檔案。
(7)學生的挂科情況:統計學生成績小于60的數目。
(8)學生成績的顯示:顯示所有學生成績等資訊。
(9)學生成績的儲存:把學生成績等資訊儲存到檔案。
2.2三個類功能需求分析
2.2.1 Person類
如圖 1所示:
Person類派生出Student類。
Person類中受保護的資料成員有:姓名,性别,生日包含年月日。生日這個成員函數是BirthDate類的對象。
Person類中公用的成員函數有:
(1)輸入函數
(2)顯示函數
(3)重載<<,>>的函數
(4)基本的構造函數,預設構造函數,析構函數。
2.2.2 Student類
Student類派生出Undergraduate類。
Student類的受保護的資料成員:學号,五門課程名,五門課的成績,總成績。靜态資料成員N用來統計學生人數。
Student類的公用的成員函數:
(1)輸入函數
輸入學生成績等資訊,并寫入student.dat檔案。
(2)顯示函數
從student.dat 檔案中讀,并列印到終端。
(3)重載<<,>>的函數
(4)構造函數,預設構造函數,和析構函數。
2.2.3 Undergraduate類
Undergraduate類的私有的資料成員:專業,績點,靜态資料成員N用來統計學生人數。
Undergraduate類的公用的成員函數:
(1)重載<<,>>的函數
(2)構造函數,預設構造函數,和析構函數。
(3)輸入學生成績等資訊
輸入學生成績等資訊并寫入Undergraduate.dat檔案。
(4)顯示所有的學生成績等資訊
從Undergraduate.dat檔案中讀,并列印到終端。
(5)增加學生成績等資訊
增加學生成績等資訊并繼續寫入Undergraduate.dat檔案,如果重複輸入已存在的學生學号,需要重新輸入學生的成績等資訊。
(6)删除學生成績等資訊
在已添加的學生中查找要删除學生的學号,如果輸入學生的學号查找不到,需要重新輸入要删除學生的學号,并在檔案中删除該學生成績等資訊。
(7)查找學生成績等資訊
按學生的學号查找學生,如果輸入學生的學号查找不到,需要重新輸入要删除學生的學号。在已添加的學生中查找到學生,就列印學生成績等資訊到終端。
(8)修改學生成績等資訊
按學生的學号查找需要修改成績等資訊的學生,如果輸入學生的學号查找不到,需要重新輸入要删除學生的學号。在已添加的學生中查找到學生,就重新輸入該學生的資訊,列印修改前後學生成績等資訊到終端,并将修改後學生成績等資訊寫入檔案。
(9)判斷重複學号
判斷是否重複輸入已存在的學号。
(10)學生成績排序
從Undergraduate.dat檔案中讀,并計算學生總分,績點,并按降序排序,将成績單寫入Undergraduate_score_list.dat檔案,并列印到終端。
(11)判斷挂科的情況
從Undergraduate.dat檔案中讀,統計學生五科成績小于60的個數,并列印到終端。
(12)學生成績管理系統的菜單
菜單有0~8種選項,選擇不同的選項,分别實作上述不同的功能。
3.代碼展示
#include<iostream>
#include<fstream>
using namespace std;
class BirthDate{
public:
int year;//年
int month;//月
int day;//日
BirthDate();
BirthDate(int y,int m,int d):year(y),month(m),day(d){}
friend istream& operator>>(istream &in,BirthDate &b);
friend ostream& operator<<(ostream &out,BirthDate &b);
void change(int,int,int);//修改
};
class Person
{
protected:
string name;//姓名
string sex;//性别
BirthDate birth;//生日
public:
Person();
Person(string n,string s,int y,int m,int d):birth(y,m,d),name(n),sex(s){}
~Person();
virtual void display();//輸出
void input();//輸入
};
class Student:public Person//學生類
{
protected:
string id;//學号
string course[5];//五門課名
double score[5];//五門成績
double total;//總分
static int N;//統計學生數量
public:
Student();
Student(string n,string s,int y,int m,int d,string i,string course[5],double score[5],double t);
~Student();
friend istream& operator>>(istream &in,Student &s);
friend ostream& operator<<(ostream &out,Student &s);
void display();//列印終端
void input(Student*);//輸入并寫入檔案
};
class Undergraduate:public Student//大學生類
{
private:
string major;//專業名
double gpa;//績點
static int N;//統計學生數量
public:
Undergraduate();
Undergraduate(string n,string s,int y,int m,int d,string i,string course[5],double score[5],double t,string major):Student(n,s,y,m,d,i,course,score,t),major(major){gpa=0;}
~Undergraduate();
friend istream& operator>>(istream &in,Undergraduate &s);
friend ostream& operator<<(ostream &out,Undergraduate &s);
void display(); //顯示
void input(Undergraduate*);//導入學生成績等資訊
void add(Undergraduate*);//增加
void remove(Undergraduate*);//删除
friend void search(Undergraduate*);//查找
void change(Undergraduate*);//修改
bool unique(const Undergraduate*,const string);//判斷重複學号
friend void sort1(const Undergraduate *s);//排名
friend void sling(const Undergraduate*);//挂科
void menu(Undergraduate*); //目錄
};
//BirthDate類
BirthDate::BirthDate()
{
year=2000;
month=1;
day=1;
}
istream& operator>>(istream &in,BirthDate &b)
{
in>>b.year>>b.month>>b.day;
return in;
}
ostream& operator<<(ostream &out,BirthDate &b)
{
out<<b.year<<" "<<b.month<<" "<<b.day;
return out;
}
void BirthDate::change(int y,int m,int d)//修改
{
year=y;
month=m;
day=d;
}
//Person類
Person::Person()
{
name='0';
sex='0';
}
Person::~Person()
{
}
void Person::display()//輸出
{
cout<<name<<" "<<sex<<" ";
cout<<birth<<" ";
}
void Person::input()//輸入
{
cin>>name>>sex;
cin>>birth;
}
//Student類
int Student::N=0;
Student::Student()
{
id='0';
for(int i=0;i<5;i++)
{
course[i]='0';
score[i]=0;
}
total=0;
}
Student::Student(string n,string s,int y,int m,int d,string i,string c[5],double sc[5],double t):Person(n,s,y,m,d),id(i),total(t)
{
for(int i=0;i<5;i++)
{
course[i]=c[i];
score[i]=sc[i];
}
}
Student::~Student()
{
}
istream& operator>>(istream &in,Student &s)
{
in>>s.name>>s.sex>>s.birth;
in>>s.id;
in>>s.course[0]>>s.course[1]>>s.course[2]>>s.course[3]>>s.course[4];
in>>s.score[0]>>s.score[1]>>s.score[2]>>s.score[3]>>s.score[4];
return in;
}
ostream& operator<<(ostream &out,Student &s)
{
out<<s.name<<" "<<s.sex<<" "<<s.birth<<" ";
out<<s.id<<" "<<s.course[0]<<" "<<s.score[0]<<" ";
out<<s.course[1]<<" "<<s.score[1]<<" "<<s.course[2]<<" "<<s.score[0]<<" ";
out<<s.course[3]<<" "<<s.score[3]<<" "<<s.course[4]<<" "<<s.score[4];
return out;
}
void Student::display()
{
Student s[N];
cout<<"姓名 性别 出生日期 學号 課程名1 score1 課程名2 score2 課程名3 score3 課程名4 score4 課程名5 score5"<<endl;
ifstream infile("student.dat");
if(!infile)
{
cout<<"open error!"<<endl;
}
for(int i=0;i<N;i++)
{
infile>>s[i];
cout<<s[i]<<endl;
}
infile.close();
}
void Student::input(Student *s)
{
int n;
ofstream outfile("student.dat");
if(!outfile)
{
cout<<"open error!"<<endl;
}
cout<<"請輸入導入學生人數:"<<endl;
cin>>n;
N=n;
cout<<"請輸入學生資訊"<<endl;
cout<<"姓名 性别 出生日期:年 月 日 學号 課程名1 score1 課程名2 score2 課程名3 score3 課程名4 score4 課程名5 score5"<<endl;
for(int i=0;i<n;i++)
{
cin>>s[i];
outfile<<s[i]<<endl;
}
outfile.close();
}
//Undergradu類
int Undergraduate::N=0;
Undergraduate::Undergraduate()
{
major='0';
gpa=0;
}
Undergraduate::~Undergraduate()
{
}
istream& operator>>(istream &in,Undergraduate &s)
{
in>>s.major;
in>>s.name>>s.sex>>s.birth;
in>>s.id>>s.course[0]>>s.score[0];
in>>s.course[1]>>s.score[1]>>s.course[2]>>s.score[2];
in>>s.course[3]>>s.score[3]>>s.course[4]>>s.score[4];
return in;
}
ostream& operator<<(ostream &out,Undergraduate &s)
{
out<<s.major<<" ";
out<<s.name<<" "<<s.sex<<" "<<s.birth<<" ";
out<<s.id<<" "<<s.course[0]<<"\t"<<s.score[0]<<"\t";
out<<s.course[1]<<"\t"<<s.score[1]<<"\t"<<s.course[2]<<"\t"<<s.score[2]<<"\t";
out<<s.course[3]<<"\t"<<s.score[3]<<"\t"<<s.course[4]<<"\t"<<s.score[4];
return out;
}
void Undergraduate::display()
{
Undergraduate s[N];
cout<<"專業 姓名 性别 出生日期 學号 課程名1 score1 課程名2 score2 課程名3 score3 課程名4 score4 課程名5 score5"<<endl;
ifstream infile("Undergraduate.dat");//打開檔案
if(!infile)
{
cout<<"open error!"<<endl;
}
for(int i=0;i<N;i++)
{
infile>>s[i];//寫入檔案
cout<<s[i]<<endl;//列印終端
}
infile.close();//關閉終端
}
void Undergraduate::input(Undergraduate *s)//輸入
{
int n;
ofstream outfile("Undergraduate.dat");//打開檔案
if(!outfile)
{
cout<<"open error!"<<endl;
}
cout<<"請輸入導入學生人數:"<<endl;
cin>>n;
if(n<0)
{
cout<<"輸入錯誤"<<endl;
}
s[0].N=n;//統計學生人數
cout<<"請輸入學生成績等資訊"<<endl;
cout<<"專業 姓名 性别 出生日期:年 月 日 學号 課程名1 score1 課程名2 score2 課程名3 score3 課程名4 score4 課程名5 score5"<<endl;
try
{
for(int i=0;i<n;i++)
{
cin>>s[i];//輸入學生資訊
if(s[i].score[0]<0||s[i].score[0]>100||s[i].score[1]<0||s[i].score[1]>100||s[i].score[2]<0||s[i].score[2]>100||s[i].score[3]<0||s[i].score[3]>100||s[i].score[4]<0||s[i].score[4]>100)
{
int a;
throw a;
}
if(s[i].birth.year<=1980||s[i].birth.month<=0||s[i].birth.month>=12||s[i].birth.day<=0||s[i].birth.day>31)
{
BirthDate birth;
throw birth;
}
outfile<<s[i]<<endl;//寫入檔案
}
outfile.close();//關閉檔案
}
catch(int)
{
cout<<"輸入的成績非法"<<endl;
}
catch(BirthDate)
{
cout<<"輸入出生日期非法"<<endl;
}
}
void Undergraduate::add(Undergraduate *s)//增加
{
int n;
cout<<"請輸入要添加的人數"<<endl;
cin>>n;
cout<<"專業 姓名 性别 出生日期:年 月 日 學号 課程名1 score1 課程名2 score2 課程名3 score3 課程名4 score4 課程名5 score5"<<endl;
ofstream outfile("Undergraduate.dat",ios::app);//打開檔案
if(!outfile)
{
cout<<"open error!"<<endl;
}
try
{
for(int i=N;i<N+n;i++)
{
cin>>s[i];//輸入學生資訊
if(Undergraduate::unique(s,s[i].id))//判斷是否輸入重複學号
{
cout<<"請重新輸入學生成績等資訊:"<<endl;
cin>>s[i];
}
if(s[i].score[0]<0||s[i].score[0]>100||s[i].score[1]<0||s[i].score[1]>100||s[i].score[2]<0||s[i].score[2]>100||s[i].score[3]<0||s[i].score[3]>100||s[i].score[4]<0||s[i].score[4]>100)
{
int a;
throw a;
}
if(s[i].birth.year<=1980||s[i].birth.month<=0||s[i].birth.month>=12||s[i].birth.day<=0||s[i].birth.day>31)
{
BirthDate birth;
throw birth;
}
outfile<<s[i]<<endl;//将學生資訊寫入檔案
}
outfile.close();//關閉檔案
N+=n;//增加學生人數
}
catch(int)
{
cout<<"輸入的成績非法"<<endl;
}
catch(BirthDate)
{
cout<<"輸入出生日期非法"<<endl;
}
}
void Undergraduate::remove(Undergraduate *s)//删除
{
int n;
string id;
bool flag=false;
cout<<"請輸入要删除學生資訊的學号:"<<endl;
while(!flag)
{
cin>>id;
for(int i=0;i<N;i++)
{
if(s[i].id==id)//判斷是否找到要删除學生學号
{
flag=true;
cout<<"學生成績等資訊:"<<endl;
for(int k=0;k<N;k++) //列印到終端
{
cout<<s[k]<<endl;
}
for(int j=i;j<N-1;j++)//往前移1為,覆寫該學生資訊
s[j]=s[j+1];
cout<<"删除成功!"<<endl;
N=N-1;//減少學生人數
break;
}
}
if(!flag)
{
cout<<"未找到該學生資訊,請重新輸入"<<endl;
}
}
ofstream outfile("Undergraduate.dat");//打開檔案
if(!outfile)
{
cout<<"open error!"<<endl;
}
cout<<"删除後學生資訊表:"<<endl;
for(int i=0;i<N;i++)
{
outfile<<s[i]<<endl;//寫入檔案
cout<<s[i]<<endl;//列印終端
}
outfile.close(); //關閉檔案
}
void search(Undergraduate *s)//查找
{
string id;
bool flag=false;
int i,j,count=0;
int num[5]={-1};
int N=s[0].N;
cout<<"請輸入要查找學生資訊的學号:"<<endl;
while(!flag)
{
cin>>id;
for(i=0;i<N;i++)
{
if(s[i].id==id)//判斷是否查找到該學生資訊
{
flag=true;
cout<<"該學生資訊:"<<endl;
cout<<s[i];
for(j=0;j<5;j++)
{
if(s[i].score[j]<60)
{
count++;//統計挂科數
}
}
cout<<"\t有"<<count<<"門課挂科"<<endl;
break;
}
}
if(!flag)
{
cout<<"未找到該學生資訊,請重新輸入學号"<<endl;
}
}
}
void Undergraduate::change(Undergraduate *s)//修改
{
string id;
bool flag=false;
cout<<"請輸入要修改學生資訊的學号:"<<endl;
while(!flag)
{
cin>>id;
for(int i=0;i<N;i++)
{
if(s[i].id==id)//判斷是否找到要修改的學生
{
flag=true;
cout<<"學生成績資訊表:"<<endl;
for(int k=0;k<N;k++)
{
cout<<s[k]<<endl; //列印修改前學生的資訊
}
cout<<"請重新輸入該學生資訊:"<<endl;
cin>>s[i];
break;
}
}
if(!flag)
{
cout<<"未找到該學生資訊,請重新輸入學号:"<<endl;
}
}
ofstream outfile("Undergraduate.dat");//打開檔案
if(!outfile)
{
cout<<"open error!"<<endl;
}
cout<<"修改後學生成績資訊表:"<<endl;
for(int i=0;i<N;i++)
{
outfile<<s[i]<<endl;//将修改後學生資訊寫入檔案
cout<<s[i]<<endl;//将修改後資訊列印終端
}
outfile.close(); //關閉檔案
}
bool Undergraduate::unique(const Undergraduate *s,const string id)//判斷重複學号
{
for(int i=0;i<N;i++)
{
if(s[i].id==id)
{
cout<<s[i].id<<"已存在,請勿輸入重複學号的學生資訊!"<<endl;
return true;
}
}
return false;
}
void sort1(const Undergraduate *s)//按學生績點排序
{
int i,j,k;
int N=s[0].N;
Undergraduate temp,stu[N];
ifstream infile("Undergraduate.dat");
if(!infile)
{
cout<<"open error!"<<endl;
}
for(i=0;i<N;i++)//從檔案讀
infile>>stu[i];
infile.close();
for(i=0;i<N;i++)//求總分
{
stu[i].total=0;
for(j=0;j<5;j++)
{
stu[i].total+=stu[i].score[j];
}
stu[i].gpa=(stu[i].total/50.0)-5;//求績點
}
for(i=0;i<N-1;i++)//按學生績點冒泡排序 從高到低列印到終端
{
for(j=0;j<N-1-i;j++)
{
if(stu[j].gpa<stu[j+1].gpa)
{
temp.name=stu[j].name;temp.sex=stu[j].sex;temp.birth=stu[j].birth;temp.id=stu[j].id;
temp.name=stu[j].name;temp.total=stu[j].total;temp.gpa=stu[j].gpa;
for(k=0;k<5;k++)
{
temp.course[k]=stu[j].course[k];
temp.score[k]=stu[j].score[k];
}
stu[j].name=stu[j+1].name;stu[j].sex=stu[j+1].sex;stu[j].birth=stu[j+1].birth;stu[j].id=stu[j+1].id;
stu[j].name=stu[j+1].name;stu[j].total=stu[j+1].total;stu[j].gpa=stu[j+1].gpa;
for(k=0;k<5;k++)
{
stu[j].course[k]=stu[j+1].course[k];
stu[j].score[k]=stu[j+1].score[k];
}
stu[j+1].name=temp.name;stu[j+1].sex=temp.sex;stu[j+1].birth=temp.birth;stu[j+1].id=temp.id;
stu[j+1].name=temp.name;stu[j+1].total=temp.total;stu[j+1].gpa=temp.gpa;
for(k=0;k<5;k++)
{
stu[j+1].course[k]=temp.course[k];
stu[j+1].score[k]=temp.score[k];
}
}
}
}
ofstream outfile("Undergraduate_score_list.dat");//打開檔案
cout<<"姓名 學号 課程名1 score1 課程名2 score2 課程名3 score3 課程名4 score4 課程名5 score5\t績點"<<endl;
for(i=0;i<N;i++)
{
outfile<<stu[i].name<<" "<<stu[i].id<<" ";//寫入檔案
cout<<stu[i].name<<" "<<stu[i].id<<" ";//列印終端
for(j=0;j<5;j++)
{
outfile<<stu[i].course[j]<<"\t"<<stu[i].score[j]<<"\t";
cout<<stu[i].course[j]<<"\t"<<stu[i].score[j]<<"\t";
}
outfile<<stu[i].gpa<<endl;
cout<<stu[i].gpa<<endl;
}
outfile.close(); //關閉檔案
}
void sling(const Undergraduate *s)//學生挂科情況
{
int N=s[0].N;
int i,j,count=0;
Undergraduate stu[N];
ifstream infile("Undergraduate.dat");//打開檔案
if(!infile)
{
cout<<"open error!"<<endl;
}
for(i=0;i<N;i++)//從檔案讀
infile>>stu[i];
infile.close(); //關閉檔案
for(i=0;i<N;i++)
{
count=0;
for(j=0;j<5;j++)
{
if(stu[i].score[j]<60)
{
count++;//統計挂科數
}
}
cout<<stu[i]<<"\t"<<"有"<<count<<"門課挂科"<<endl; //列印學生成績等資訊和挂科的情況
}
}
void Undergraduate::menu(Undergraduate *s)//學生成績管理系統菜單
{
Undergraduate STU;
while(1)
{
system("color b1");
cout<<"*===============================================================================================*"<<endl;
cout<<"**&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&歡迎使用學生成績管理系統&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&**"<<endl;
cout<<"** ①輸入學生資訊 **"<<endl;
cout<<"** ②增加學生資訊 **"<<endl;
cout<<"** ③删除學生資訊 **"<<endl;
cout<<"** ④查找學生資訊 **"<<endl;
cout<<"** ⑤修改學生資訊 **"<<endl;
cout<<"** ⑥學生成績排序 **"<<endl;
cout<<"** ⑦學生挂科情況 **"<<endl;
cout<<"** ⑧顯示學生資訊 **"<<endl;
cout<<"** 〇 退出 **"<<endl;
cout<<"**&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&**" <<endl;
cout<<"*===============================================================================================*"<<endl;
cout<<"請輸入您要選擇的服務種類:( 0 ~ 8)"<<endl;
int num;
cin>>num;
switch(num)
{
case 1 :system("CLS");STU.input(s);system("pause");break;
case 2 :system("CLS");STU.add(s);system("pause");break;
case 3 :system("CLS");STU.remove(s);system("pause");break;
case 4 :system("CLS");search(s);system("pause");break;
case 5 :system("CLS");STU.change(s);system("pause");break;
case 6 :system("CLS");sort1(s);system("pause");break;
case 7 :system("CLS");sling(s);system("pause");break;
case 8 :system("CLS");STU.display();system("pause");break;
case 0 :system("CLS");cout<<"謝謝使用!";exit(0);system("pause");
default:system("CLS");printf("無效輸入!\n\n");system("pause");
}
}
}
int main()
{
Undergraduate undergraduate;
Undergraduate stu[80];
undergraduate.menu(stu);
return 0;
}
`4.結束語
我設計的學生成績管理系統的缺點,缺點很多,由于能力有限,隻是實作了簡單的幾個功能,設計的學生成績管理系統很脆弱,很容易就崩了。
優點:能夠對輸入成績按績點排序,統計挂科數,對輸入錯誤的生日,成績進行異常處理。
缺點一:沒有動态的開辟空間,對指針不熟,沒有用連結清單增删學生資訊,就定義80大小對象數組存放學生成績等資訊。
缺點二:每次使用都需要先輸入學生成績等資訊。等到下一次使用時,上一次輸入學生的成績就沒了。
缺點三:需要依次輸入學生專業,姓名,性别,年,月,日,學号,課程1,成績1......該輸入int,輸入string。多輸入或少輸入資訊。系統都可能會崩。
......
缺點很多,這些問題仍需繼續解決。
通過課程設計,發現了自己在知識上還存在很多問題。學到了不少東西,這個學生成績管理系統不能很好展現繼承和多态性。雖然用到了繼承,但用的并不好。我覺得課程設計考察對知識的綜合運用,用到了學過的很多知識,通過這次機會,找到自己的知識漏洞,查課本,查資料,收獲很多。