小白模拟实现一个班级 学生期末成绩系统
数据: 姓名 学号 性别 5门成绩 (大物 大英 大数 大思 大C)
#include<iostream>
#include<string>
#include<cstring>
#include <pthread.h>
#include<windows.h>
#include<fstream>
#include<iomanip>
using namespace std;
template<typename T>
class Person{
protected:
T Name;
T ID;
T Sex;
public:
Person(){}
Person(T name,T id,T sex);
Person(Person&);
~Person(){}
virtual void Show();
T GetID();
T GetName();
T GetSex();
void SetID(T);
void SetName(T);
void SetSex(T);
};
int FaliCount[5], PassCount[5];
template<typename T>
class Student:public Person<T>{
protected:
double* Score;
double Sum;
public:
Student(Student&);
Student();
Student(T name,T id,T sex);
~Student(){delete[]Score;}
void Show();
void SetScore();
operator=(Student& stu){
this->Name=stu.Name;
this->ID=stu.ID;
this->Sex=stu.Sex;
this->Sum=stu.Sum;
this->Score=new double[5];
for(int i=0;i<5;i++)
this->Score[i]=stu.Score[i];
}
friend istream&operator>>(istream& input,Student& stu){
input>>stu.Name>>stu.ID>>stu.Sex;
for(int i=0;i<5;i++){
input>>stu.Score[i];
stu.Sum+=stu.Score[i];
if(stu.Score[i]<60)
FaliCount[i]++;
else
PassCount[i]++;
}
return input;
}
friend ostream&operator<<(ostream& print,Student& stu){
print<<stu.Name<<" "<<stu.ID<<" "<<stu.Sex<<endl;
for(int i=0;i<5;i++)
print<<stu.Score[i]<<" ";
print<<endl;
return print;
}
double ScoreSum();
private:
void SetScore(int);
};
template<typename T>
class Class{
protected:
Student<T>* stus;
int Size;//学生人数
int BeginSize;//便于省空间
public:
Class();
~Class();
bool AddStu();
void ShowALLStus();
bool SortBySum();
bool SortByID();
bool Search();
bool ClearTxT();
bool Update();
void SavaData();
void Logging();
void ShowSomeFail();
bool DeleteStu();
private:
void DeleteStu(T);
void Update(T);
bool Save();
bool Lodding();
int BinarySearch(T,int,int);
Student<T>* ExpandSize();//空间不足增加空间
void AddStu(const int& count);
bool SortByName();
void SortBySum(Student<T>*,int,int);
void SortByID(int,int);
void SortByName(int,int);
int partitionBySum(Student<T>*,int,int);
int partitionByID(int,int);
int partitionByName(int,int);
void Clear();
};
inline void JustShowMenu();
inline void Option();
inline void JustMakeFun();
int main(){
Option();
JustMakeFun();
return 0;
}
inline void Option(){
memset(FaliCount,0,sizeof(int));
memset(PassCount,0,sizeof(int));
Class<string> t;
JustShowMenu();
int select;
while(true){
cout<<"Select your choice:";
cin>>select;
switch(select){
case 1:
t.ShowALLStus();
break;
case 2:
t.SortBySum();
break;
case 3:
t.SortByID();
break;
case 4:
t.Search();
break;
case 5:
t.DeleteStu();
break;
case 6:
t.ShowSomeFail();
break;
case 7:
t.Update();
break;
case 8:
t.AddStu();
break;
case 9:
t.SavaData();
break;
case 0:
return;
default:
cout<<"你的选择有误,请重试,为你显示菜单"<<endl;
JustShowMenu();
}
}
}
inline void JustShowMenu(){
cout<<"\t\t\t*****$$$$$*****$$$$$*****$$$$$*****$$$$$*****$$$$$*****"<<endl;
cout<<"\t\t\t$$$ &----------->Weclome<---------@ $$$"<<endl;
cout<<"\t\t\t$$$ $&*&*&*&*&*&*Select$%$%%$%$%$%@ $$$"<<endl;
cout<<"\t\t\t$$$ %-_-~_~-!_+3@your+_+-_-!~#&#*#! $$$"<<endl;
cout<<"\t\t\t$$$ @_-_-_-_-_-_>choice<_-_-_-_-_-! $$$"<<endl;
cout<<"\t\t\t*****$$$$$*****$$$$$*****$$$$$*****$$$$$*****$$$$$*****"<<endl;
cout<<"\t\t\t$$$ &&*&& $$$"<<endl;
cout<<"\t\t\t$$$1:显示所有学生 \t6:显示各科目不及格人数 $$$"<<endl;
cout<<"\t\t\t$$$2:按总分排序学生\t7:更新某个学生数据 $$$"<<endl;
cout<<"\t\t\t$$$3:按学号排序学生\t8:增加学生数据 $$$"<<endl;
cout<<"\t\t\t$$$4:按姓名查找学生\t9:保存 $$$"<<endl;
cout<<"\t\t\t$$$5:删除个学生数据\t0:退出 $$$"<<endl;
cout<<"\t\t\t$$$ &&*&& $$$"<<endl;
cout<<"\t\t\t$$$$$**********$$$$$后续功能&待添加$$$$$**********$$$$$"<<endl;
cout<<"\t\t\t$$$ &&*&& $$$"<<endl;
cout<<"\t\t\t*****_____*****_____*****_____*****_____*****_____*****"<<endl;
}
template<typename T>
void Class<T>::ShowSomeFail(){
if(Size==0){
cout<<"无数据"<<endl;
return;
}
cout<<"数据如下: ★表示及格 ☆表示不及格"<<endl;
cout<<setw(10)<<" 大物"<<setw(10)<<"大英"<<setw(10)<<"大数"<<setw(10)<<"大思"<<setw(10)<<"大C+"<<endl<<endl;
int Count[5];
bool flag;
for(int i=0;i<5;i++){
Count[i]=PassCount[i];
if(Count[i]!=0)
flag=true;
}
while(flag){
for(int i=0;i<5;i++)
if(Count[i]){
Count[i]--;
cout<<setw(10)<<"★";
}
else
cout<<setw(10)<<"☆";
cout<<endl<<endl;
if(!Count[0]&&!Count[1]&&!Count[2]&&!Count[3]&&!Count[4])
break;
}
cout<<setw(10)<<" 大物"<<setw(10)<<"大英"<<setw(10)<<"大数"<<setw(10)<<"大思"<<setw(10)<<"大C+"<<endl<<endl;
cout<<" 大物:及格人数:"<<PassCount[0]<<" 不及格人数:"<<Size-PassCount[0]<<" 及格率:"<<1.0*PassCount[0]/Size<<endl;
cout<<" 大英:及格人数:"<<PassCount[1]<<" 不及格人数:"<<Size-PassCount[1]<<" 及格率:"<<1.0*PassCount[1]/Size<<endl;
cout<<" 大数:及格人数:"<<PassCount[2]<<" 不及格人数:"<<Size-PassCount[2]<<" 及格率:"<<1.0*PassCount[2]/Size<<endl;
cout<<" 大思:及格人数:"<<PassCount[3]<<" 不及格人数:"<<Size-PassCount[3]<<" 及格率:"<<1.0*PassCount[3]/Size<<endl;
cout<<" 大C+:及格人数:"<<PassCount[4]<<" 不及格人数:"<<Size-PassCount[4]<<" 及格率:"<<1.0*PassCount[4]/Size<<endl;
}
template<typename T>
bool Class<T>::ClearTxT(){
Clear();
cout<<"清除数据成功"<<endl;
return true;
}
template<typename T>
void Class<T>::Clear(){
ifstream o;
o.open("DataFile.txt",ios::trunc);
o.close();
}
template<typename T>
Person<T>::Person(T name,T id,T sex)
:Name(name),ID(id),Sex(sex){
}
template<typename T>
Person<T>::Person(Person& old){
this->Name=old.Name;
this->ID=old.ID;
this->Sex=old.Sex;
}
template<typename T>
void Person<T>::Show(){
cout<<"姓名:"<<Name<<" 学号:"<<ID<<" 性别:"<<Sex<<endl;
}
template<typename T>
T Person<T>::GetID(){
return ID;
}
template<typename T>
T Person<T>::GetSex(){
return Sex;
}
template<typename T>
T Person<T>::GetName(){
return Name;
}
template<typename T>
void Person<T>::SetID(T id){
ID=id;
}
template<typename T>
void Person<T>::SetName(T name){
Name=name;
}
template<typename T>
void Person<T>::SetSex(T sex){
Sex=sex;
}
template<typename T>
Student<T>::Student(Student& old){
this->Name=old.Name;
this->ID=old.ID;
this->Sex=old.Sex;
this->Sum=old.Sum;
this->Score=new double[5];
for(int i=0;i<5;i++)
this->Score[i]=old.Score[i];
}
template<typename T>
void Student<T>::SetScore(){
int WhichSubject;
cout<<"输入你要修改的科目:\n"
<<"1.大物 2.大英 3.大数 4.大思 5.大C"<<endl;
cin>>WhichSubject;
if(WhichSubject<0||WhichSubject>5)
cout<<"你的选择有误"<<endl;
else
SetScore(WhichSubject-1);
}
template<typename T>
void Student<T>::SetScore(int Index){
double score;
cout<<"输入学生"<<this->Name<<"的新分数:";
cin>>score;
if(score>=60&&Score[Index]<60){
PassCount[Index]++;
FaliCount[Index]--;
}else if(score<60&&Score[Index]>=60){
PassCount[Index]--;
FaliCount[Index]++;
}
this->Sum-=Score[Index];
this->Sum+=score;
cout<<"学生"<<this->Name<<"的分数由"<<Score[Index]<<"变更为:"<<score<<endl;
this->Score[Index]=score;
}
template<typename T>
double Student<T>::ScoreSum(){
return Sum;
}
template<typename T>
Student<T>::Student():Sum(0){
Score=new double[5];
if(Score==NULL){
cout<<"分配内存出错"<<endl;
exit(0);
}
}
template<typename T>
Student<T>::Student(T name,T id,T sex):Person<T>(name,id,sex){
Score=new double[5];
if(Score==NULL){
cout<<"分配内存出错"<<endl;
exit(0);
}
cout<<">>>依次输入5门成绩:(大物 大英 大数 大思 大C)";
for(int i=0;i<5;i++)
cin>>Score[i];
}
template<typename T>
void Student<T>::Show(){
Person<T>::Show();
cout<<"大物"<<setw(5)<<"大英"<<setw(5)<<"大数"<<setw(5)<<"大思"<<setw(4)<<"大C"<<setw(5)<<"总分"<<endl;
for(int i=0;i<=5;i++){
if(!i)
cout<<" "<<Score[i];
else if(i<5)
cout<<setw(5)<<Score[i];
else
cout<<setw(5)<<Sum<<endl<<endl;
}
}
template<typename T>
bool Class<T>::DeleteStu(){
if(Size==0){
cout<<"无数据"<<endl;
return false;
}
cout<<"输入要删除的学生名字:";
T name;
cin>>name;
DeleteStu(name);
}
template<typename T>
void Class<T>::DeleteStu(T name){
this->SortByName();
int res=BinarySearch(name,0,this->Size);
if(res==-1)
cout<<"不存在学生"<<name<<endl;
else{
cout<<"学生信息如下:" <<endl;
this->stus[res].Show();
T name=stus[res].GetName();
for(int i=res;i<Size-1;i++)
stus[i]=stus[i+1];
Size--;
cout<<"删除学生"<<name<<"成功"<<endl;
}
}
template<typename T>
bool Class<T>::Save(){
if(this->Size==0){
cout<<"无数据可存"<<endl;
return false;
}
ofstream save;
save.open("DataFile.txt",ios::out | ios::trunc);
if(!save.is_open()){
cout<<"文件存取失败"<<endl;
return false;
}
for(int i=0;i<Size;i++)
save<<stus[i]<<endl;
save.close();
if(Size)
cout<<"存取成功,存入了"<<this->Size<<"个数据"<<endl;
return true;
}
template<typename T>
bool Class<T>::Lodding(){
ifstream read;
read.open("DataFile.txt", ios::out | ios::in);
if(!read.is_open()){
cout<<"文件读取失败"<<endl;
return false;
}
while(true){
if(Size>=BeginSize)
this->stus=ExpandSize();
read>>stus[Size];
if(read.eof())
break;
Size++;
}
read.close();
if(Size)
cout<<"读取成功,加载了"<<this->Size<<"个数据"<<endl;
else{
cout<<"文件无数据可加载"<<endl;
return false;
}
return true;
}
template<typename T>
void Class<T>::ShowALLStus(){
if(!Size){
cout<<"无数据可显示"<<endl;
return;
}
for(int i=0;i<Size;i++)
stus[i].Show();
}
template<typename T>
Student<T>* Class<T>::ExpandSize(){
BeginSize=BeginSize+(BeginSize>>1);
Student<T>* Newstus=new Student<T>[this->BeginSize];
for(int i=0;i<Size;i++)
Newstus[i]=stus[i];
return Newstus;
}
template<typename T>
bool Class<T>::AddStu(){
cout<<">>>输入录入学生人数:";
int count;
cin>>count;
AddStu(count);
return true;
}
template<typename T>
void Class<T>::AddStu(const int& count){
if(count>BeginSize)
this->stus=ExpandSize();
int cnt=count;
while(cnt--){
cout<<">>>依次输入姓名 学号 性别及5门成绩:"<<endl;
cin>>stus[Size++];
}
cout<<"录入"<<count<<"位学生,共"<<Size<<"位学生"<<endl;
}
template<typename T>
Class<T>::Class()
:Size(0),BeginSize(20){
stus=new Student<T>[BeginSize];
if(stus==NULL){
cout<<"分配内存出错"<<endl;
exit(0);
}
char flag;
cout<<">>>是否加载数据(Y/N):";
cin>>flag;
if(flag=='Y'||flag=='y')
this->Lodding();
else
cout<<"注意数据的加载!"<<endl;
}
template<typename T>
Class<T>::~Class(){
char flag;
cout<<">>>是否保存数据(Y/N):";
cin>>flag;
if(flag=='Y'||flag=='y')
Save();
else
cout<<"很高兴为您服务,欢迎下次光临!"<<endl;
delete[]stus;
}
template<typename T>
void Class<T>::SavaData(){
bool flag=Save();
if(!flag)
cout<<"保存失败"<<endl;
}
template<typename T>
void Class<T>::Logging(){
bool flag=Lodding();
if(!flag)
cout<<"读取失败"<<endl;
}
template<typename T>
bool Class<T>::Update(){
if(!Size){
cout<<"无数据"<<endl;
return false;
}
cout<<"输入你要更新的姓名:";
T name;
cin>>name;
Update(name);
}
template<typename T>
void Class<T>::Update(T name){
int res=BinarySearch(name,0,this->Size);
if(res==-1)
cout<<"不存在学生"<<name<<endl;
else{
cout<<"学生信息如下:(select what you want to update):" <<endl;
this->stus[res].Show();
cout<<"1.姓名 2.学号 3.性别 4.5门成绩"<<endl;
int select;
T New;
cin>>select;
switch(select){
case 1:
cout<<">>>输入变换的名字:";
cin>>New;
cout<<"学生"<<stus[res].GetName()<<"的名字更改为"<<New;
stus[res].SetName(New);
break;
case 2:
cout<<">>>输入变换的学号:";
cin>>New;
cout<<"学生"<<stus[res].GetName()<<"的学号"<<stus[res].GetID()<<"更改为"<<New;
stus[res].SetID(New);
break;
case 3:
cout<<">>>输入变换的性别:";
cin>>New;
cout<<"学生"<<stus[res].GetName()<<"的性别"<<stus[res].GetSex()<<"更改为"<<New<<endl;
stus[res].SetSex(New);
break;
case 4:
int change;
do{
stus[res].SetScore();
cout<<"是否继续修改(1/0):";
cin>>change;
}while(change);
break;
default:
cout<<"Your choice is error,please retry"<<endl;
}
cout<<"修改成功,新信息如下:"<<endl;
stus[res].Show();
}
}
template<typename T>
bool Class<T>::Search(){
if(!Size){
cout<<"无数据"<<endl;
return false;
}
cout<<">>>输入你要查找学生的姓名:";
T name;
cin>>name;
this->SortByName();
int Index=BinarySearch(name,0,this->Size);
if(Index==-1){
cout<<"学生"<<name<<"不存在!"<<endl;
return -1;
}
cout<<"学生所在排名:"<<Index+1<<endl;
cout<<"其他数据如下:"<<endl;
this->stus[Index].Show();
return true;
}
template<typename T>
int Class<T>::BinarySearch(T x,int low,int high){
int i=low,j=high-1,mid;
while(i<=j){
mid=i+((j-i)>>1);
if(x>this->stus[mid].GetName())
i=mid+1;
else if(x<this->stus[mid].GetName())
j=mid-1;
else
return mid;
}
return -1;
}
template<typename T>
bool Class<T>::SortBySum(){
if(Size==1||Size==0){
cout<<"数据不到2个还排什么?"<<endl;
return false;
}
SortBySum(stus,0,Size-1);
cout<<"排序成功"<<endl;
return true;
}
template<typename T>
void Class<T>::SortBySum(Student<T>* arr,int low,int high){
if(low<high){
int Index=partitionBySum(arr,low,high);
SortBySum(arr,low,Index-1);
SortBySum(arr,Index+1,high);
}
}
template<typename T>
int Class<T>::partitionBySum(Student<T>* arr,int low,int high){
int i=low,j=high;
Student<T> x=arr[low];
while(i<j){
while(i<j&&arr[j].ScoreSum()>=x.ScoreSum())j--;
if(i<j){
arr[i]=arr[j];
i++;
}
while(i<j&&arr[i].ScoreSum()<x.ScoreSum())i++;
if(i<j){
arr[j]=arr[i];
j--;
}
}
arr[i]=x;
return i;
}
template<typename T>
bool Class<T>::SortByID(){
if(Size==1||Size==0){
cout<<"数据不到2个 还排什么?"<<endl;
return false;
}
SortByID(0,Size-1);
cout<<"排序成功"<<endl;
return true;
}
template<typename T>
void Class<T>::SortByID(int low,int high){
if(low<high){
int Index=partitionByID(low,high);
SortByID(low,Index-1);
SortByID(Index+1,high);
}
}
template<typename T>
int Class<T>::partitionByID(int low,int high){
int i=low,j=high;
Student<T> x(stus[low]);
while(i<j){
while(i<j&&stus[j].GetID()>=x.GetID())j--;
if(i<j){
stus[i]=stus[j];
i++;
}
while(i<j&&stus[i].GetID()<x.GetID())i++;
if(i<j){
stus[j]=stus[i];
j--;
}
}
stus[i]=x;
return i;
}
template<typename T>
bool Class<T>::SortByName(){
if(Size==1||Size==0)
return false;
SortByName(0,Size-1);
return true;
}
template<typename T>
void Class<T>::SortByName(int low,int high){
if(low<high){
int Index=partitionByID(low,high);
SortByName(low,Index-1);
SortByName(Index+1,high);
}
}
template<typename T>
int Class<T>::partitionByName(int low,int high){
int i=low,j=high;
Student<T> x(stus[low]);
while(i<j){
while(i<j&&stus[j].GetName()>=x.GetName())j--;
if(i<j){
stus[i]=stus[j];
i++;
}
while(i<j&&stus[i].GetName()<x.GetName())i++;
if(i<j){
stus[j]=stus[i];
j--;
}
}
stus[i]=x;
return i;
}
inline void JustMakeFun(){
int time=300;
Sleep(time);
cout<<" ★★★★★★★★ ★ ★ ★★★★★★★★★ ★"<<endl;
Sleep(time);
cout<<" ★ ★ ★ ★ ★ ★"<<endl;
Sleep(time);
cout<<" ★ ★ ★ ★ ★ ★"<<endl;
Sleep(time);
cout<<" ★ ★ ★ ★ ★"<<endl;
Sleep(time);
cout<<" ★★★★★★★★ ★ ★★★★★★★★★★ ★"<<endl;
Sleep(time);
cout<<" ★ ★ ★ ★ ★"<<endl;
Sleep(time);
cout<<" ★ ★ ★ ★ ★"<<endl;
Sleep(time);
cout<<" ★ ★ ★ ★ ★"<<endl;
Sleep(time);
cout<<" ★★★★★★★★ ★ ★★★★★★★★★ ☆"<<endl<<endl;
time-=200;
Sleep(time);
cout<<" ★ ★ ★ "<<endl;
Sleep(time);
cout<<" ★ ★ ★ "<<endl;
Sleep(time);
cout<<" ★ ★ ★ "<<endl;
Sleep(time);
cout<<" ★ ★ ★ "<<endl;
Sleep(time);
cout<<" ★ ★ ★ "<<endl;
Sleep(time);
cout<<" ★ ★ ★ "<<endl;
Sleep(time);
cout<<" ★ ★★★★★★★★★★★★★ ★★★★★★★★★★★★★"<<endl;
Sleep(time);
cout<<" ★ ★ ★ "<<endl;
Sleep(time);
cout<<" ★ ★ ★ "<<endl;
Sleep(time);
cout<<" ★ ★ ★ "<<endl;
Sleep(time);
cout<<" ★ ★ ★ "<<endl;
Sleep(time);
cout<<" ★ ★ ★ "<<endl;
Sleep(time);
cout<<" ★ ★ ★ "<<endl;
Sleep(time);
}