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

(1)定义学生类,其中包含姓名、C++课、高数和英语成绩及总分数据成员。

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

代码

#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
//定义学生类
class Student
{
public:
    //声明必要的成员函数
    Student() {}
    friend istream &operator>>(istream &in,Student &s);
    friend ostream &operator<<(ostream &out,Student &s);
    int getnum()
    {
        return stu_num;
    }
    double getsum()
    {
        return total_sum;
    }
    double gettotal()
    {
        return total;
    }
    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;
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.cpp+s.math+s.english;
    return in;
}
ostream &operator<<(ostream &out,Student &s)
{
    out<<s.name<<" "<<s.cpp<<" "<<s.math<<" "<<s.english;
    return out;
}
bool Student::pass()
{
    if(cpp>=60&&english>=60&&math>=60)
        return true;
    else
        return false;
}
int main( )
{
    Student stud[200],t; //stud[200]为保存数据的对象数组
    string sname;
    double total_avg;
    int i=0;
    //从文件score.dat中读入数据,保存到对象数组中
    fstream infile,outfile;
    infile.open("score.dat",ios::in);
    if(!infile)
    {
        cout<<"score.dat can’t open."<<endl;
        exit(1);
    }
    while(!infile.eof())
    {
        infile>>stud[i];
        i++;
    }
    infile.close();
    //总分高于平均总分且没挂科的同学的信息保存到文件pass_score.dat中
    outfile.open("pass_score.dat",ios::out);
    if(!outfile)
    {
        cout<<"pass_score.dat can’t open."<<endl;
        exit(1);
    }
    total_avg=stud[i-1].getsum()/stud[i-1].getnum();
    for(i--;i>=0;--i)
    {
        if(stud[i].gettotal()>=total_avg&&stud[i].pass())
            outfile<<stud[i]<<endl;
    }
    outfile.close();
    return 0;
}

1、读取、写入文件用infile和outfile,我经常犯用in和out的毛病

2、由于类中的数据成员都是私有的,一个一个用函数调用不现实,最好的方法就是用运算符重载

时间: 2024-08-09 00:09:48

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

第14周上机实践项目3——电子词典

做一个简单的电子词典.在文件dictionary.txt中,保存的是英汉对照的一个词典,词汇量近8000个,英文.中文释义与词性间用'\t'隔开. 代码 #include <iostream> #include <fstream> #include <cstdlib> using namespace std; class dictionary; class Word { public: Word(){} void set(string e,string c,string

第14周上机实践项目1——小玩文件(1)

(1)下面程序的功能是统计文本文件abc.txt中的字符个数 代码 #include <iostream> #include <cstdlib> #include <fstream> using namespace std; int main() { fstream file; file.open("abc.txt", ios::out); // (2) if(!file) { cout<<"abc.txt can¡¯t ope

第14周上机实践项目1——小玩文件(2)

将文本文件abc.txt中的所有行加上行号后写到newabc.txt文件中 代码 #include <iostream> #include <cstdlib> #include <fstream> using namespace std; int main() { fstream outfile,infile; infile.open("abc.txt",ios::in); // (1) if(!infile) { cout<<"

第14周上机实践项目1——小玩文件(3)

(3)用键盘输入文件名,统计输出文件中每个字母.数字字符出现的次数: 代码 #include <iostream> #include <cstdlib> #include <fstream> #include <string> using namespace std; int main() { fstream infile; char textname[80]; gets(textname); infile.open(textname,ios::in); i

第14周上机实践项目1——折腾二维数组(1)

问题及代码 /* * Copyright (c) 2014, 烟台大学计算机学院 * All rights reserved. * 文件名称:test.cpp * 作 者:辛彬 * 完成日期:2014年 11 月 27 日 * 版 本 号:v1.0 * * 问题描述: 按行序优先输出数组元素. * 输入描述:后两列元素. * 程序输出:数组元素: */ #include <iostream> using namespace std; int main() { int i,x,y; int a[

第14周上机实践项目1——折腾二维数组(2)

问题及代码 /* * Copyright (c) 2014, 烟台大学计算机学院 * All rights reserved. * 文件名称:test.cpp * 作 者:辛彬 * 完成日期:2014年 11 月 27 日 * 版 本 号:v1.0 * * 问题描述: 按列序优先输出. * 输入描述:后两列元素. * 程序输出:数组元素: */ #include <iostream> using namespace std; int main() { int i,x,y; int a[5][4

第14周上机实践项目1——折腾二维数组(3)

问题及代码 /* * Copyright (c) 2014, 烟台大学计算机学院 * All rights reserved. * 文件名称:test.cpp * 作 者:辛彬 * 完成日期:2014年 11 月 27 日 * 版 本 号:v1.0 * * 问题描述: 倒序输出. * 输入描述:后两列元素. * 程序输出:数组元素: */ #include <iostream> using namespace std; int main() { int i,x,y; int a[5][4]=

第14周上机实践项目1——折腾二维数组(4)

问题及代码 /* * Copyright (c) 2014, 烟台大学计算机学院 * All rights reserved. * 文件名称:test.cpp * 作 者:辛彬 * 完成日期:2014年 11 月 27 日 * 版 本 号:v1.0 * * 问题描述: 输出所有偶数. * 输入描述:后两列元素. * 程序输出:偶数数组元素: */ #include <iostream> using namespace std; int main() { int i,x,y; int a[5][

第14周上机实践项目1——折腾二维数组(5)

问题及代码 /* * Copyright (c) 2014, 烟台大学计算机学院 * All rights reserved. * 文件名称:test.cpp * 作 者:辛彬 * 完成日期:2014年 11 月 27 日 * 版 本 号:v1.0 * * 问题描述: 输出下标之和为3的倍数的元素. * 输入描述:后两列元素. * 程序输出:下标之和为3的倍数的元素: */ #include <iostream> using namespace std; int main() { int i,