用文件保存的学生名单

来源:互联网 发布:程序员找男朋友 编辑:程序博客网 时间:2024/05/22 12:07
编号及代码:/**Copyright(c)2015,烟台大学计算机与工程学院*All rights reserved;*文件名称:score.cpp*作者:范星月*完成日期:2015年6月9日*版本号:v1.0**问题描述:文件score.dat中保存的是若干名学生的姓名和C++,高数,英语成绩,定义学生类,其中包含姓名,c++课,高数,英语及总分数据成员,用对象数组惊醒存储学生的成绩,读入成绩并积分计算,将总分高于平均分且没挂科的同学信息保存到文件pass_score.dat中*问题输入:*问题输出:*/#include<iostream>#include <cstdlib>#include<fstream>using namespace std;class Student{public:    Student() {};    ~Student();    double get_total();    static int get_stu_num();    static double get_total_sum();    friend istream &operator>>(istream&input,Student &s);    friend ostream&operator<<(ostream&output,Student &s);    bool pass();private:    string name;    double cpp;    double math;    double english;    double total;    static int stu_num;    static double total_sum;};int Student::stu_num=0;double Student::total_sum=0;Student::~Student() {}double Student::get_total()//一个人总分{    return total;}double Student::get_total_sum()//班级总分数{    return total_sum;}int  Student::get_stu_num()//人数{    return stu_num;}istream &operator>>(istream&input,Student &s){    input>>s.name>>s.cpp>>s.math>>s.english;    s.total=s.cpp+s.math+s.english;    Student::stu_num++;    Student::total_sum+=s.total;    return input;}ostream&operator<<(ostream&out,Student &s){    out<<s.name<<"\t";    out<<s.cpp<<"\t";    out<<s.math<<"\t";    out<<s.english<<"\t";    out<<s.total;    return out;}bool Student::pass(){    return cpp>=60&&math>=60&&english>=60;}int main(){    Student stud[200],t;    string sname;    double total_avg;    int i=0;//从文件score.dat中读入数据,保存到对象数组中    ifstream infile("score.dat",ios::in);//以读入的方式打开文件    if(!infile)//测试是否打开    {        cerr<<"open error!"<<"\t";        exit(1);    }    while (!infile.eof())    {        infile>>stud[i++];    }    infile.close();//总分高于平均分且没挂科的同学信息保存到文件pass_score.dat中    if(Student::get_stu_num()>0)    {        total_avg=Student::get_total_sum()/Student::get_stu_num();        ofstream outfile("pass_score.dat",ios::out);        if(!outfile)        {            cerr<<"open error!"<<"\t";            exit(1);        }        for(int i=0; i<Student::get_stu_num(); i++)        {            if(stud[i].get_total()>=total_avg&&stud[i].pass())            {                outfile<<stud[i]<<endl;            }        }        outfile.close();        cout<<"请到pass_score.dat找名单"<<endl;    }    return 0;}


学习总结:呵呵

0 0
原创粉丝点击