15第十五周项目一——用二进制处理学生成绩

来源:互联网 发布:java循环打印三角形 编辑:程序博客网 时间:2024/05/21 07:13

/*
 * Copyright (c) 2014, 烟台大学计算机学院
 * All rights reserved.
 * 文件名称:test.cpp
 * 作    者:李晓凯
 * 完成日期:2015年 6 月 14 日
 * 版 本 号:v1.0
 *
 * 问题描述:【项目1-用二进制文件处理学生成绩】 
(1)定义学生类,其中包含学号、姓名、C++课、高数和英语成绩及总分数据成员,成员函数根据需要确定。 
(2)读入学生的成绩,并求出总分,用对象数组进行存储。ASCII文件score.dat中保存的是100名学生的学号、姓名和C++课、高数和英语成绩。 
(3)将所有数据保存到一个二进制文件binary_score.dat中,最后通过键盘输入你的信息,并写入到文件中(咱不谦虚,三科全100分,期末求好运)。 
(4)为验证输出文件正确,再将binary_score.dat中的记录逐一读出到学生对象中并输出查看。 
(5)用BinaryViewer命令查看二进制文件文件 
 * 输入描述:我的学号,姓名,cpp,高数,英语成绩
 * 程序输出:

 */

#include <iostream>#include <fstream>#include <cstring>#include <cstdlib>using namespace std;class Student{public:    Student(){};    Student(int nu,string na,double c,double m,double e):num(nu),name(na),cpp(c),math(m),english(e){total=c+m+e;}    void set_value(int nu,string na,double c,double m,double e);    string get_name(){return name;}    double get_cpp(){return cpp;}    double get_math(){return math;}    double get_english(){return english;}    double get_total(){return total;}    void set_cpp(double c);    void set_math(double m);    void set_english(double e);    friend ostream &operator<<(ostream &output,Student& stu);private:    int num;    string name;    double cpp;    double math;    double english;    double total;};void Student::set_value(int nu,string na,double c,double m,double e){    num=nu;    name=na;    cpp=c;    math=m;    english=e;    total=c+m+e;}void Student::set_cpp(double c){    cpp=c;}void Student::set_math(double m){    math=m;}void Student::set_english(double e){    english=e;}ostream &operator<<(ostream &output,Student& stu){    output<<stu.num<<" "<<stu.name<<" "<<stu.cpp<<" "<<stu.math<<" "<<stu.english<<" "<<stu.total<<'\n';    return output;}int main(){    Student stud[100];    int i,n;    string sname;    double scpp,smath,senglish;    ifstream infile("score.dat",ios::out);    if(!infile)    {        cerr<<"open error!"<<endl;        exit(1);    }    for(i=0;i<100;++i)    {        infile>>n>>sname>>scpp>>smath>>senglish;        stud[i].set_value(n,sname,scpp,smath,senglish);    }    infile.close();    ofstream outfile("binary_score.dat",ios::out|ios::binary);    if(!outfile)    {        cerr<<"open error!"<<endl;        exit(1);    }    for(i=0;i<100;++i)    {        outfile.write((char*)&stud[i],sizeof(stud[i]));    }    cout<<"请输入你的成绩"<<endl;    cin>>n>>sname>>scpp>>smath>>senglish;    Student me(n,sname,scpp,smath,senglish);    outfile.write((char*)&me,sizeof(me));    outfile.close();    Student s;    ifstream infile1("binary_score.dat",ios::in|ios::binary);    if(!infile1)    {        cerr<<"open error!"<<endl;        exit(1);    }    while(1)    {        infile1.read((char*)&s,sizeof(s));        if(infile1.eof())            break;        cout<<s;    }    infile1.close();    return 0;}


0 0
原创粉丝点击