第14周 项目二-用文件保存学生名单

来源:互联网 发布:高清网络摄像机怎么用 编辑:程序博客网 时间:2024/05/01 06:17

文件score.dat中保存的是若干名学生的姓名和C++课、高数和英语成绩。
(1)定义学生类,其中包含姓名、C++课、高数和英语成绩及总分数据成员。

//定义学生类class Student{public:    //声明必要的成员函数private:    string name;    double cpp;    double math;    double english;    double total;    static int stu_num;  //学生人数,处理为类的静态成员合适    static double total_sum; //学生总分和};


(2)用对象数组进行存储学生的成绩,读入成绩并计算总分;将总分高于平均总分且没挂科的同学的信息保存到文件pass_score.dat中。

 

int main( ){    Student stud[200],t; //stud[200]为保存数据的对象数组    string sname;    double total_avg;    int i=0;    //从文件score.dat中读入数据,保存到对象数组中    //总分高于平均总分且没挂科的同学的信息保存到文件pass_score.dat中    return 0;}

讨论:学生人数和总分的另外一种解决方法是用全局变量。但这两种信息与学生有关,是学生类的“属性”,成为学生类的数据成员合适;这两种信息由学生整体决定,用作静态数据成员合适。如不理解这样设计的理由,复习课程前面的相关内容。

#include <fstream>#include<iostream>#include<string>#include<cstdlib>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 &in, Student &s);  //可以定义input函数替代    friend ostream& operator<<(ostream &out, Student &s); //可以定义display函数替代    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(){    total_sum-=total;    stu_num--;}double Student::get_total(){    return total;}int Student::get_stu_num(){    return stu_num;}double Student::get_total_sum(){    return total_sum;}istream& operator>>(istream &in, Student &s){    in>>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 in;}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; //stud[200]为保存数据的对象数组    string sname;    double total_avg;    int i=0;    //将文件中的数据读入到对象数组中    ifstream infile("score.dat",ios::in);  //以输入的方式打开文件    if(!infile)       //测试是否成功打开    {        cerr<<"open error!"<<endl;        exit(1);    }    while(!infile.eof())    {        infile>>stud[i++];   //读数据中,人数等信息自动地记录到静态成员中,见运算符重载的实现    }    infile.close();    //求总分平均并输出    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!"<<endl;            exit(1);        }        for(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;}


运行结果:

王锐 63 90 96 249
何煜中 90 73 82 245
王磊 87 86 92 265
冯松 89 98 83 270
裴培 75 82 91 248
马婧 98 84 87 269
李桐 93 83 86 262
张迪 99 88 80 267
张里响 85 65 96 246
王瑞麒 89 83 91 263
宋宗杰 94 100 92 286
张佳玮 61 98 96 255
王姝 70 91 90 251
宋媛媛 61 94 92 247
王竞 90 87 67 244
崔赞 91 67 93 251
李朋 90 82 97 269
马佳 60 90 100 250
韩明 83 97 88 268
马立 73 90 83 246
吴清正 89 97 85 271
冷云 89 88 71 248
葛志伟 100 79 71 250
范振光 98 87 89 274
王芳 71 97 99 267
杨梦婕 89 99 67 255
王琦 98 95 98 291
杨阔 90 91 98 279
刘亚新 77 81 95 253
董一伟 93 88 80 261
张笑 86 88 76 250
文静 93 88 85 266
徐嘉琦 90 75 87 252
魏佳 100 94 80 274
高举 81 99 91 271
赵旭洋 87 91 94 272
张昊 94 83 96 273
高清 76 83 84 243
叶丹 87 80 96 263
刘盈 99 72 93 264
杨洁 96 79 87 262
王蒙 67 97 89 253
张敏 85 75 89 249
吴佳林 96 65 82 243
黄金龙 85 90 78 253
冼丹 100 89 89 278
刘紫亮 72 98 84 254

 

 

学习心得:

实践出真知

 

0 0
原创粉丝点击