您的位置:首页 > 编程语言 > C语言/C++

十七周任务一学生文件处理,保存为二进制文件

2012-06-11 19:24 417 查看
/* (程序头部注释开始)
* 程序的版权和版本声明部分
* Copyright (c) 2011, 烟台大学计算机学院学生
* All rights reserved.
* 文件名称: 学生文件处理,保存为二进制文件
* 作    者: 石丽君
* 完成日期:  2012       年   6    月     11   日
* 版 本 号:

* 对任务及求解方法的描述部分
* 输入描述:
* 问题描述:
* 程序输出:
* 程序头部的注释结束
*/

#include<iostream>
#include<fstream>
#include<string>
using namespace std;
class Student
{
private:
string name;
int C;
int Math;
int English;
int All_score;
double Average;
public:
Student();
Student(int c,int math,int english,int all_score,double average):C(c),Math(math),English(english),All_score(all_score),Average(average){}
friend void arrange_all_score(Student stu[]);  //排列总成绩
friend void get_frome_file(Student stu[]);  //从文件中读入成绩
friend void save_to_file(Student stu[]);  //保存到文件中
friend void get_heigh_allscore(Student stu[]);  //得到最高分
friend void get_height_Cscore(Student stu[]);  //得到最高c++成绩
friend void get_height_Mathscore(Student stu[]);  //得到最高高数成绩
friend void get_height_Englishscore(Student stu[]);//得到最高英语成绩
friend void getagain_fome_file(Student stu[]); //再次从文件中读出

};
Student::Student()
{
C=0;
Math=0;
English=0;
All_score=0;
Average=0;
}
void arrange_all_score(Student stu[])
{
int i,j;
Student t;
for( j=0;j<100;j++)
{
for( i=0;i<100-j;i++)
if(stu[i].All_score<stu[i+1].All_score)
{
t=stu[i+1];
stu[i+1]=stu[i];
stu[i]=t;
}
}
}
void get_heigh_allscore(Student stu[])
{
Student s;
int i=0;
s.All_score=stu[i].All_score;
for(i=0;i<101;i++)
{
if(stu[i].All_score>s.All_score)
{
s.All_score =stu[i].All_score ;
//s.name =stu[i].name ;
}
}
cout<<"总分成绩最高为:"<<s.All_score<<'\t'<<"成员:";
for(i=0;i<101;i++)
if(s.All_score ==stu[i].All_score )
cout<<stu[i].name<<" ";
}
void get_height_Cscore(Student stu[])
{
Student s;
int i=0;
s.C=stu[i].C;
for(i=0;i<101;i++)
{
if(stu[i].C>s.C)
{
s.C =stu[i].C;
s.name =stu[i].name ;
}
}
cout<<"C++成绩最高为:"<<s.C<<'\t'<<"成员:";
for(i=0;i<101;i++)
if(s.C ==stu[i].C )
cout<<stu[i].name<<" ";

}
void get_height_Mathscore(Student stu[])
{
Student s;
int i=0;
s.Math=stu[i].Math;
for(i=0;i<101;i++)
{
if(stu[i].Math>s.Math)
{
s.Math =stu[i].Math;
s.name =stu[i].name ;
}
}
cout<<"高数成绩最高为:"<<s.Math<<'\t'<<"成员:";
for(i=0;i<101;i++)
if(s.Math==stu[i].Math )
cout<<stu[i].name<<" ";
}
void get_height_Englishscore(Student stu[])
{
Student s;
int i=0;
s.English=stu[i].English;
for(i=0;i<101;i++)
{
if(stu[i].English>s.English)
{
s.English =stu[i].English;
s.name =stu[i].name ;
}
}
cout<<"英语成绩最高为:"<<s.English<<'\t'<<"成员:";
for(i=0;i<101;i++)
if(s.English==stu[i].English )
cout<<stu[i].name<<" ";

}

void get_frome_file(Student stu[])
{
ifstream infile("score.dat",ios::in);
if(!infile)
{
cerr<<"open score.dat error!"<<endl;
exit(1);
}
for(int i=0;i<100;i++)
{
infile>>stu[i].name>>stu[i].C>>stu[i].Math>>stu[i].English;
stu[i].Average=(stu[i].C+stu[i].Math+stu[i].English)/3;
stu[i].All_score=stu[i].C+stu[i].Math+stu[i].English;
}
stu[100].name="石丽君";
stu[100].C=100;
stu[100].Math=100;
stu[100].English=100;
stu[100].Average =100;
stu[100].All_score =300;
infile.close();

}
void save_to_file(Student stu[])
{
ofstream outfile("binary_score.dat",ios::binary);
if (!outfile)
{
cerr<<"open error!"<<endl;

}
for(int i=0;i<101;i++)
{
outfile.write((char*)&stu[i],sizeof(stu[i]));
}

outfile.close();
}
void getagain_fome_file(Student stu[])
{
ifstream infile("binary_score.dat",ios::binary);
if(!infile)
{
cerr<<"open score.dat error!"<<endl;
exit(1);
}
cout<<"姓名"<<'\t'<<"C++"<<'\t'<<"高数"<<'\t'<<"英语"<<'\t'<<"平均分"<<'\t'<<"总分"<<endl;
for(int i=0;i<101;i++)
{
infile.read((char*)&stu[i],sizeof(stu[i]));
cout<<stu[i].name<<'\t'<<stu[i].C<<'\t'<<stu[i].Math<<'\t'<<stu[i].English<<'\t'<<stu[i].Average<<'\t'<<stu[i].All_score<<endl;

}
infile.close();

}
int main()
{
Student stu[101];
get_frome_file(stu); //读入原始文件
arrange_all_score( stu);
get_heigh_allscore(stu);
cout<<endl;
get_height_Cscore( stu);
cout<<endl;
get_height_Mathscore( stu);
cout<<endl;
get_height_Englishscore( stu);
cout<<endl;
cout<<endl;
save_to_file(stu);  //保存到文件中
cout<<endl;
getagain_fome_file(stu);
system("pause");
return 0;

}




 

感言:还是老师厉害!!!!!!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  任务 math file c++ ios system
相关文章推荐