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

文件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

学习心得:

实践出真知

时间: 2024-08-11 03:25:49

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

第14周项目4-立体类族共有的抽象类

/* *Corpyright (c)2013,烟台大学计算机学院 *All right reseved. *作者:张凯 *完成日期:2014年5月28日 *版本号:v1.0 *输入描述: *问题描述: *程序输出: *问题分析: *算法设计: */ #include<iostream> #include<cstring> using namespace std; class CSolid { public: virtual double area()=0; virtual doub

14周 项目3 立体类族共有的抽象类

#include <iostream> using namespace std; class CSolid { public: virtual double area() const=0; virtual double volume() const=0; }; class CCube:public CSolid { public: CCube(double s):side(s) {} virtual double area()const { return 6*side*side; } virt

14周 项目2 形状类族中的纯虚函数

#include <iostream> using namespace std; class Shape { public: virtual double area()=0; }; class Circle:public Shape { public: Circle(double r):R(r) {} virtual double area() { return R*R*3.14; } protected: double R; }; class Rectangle:public Shape {

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

(1)定义学生类,其中包含姓名.C++课.高数和英语成绩及总分数据成员. (2)用对象数组进行存储学生的成绩,读入成绩并计算总分:将总分高于平均总分且没挂科的同学的信息保存到文件pass_score.dat中. 代码 #include <iostream> #include <fstream> #include <cstdlib> using namespace std; //定义学生类 class Student { public: //声明必要的成员函数 Stude

第14周项目1-折腾二维数组

/* * Copyright (c) 2014, 烟台大学计算机学院 * All rights reserved. * 文件名称:test.cpp * 作 者:刘畅 * 完成日期:2014 年 11 月 26 日 * 版 本 号:v1.0 * * 问题描述:创建一个5行4列的二维整型数组,通过初始化,为数组中的前两列的10个元素赋初值,然后满足 题目要求输出: * 输入描述:输入后两列10个元素的值: * 程序输出:按要求输出. #include <iostream> #include <

第14周 项目一-小玩文件(2)

(2)下面程序的功能是将文本文件abc.txt中的所有行加上行号后写到newabc.txt文件中,请填空将程序补充完整. #include <iostream> #include <cstdlib> #include <fstream> using namespace std; int main() { fstream outfile,infile; infile.open("abc.txt",_________); // (1) if(!infil

第14周 项目一-小玩文件(1)

(1)下面程序的功能是统计文本文件abc.txt中的字符个数,请填空将程序补充完整. #include <iostream> #include <cstdlib> #include _____________ // (1) using namespace std; int main() { fstream file; file.open("abc.txt", _________); // (2) if(!file) { cout<<"abc

第八周项目 二 【项目2-Time类中的运算符重载】

[项目2-Time类中的运算符重载] 实现Time类中的运算符重载. [cpp] view plaincopyprint? class CTime { private: unsigned short int hour;    // 时 unsigned short int minute;  // 分 unsigned short int second;  // 秒 public: CTime(int h=0,int m=0,int s=0); void setTime(int h,int m,i

第六周项目二——我的数组类

下面的程序,因为存在指针类型的数据成员,需要能完成深复制的构造函数.请补充完整构造函数和析构函数(其他不必动).其中,构造函数要完成下面三个任务: (1)为各成员函数赋值,按照深复制原则,其中arrayAddr应该是为保存数据新分配的连续空间的首地址: (2)MyArray(int *a, int n)中,要将a指向的数组中的数值,逐个地复制到新分配的arrayAddr指向的空间中: (3)getMax( )函数采取的策略是直接返回max(所以,计算max的工作,由构造函数完成) #includ