2015.6.14用文件保存学生名单

#include<iostream>
#include<fstream>
#include<string>
#include<cstdlib>
using namespace std;
//定义学生类
class Student
{
public:
    Student() {};
    string get_mame(){return name;}
    double get_cpp(){return cpp;}
    double get_math(){return math;}
    double get_english(){return english;}
    double get_total(){return total;}
    static int get_stu_num(){return stu_num;}
    static double get_total_sum(){return total_sum;}
    friend istream &operator>>(istream &putin,Student &a);
    friend ostream &operator<<(ostream &putout,Student &a);
    ~Student() {};
private:
    string name;
    double cpp;
    double math;
    double english;
    double total;
    static int stu_num;  //学生人数,处理为类的静态成员合适
    static double total_sum; //学生总分和
};

istream &operator>>(istream &putin,Student &a)
{
    putin>>a.name>>a.cpp>>a.math>>a.english;
    a.total=a.cpp+a.math+a.english;
    ++Student::stu_num;
    Student::total_sum+=a.total;
    return putin;
}
ostream &operator<<(ostream &putout,Student &a)
{
    putout<<a.name<<'\t';
    putout<<a.cpp<<'\t';
    putout<<a.math<<'\t';
    putout<<a.english<<'\t';
    putout<<'\n';
    return putout;
}
int Student::stu_num=0;
double Student::total_sum=0;
int main( )
{
    Student stud[200],t; //stud[200]为保存数据的对象数组
    string sname;
    double total_avg;
    int i=0;
    ifstream infile;
    infile.open("score.dat",ios::in);
    if(!infile)
    {
        cerr<<"open error";
        exit(1);
    }
    else
    {
        while(!infile.eof())
        {
            infile>>stud[i++];
        }
    }
    infile.close();
    total_avg=Student::get_total_sum()/Student::get_stu_num();
    int m=0;
    ofstream outfile("pass_score",ios::out);
    if(!outfile)
    {
        cerr<<"create error";
        exit(1);
    }
    else
    {
        for(m=0;m<Student::get_stu_num();++m)
        {
            t=stud[m];
            if(t.get_cpp()>=60&&t.get_math()>=60&&t.get_english()&&t.get_total()>=total_avg)
            {
                outfile<<t;
            }
        }
    }
    outfile.close();
    return 0;
}
<img src="https://img-blog.csdn.net/20150614105015159?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd2xpdXpoaWxpbw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值