//所用的头文件
#include
#include
#include
#include
char strinfo[80];
int nCountRs;//统计输入成绩的数目
class CStudent//自定义类(类型)CStudent
{
public:
CStudent();//构造函数
void Display();//显示所有学生数据
float Validate();//输入数据
void Setname(char *p);//输入姓名
void Setxuehao(char *p);//输入学号
void Setbanji(char *p);//输入班级
void Setc(float i,float j,float k,float m,float n);//五门功课成绩
float Getyingyu();//英语成绩
float GetC();//C++成绩
float Getshuxue();//数学成绩
float GetWuli();//物理成绩
float GetHuaxue();//化学成绩
float GetAve();//平均成绩
char *GetName();//输入姓名
char *Getxuehao();//输入学号
char *Getbanji();//输入班级
CStudent &operator=(CStudent &stu);//重载运算符“=”
private:
char name[20];//姓名
char xuehao[20];//学号
char banji[20];//班级
float fyingyu;//英语
float fC;//C++
float fshuxue;//数学
float fWuli;//物理
float fHuaxue;//化学
float fCount;//总成绩
float fAve;//平均成绩
};
CStudent &CStudent::operator=(CStudent &stu)//重载运算符“=”
{
strcpy(name,stu.name);//strcpy需要的头文件是string.h
strcpy(xuehao,stu.xuehao);strcpy(banji,stu.banji);
fyingyu=stu.fyingyu;fC=stu.fC;fshuxue=stu.fshuxue;fWuli=stu.fWuli;fHuaxue=stu.fHuaxue;
fCount=stu.fCount;fAve=stu.fAve;
return *this;//返回this指针
}
CStudent::CStudent()//初始化构造函数
{
strcpy(name,"");strcpy(xuehao,"");strcpy(banji,"");
fyingyu=0;fC=0;fshuxue=0;fWuli=0;fHuaxue=0;fCount=0;fAve=0;
}
void CStudent::Display()//显示学生的信息
{
cout<
float CStudent::Validate()//输入学生成绩
{
float i;
cin>>i;
return i;
}
char * CStudent::GetName()
{
return (char *)name;
}
char * CStudent::Getxuehao()
{
return (char *)xuehao;
}
char * CStudent::Getbanji()
{
return (char *)banji;
}
float CStudent::Getyingyu()
{
return fyingyu;
}
float CStudent::GetC()
{
return fC;
}
float CStudent::Getshuxue()
{
return fshuxue;
}
float CStudent::GetWuli()
{
return fWuli;
}
float CStudent::GetHuaxue()
{
return fHuaxue;
}
float CStudent::GetAve()
{
return fAve;
}
void CStudent::Setname(char *p)
{
strncpy(name,p,20);
}
void CStudent::Setxuehao(char *p)
{
strncpy(xuehao,p,20);
}
void CStudent::Setbanji(char *p)
{
strncpy(banji,p,20);
}
void CStudent::Setc(float i,float j,float k,float m,float n)//总分
{
fyingyu=i;
fC=j;
fshuxue=k;
fWuli=m;
fHuaxue=n;
fCount=i+j+k+m+n;
fAve=fCount/5.0;
}
class CStufile
{
public:
void AddTo(CStudent stu);
void Delete(char *p);
void Sort();
int Seekxuehao(char *p);
int SeekName(char *p);
int Seekbanji(char *p);
void List();
void GetCount();
};
void CStufile::Delete(char *p)
{
fstream file2;
char ch;
CStudent stu;
CStudent *Csortstu=new CStudent[::nCountRs];
int i=0,j=0,l=0;
char strName[20],strxuehao[20],strbanji[20];
float fa,fb,fc,ff,fg,fd,fe;
const int size=sizeof(CStudent);
file2.open("stu.dat",ios::out|ios::in);
file2.read(&ch,1);
if(file2.eof())
{
strcpy(::strinfo,"数据库为空,请添加数据");
}
else
{
while(!file2.eof())
{
file2.seekp(size*i,ios::beg);
file2.read(strName,20);
file2.read(strxuehao,20);
file2.read(strbanji,20);
file2.read((char*)&fa,4);
file2.read((char*)&fb,4);
file2.read((char*)&fc,4);
file2.read((char*)&ff,4);
file2.read((char*)&fg,4);
stu.Setname(strName);
stu.Setxuehao(strxuehao);
stu.Setbanji(strbanji);
stu.Setc(fa,fb,fc,ff,fg);
if(!file2.eof())
{
Csortstu[j]=stu;
j++;
}
i++;
file2.read(&ch,1);
}
}
file2.close();
file2.open("stu.dat",ios::out|ios::in|ios::trunc);
if(::nCountRs>0)
{
for(l=0;l<::nCountRs;l++)
{
if(strcmp(p,Csortstu[l].Getxuehao()))
{
fa=Csortstu[l].Getyingyu();
fb=Csortstu[l].GetC();
fc=Csortstu[l].Getshuxue();
ff=Csortstu[l].GetWuli();
fg=Csortstu[l].GetHuaxue();
fd=fa+fb+fc+ff+fg;
fe=fd/5.0;
file2.write(Csortstu[l].GetName(),20);
file2.write(Csortstu[l].Getxuehao(),20);
file2.write(Csortstu[l].Getbanji(),20);
file2.write((char*)&fa,4);
file2.write((char*)&fb,4);
file2.write((char*)&fc,4);
file2.write((char*)&fd,4);
file2.write((char*)&fe,4);
}
}
::nCountRs--;
}
delete []Csortstu;
}
void CStufile::Sort()
{
fstream file2;
char ch;
CStudent stu;
CStudent *Csortstu=new CStudent[::nCountRs];
int j=0;
const int size=sizeof(CStudent);
file2.open("stu.dat",ios::out|ios::in);
char strName[20],strxuehao[20],strbanji[20];
float fa,fb,fc,ff,fg;
int i=0;
file2.read(&ch,1);
if(file2.eof())
{
strcpy(::strinfo,"数据库为空,请添加数据");
}
else
{
while(!file2.eof())
{
file2.seekp(size*i,ios::beg);
file2.read(strName,20);
file2.read(strxuehao,20);
file2.read(strbanji,20);
file2.read((char*)&fa,4);
file2.read((char*)&fb,4);
file2.read((char*)&fc,4);
file2.read((char*)&ff,4);
file2.read((char*)&fg,4);
stu.Setname(strName);
stu.Setxuehao(strxuehao);
stu.Setbanji(strbanji);
stu.Setc(fa,fb,fc,ff,fg);
if(!file2.eof())
{
Csortstu[j]=stu;
j++;
}
i++;
file2.read(&ch,1);
}
}
if(::nCountRs>0)
{
for(int l=0;l<::nCountRs;l++)
{
for(j=0;j<(::nCountRs-l);j++)
{
if(Csortstu[j].GetAve()
stu=Csortstu[j];
Csortstu[j]=Csortstu[j+1];
Csortstu[j+1]=stu;
}
}
}
for(l=0;l<::nCountRs;l++)
{
Csortstu[l].Display();
}
cout<<"数据显示完毕! 输入任意符号返回:";
cin>>ch;
}
file2.close();
delete []Csortstu;
}
int CStufile::Seekxuehao(char *p)//按学号查找
{
int nReturn=0;
fstream file1;
CStudent stu;
char strName[20],strxuehao[20],strbanji[20];
char ch;
const int size=sizeof(CStudent);
float fa,fb,fc,ff,fg;
int i=0;
strcpy(::strinfo,"没有找到要查找的数据!");
file1.open("stu.dat",ios::out|ios::in);
file1.read(&ch,1);
if(file1.eof())
{
strcpy(::strinfo,"数据库为空,请添加数据");
}
else
{
while(!file1.eof())
{
file1.seekp(size*i,ios::beg);
file1.read(strName,20);
file1.read(strxuehao,20);
file1.read(strbanji,20);
file1.read((char*)&fa,4);
file1.read((char*)&fb,4);
file1.read((char*)&fc,4);
file1.read((char*)&ff,4);
file1.read((char*)&fg,4);
stu.Setname(strName);
stu.Setxuehao(strxuehao);
stu.Setbanji(strbanji);
stu.Setc(fa,fb,fc,ff,fg);
if(!file1.eof())
{
if(!strcmp(p,stu.Getxuehao()))
{
stu.Display();
cout<<"数据显示完毕! 输入任意符号返回:";
cin>>ch;
nReturn=1;
strcpy(::strinfo,"");
break;
}
}
i++;
file1.read(&ch,1);
}
}
return nReturn;
file1.close();
}
int CStufile::SeekName(char *p)//按姓名查找
{
int nReturn=0;
fstream file1;
CStudent stu;
char strName[20],strxuehao[20],strbanji[20];
char ch;
const int size=sizeof(CStudent);
float fa,fb,fc,ff,fg;
int i=0;
strcpy(::strinfo,"没有找到要查找的数据!");
file1.open("stu.dat",ios::out|ios::in);
file1.read(&ch,1);
if(file1.eof())
{
strcpy(::strinfo,"数据库为空,请添加数据");
}
else
{
while(!file1.eof())
{
file1.seekp(size*i,ios::beg);
file1.read(strName,20);
file1.read(strxuehao,20);
file1.read(strbanji,20);
file1.read((char*)&fa,4);
file1.read((char*)&fb,4);
file1.read((char*)&fc,4);
file1.read((char*)&ff,4);
file1.read((char*)&fg,4);
stu.Setname(strName);
stu.Setxuehao(strxuehao);
stu.Setbanji(strbanji);
stu.Setc(fa,fb,fc,ff,fg);
if(!file1.eof())
{
if(!strcmp(p,stu.GetName()))
{
stu.Display();
cout<<"数据显示完毕! 输入任意符号返回:";
cin>>ch;
nReturn=1;
strcpy(::strinfo,"");
break;
}
}
i++;
file1.read(&ch,1);
}
}
return nReturn;
file1.close();
}
int CStufile::Seekbanji(char *p)//按班级查找
{
int nReturn=0;
fstream file1;
CStudent stu;
char strName[20],strxuehao[20],strbanji[20];
char ch;
const int size=sizeof(CStudent);
float fa,fb,fc,ff,fg;
int i=0;
strcpy(::strinfo,"没有找到要查找的数据!");
file1.open("stu.dat",ios::out|ios::in);
file1.read(&ch,1);
if(file1.eof())
{
strcpy(::strinfo,"数据库为空,请添加数据");
}
else
{
while(!file1.eof())
{
file1.seekp(size*i,ios::beg);
file1.read(strName,20);
file1.read(strxuehao,20);
file1.read(strbanji,20);
file1.read((char*)&fa,4);
file1.read((char*)&fb,4);
file1.read((char*)&fc,4);
file1.read((char*)&ff,4);
file1.read((char*)&fg,4);
stu.Setname(strName);
stu.Setxuehao(strxuehao);
stu.Setbanji(strbanji);
stu.Setc(fa,fb,fc,ff,fg);
if(!file1.eof())
{
if(!strcmp(p,stu.Getbanji()))
{
stu.Display();
cout<<"数据显示完毕! 输入任意符号返回:";
cin>>ch;
nReturn=1;
strcpy(::strinfo,"");
break;
}
}
i++;
file1.read(&ch,1);
}
}
return nReturn;
file1.close();
}
void CStufile::List()
{
fstream file2;
char ch;
CStudent stu;
const int size=sizeof(CStudent);
file2.open("stu.dat",ios::out|ios::in);
char strName[20],strxuehao[20],strbanji[20];
float fa,fb,fc,ff,fg;
int i=0;
file2.read(&ch,1);
cout<<"共有 "<<::nCountRs<<" 条记录."<
{
strcpy(::strinfo,"数据库为空,请添加数据");
}
else
{
while(!file2.eof())
{
file2.seekp(size*i,ios::beg);
file2.read(strName,20);
file2.read(strxuehao,20);
file2.read(strbanji,20);
file2.read((char*)&fa,4);
file2.read((char*)&fb,4);
file2.read((char*)&fc,4);
file2.read((char*)&ff,4);
file2.read((char*)&fg,4);
stu.Setname(strName);
stu.Setxuehao(strxuehao);
stu.Setbanji(strbanji);
stu.Setc(fa,fb,fc,ff,fg);
if(!file2.eof())
stu.Display();
i++;
file2.read(&ch,1);
}
cout<<"数据显示完毕! 输入任意符号返回:";
cin>>ch;
}
file2.close();
}
void CStufile::AddTo(CStudent stu)
{
fstream file1;
float a,b,c,f,g,d,e;
a=stu.Getyingyu();
b=stu.GetC();
c=stu.Getshuxue();
f=stu.GetWuli();
g=stu.GetHuaxue();
d=a+b+c+f+g;
e=d/5.0;
file1.open("stu.dat",ios::out|ios::in|ios::app);
file1.write(stu.GetName(),20);
file1.write(stu.Getxuehao(),20);
file1.write(stu.Getbanji(),20);
file1.write((char*)&a,4);
file1.write((char*)&b,4);
file1.write((char*)&c,4);
file1.write((char*)&f,4);
file1.write((char*)&g,4);
file1.write((char*)&d,4);
file1.write((char*)&e,4);
file1.close();
strcpy(::strinfo,"数据添加成功! ");
this->GetCount();
}
void CStufile::GetCount()
{
fstream file2;
char ch;
const int size=sizeof(CStudent);
file2.open("stu.dat",ios::out|ios::in);
int i=0;
file2.read(&ch,1);
while(!file2.eof())
{
file2.seekp(size*i,ios::beg);
::nCountRs=i++;
file2.read(&ch,1);
}
file2.close();
}
void SetInfo(CStudent &stu)
{
float di,dj,dk,dm,dn;
char p1[20],p2[20],p3[20];
cout<<"输入姓名:";
cin>>p1;
stu.Setname(p1);
cout<<"输入学号:";
cin>>p2;
stu.Setxuehao(p2);
cout<<"输入班级:";
cin>>p3;
stu.Setbanji(p3);
cout<<"输入英语成绩(必须是一个数):";
di=stu.Validate();
cout<<"输入C++成绩(必须是一个数):";
dj=stu.Validate();
cout<<"输入数学成绩(必须是一个数):";
dk=stu.Validate();
cout<<"输入物理成绩(必须是一个数):";
dm=stu.Validate();
cout<<"输入化学成绩(必须是一个数):";
dn=stu.Validate();
stu.Setc(di,dj,dk,dm,dn);
stu.Display();
}
void SetInfo(CStudent &stu);
void main()
{
CStudent A;
CStufile FA;
int i=0,T,intExit=0;
char strSekxuehao[20];
char strSekName[20];
char strSekbanji[20];
strcpy(strinfo,"");
FA.GetCount();
while(1)
{
system("cls");
cout<<" 学生成绩管理 \n"<
cout<<"1.显示所有学生数据."<
cin>>T;
switch(T)
{
case 1://显示所有学生数据
FA.List();
break;
case 2://按平均分排序
FA.Sort();
break;
case 3://添加新数据
SetInfo(A);
FA.AddTo(A);
break;
case 4://查找数据
cout<<"输入要查找的学号:";
cin>>strSekxuehao;
FA.Seekxuehao(strSekxuehao);
break;
case 5://查找数据
cout<<"输入要查找的姓名:";
cin>>strSekName;
FA.SeekName(strSekName);
break;
case 6:
cout<<"输入要查找的班级:";
cin>>strSekbanji;
FA.Seekbanji(strSekbanji);
break;
case 7://删除学生数据
strSekxuehao[0]='\0';
cout<<"输入要删除的学号:";
cin>>strSekxuehao;
cout<<"\n要删除的数据信息:"<
{
FA.Delete(strSekxuehao);
strcpy(::strinfo,"数据删除成功!");
}
else
{
strcpy(::strinfo,"删除失败,数据不存在");
}
break;
default://退出系统
intExit=1;
break;
}
if(intExit)
{
break;
}
}
}
//进入程序后,按a输入数据,按s统计及格与不及格人数,按e推出
#include
#include
using namespace std;
class student
{
private:
int result,count1,count2;
public:
student():count1(0),count2(0)
{}
void getdate()
{ cout<<"please input the result"<
result>=60?count1++:count2++ ;
}
void search()
{
cout<<"\nthe passer's count: "<
} ;
int main()
{
student stu;
int i,j=0,k;
cout<<"Press A to input data\nPress s to search data\nPress e to exit\n"<
for(;j!=27;)
{
switch(j)
{
case 'a':stu.getdate();break;
case 's':stu.search();break;
case 'e':cout<<"EXIT"<
j=getch();
}
return 0;
}
写的不错