2015.6.14用文件保存学生名单

来源:互联网 发布:ubuntu 网络文件夹 编辑:程序博客网 时间:2024/06/04 19:21
#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="http://img.blog.csdn.net/20150614105015159?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd2xpdXpoaWxpbw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />

0 0
原创粉丝点击