/*
* 程序的版权和版本声明部分
* Copyright (c)2012, 烟台大学计算机学院学生
* All rightsreserved.
* 文件名称: fun.cpp
* 作 者: 李洋
* 完成日期: 2013 年 6 月 20 日
* 版本号: v1.0
* 输入描述:略
* 问题描述:略
* 程序输出:略
*/
#include <fstream>
#include<iostream>
#include<string>
#include<cstdlib>
using namespace std;
int number=0;
//定义学生类
class Student
{
public:
string name;
double cpp;
double math;
double english;
double total;
};
int main( )
{
Student stu[200]; //stu[200]为保存数据的对象数组
//将文件中的数据读入到对象数组中
double stotal=0 ,average=0;
ifstream infile("score.dat",ios::in); //以输入的方式打开文件
if(!infile) //测试是否成功打开
{
cerr<<"open error!"<<endl;
exit(1);
}
while(!infile.eof())
{
infile>>stu[number].name>>stu[number].cpp>>stu[number].math>>stu[number].english; //读数据中
stu[number].total=stu[number].cpp+stu[number].math+stu[number].english;
stotal=stotal+stu[number].total;
average=stotal/number;
number++;
}
infile.close();//关闭
ofstream outfile("pass_score.txt",ios::out);
if(!outfile) //测试是否成功打开
{
cerr<<"open error!"<<endl;
exit(1);
}
for(int i=0; i<number;++i)
{
if(stu[i].total>=average&&stu[i].cpp>=60&&stu[i].math>=60&&stu[i].english>=60)
{
outfile<<stu[i].name<<" "<<stu[i].cpp<<" "<<stu[i].math<<" "<<stu[i].english<<endl;//写数据
}
}
outfile.close();//关闭
cout<<"请到文件pass_score.txt中查看名单:"<<endl;
return 0;
}
感悟:虽然写法有点简单,但是对于文件的相关操作有一定的体会!但还是有很多不明白的地方,还需努力。。。。。