17周任务1

 
/* (程序头部注释开始) 
* 程序的版权和版本声明部分 
* Copyright (c) 2011, 烟台大学计算机学院学生  
* All rights reserved. 
* 文件名称: 学生文件处理,保存为二进制文件                              
* 作    者: 苗影                        
* 完成日期:  2012       年   6    月     112  日 
* 版 本 号:           
 
* 对任务及求解方法的描述部分 
* 输入描述:  
* 问题描述:  
* 程序输出:  
* 程序头部的注释结束 
*/  
  


#include<iostream>    
#include<fstream>    
#include<string>    
using namespace std;    
class Student    
{    
private:    
    string name; 
    int cpp;    
    int Math;    
    int English;    
    int All_score;    
    double Average;    
public:    
    Student();    
    Student(int c,int math,int english,int all_score,double average):cpp(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()    
{    
    cpp=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 ;    
              
        }    
    }    
    cout<<"总分成绩最高为:"<<s.All_score<<endl;    
   
}    
void get_height_Cscore(Student stu[])    
{    
    Student s;    
    int i=0;    
    s.cpp=stu[i].cpp;    
    for(i=0;i<101;i++)    
    {    
        if(stu[i].cpp>s.cpp)    
        {    
            s.cpp=stu[i].cpp;    
            s.name =stu[i].name ;    
        }    
    }    
    cout<<"C++成绩最高为:"<<s.cpp<<'\t'<<endl; 
  
}    
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<<endl;    
  
}    
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<<endl;    
    
  
}    
  
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].cpp>>stu[i].Math>>stu[i].English;    
        stu[i].Average=(stu[i].cpp+stu[i].Math+stu[i].English)/3;    
        stu[i].All_score=stu[i].cpp+stu[i].Math+stu[i].English;    
    }   
    stu[100].name="苗影";  
    stu[100].cpp=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].cpp<<'\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;    
  }

感言:一段旅程的结束意味着另一段旅程的开始,c++结束了,考试要开始了。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值