/* (程序頭部註釋開始)
* 程序的版權和版本聲明部分
* 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;
}
感言:還是老師厲害!!!!!!