第十五周上机实践项目——用文件保存的学生名单

来源:互联网 发布:linux查看git服务 编辑:程序博客网 时间:2024/06/04 18:35
/**程序的版权和版本声明部分:*Copyright(c)2014,烟台大学计算机学院学生*All rights reserved.*文件名称:用文件保存的学生名单*作者:刘中林*完成日期:2013年 6月 05 日*版本号:v1.0*对任务及求解方法的描述部分:文件score.dat中保存的是若干名学生的姓名和C++课、高数和英语成绩。                             定义学生类,其中包含姓名、C++课、高数和英语成绩及总分数据成员,成员函数根据需要确定。                             用对象数组进行存储学生的成绩,读入成绩并计算总分;将总分高于平均总分且没挂科的同学的信息保存到文件pass_score.dat中。*输入描述: *问题描述:从文件中读取数据*程序输出:处理后的数据*问题分析:*算法设计:*/#include <iostream>#include <cstdlib>#include <string>#include <fstream>using namespace std;//定义学生类class Student{public:    //声明必要的成员函数    Student() {}    int average_score()    {        return (get_total_sum() / get_stu_num());    }    double get_total();    static int get_stu_num();    static double get_total_sum();    bool examination()    {        if(get_total()>average_score()&&cpp>=60&&english>=60&&math>=60)        {            return true;        }        else        {            return false;        }    }    friend ostream&operator<<(ostream&,Student&);    friend istream&operator>>(istream&,Student&);private:    string name;    double cpp;    double math;    double english;    double total;    static int stu_num;  //学生人数,处理为类的静态成员合适    static double total_sum; //学生总分和};double Student::get_total(){    return total;}int Student::get_stu_num(){    return stu_num;}double Student::get_total_sum(){    return total_sum;}int Student::stu_num=0;double Student::total_sum=0;ostream & operator<<(ostream& outfile,Student& stu){    outfile<<stu.name<<"  "<<stu.cpp<<"   "<<stu.english<<"    "<<stu.math;    return outfile;}istream & operator>>(istream& infile,Student& stu){    infile>>stu.name>>stu.cpp>>stu.english>>stu.math;    Student::stu_num++;    stu.total=stu.cpp+stu.english+stu.math;    Student::total_sum+=Student::total_sum;    return infile;}int main(){    Student stud[200],t; //stud[200]为保存数据的对象数组    string sname;    int i=0;    //从文件score.dat中读入数据,保存到对象数组中    ifstream infile("E:\\work\\faaa\\score.dat",ios::in);    if(!infile)    {        cerr<<"error!"<<endl;        exit(0);    }    while(infile>>stud[i])    {        i++;    }    infile.close();    ofstream outfile("E:\\work\\faaa\\copyscore.dat",ios::out);    if(!outfile)    {        cerr<<"error!"<<endl;        exit(1);    }    for(int j=0; j<i; j++)    {        if(stud[j].examination())        {            outfile<<stud[j]<<"\n";        }    }    outfile.close();    //总分高于平均总分且没挂科的同学的信息保存到文件pass_score.dat中    return 0;}

*样例输出:

*心得体会:不会做人,再成功也是暂时的。。

0 0