满意答案
编程如下:
#include
#include
#include
#include //使用exit函数
class CStudent
{
private:
int number;
char name[20],sex;
double age,math,computer,English;
public:
CStudent() {}
CStudent(int num,char n[20],char se,double ag,double ma,double com,double Eng)
{
strcpy(name,n);number=num;sex=se;age=ag;math=ma;computer=com;English=Eng;
}
friend ostream& operator<
friend void main(); //将main函数定义为友元函数,能够调用类中的私有数据成员
};
ostream& operator<
{
output<
output<
output<
output<
output<
output<
output<
output<
return output;
}
void main()
{
cout<
cout<
cout<
cout<
cout<
cout<
cout<
cout<
cout<
cout<
cout<
cout<
cout<
cout<
cout<
char p,w;
CStudent *s[50];
ostream *file[50];
int i=0,j=0;
bool flag2=0;
do
{
cin>>p;
if(p>='0'&&p<='7')
flag2=1;
else cout<
}while(flag2==0);
do
{
switch(p)
{
case '0': //输入数据功能
{char c;
int number;
char name[20],sex;
double age,math,computer,English;
do
{
cout<
cin>>number;
cout<
cin>>name;
cout<
cin>>sex;
cout<
cin>>age;
cout<
cin>>math;
cout<
cin>>computer;
cout<
cin>>English;
file[j]=new ofstream("c:\\student.dat",ios::ate|ios::binary);
*file[j]<
j++;
s[i]=new CStudent(number,name,sex,age,math,computer,English);
i++;
cout<
cin>>c;
flag2=0;
do
{
if(c!='y'&&c!='n')
{
cout<
cin>>c;
}
else flag2=1;
}while(flag2==0);
}while(c=='y');
break;
}
case '1': //增加数据功能
{char c;
int number;
char name[20],sex;
double age,math,computer,English;
do
{
cout<
cin>>number;
cout<
cin>>name;
cout<
cin>>sex;
cout<
cin>>age;
cout<
cin>>math;
cout<
cin>>computer;
cout<
cin>>English;
file[j]=new ofstream("c:\\student.dat",ios::ate|ios::binary); //使用随机读写处理方式在c盘建立binary文件,使文件每次打开指针指向文件结尾
*file[j]<
j++;
s[i]=new CStudent(number,name,sex,age,math,computer,English);
i++;
cout<
cin>>c;
flag2=0;
do
{
if(c!='y'&&c!='n')
{
cout<
cin>>c;
}
else flag2=1;
}while(flag2==0);
}while(c=='y');
break;
}
case '2': //修改数据功能
{char c;
int num;
char n[20],se;
double ag,ma,com,Eng;
if(i==0)
{
cout<
break;
}
do
{
cout<
cin>>n;
for(int h=0;h
{
if(strcmp(n,s[h]->name)==0)
{
flag2=1;
cout<
cin>>num;
cout<
cin>>se;
cout<
cin>>ag;
cout<
cin>>ma;
cout<
cin>>com;
cout<
cin>>Eng;
s[h]->number=num;
s[h]->sex=se;
s[h]->age=ag;
s[h]->math=ma;
s[h]->computer=com;
s[h]->English=Eng;
cout<
}
}
if(flag2==0)
{
cout<
}
cout<
cin>>c;
flag2=0;
do
{
if(c!='y'&&c!='n')
{
cout<
cin>>c;
}
else flag2=1;
}while(flag2==0);
}while(c=='y');
break;
}
case '3': //按编号查询功能
{
int n,j=0;char c;
if(i==0)
{
cout<
break;
}
do
{
int flag=0;
cout<
cin>>n;
for(int j=0;j
{
if(s[j]->number==n)
{
flag=1;
cout<
cout<
}
}
if(flag==0)
cout<
cout<
cin>>c;
flag2=0;
do
{
if(c!='y'&&c!='n')
{
cout<
cin>>c;
}
else flag2=1;
}while(flag2==0);
}while(c=='y');
break;
}
case '4': //按姓名查询功能
{
char n[20];int j=0;char c;
if(i==0)
{
cout<
break;
}
do
{
int flag=0;
cout<
cin>>n;
for(int j=0;j
{
if(strcmp(n,(*s[j]).name)==0)
{
flag=1;
cout<
cout<
}
}
if(flag==0)
cout<
cout<
cin>>c;
flag2=0;
do
{
if(c!='y'&&c!='n')
{
cout<
cin>>c;
}
else flag2=1;
}while(flag2==0);
}while(c=='y');
break;
}
case '5': //统计总成绩和平均成绩功能
{
int n,j=0;double sum,Average;char c;
if(i==0)
{
cout<
break;
}
do
{
int flag=0;
cout<
cin>>n;
for(int j=0;j
{
if(s[j]->number==n)
{
flag=1;
sum=(*s[j]).math+(*s[j]).computer+(*s[j]).English;
Average=sum/3;
cout<
cout<
}
}
if(flag==0)
cout<
cout<
cin>>c;
flag2=0;
do
{
if(c!='y'&&c!='n')
{
cout<
cin>>c;
}
else flag2=1;
}while(flag2==0);
}while(c=='y');
break;
}
case '6': //按成绩与性别查询功能
{
double x,sum;char k,c;
if(i==0)
{
cout<
break;
}
do
{
int flag=0;
cout<
cin>>x;
cout<
cin>>k;
for(int j=0;j
{
sum=(*s[j]).math+(*s[j]).computer+(*s[j]).English;
if(sum>x&&k==s[j]->sex)
{
flag=1;
cout<
}
}
if(flag==0)
cout<
cout<
cin>>c;
flag2=0;
do
{
if(c!='y'&&c!='n')
{
cout<
cin>>c;
}
else flag2=1;
}while(flag2==0);
}while(c=='y');
break;
}
case '7': //退出系统功能
{
exit(0);
}
}
cout<
bool flag4=0;
do
{
cin>>w;
if(w!='y'&&w!='n')
cout<
else
flag4=1;
}while(flag4==0);
if(w=='y')
cout<
cin>>p;
}while(w=='y');
}
10分享举报