《C++语言基础》实践参考——用二进制文件处理学生成绩
2015-06-09 17:08
666 查看
返回:贺老师课程教学链接 项目要求
【项目1-用二进制文件处理学生成绩】
(1)定义学生类,其中包含学号、姓名、C++课、高数和英语成绩及总分数据成员,成员函数根据需要确定。
(2)读入学生的成绩,并求出总分,用对象数组进行存储。ASCII文件score.dat中保存的是100名学生的学号、姓名和C++课、高数和英语成绩。
(3)将所有数据保存到一个二进制文件binary_score.dat中,最后通过键盘输入你的信息,并写入到文件中(咱不谦虚,三科全100分,期末求好运)。
(4)为验证输出文件正确,再将binary_score.dat中的记录逐一读出到学生对象中并输出查看。
(5)用BinaryViewer命令查看二进制文件文件
文件下载
[参考解答]
<本文完>
【项目1-用二进制文件处理学生成绩】
(1)定义学生类,其中包含学号、姓名、C++课、高数和英语成绩及总分数据成员,成员函数根据需要确定。
(2)读入学生的成绩,并求出总分,用对象数组进行存储。ASCII文件score.dat中保存的是100名学生的学号、姓名和C++课、高数和英语成绩。
(3)将所有数据保存到一个二进制文件binary_score.dat中,最后通过键盘输入你的信息,并写入到文件中(咱不谦虚,三科全100分,期末求好运)。
(4)为验证输出文件正确,再将binary_score.dat中的记录逐一读出到学生对象中并输出查看。
(5)用BinaryViewer命令查看二进制文件文件
文件下载
[参考解答]
#include <fstream> #include<iostream> #include<cstring> #include<cstdlib> using namespace std; //(一)定义学生类 class Student { public: Student(){}; Student(int n, string nam, double c, double m, double e):num(n),name(nam),cpp(c),math(m),english(e){total=c+m+e;} void set_value(int n,string nam, 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){cpp=c;} void set_math(double m){math=m;} void set_english(double e){english=e;} void set_total(double t){total=t;} friend ostream& operator<<(ostream&, Student&); private: int num; string name; double cpp; double math; double english; double total; }; void Student::set_value(int n,string nam, double c, double m, double e) { num=n; name=nam; cpp=c; math=m; english=e; total=c+m+e; } ostream& operator<<(ostream& out, Student& s) { out<<s.num<<" "<<s.name<<" "<<s.cpp<<" "<<s.math<<" "<<s.english<<" "<<s.total<<endl; return out; } int main( ) { Student stud[100]; //stud[100]为保存数据的对象数组 int i,n; string sname; double scpp, smath, senglish; //(二)读入学生的成绩,并求出总分,用对象数组进行存储。 ifstream infile("score.dat",ios::in); //以输入的方式打开文件,ASCII文件 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(); //(三)将所有数据保存到一个二进制文件binary_score.dat中 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<<"输入你自己的信息:"; cin>>n>>sname>>scpp>>smath>>senglish; Student me(n,sname, scpp, smath, senglish); outfile.write((char*)&me, sizeof(me)); outfile.close(); //(四)为验证输出文件正确,再将binary_score.dat中的记录逐一读出到学生对象中并输出查看。 Student s; ifstream infile2("binary_score.dat",ios::in|ios::binary); if(!infile2) { cerr<<"open error!"<<endl; exit(1); } while(true) { infile2.read((char*)&s, sizeof(s)); if(infile2.eof()) break; cout<<s; } infile2.close(); return 0; }
<本文完>
相关文章推荐
- 《C++语言基础》实践项目——二进制文件及文件的随机读写
- 《C++语言基础》程序阅读——二进制文件及文件的随机读写
- C++ 多重集的使用
- VC++ 使用 typedef, Tuple, Array 来模拟一个对象的列表,并对列表进行增减、赋值和访问,
- C++ set使用
- VC++6.0 classview类不见了的情况
- 深入解析C++ STL中的常用容器
- VS2010 C++ MFC框架学习笔记4 - C++面向对象编程基础2
- C语言 链表反向打印与反转
- C++——vector操作
- 有用的C/C++网站
- C语言 二叉查找树相关算法
- C++析构函数
- c++ --> const关键字总结
- C++9.7、9.8 容器适配器----queue、stack
- 使用C++实现简单二叉树
- VC++ 字符串赋值例子(2)
- 【Divided Two】cpp
- ###《More Effective C++》- 基础议题
- Trie树