2、成绩查询:
(1)按照学生号查询,输出查询结果。
(2)按照姓名查询,输出查询结果
(3 )按条件查询
#include<iostream.h>
class CStudent
{
private:
int number;
char *name;
double chinese;
double english;
double math;
public:
CStudent()//建立构造函数
{
name=new char[];
number=0;
chinese=0;
english=0;
math=0;
}
CStudent(int num,char *str,double ch,double eng,double ma)
{
name=new char[];//变量声明
strcpy(name,str);//拷贝构造函数
number=num;
chinese=ch;
english=eng;
math=ma;
}
char *getname(char *str)//获得当前对象姓名函数
{str=name;return str;}
void setname(char *str)//设置当前对象的姓名
{strcpy(name,str);}
int getnumber(int &num)//获得当前对象学号
{num=number;return num;}
void setnumber(int &num)//设置当前对象学号
{number=num;}
//数据录入======================================================
void student_info()
{
system("cls");
int i,j;
fstream outfile;//流对象
cout<<"Please input the student's amount: ";
outfile.open("student.dat",ios::out|ios::trunc);//没次都以新文件打开文件
if(!outfile)//文件为空时打开失败
{
cout<<"open fail";
abort();//调用退出函数,退出
}
cin>>j;//输入录入学生的个数
outfile<<"Name\t"<<"ID\t"<<"Chinese\t"<<"English\t"<<"Math\t"<<endl;
for(i=0;i<j;i++)//利用循环保存对象的数据
{
cout<<"\n--------------------------------\n";
cout<<"please input the student's name: ";
cin>>name;
cout<<"Please input the student's number: ";
cin>>number;
//录入成绩--------------------------------------------------
cout<<"请输入以下科目的学生成绩:"<<endl;
for(int i=1;i<2;i++)
{
cout<<"Chinese: ";
cin>>chinese;
cout<<"English: ";
cin>>english;
cout<<"Math: ";
cin>>math;
}
cout<<"\n----------------------------------\n";
//system("cls");
outfile<<name<<'\t';//使用'\t'自动对齐
outfile<<number<<'\t';
for(int m=1;m<2;m++)
{
outfile<<chinese<<'\t';
outfile<<english<<'\t';
outfile<<math<<'\t'<<endl;
}
}
outfile.close();
//在屏幕上显示出来
cout<<"Do you want to display the data you input(Y & N)? ";
char h;
cin>>h;
if(h=='Y'||h=='y')
{
system("cls");
cout<<"show information to the screen: "<<endl;
fstream infile;
infile.open("student.dat",ios::in);//打开文件
char s[80];
if(!infile)//文件为空时打开失败
{
cout<<"open fail"<<endl;
abort();
}
while(!infile.eof())//一直读,直到读到文件尾
{
infile.getline(s,sizeof(s));//用getline整行整行读
cout<<s<<endl;//在屏幕显示
}
}
if(h=='N'||h=='n')
{
cout<<endl<<endl<<endl;
cout<<" End of inputing"<<endl;
}
}
//追加数据====================================================================
void add_student()
{
system("cls");
int i,j;
fstream outfile;//流对象
cout<<"Please input the student's amount: ";
outfile.open("student.dat",ios::out|ios::app);//打开存在的文件
if(!outfile)//文件为空时打开失败
{
cout<<"open fail";
abort();//调用退出函数,退出
}
cin>>j;//写函数的作用
outfile<<"Name\t"<<"ID\t"<<"Chinese\t"<<"English\t"<<"Math\t"<<endl;
for(i=0;i<j;i++)//利用循环保存对象的数据
{
cout<<"\n--------------------------------\n";
cout<<"please input the student's name: ";
cin>>name;
cout<<"Please input the student'number: ";
cin>>number;
cout<<"请输入以下科目的学生成绩:"<<endl;
for(int i=1;i<2;i++)
{
cout<<"Chinese: ";
cin>>chinese;
cout<<"English: ";
cin>>english;
cout<<"Math: ";
cin>>math;
}
cout<<"\n----------------------------------\n";
//system("cls");
outfile<<name<<'\t';//使用'\t'自动对齐
outfile<<number<<'\t';
for(int m=1;m<2;m++)
{
outfile<<chinese<<'\t';
outfile<<english<<'\t';
outfile<<math<<'\t'<<endl;
}
}
outfile.close();
//在屏幕上显示出来
cout<<"Do you want to display the data you input(Y & N)? ";
char h;
cin>>h;
if(h=='Y'||h=='y')
{
system("cls");
cout<<"show information to the screen: "<<endl;
fstream infile;
infile.open("student.dat",ios::in);//打开文件
char s[80];
if(!infile)//文件为空时打开失败
{
cout<<"open fail"<<endl;
abort();
}
while(!infile.eof())//一直读,直到读到文件尾
{
infile.getline(s,sizeof(s));//用getline整行整行读
cout<<s<<endl;//在屏幕显示
}
}
if(h=='N'||h=='n')
{
cout<<endl<<endl<<endl;
cout<<" End of inputing"<<endl;
}
}//查找函数============================================================== //姓名查找
void read_data(char *name)
{
char a[10];
int b,c,d,e;
fstream infile;
infile.open("student.dat",ios::in);
if(!infile)
{
cout<<"can't open.\n"<<endl;
abort();
} while((cin.get())!=infile.eof())
{
infile>>a;
infile.seekg(35,ios::beg);
infile>>b;
infile.seekg(40,ios::beg);
infile>>c;
infile>>d;
infile>>e;
if(strcmp(name,a))
{
cout<<"ID: "<<b<<" Chinese: "<<c<<" English: "<<d<<" Math: "<<e<<endl;
break;
}
else
{
cout<<"Can't find........"<<endl;
break;
}
}
infile.close();
}
//学号查找
void read_data(int &id)
{
char a[10];
int b,c,d,e;
fstream infile;
infile.open("student.dat",ios::in);
if(!infile)
{
cout<<"can't open.\n"<<endl;
abort();
} while((cin.get())!=infile.eof())
{
infile.seekg(31,ios::beg);
infile>>a;
infile.seekg(35,ios::beg);
infile>>b;
infile.seekg(40,ios::beg);
infile>>c;
infile>>d;
infile>>e;
if(id=b)
{
cout<<"Name: "<<a<<" Chinese: "<<c<<" English: "<<d<<" Math: "<<e<<endl;
break;
}
else
{
cout<<"Can't find........"<<endl;
break;
}
}
infile.close();
}
~CStudent()//析构函数
{delete name;}
};
(1)按照学生号查询,输出查询结果。
(2)按照姓名查询,输出查询结果
(3 )按条件查询
#include<iostream.h>
class CStudent
{
private:
int number;
char *name;
double chinese;
double english;
double math;
public:
CStudent()//建立构造函数
{
name=new char[];
number=0;
chinese=0;
english=0;
math=0;
}
CStudent(int num,char *str,double ch,double eng,double ma)
{
name=new char[];//变量声明
strcpy(name,str);//拷贝构造函数
number=num;
chinese=ch;
english=eng;
math=ma;
}
char *getname(char *str)//获得当前对象姓名函数
{str=name;return str;}
void setname(char *str)//设置当前对象的姓名
{strcpy(name,str);}
int getnumber(int &num)//获得当前对象学号
{num=number;return num;}
void setnumber(int &num)//设置当前对象学号
{number=num;}
//数据录入======================================================
void student_info()
{
system("cls");
int i,j;
fstream outfile;//流对象
cout<<"Please input the student's amount: ";
outfile.open("student.dat",ios::out|ios::trunc);//没次都以新文件打开文件
if(!outfile)//文件为空时打开失败
{
cout<<"open fail";
abort();//调用退出函数,退出
}
cin>>j;//输入录入学生的个数
outfile<<"Name\t"<<"ID\t"<<"Chinese\t"<<"English\t"<<"Math\t"<<endl;
for(i=0;i<j;i++)//利用循环保存对象的数据
{
cout<<"\n--------------------------------\n";
cout<<"please input the student's name: ";
cin>>name;
cout<<"Please input the student's number: ";
cin>>number;
//录入成绩--------------------------------------------------
cout<<"请输入以下科目的学生成绩:"<<endl;
for(int i=1;i<2;i++)
{
cout<<"Chinese: ";
cin>>chinese;
cout<<"English: ";
cin>>english;
cout<<"Math: ";
cin>>math;
}
cout<<"\n----------------------------------\n";
//system("cls");
outfile<<name<<'\t';//使用'\t'自动对齐
outfile<<number<<'\t';
for(int m=1;m<2;m++)
{
outfile<<chinese<<'\t';
outfile<<english<<'\t';
outfile<<math<<'\t'<<endl;
}
}
outfile.close();
//在屏幕上显示出来
cout<<"Do you want to display the data you input(Y & N)? ";
char h;
cin>>h;
if(h=='Y'||h=='y')
{
system("cls");
cout<<"show information to the screen: "<<endl;
fstream infile;
infile.open("student.dat",ios::in);//打开文件
char s[80];
if(!infile)//文件为空时打开失败
{
cout<<"open fail"<<endl;
abort();
}
while(!infile.eof())//一直读,直到读到文件尾
{
infile.getline(s,sizeof(s));//用getline整行整行读
cout<<s<<endl;//在屏幕显示
}
}
if(h=='N'||h=='n')
{
cout<<endl<<endl<<endl;
cout<<" End of inputing"<<endl;
}
}
//追加数据====================================================================
void add_student()
{
system("cls");
int i,j;
fstream outfile;//流对象
cout<<"Please input the student's amount: ";
outfile.open("student.dat",ios::out|ios::app);//打开存在的文件
if(!outfile)//文件为空时打开失败
{
cout<<"open fail";
abort();//调用退出函数,退出
}
cin>>j;//写函数的作用
outfile<<"Name\t"<<"ID\t"<<"Chinese\t"<<"English\t"<<"Math\t"<<endl;
for(i=0;i<j;i++)//利用循环保存对象的数据
{
cout<<"\n--------------------------------\n";
cout<<"please input the student's name: ";
cin>>name;
cout<<"Please input the student'number: ";
cin>>number;
cout<<"请输入以下科目的学生成绩:"<<endl;
for(int i=1;i<2;i++)
{
cout<<"Chinese: ";
cin>>chinese;
cout<<"English: ";
cin>>english;
cout<<"Math: ";
cin>>math;
}
cout<<"\n----------------------------------\n";
//system("cls");
outfile<<name<<'\t';//使用'\t'自动对齐
outfile<<number<<'\t';
for(int m=1;m<2;m++)
{
outfile<<chinese<<'\t';
outfile<<english<<'\t';
outfile<<math<<'\t'<<endl;
}
}
outfile.close();
//在屏幕上显示出来
cout<<"Do you want to display the data you input(Y & N)? ";
char h;
cin>>h;
if(h=='Y'||h=='y')
{
system("cls");
cout<<"show information to the screen: "<<endl;
fstream infile;
infile.open("student.dat",ios::in);//打开文件
char s[80];
if(!infile)//文件为空时打开失败
{
cout<<"open fail"<<endl;
abort();
}
while(!infile.eof())//一直读,直到读到文件尾
{
infile.getline(s,sizeof(s));//用getline整行整行读
cout<<s<<endl;//在屏幕显示
}
}
if(h=='N'||h=='n')
{
cout<<endl<<endl<<endl;
cout<<" End of inputing"<<endl;
}
}//查找函数============================================================== //姓名查找
void read_data(char *name)
{
char a[10];
int b,c,d,e;
fstream infile;
infile.open("student.dat",ios::in);
if(!infile)
{
cout<<"can't open.\n"<<endl;
abort();
} while((cin.get())!=infile.eof())
{
infile>>a;
infile.seekg(35,ios::beg);
infile>>b;
infile.seekg(40,ios::beg);
infile>>c;
infile>>d;
infile>>e;
if(strcmp(name,a))
{
cout<<"ID: "<<b<<" Chinese: "<<c<<" English: "<<d<<" Math: "<<e<<endl;
break;
}
else
{
cout<<"Can't find........"<<endl;
break;
}
}
infile.close();
}
//学号查找
void read_data(int &id)
{
char a[10];
int b,c,d,e;
fstream infile;
infile.open("student.dat",ios::in);
if(!infile)
{
cout<<"can't open.\n"<<endl;
abort();
} while((cin.get())!=infile.eof())
{
infile.seekg(31,ios::beg);
infile>>a;
infile.seekg(35,ios::beg);
infile>>b;
infile.seekg(40,ios::beg);
infile>>c;
infile>>d;
infile>>e;
if(id=b)
{
cout<<"Name: "<<a<<" Chinese: "<<c<<" English: "<<d<<" Math: "<<e<<endl;
break;
}
else
{
cout<<"Can't find........"<<endl;
break;
}
}
infile.close();
}
~CStudent()//析构函数
{delete name;}
};
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货