score.dat文件为自行建立。
#include<iostream>
#include<fstream>
#include<string>
#include<cstdlib>
#define NUM 5
using namespace std;
struct Studentscore
{
string name;
char gender; //"F" represent female,"M" represent man
float Cscore; //C++ score
float Mscore; //Math score
float Escore; //English score
};
void main()
{
Studentscore s[NUM];
ofstream outfile("score.dat",ios::out); //open the file "score"
if(!outfile)
{
cerr<<"open error!";
exit(1);
}
for(int i=0;i<NUM;i++)
{
cin>>s[i].name>>s[i].gender>>s[i].Cscore>>s[i].Mscore>>s[i].Escore;
cout<<"\n";
outfile<<s[i].name<<" "<<s[i].gender<<" "<<s[i].Cscore<<" "<<s[i].Mscore<<" "<<s[i].Escore<<endl; //datas are writen into file"score"
}
outfile.close();
cout<<"work is done:please open the file to see the result"<<endl;
}
学生系统代码如下:
#include<iostream>
#include<fstream>
#include<cstdlib>
#include<string>
#define NUM 20
using namespace std;
struct Studentscore
{
string name;
char gender; //"F" represent female,"M" represent man
float Cscore; //C++ score
float Mscore; //Math score
float Escore; //English score
float score;
};
class Fun
{
public:
int getdata(Studentscore stu[]);//get the data from file"score.dat"
void outputdata(Studentscore stu[],int i);//output data
void sortdata(Studentscore stu[],int i);//sort data
void shcolar(Studentscore stu[],int i);//judge sscholarship
void failCscore(Studentscore stu[],int i);//score of C++ is less than 60
};
int Fun::getdata(Studentscore stu[])
{
int i=0;
ifstream infile("score.dat",ios::in);
if(!infile)
{
cerr<<"open error!"<<endl;
exit(1);
}
while(infile>>stu[i].name>>stu[i].gender>>stu[i].Cscore>>stu[i].Mscore>>stu[i].Escore)
{
stu[i].score=stu[i].Cscore+stu[i].Mscore+stu[i].Escore;
i++;
}
infile.close();
return i;
}
void Fun::outputdata(Studentscore stu[],int i)
{
int num=i; //the num of sutdents
for(int k=0;k<num;k++)
{
cout<<stu[k].name<<" "<<stu[k].gender<<" "<<stu[k].Cscore<<" "<<stu[k].Mscore<<" "<<stu[k].Escore<<" "<<stu[k].score<<"\n";
}
}
void Fun::sortdata(Studentscore stu[],int i)
{
int k=0,j=0,num=i;
for(k=0;k<num;k++)
for(j=k+1;j<num;j++)
{
Studentscore temp;
if(stu[k].score<stu[j].score)
{
temp=stu[k];
stu[k]=stu[j];
stu[j]=temp;
}
}
for(k=0;k<num;k++)
cout<<stu[k].name<<" "<<stu[k].gender<<" "<<stu[k].Cscore<<" "<<stu[k].Mscore<<" "<<stu[k].Escore<<" "<<stu[k].score<<"\n";
}
void Fun::shcolar(Studentscore stu[],int i)
{
int num=i;
for(int k=0;k<num;k++)
{
if(stu[k].Cscore>80&&stu[k].Mscore>80&&stu[k].Escore>80)
cout<<stu[k].name<<endl;
}
}
void Fun::failCscore(Studentscore stu[],int i)
{
int num=i;
for(int k=0;k<num;k++)
{
if(stu[k].Cscore<60)
cout<<stu[k].name<<" "<<stu[k].Cscore<<endl;
}
}
void main()
{
Fun f;
Studentscore stu[NUM];
int stunum=0;
stunum=f.getdata(stu);
cout<<"数据读取完毕"<<endl;
cout<<"请选择下面的功能"<<endl;
cout<<"1.输出成绩单"<<endl;
cout<<"2.按总分排序并输出成绩单"<<endl;
cout<<"3.输出获得奖学金的同学的名单"<<endl;
cout<<"4.输出C++不及格同学同学姓名和C++成绩"<<endl;
cout<<"5.你可以想出的成绩管理系统的其他功能"<<endl;
cout<<"0.结束"<<endl;
cout<<"请选择(0-5):"<<endl;
int choice;
cin>>choice;
switch(choice)
{
case 1:
f.outputdata(stu,stunum);break;
case 2:
f.sortdata(stu,stunum);break;
case 3:
f.shcolar(stu,stunum);break;
case 4:
f.failCscore(stu,stunum);break;
case 5:
cout<<"The function is not available"<<endl;break;
case 0:
exit(1);break;
}
}